slab_common.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * Slab allocator functions that are independent of the allocator strategy
  3. *
  4. * (C) 2012 Christoph Lameter <cl@linux.com>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/mm.h>
  8. #include <linux/poison.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/memory.h>
  11. #include <linux/compiler.h>
  12. #include <linux/module.h>
  13. #include <linux/cpu.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/proc_fs.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/page.h>
  20. #include <linux/memcontrol.h>
  21. #define CREATE_TRACE_POINTS
  22. #include <trace/events/kmem.h>
  23. #include "slab.h"
  24. enum slab_state slab_state;
  25. LIST_HEAD(slab_caches);
  26. DEFINE_MUTEX(slab_mutex);
  27. struct kmem_cache *kmem_cache;
  28. /*
  29. * Set of flags that will prevent slab merging
  30. */
  31. #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  32. SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
  33. SLAB_FAILSLAB)
  34. #define SLAB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  35. SLAB_CACHE_DMA | SLAB_NOTRACK)
  36. /*
  37. * Merge control. If this is set then no merging of slab caches will occur.
  38. * (Could be removed. This was introduced to pacify the merge skeptics.)
  39. */
  40. static int slab_nomerge;
  41. static int __init setup_slab_nomerge(char *str)
  42. {
  43. slab_nomerge = 1;
  44. return 1;
  45. }
  46. #ifdef CONFIG_SLUB
  47. __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
  48. #endif
  49. __setup("slab_nomerge", setup_slab_nomerge);
  50. /*
  51. * Determine the size of a slab object
  52. */
  53. unsigned int kmem_cache_size(struct kmem_cache *s)
  54. {
  55. return s->object_size;
  56. }
  57. EXPORT_SYMBOL(kmem_cache_size);
  58. #ifdef CONFIG_DEBUG_VM
  59. static int kmem_cache_sanity_check(const char *name, size_t size)
  60. {
  61. struct kmem_cache *s = NULL;
  62. if (!name || in_interrupt() || size < sizeof(void *) ||
  63. size > KMALLOC_MAX_SIZE) {
  64. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  65. return -EINVAL;
  66. }
  67. list_for_each_entry(s, &slab_caches, list) {
  68. char tmp;
  69. int res;
  70. /*
  71. * This happens when the module gets unloaded and doesn't
  72. * destroy its slab cache and no-one else reuses the vmalloc
  73. * area of the module. Print a warning.
  74. */
  75. res = probe_kernel_address(s->name, tmp);
  76. if (res) {
  77. pr_err("Slab cache with size %d has lost its name\n",
  78. s->object_size);
  79. continue;
  80. }
  81. }
  82. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  83. return 0;
  84. }
  85. #else
  86. static inline int kmem_cache_sanity_check(const char *name, size_t size)
  87. {
  88. return 0;
  89. }
  90. #endif
  91. #ifdef CONFIG_MEMCG_KMEM
  92. void slab_init_memcg_params(struct kmem_cache *s)
  93. {
  94. s->memcg_params.is_root_cache = true;
  95. INIT_LIST_HEAD(&s->memcg_params.list);
  96. RCU_INIT_POINTER(s->memcg_params.memcg_caches, NULL);
  97. }
  98. static int init_memcg_params(struct kmem_cache *s,
  99. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  100. {
  101. struct memcg_cache_array *arr;
  102. if (memcg) {
  103. s->memcg_params.is_root_cache = false;
  104. s->memcg_params.memcg = memcg;
  105. s->memcg_params.root_cache = root_cache;
  106. return 0;
  107. }
  108. slab_init_memcg_params(s);
  109. if (!memcg_nr_cache_ids)
  110. return 0;
  111. arr = kzalloc(sizeof(struct memcg_cache_array) +
  112. memcg_nr_cache_ids * sizeof(void *),
  113. GFP_KERNEL);
  114. if (!arr)
  115. return -ENOMEM;
  116. RCU_INIT_POINTER(s->memcg_params.memcg_caches, arr);
  117. return 0;
  118. }
  119. static void destroy_memcg_params(struct kmem_cache *s)
  120. {
  121. if (is_root_cache(s))
  122. kfree(rcu_access_pointer(s->memcg_params.memcg_caches));
  123. }
  124. static int update_memcg_params(struct kmem_cache *s, int new_array_size)
  125. {
  126. struct memcg_cache_array *old, *new;
  127. if (!is_root_cache(s))
  128. return 0;
  129. new = kzalloc(sizeof(struct memcg_cache_array) +
  130. new_array_size * sizeof(void *), GFP_KERNEL);
  131. if (!new)
  132. return -ENOMEM;
  133. old = rcu_dereference_protected(s->memcg_params.memcg_caches,
  134. lockdep_is_held(&slab_mutex));
  135. if (old)
  136. memcpy(new->entries, old->entries,
  137. memcg_nr_cache_ids * sizeof(void *));
  138. rcu_assign_pointer(s->memcg_params.memcg_caches, new);
  139. if (old)
  140. kfree_rcu(old, rcu);
  141. return 0;
  142. }
  143. int memcg_update_all_caches(int num_memcgs)
  144. {
  145. struct kmem_cache *s;
  146. int ret = 0;
  147. mutex_lock(&slab_mutex);
  148. list_for_each_entry(s, &slab_caches, list) {
  149. ret = update_memcg_params(s, num_memcgs);
  150. /*
  151. * Instead of freeing the memory, we'll just leave the caches
  152. * up to this point in an updated state.
  153. */
  154. if (ret)
  155. break;
  156. }
  157. mutex_unlock(&slab_mutex);
  158. return ret;
  159. }
  160. #else
  161. static inline int init_memcg_params(struct kmem_cache *s,
  162. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  163. {
  164. return 0;
  165. }
  166. static inline void destroy_memcg_params(struct kmem_cache *s)
  167. {
  168. }
  169. #endif /* CONFIG_MEMCG_KMEM */
  170. /*
  171. * Find a mergeable slab cache
  172. */
  173. int slab_unmergeable(struct kmem_cache *s)
  174. {
  175. if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
  176. return 1;
  177. if (!is_root_cache(s))
  178. return 1;
  179. if (s->ctor)
  180. return 1;
  181. /*
  182. * We may have set a slab to be unmergeable during bootstrap.
  183. */
  184. if (s->refcount < 0)
  185. return 1;
  186. return 0;
  187. }
  188. struct kmem_cache *find_mergeable(size_t size, size_t align,
  189. unsigned long flags, const char *name, void (*ctor)(void *))
  190. {
  191. struct kmem_cache *s;
  192. if (slab_nomerge || (flags & SLAB_NEVER_MERGE))
  193. return NULL;
  194. if (ctor)
  195. return NULL;
  196. size = ALIGN(size, sizeof(void *));
  197. align = calculate_alignment(flags, align, size);
  198. size = ALIGN(size, align);
  199. flags = kmem_cache_flags(size, flags, name, NULL);
  200. list_for_each_entry_reverse(s, &slab_caches, list) {
  201. if (slab_unmergeable(s))
  202. continue;
  203. if (size > s->size)
  204. continue;
  205. if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
  206. continue;
  207. /*
  208. * Check if alignment is compatible.
  209. * Courtesy of Adrian Drzewiecki
  210. */
  211. if ((s->size & ~(align - 1)) != s->size)
  212. continue;
  213. if (s->size - size >= sizeof(void *))
  214. continue;
  215. if (IS_ENABLED(CONFIG_SLAB) && align &&
  216. (align > s->align || s->align % align))
  217. continue;
  218. return s;
  219. }
  220. return NULL;
  221. }
  222. /*
  223. * Figure out what the alignment of the objects will be given a set of
  224. * flags, a user specified alignment and the size of the objects.
  225. */
  226. unsigned long calculate_alignment(unsigned long flags,
  227. unsigned long align, unsigned long size)
  228. {
  229. /*
  230. * If the user wants hardware cache aligned objects then follow that
  231. * suggestion if the object is sufficiently large.
  232. *
  233. * The hardware cache alignment cannot override the specified
  234. * alignment though. If that is greater then use it.
  235. */
  236. if (flags & SLAB_HWCACHE_ALIGN) {
  237. unsigned long ralign = cache_line_size();
  238. while (size <= ralign / 2)
  239. ralign /= 2;
  240. align = max(align, ralign);
  241. }
  242. if (align < ARCH_SLAB_MINALIGN)
  243. align = ARCH_SLAB_MINALIGN;
  244. return ALIGN(align, sizeof(void *));
  245. }
  246. static struct kmem_cache *
  247. do_kmem_cache_create(const char *name, size_t object_size, size_t size,
  248. size_t align, unsigned long flags, void (*ctor)(void *),
  249. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  250. {
  251. struct kmem_cache *s;
  252. int err;
  253. err = -ENOMEM;
  254. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  255. if (!s)
  256. goto out;
  257. s->name = name;
  258. s->object_size = object_size;
  259. s->size = size;
  260. s->align = align;
  261. s->ctor = ctor;
  262. err = init_memcg_params(s, memcg, root_cache);
  263. if (err)
  264. goto out_free_cache;
  265. err = __kmem_cache_create(s, flags);
  266. if (err)
  267. goto out_free_cache;
  268. s->refcount = 1;
  269. list_add(&s->list, &slab_caches);
  270. out:
  271. if (err)
  272. return ERR_PTR(err);
  273. return s;
  274. out_free_cache:
  275. destroy_memcg_params(s);
  276. kmem_cache_free(kmem_cache, s);
  277. goto out;
  278. }
  279. /*
  280. * kmem_cache_create - Create a cache.
  281. * @name: A string which is used in /proc/slabinfo to identify this cache.
  282. * @size: The size of objects to be created in this cache.
  283. * @align: The required alignment for the objects.
  284. * @flags: SLAB flags
  285. * @ctor: A constructor for the objects.
  286. *
  287. * Returns a ptr to the cache on success, NULL on failure.
  288. * Cannot be called within a interrupt, but can be interrupted.
  289. * The @ctor is run when new pages are allocated by the cache.
  290. *
  291. * The flags are
  292. *
  293. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  294. * to catch references to uninitialised memory.
  295. *
  296. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  297. * for buffer overruns.
  298. *
  299. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  300. * cacheline. This can be beneficial if you're counting cycles as closely
  301. * as davem.
  302. */
  303. struct kmem_cache *
  304. kmem_cache_create(const char *name, size_t size, size_t align,
  305. unsigned long flags, void (*ctor)(void *))
  306. {
  307. struct kmem_cache *s;
  308. const char *cache_name;
  309. int err;
  310. get_online_cpus();
  311. get_online_mems();
  312. memcg_get_cache_ids();
  313. mutex_lock(&slab_mutex);
  314. err = kmem_cache_sanity_check(name, size);
  315. if (err) {
  316. s = NULL; /* suppress uninit var warning */
  317. goto out_unlock;
  318. }
  319. /*
  320. * Some allocators will constraint the set of valid flags to a subset
  321. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  322. * case, and we'll just provide them with a sanitized version of the
  323. * passed flags.
  324. */
  325. flags &= CACHE_CREATE_MASK;
  326. s = __kmem_cache_alias(name, size, align, flags, ctor);
  327. if (s)
  328. goto out_unlock;
  329. cache_name = kstrdup_const(name, GFP_KERNEL);
  330. if (!cache_name) {
  331. err = -ENOMEM;
  332. goto out_unlock;
  333. }
  334. s = do_kmem_cache_create(cache_name, size, size,
  335. calculate_alignment(flags, align, size),
  336. flags, ctor, NULL, NULL);
  337. if (IS_ERR(s)) {
  338. err = PTR_ERR(s);
  339. kfree_const(cache_name);
  340. }
  341. out_unlock:
  342. mutex_unlock(&slab_mutex);
  343. memcg_put_cache_ids();
  344. put_online_mems();
  345. put_online_cpus();
  346. if (err) {
  347. if (flags & SLAB_PANIC)
  348. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  349. name, err);
  350. else {
  351. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  352. name, err);
  353. dump_stack();
  354. }
  355. return NULL;
  356. }
  357. return s;
  358. }
  359. EXPORT_SYMBOL(kmem_cache_create);
  360. static int do_kmem_cache_shutdown(struct kmem_cache *s,
  361. struct list_head *release, bool *need_rcu_barrier)
  362. {
  363. if (__kmem_cache_shutdown(s) != 0) {
  364. printk(KERN_ERR "kmem_cache_destroy %s: "
  365. "Slab cache still has objects\n", s->name);
  366. dump_stack();
  367. return -EBUSY;
  368. }
  369. if (s->flags & SLAB_DESTROY_BY_RCU)
  370. *need_rcu_barrier = true;
  371. #ifdef CONFIG_MEMCG_KMEM
  372. if (!is_root_cache(s))
  373. list_del(&s->memcg_params.list);
  374. #endif
  375. list_move(&s->list, release);
  376. return 0;
  377. }
  378. static void do_kmem_cache_release(struct list_head *release,
  379. bool need_rcu_barrier)
  380. {
  381. struct kmem_cache *s, *s2;
  382. if (need_rcu_barrier)
  383. rcu_barrier();
  384. list_for_each_entry_safe(s, s2, release, list) {
  385. #ifdef SLAB_SUPPORTS_SYSFS
  386. sysfs_slab_remove(s);
  387. #else
  388. slab_kmem_cache_release(s);
  389. #endif
  390. }
  391. }
  392. #ifdef CONFIG_MEMCG_KMEM
  393. /*
  394. * memcg_create_kmem_cache - Create a cache for a memory cgroup.
  395. * @memcg: The memory cgroup the new cache is for.
  396. * @root_cache: The parent of the new cache.
  397. *
  398. * This function attempts to create a kmem cache that will serve allocation
  399. * requests going from @memcg to @root_cache. The new cache inherits properties
  400. * from its parent.
  401. */
  402. void memcg_create_kmem_cache(struct mem_cgroup *memcg,
  403. struct kmem_cache *root_cache)
  404. {
  405. static char memcg_name_buf[NAME_MAX + 1]; /* protected by slab_mutex */
  406. struct cgroup_subsys_state *css = mem_cgroup_css(memcg);
  407. struct memcg_cache_array *arr;
  408. struct kmem_cache *s = NULL;
  409. char *cache_name;
  410. int idx;
  411. get_online_cpus();
  412. get_online_mems();
  413. mutex_lock(&slab_mutex);
  414. /*
  415. * The memory cgroup could have been deactivated while the cache
  416. * creation work was pending.
  417. */
  418. if (!memcg_kmem_is_active(memcg))
  419. goto out_unlock;
  420. idx = memcg_cache_id(memcg);
  421. arr = rcu_dereference_protected(root_cache->memcg_params.memcg_caches,
  422. lockdep_is_held(&slab_mutex));
  423. /*
  424. * Since per-memcg caches are created asynchronously on first
  425. * allocation (see memcg_kmem_get_cache()), several threads can try to
  426. * create the same cache, but only one of them may succeed.
  427. */
  428. if (arr->entries[idx])
  429. goto out_unlock;
  430. cgroup_name(css->cgroup, memcg_name_buf, sizeof(memcg_name_buf));
  431. cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
  432. css->id, memcg_name_buf);
  433. if (!cache_name)
  434. goto out_unlock;
  435. s = do_kmem_cache_create(cache_name, root_cache->object_size,
  436. root_cache->size, root_cache->align,
  437. root_cache->flags, root_cache->ctor,
  438. memcg, root_cache);
  439. /*
  440. * If we could not create a memcg cache, do not complain, because
  441. * that's not critical at all as we can always proceed with the root
  442. * cache.
  443. */
  444. if (IS_ERR(s)) {
  445. kfree(cache_name);
  446. goto out_unlock;
  447. }
  448. list_add(&s->memcg_params.list, &root_cache->memcg_params.list);
  449. /*
  450. * Since readers won't lock (see cache_from_memcg_idx()), we need a
  451. * barrier here to ensure nobody will see the kmem_cache partially
  452. * initialized.
  453. */
  454. smp_wmb();
  455. arr->entries[idx] = s;
  456. out_unlock:
  457. mutex_unlock(&slab_mutex);
  458. put_online_mems();
  459. put_online_cpus();
  460. }
  461. void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
  462. {
  463. int idx;
  464. struct memcg_cache_array *arr;
  465. struct kmem_cache *s, *c;
  466. idx = memcg_cache_id(memcg);
  467. get_online_cpus();
  468. get_online_mems();
  469. mutex_lock(&slab_mutex);
  470. list_for_each_entry(s, &slab_caches, list) {
  471. if (!is_root_cache(s))
  472. continue;
  473. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  474. lockdep_is_held(&slab_mutex));
  475. c = arr->entries[idx];
  476. if (!c)
  477. continue;
  478. __kmem_cache_shrink(c, true);
  479. arr->entries[idx] = NULL;
  480. }
  481. mutex_unlock(&slab_mutex);
  482. put_online_mems();
  483. put_online_cpus();
  484. }
  485. void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
  486. {
  487. LIST_HEAD(release);
  488. bool need_rcu_barrier = false;
  489. struct kmem_cache *s, *s2;
  490. get_online_cpus();
  491. get_online_mems();
  492. mutex_lock(&slab_mutex);
  493. list_for_each_entry_safe(s, s2, &slab_caches, list) {
  494. if (is_root_cache(s) || s->memcg_params.memcg != memcg)
  495. continue;
  496. /*
  497. * The cgroup is about to be freed and therefore has no charges
  498. * left. Hence, all its caches must be empty by now.
  499. */
  500. BUG_ON(do_kmem_cache_shutdown(s, &release, &need_rcu_barrier));
  501. }
  502. mutex_unlock(&slab_mutex);
  503. put_online_mems();
  504. put_online_cpus();
  505. do_kmem_cache_release(&release, need_rcu_barrier);
  506. }
  507. #endif /* CONFIG_MEMCG_KMEM */
  508. void slab_kmem_cache_release(struct kmem_cache *s)
  509. {
  510. destroy_memcg_params(s);
  511. kfree_const(s->name);
  512. kmem_cache_free(kmem_cache, s);
  513. }
  514. void kmem_cache_destroy(struct kmem_cache *s)
  515. {
  516. struct kmem_cache *c, *c2;
  517. LIST_HEAD(release);
  518. bool need_rcu_barrier = false;
  519. bool busy = false;
  520. BUG_ON(!is_root_cache(s));
  521. get_online_cpus();
  522. get_online_mems();
  523. mutex_lock(&slab_mutex);
  524. s->refcount--;
  525. if (s->refcount)
  526. goto out_unlock;
  527. for_each_memcg_cache_safe(c, c2, s) {
  528. if (do_kmem_cache_shutdown(c, &release, &need_rcu_barrier))
  529. busy = true;
  530. }
  531. if (!busy)
  532. do_kmem_cache_shutdown(s, &release, &need_rcu_barrier);
  533. out_unlock:
  534. mutex_unlock(&slab_mutex);
  535. put_online_mems();
  536. put_online_cpus();
  537. do_kmem_cache_release(&release, need_rcu_barrier);
  538. }
  539. EXPORT_SYMBOL(kmem_cache_destroy);
  540. /**
  541. * kmem_cache_shrink - Shrink a cache.
  542. * @cachep: The cache to shrink.
  543. *
  544. * Releases as many slabs as possible for a cache.
  545. * To help debugging, a zero exit status indicates all slabs were released.
  546. */
  547. int kmem_cache_shrink(struct kmem_cache *cachep)
  548. {
  549. int ret;
  550. get_online_cpus();
  551. get_online_mems();
  552. ret = __kmem_cache_shrink(cachep, false);
  553. put_online_mems();
  554. put_online_cpus();
  555. return ret;
  556. }
  557. EXPORT_SYMBOL(kmem_cache_shrink);
  558. int slab_is_available(void)
  559. {
  560. return slab_state >= UP;
  561. }
  562. #ifndef CONFIG_SLOB
  563. /* Create a cache during boot when no slab services are available yet */
  564. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  565. unsigned long flags)
  566. {
  567. int err;
  568. s->name = name;
  569. s->size = s->object_size = size;
  570. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  571. slab_init_memcg_params(s);
  572. err = __kmem_cache_create(s, flags);
  573. if (err)
  574. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  575. name, size, err);
  576. s->refcount = -1; /* Exempt from merging for now */
  577. }
  578. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  579. unsigned long flags)
  580. {
  581. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  582. if (!s)
  583. panic("Out of memory when creating slab %s\n", name);
  584. create_boot_cache(s, name, size, flags);
  585. list_add(&s->list, &slab_caches);
  586. s->refcount = 1;
  587. return s;
  588. }
  589. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  590. EXPORT_SYMBOL(kmalloc_caches);
  591. #ifdef CONFIG_ZONE_DMA
  592. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  593. EXPORT_SYMBOL(kmalloc_dma_caches);
  594. #endif
  595. /*
  596. * Conversion table for small slabs sizes / 8 to the index in the
  597. * kmalloc array. This is necessary for slabs < 192 since we have non power
  598. * of two cache sizes there. The size of larger slabs can be determined using
  599. * fls.
  600. */
  601. static s8 size_index[24] = {
  602. 3, /* 8 */
  603. 4, /* 16 */
  604. 5, /* 24 */
  605. 5, /* 32 */
  606. 6, /* 40 */
  607. 6, /* 48 */
  608. 6, /* 56 */
  609. 6, /* 64 */
  610. 1, /* 72 */
  611. 1, /* 80 */
  612. 1, /* 88 */
  613. 1, /* 96 */
  614. 7, /* 104 */
  615. 7, /* 112 */
  616. 7, /* 120 */
  617. 7, /* 128 */
  618. 2, /* 136 */
  619. 2, /* 144 */
  620. 2, /* 152 */
  621. 2, /* 160 */
  622. 2, /* 168 */
  623. 2, /* 176 */
  624. 2, /* 184 */
  625. 2 /* 192 */
  626. };
  627. static inline int size_index_elem(size_t bytes)
  628. {
  629. return (bytes - 1) / 8;
  630. }
  631. /*
  632. * Find the kmem_cache structure that serves a given size of
  633. * allocation
  634. */
  635. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  636. {
  637. int index;
  638. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  639. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  640. return NULL;
  641. }
  642. if (size <= 192) {
  643. if (!size)
  644. return ZERO_SIZE_PTR;
  645. index = size_index[size_index_elem(size)];
  646. } else
  647. index = fls(size - 1);
  648. #ifdef CONFIG_ZONE_DMA
  649. if (unlikely((flags & GFP_DMA)))
  650. return kmalloc_dma_caches[index];
  651. #endif
  652. return kmalloc_caches[index];
  653. }
  654. /*
  655. * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
  656. * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
  657. * kmalloc-67108864.
  658. */
  659. static struct {
  660. const char *name;
  661. unsigned long size;
  662. } const kmalloc_info[] __initconst = {
  663. {NULL, 0}, {"kmalloc-96", 96},
  664. {"kmalloc-192", 192}, {"kmalloc-8", 8},
  665. {"kmalloc-16", 16}, {"kmalloc-32", 32},
  666. {"kmalloc-64", 64}, {"kmalloc-128", 128},
  667. {"kmalloc-256", 256}, {"kmalloc-512", 512},
  668. {"kmalloc-1024", 1024}, {"kmalloc-2048", 2048},
  669. {"kmalloc-4096", 4096}, {"kmalloc-8192", 8192},
  670. {"kmalloc-16384", 16384}, {"kmalloc-32768", 32768},
  671. {"kmalloc-65536", 65536}, {"kmalloc-131072", 131072},
  672. {"kmalloc-262144", 262144}, {"kmalloc-524288", 524288},
  673. {"kmalloc-1048576", 1048576}, {"kmalloc-2097152", 2097152},
  674. {"kmalloc-4194304", 4194304}, {"kmalloc-8388608", 8388608},
  675. {"kmalloc-16777216", 16777216}, {"kmalloc-33554432", 33554432},
  676. {"kmalloc-67108864", 67108864}
  677. };
  678. /*
  679. * Patch up the size_index table if we have strange large alignment
  680. * requirements for the kmalloc array. This is only the case for
  681. * MIPS it seems. The standard arches will not generate any code here.
  682. *
  683. * Largest permitted alignment is 256 bytes due to the way we
  684. * handle the index determination for the smaller caches.
  685. *
  686. * Make sure that nothing crazy happens if someone starts tinkering
  687. * around with ARCH_KMALLOC_MINALIGN
  688. */
  689. void __init setup_kmalloc_cache_index_table(void)
  690. {
  691. int i;
  692. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  693. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  694. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  695. int elem = size_index_elem(i);
  696. if (elem >= ARRAY_SIZE(size_index))
  697. break;
  698. size_index[elem] = KMALLOC_SHIFT_LOW;
  699. }
  700. if (KMALLOC_MIN_SIZE >= 64) {
  701. /*
  702. * The 96 byte size cache is not used if the alignment
  703. * is 64 byte.
  704. */
  705. for (i = 64 + 8; i <= 96; i += 8)
  706. size_index[size_index_elem(i)] = 7;
  707. }
  708. if (KMALLOC_MIN_SIZE >= 128) {
  709. /*
  710. * The 192 byte sized cache is not used if the alignment
  711. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  712. * instead.
  713. */
  714. for (i = 128 + 8; i <= 192; i += 8)
  715. size_index[size_index_elem(i)] = 8;
  716. }
  717. }
  718. static void __init new_kmalloc_cache(int idx, unsigned long flags)
  719. {
  720. kmalloc_caches[idx] = create_kmalloc_cache(kmalloc_info[idx].name,
  721. kmalloc_info[idx].size, flags);
  722. }
  723. /*
  724. * Create the kmalloc array. Some of the regular kmalloc arrays
  725. * may already have been created because they were needed to
  726. * enable allocations for slab creation.
  727. */
  728. void __init create_kmalloc_caches(unsigned long flags)
  729. {
  730. int i;
  731. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  732. if (!kmalloc_caches[i])
  733. new_kmalloc_cache(i, flags);
  734. /*
  735. * Caches that are not of the two-to-the-power-of size.
  736. * These have to be created immediately after the
  737. * earlier power of two caches
  738. */
  739. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  740. new_kmalloc_cache(1, flags);
  741. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  742. new_kmalloc_cache(2, flags);
  743. }
  744. /* Kmalloc array is now usable */
  745. slab_state = UP;
  746. #ifdef CONFIG_ZONE_DMA
  747. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  748. struct kmem_cache *s = kmalloc_caches[i];
  749. if (s) {
  750. int size = kmalloc_size(i);
  751. char *n = kasprintf(GFP_NOWAIT,
  752. "dma-kmalloc-%d", size);
  753. BUG_ON(!n);
  754. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  755. size, SLAB_CACHE_DMA | flags);
  756. }
  757. }
  758. #endif
  759. }
  760. #endif /* !CONFIG_SLOB */
  761. /*
  762. * To avoid unnecessary overhead, we pass through large allocation requests
  763. * directly to the page allocator. We use __GFP_COMP, because we will need to
  764. * know the allocation order to free the pages properly in kfree.
  765. */
  766. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  767. {
  768. void *ret;
  769. struct page *page;
  770. flags |= __GFP_COMP;
  771. page = alloc_kmem_pages(flags, order);
  772. ret = page ? page_address(page) : NULL;
  773. kmemleak_alloc(ret, size, 1, flags);
  774. kasan_kmalloc_large(ret, size);
  775. return ret;
  776. }
  777. EXPORT_SYMBOL(kmalloc_order);
  778. #ifdef CONFIG_TRACING
  779. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  780. {
  781. void *ret = kmalloc_order(size, flags, order);
  782. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  783. return ret;
  784. }
  785. EXPORT_SYMBOL(kmalloc_order_trace);
  786. #endif
  787. #ifdef CONFIG_SLABINFO
  788. #ifdef CONFIG_SLAB
  789. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  790. #else
  791. #define SLABINFO_RIGHTS S_IRUSR
  792. #endif
  793. static void print_slabinfo_header(struct seq_file *m)
  794. {
  795. /*
  796. * Output format version, so at least we can change it
  797. * without _too_ many complaints.
  798. */
  799. #ifdef CONFIG_DEBUG_SLAB
  800. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  801. #else
  802. seq_puts(m, "slabinfo - version: 2.1\n");
  803. #endif
  804. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  805. "<objperslab> <pagesperslab>");
  806. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  807. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  808. #ifdef CONFIG_DEBUG_SLAB
  809. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  810. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  811. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  812. #endif
  813. seq_putc(m, '\n');
  814. }
  815. void *slab_start(struct seq_file *m, loff_t *pos)
  816. {
  817. mutex_lock(&slab_mutex);
  818. return seq_list_start(&slab_caches, *pos);
  819. }
  820. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  821. {
  822. return seq_list_next(p, &slab_caches, pos);
  823. }
  824. void slab_stop(struct seq_file *m, void *p)
  825. {
  826. mutex_unlock(&slab_mutex);
  827. }
  828. static void
  829. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  830. {
  831. struct kmem_cache *c;
  832. struct slabinfo sinfo;
  833. if (!is_root_cache(s))
  834. return;
  835. for_each_memcg_cache(c, s) {
  836. memset(&sinfo, 0, sizeof(sinfo));
  837. get_slabinfo(c, &sinfo);
  838. info->active_slabs += sinfo.active_slabs;
  839. info->num_slabs += sinfo.num_slabs;
  840. info->shared_avail += sinfo.shared_avail;
  841. info->active_objs += sinfo.active_objs;
  842. info->num_objs += sinfo.num_objs;
  843. }
  844. }
  845. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  846. {
  847. struct slabinfo sinfo;
  848. memset(&sinfo, 0, sizeof(sinfo));
  849. get_slabinfo(s, &sinfo);
  850. memcg_accumulate_slabinfo(s, &sinfo);
  851. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  852. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  853. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  854. seq_printf(m, " : tunables %4u %4u %4u",
  855. sinfo.limit, sinfo.batchcount, sinfo.shared);
  856. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  857. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  858. slabinfo_show_stats(m, s);
  859. seq_putc(m, '\n');
  860. }
  861. static int slab_show(struct seq_file *m, void *p)
  862. {
  863. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  864. if (p == slab_caches.next)
  865. print_slabinfo_header(m);
  866. if (is_root_cache(s))
  867. cache_show(s, m);
  868. return 0;
  869. }
  870. #ifdef CONFIG_MEMCG_KMEM
  871. int memcg_slab_show(struct seq_file *m, void *p)
  872. {
  873. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  874. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  875. if (p == slab_caches.next)
  876. print_slabinfo_header(m);
  877. if (!is_root_cache(s) && s->memcg_params.memcg == memcg)
  878. cache_show(s, m);
  879. return 0;
  880. }
  881. #endif
  882. /*
  883. * slabinfo_op - iterator that generates /proc/slabinfo
  884. *
  885. * Output layout:
  886. * cache-name
  887. * num-active-objs
  888. * total-objs
  889. * object size
  890. * num-active-slabs
  891. * total-slabs
  892. * num-pages-per-slab
  893. * + further values on SMP and with statistics enabled
  894. */
  895. static const struct seq_operations slabinfo_op = {
  896. .start = slab_start,
  897. .next = slab_next,
  898. .stop = slab_stop,
  899. .show = slab_show,
  900. };
  901. static int slabinfo_open(struct inode *inode, struct file *file)
  902. {
  903. return seq_open(file, &slabinfo_op);
  904. }
  905. static const struct file_operations proc_slabinfo_operations = {
  906. .open = slabinfo_open,
  907. .read = seq_read,
  908. .write = slabinfo_write,
  909. .llseek = seq_lseek,
  910. .release = seq_release,
  911. };
  912. static int __init slab_proc_init(void)
  913. {
  914. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  915. &proc_slabinfo_operations);
  916. return 0;
  917. }
  918. module_init(slab_proc_init);
  919. #endif /* CONFIG_SLABINFO */
  920. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  921. gfp_t flags)
  922. {
  923. void *ret;
  924. size_t ks = 0;
  925. if (p)
  926. ks = ksize(p);
  927. if (ks >= new_size) {
  928. kasan_krealloc((void *)p, new_size);
  929. return (void *)p;
  930. }
  931. ret = kmalloc_track_caller(new_size, flags);
  932. if (ret && p)
  933. memcpy(ret, p, ks);
  934. return ret;
  935. }
  936. /**
  937. * __krealloc - like krealloc() but don't free @p.
  938. * @p: object to reallocate memory for.
  939. * @new_size: how many bytes of memory are required.
  940. * @flags: the type of memory to allocate.
  941. *
  942. * This function is like krealloc() except it never frees the originally
  943. * allocated buffer. Use this if you don't want to free the buffer immediately
  944. * like, for example, with RCU.
  945. */
  946. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  947. {
  948. if (unlikely(!new_size))
  949. return ZERO_SIZE_PTR;
  950. return __do_krealloc(p, new_size, flags);
  951. }
  952. EXPORT_SYMBOL(__krealloc);
  953. /**
  954. * krealloc - reallocate memory. The contents will remain unchanged.
  955. * @p: object to reallocate memory for.
  956. * @new_size: how many bytes of memory are required.
  957. * @flags: the type of memory to allocate.
  958. *
  959. * The contents of the object pointed to are preserved up to the
  960. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  961. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  962. * %NULL pointer, the object pointed to is freed.
  963. */
  964. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  965. {
  966. void *ret;
  967. if (unlikely(!new_size)) {
  968. kfree(p);
  969. return ZERO_SIZE_PTR;
  970. }
  971. ret = __do_krealloc(p, new_size, flags);
  972. if (ret && p != ret)
  973. kfree(p);
  974. return ret;
  975. }
  976. EXPORT_SYMBOL(krealloc);
  977. /**
  978. * kzfree - like kfree but zero memory
  979. * @p: object to free memory of
  980. *
  981. * The memory of the object @p points to is zeroed before freed.
  982. * If @p is %NULL, kzfree() does nothing.
  983. *
  984. * Note: this function zeroes the whole allocated buffer which can be a good
  985. * deal bigger than the requested buffer size passed to kmalloc(). So be
  986. * careful when using this function in performance sensitive code.
  987. */
  988. void kzfree(const void *p)
  989. {
  990. size_t ks;
  991. void *mem = (void *)p;
  992. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  993. return;
  994. ks = ksize(mem);
  995. memset(mem, 0, ks);
  996. kfree(mem);
  997. }
  998. EXPORT_SYMBOL(kzfree);
  999. /* Tracepoints definitions. */
  1000. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  1001. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  1002. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  1003. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  1004. EXPORT_TRACEPOINT_SYMBOL(kfree);
  1005. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);