slab_common.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. * Create the kmalloc array. Some of the regular kmalloc arrays
  656. * may already have been created because they were needed to
  657. * enable allocations for slab creation.
  658. */
  659. void __init create_kmalloc_caches(unsigned long flags)
  660. {
  661. int i;
  662. /*
  663. * Patch up the size_index table if we have strange large alignment
  664. * requirements for the kmalloc array. This is only the case for
  665. * MIPS it seems. The standard arches will not generate any code here.
  666. *
  667. * Largest permitted alignment is 256 bytes due to the way we
  668. * handle the index determination for the smaller caches.
  669. *
  670. * Make sure that nothing crazy happens if someone starts tinkering
  671. * around with ARCH_KMALLOC_MINALIGN
  672. */
  673. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  674. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  675. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  676. int elem = size_index_elem(i);
  677. if (elem >= ARRAY_SIZE(size_index))
  678. break;
  679. size_index[elem] = KMALLOC_SHIFT_LOW;
  680. }
  681. if (KMALLOC_MIN_SIZE >= 64) {
  682. /*
  683. * The 96 byte size cache is not used if the alignment
  684. * is 64 byte.
  685. */
  686. for (i = 64 + 8; i <= 96; i += 8)
  687. size_index[size_index_elem(i)] = 7;
  688. }
  689. if (KMALLOC_MIN_SIZE >= 128) {
  690. /*
  691. * The 192 byte sized cache is not used if the alignment
  692. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  693. * instead.
  694. */
  695. for (i = 128 + 8; i <= 192; i += 8)
  696. size_index[size_index_elem(i)] = 8;
  697. }
  698. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  699. if (!kmalloc_caches[i]) {
  700. kmalloc_caches[i] = create_kmalloc_cache(NULL,
  701. 1 << i, flags);
  702. }
  703. /*
  704. * Caches that are not of the two-to-the-power-of size.
  705. * These have to be created immediately after the
  706. * earlier power of two caches
  707. */
  708. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  709. kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
  710. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  711. kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
  712. }
  713. /* Kmalloc array is now usable */
  714. slab_state = UP;
  715. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  716. struct kmem_cache *s = kmalloc_caches[i];
  717. char *n;
  718. if (s) {
  719. n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
  720. BUG_ON(!n);
  721. s->name = n;
  722. }
  723. }
  724. #ifdef CONFIG_ZONE_DMA
  725. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  726. struct kmem_cache *s = kmalloc_caches[i];
  727. if (s) {
  728. int size = kmalloc_size(i);
  729. char *n = kasprintf(GFP_NOWAIT,
  730. "dma-kmalloc-%d", size);
  731. BUG_ON(!n);
  732. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  733. size, SLAB_CACHE_DMA | flags);
  734. }
  735. }
  736. #endif
  737. }
  738. #endif /* !CONFIG_SLOB */
  739. /*
  740. * To avoid unnecessary overhead, we pass through large allocation requests
  741. * directly to the page allocator. We use __GFP_COMP, because we will need to
  742. * know the allocation order to free the pages properly in kfree.
  743. */
  744. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  745. {
  746. void *ret;
  747. struct page *page;
  748. flags |= __GFP_COMP;
  749. page = alloc_kmem_pages(flags, order);
  750. ret = page ? page_address(page) : NULL;
  751. kmemleak_alloc(ret, size, 1, flags);
  752. kasan_kmalloc_large(ret, size);
  753. return ret;
  754. }
  755. EXPORT_SYMBOL(kmalloc_order);
  756. #ifdef CONFIG_TRACING
  757. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  758. {
  759. void *ret = kmalloc_order(size, flags, order);
  760. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  761. return ret;
  762. }
  763. EXPORT_SYMBOL(kmalloc_order_trace);
  764. #endif
  765. #ifdef CONFIG_SLABINFO
  766. #ifdef CONFIG_SLAB
  767. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  768. #else
  769. #define SLABINFO_RIGHTS S_IRUSR
  770. #endif
  771. static void print_slabinfo_header(struct seq_file *m)
  772. {
  773. /*
  774. * Output format version, so at least we can change it
  775. * without _too_ many complaints.
  776. */
  777. #ifdef CONFIG_DEBUG_SLAB
  778. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  779. #else
  780. seq_puts(m, "slabinfo - version: 2.1\n");
  781. #endif
  782. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  783. "<objperslab> <pagesperslab>");
  784. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  785. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  786. #ifdef CONFIG_DEBUG_SLAB
  787. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  788. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  789. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  790. #endif
  791. seq_putc(m, '\n');
  792. }
  793. void *slab_start(struct seq_file *m, loff_t *pos)
  794. {
  795. mutex_lock(&slab_mutex);
  796. return seq_list_start(&slab_caches, *pos);
  797. }
  798. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  799. {
  800. return seq_list_next(p, &slab_caches, pos);
  801. }
  802. void slab_stop(struct seq_file *m, void *p)
  803. {
  804. mutex_unlock(&slab_mutex);
  805. }
  806. static void
  807. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  808. {
  809. struct kmem_cache *c;
  810. struct slabinfo sinfo;
  811. if (!is_root_cache(s))
  812. return;
  813. for_each_memcg_cache(c, s) {
  814. memset(&sinfo, 0, sizeof(sinfo));
  815. get_slabinfo(c, &sinfo);
  816. info->active_slabs += sinfo.active_slabs;
  817. info->num_slabs += sinfo.num_slabs;
  818. info->shared_avail += sinfo.shared_avail;
  819. info->active_objs += sinfo.active_objs;
  820. info->num_objs += sinfo.num_objs;
  821. }
  822. }
  823. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  824. {
  825. struct slabinfo sinfo;
  826. memset(&sinfo, 0, sizeof(sinfo));
  827. get_slabinfo(s, &sinfo);
  828. memcg_accumulate_slabinfo(s, &sinfo);
  829. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  830. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  831. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  832. seq_printf(m, " : tunables %4u %4u %4u",
  833. sinfo.limit, sinfo.batchcount, sinfo.shared);
  834. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  835. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  836. slabinfo_show_stats(m, s);
  837. seq_putc(m, '\n');
  838. }
  839. static int slab_show(struct seq_file *m, void *p)
  840. {
  841. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  842. if (p == slab_caches.next)
  843. print_slabinfo_header(m);
  844. if (is_root_cache(s))
  845. cache_show(s, m);
  846. return 0;
  847. }
  848. #ifdef CONFIG_MEMCG_KMEM
  849. int memcg_slab_show(struct seq_file *m, void *p)
  850. {
  851. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  852. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  853. if (p == slab_caches.next)
  854. print_slabinfo_header(m);
  855. if (!is_root_cache(s) && s->memcg_params.memcg == memcg)
  856. cache_show(s, m);
  857. return 0;
  858. }
  859. #endif
  860. /*
  861. * slabinfo_op - iterator that generates /proc/slabinfo
  862. *
  863. * Output layout:
  864. * cache-name
  865. * num-active-objs
  866. * total-objs
  867. * object size
  868. * num-active-slabs
  869. * total-slabs
  870. * num-pages-per-slab
  871. * + further values on SMP and with statistics enabled
  872. */
  873. static const struct seq_operations slabinfo_op = {
  874. .start = slab_start,
  875. .next = slab_next,
  876. .stop = slab_stop,
  877. .show = slab_show,
  878. };
  879. static int slabinfo_open(struct inode *inode, struct file *file)
  880. {
  881. return seq_open(file, &slabinfo_op);
  882. }
  883. static const struct file_operations proc_slabinfo_operations = {
  884. .open = slabinfo_open,
  885. .read = seq_read,
  886. .write = slabinfo_write,
  887. .llseek = seq_lseek,
  888. .release = seq_release,
  889. };
  890. static int __init slab_proc_init(void)
  891. {
  892. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  893. &proc_slabinfo_operations);
  894. return 0;
  895. }
  896. module_init(slab_proc_init);
  897. #endif /* CONFIG_SLABINFO */
  898. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  899. gfp_t flags)
  900. {
  901. void *ret;
  902. size_t ks = 0;
  903. if (p)
  904. ks = ksize(p);
  905. if (ks >= new_size) {
  906. kasan_krealloc((void *)p, new_size);
  907. return (void *)p;
  908. }
  909. ret = kmalloc_track_caller(new_size, flags);
  910. if (ret && p)
  911. memcpy(ret, p, ks);
  912. return ret;
  913. }
  914. /**
  915. * __krealloc - like krealloc() but don't free @p.
  916. * @p: object to reallocate memory for.
  917. * @new_size: how many bytes of memory are required.
  918. * @flags: the type of memory to allocate.
  919. *
  920. * This function is like krealloc() except it never frees the originally
  921. * allocated buffer. Use this if you don't want to free the buffer immediately
  922. * like, for example, with RCU.
  923. */
  924. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  925. {
  926. if (unlikely(!new_size))
  927. return ZERO_SIZE_PTR;
  928. return __do_krealloc(p, new_size, flags);
  929. }
  930. EXPORT_SYMBOL(__krealloc);
  931. /**
  932. * krealloc - reallocate memory. The contents will remain unchanged.
  933. * @p: object to reallocate memory for.
  934. * @new_size: how many bytes of memory are required.
  935. * @flags: the type of memory to allocate.
  936. *
  937. * The contents of the object pointed to are preserved up to the
  938. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  939. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  940. * %NULL pointer, the object pointed to is freed.
  941. */
  942. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  943. {
  944. void *ret;
  945. if (unlikely(!new_size)) {
  946. kfree(p);
  947. return ZERO_SIZE_PTR;
  948. }
  949. ret = __do_krealloc(p, new_size, flags);
  950. if (ret && p != ret)
  951. kfree(p);
  952. return ret;
  953. }
  954. EXPORT_SYMBOL(krealloc);
  955. /**
  956. * kzfree - like kfree but zero memory
  957. * @p: object to free memory of
  958. *
  959. * The memory of the object @p points to is zeroed before freed.
  960. * If @p is %NULL, kzfree() does nothing.
  961. *
  962. * Note: this function zeroes the whole allocated buffer which can be a good
  963. * deal bigger than the requested buffer size passed to kmalloc(). So be
  964. * careful when using this function in performance sensitive code.
  965. */
  966. void kzfree(const void *p)
  967. {
  968. size_t ks;
  969. void *mem = (void *)p;
  970. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  971. return;
  972. ks = ksize(mem);
  973. memset(mem, 0, ks);
  974. kfree(mem);
  975. }
  976. EXPORT_SYMBOL(kzfree);
  977. /* Tracepoints definitions. */
  978. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  979. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  980. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  981. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  982. EXPORT_TRACEPOINT_SYMBOL(kfree);
  983. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);