slab_common.c 31 KB

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