slab_common.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Slab allocator functions that are independent of the allocator strategy
  4. *
  5. * (C) 2012 Christoph Lameter <cl@linux.com>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/mm.h>
  9. #include <linux/poison.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/memory.h>
  12. #include <linux/cache.h>
  13. #include <linux/compiler.h>
  14. #include <linux/module.h>
  15. #include <linux/cpu.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/proc_fs.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/page.h>
  22. #include <linux/memcontrol.h>
  23. #define CREATE_TRACE_POINTS
  24. #include <trace/events/kmem.h>
  25. #include "slab.h"
  26. enum slab_state slab_state;
  27. LIST_HEAD(slab_caches);
  28. DEFINE_MUTEX(slab_mutex);
  29. struct kmem_cache *kmem_cache;
  30. #ifdef CONFIG_HARDENED_USERCOPY
  31. bool usercopy_fallback __ro_after_init =
  32. IS_ENABLED(CONFIG_HARDENED_USERCOPY_FALLBACK);
  33. module_param(usercopy_fallback, bool, 0400);
  34. MODULE_PARM_DESC(usercopy_fallback,
  35. "WARN instead of reject usercopy whitelist violations");
  36. #endif
  37. static LIST_HEAD(slab_caches_to_rcu_destroy);
  38. static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
  39. static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
  40. slab_caches_to_rcu_destroy_workfn);
  41. /*
  42. * Set of flags that will prevent slab merging
  43. */
  44. #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  45. SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
  46. SLAB_FAILSLAB | SLAB_KASAN)
  47. #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
  48. SLAB_ACCOUNT)
  49. /*
  50. * Merge control. If this is set then no merging of slab caches will occur.
  51. */
  52. static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);
  53. static int __init setup_slab_nomerge(char *str)
  54. {
  55. slab_nomerge = true;
  56. return 1;
  57. }
  58. #ifdef CONFIG_SLUB
  59. __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
  60. #endif
  61. __setup("slab_nomerge", setup_slab_nomerge);
  62. /*
  63. * Determine the size of a slab object
  64. */
  65. unsigned int kmem_cache_size(struct kmem_cache *s)
  66. {
  67. return s->object_size;
  68. }
  69. EXPORT_SYMBOL(kmem_cache_size);
  70. #ifdef CONFIG_DEBUG_VM
  71. static int kmem_cache_sanity_check(const char *name, unsigned int size)
  72. {
  73. if (!name || in_interrupt() || size < sizeof(void *) ||
  74. size > KMALLOC_MAX_SIZE) {
  75. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  76. return -EINVAL;
  77. }
  78. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  79. return 0;
  80. }
  81. #else
  82. static inline int kmem_cache_sanity_check(const char *name, unsigned int size)
  83. {
  84. return 0;
  85. }
  86. #endif
  87. void __kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
  88. {
  89. size_t i;
  90. for (i = 0; i < nr; i++) {
  91. if (s)
  92. kmem_cache_free(s, p[i]);
  93. else
  94. kfree(p[i]);
  95. }
  96. }
  97. int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
  98. void **p)
  99. {
  100. size_t i;
  101. for (i = 0; i < nr; i++) {
  102. void *x = p[i] = kmem_cache_alloc(s, flags);
  103. if (!x) {
  104. __kmem_cache_free_bulk(s, i, p);
  105. return 0;
  106. }
  107. }
  108. return i;
  109. }
  110. #ifdef CONFIG_MEMCG_KMEM
  111. LIST_HEAD(slab_root_caches);
  112. void slab_init_memcg_params(struct kmem_cache *s)
  113. {
  114. s->memcg_params.root_cache = NULL;
  115. RCU_INIT_POINTER(s->memcg_params.memcg_caches, NULL);
  116. INIT_LIST_HEAD(&s->memcg_params.children);
  117. s->memcg_params.dying = false;
  118. }
  119. static int init_memcg_params(struct kmem_cache *s,
  120. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  121. {
  122. struct memcg_cache_array *arr;
  123. if (root_cache) {
  124. s->memcg_params.root_cache = root_cache;
  125. s->memcg_params.memcg = memcg;
  126. INIT_LIST_HEAD(&s->memcg_params.children_node);
  127. INIT_LIST_HEAD(&s->memcg_params.kmem_caches_node);
  128. return 0;
  129. }
  130. slab_init_memcg_params(s);
  131. if (!memcg_nr_cache_ids)
  132. return 0;
  133. arr = kvzalloc(sizeof(struct memcg_cache_array) +
  134. memcg_nr_cache_ids * sizeof(void *),
  135. GFP_KERNEL);
  136. if (!arr)
  137. return -ENOMEM;
  138. RCU_INIT_POINTER(s->memcg_params.memcg_caches, arr);
  139. return 0;
  140. }
  141. static void destroy_memcg_params(struct kmem_cache *s)
  142. {
  143. if (is_root_cache(s))
  144. kvfree(rcu_access_pointer(s->memcg_params.memcg_caches));
  145. }
  146. static void free_memcg_params(struct rcu_head *rcu)
  147. {
  148. struct memcg_cache_array *old;
  149. old = container_of(rcu, struct memcg_cache_array, rcu);
  150. kvfree(old);
  151. }
  152. static int update_memcg_params(struct kmem_cache *s, int new_array_size)
  153. {
  154. struct memcg_cache_array *old, *new;
  155. new = kvzalloc(sizeof(struct memcg_cache_array) +
  156. new_array_size * sizeof(void *), GFP_KERNEL);
  157. if (!new)
  158. return -ENOMEM;
  159. old = rcu_dereference_protected(s->memcg_params.memcg_caches,
  160. lockdep_is_held(&slab_mutex));
  161. if (old)
  162. memcpy(new->entries, old->entries,
  163. memcg_nr_cache_ids * sizeof(void *));
  164. rcu_assign_pointer(s->memcg_params.memcg_caches, new);
  165. if (old)
  166. call_rcu(&old->rcu, free_memcg_params);
  167. return 0;
  168. }
  169. int memcg_update_all_caches(int num_memcgs)
  170. {
  171. struct kmem_cache *s;
  172. int ret = 0;
  173. mutex_lock(&slab_mutex);
  174. list_for_each_entry(s, &slab_root_caches, root_caches_node) {
  175. ret = update_memcg_params(s, num_memcgs);
  176. /*
  177. * Instead of freeing the memory, we'll just leave the caches
  178. * up to this point in an updated state.
  179. */
  180. if (ret)
  181. break;
  182. }
  183. mutex_unlock(&slab_mutex);
  184. return ret;
  185. }
  186. void memcg_link_cache(struct kmem_cache *s)
  187. {
  188. if (is_root_cache(s)) {
  189. list_add(&s->root_caches_node, &slab_root_caches);
  190. } else {
  191. list_add(&s->memcg_params.children_node,
  192. &s->memcg_params.root_cache->memcg_params.children);
  193. list_add(&s->memcg_params.kmem_caches_node,
  194. &s->memcg_params.memcg->kmem_caches);
  195. }
  196. }
  197. static void memcg_unlink_cache(struct kmem_cache *s)
  198. {
  199. if (is_root_cache(s)) {
  200. list_del(&s->root_caches_node);
  201. } else {
  202. list_del(&s->memcg_params.children_node);
  203. list_del(&s->memcg_params.kmem_caches_node);
  204. }
  205. }
  206. #else
  207. static inline int init_memcg_params(struct kmem_cache *s,
  208. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  209. {
  210. return 0;
  211. }
  212. static inline void destroy_memcg_params(struct kmem_cache *s)
  213. {
  214. }
  215. static inline void memcg_unlink_cache(struct kmem_cache *s)
  216. {
  217. }
  218. #endif /* CONFIG_MEMCG_KMEM */
  219. /*
  220. * Figure out what the alignment of the objects will be given a set of
  221. * flags, a user specified alignment and the size of the objects.
  222. */
  223. static unsigned int calculate_alignment(slab_flags_t flags,
  224. unsigned int align, unsigned int size)
  225. {
  226. /*
  227. * If the user wants hardware cache aligned objects then follow that
  228. * suggestion if the object is sufficiently large.
  229. *
  230. * The hardware cache alignment cannot override the specified
  231. * alignment though. If that is greater then use it.
  232. */
  233. if (flags & SLAB_HWCACHE_ALIGN) {
  234. unsigned int ralign;
  235. ralign = cache_line_size();
  236. while (size <= ralign / 2)
  237. ralign /= 2;
  238. align = max(align, ralign);
  239. }
  240. if (align < ARCH_SLAB_MINALIGN)
  241. align = ARCH_SLAB_MINALIGN;
  242. return ALIGN(align, sizeof(void *));
  243. }
  244. /*
  245. * Find a mergeable slab cache
  246. */
  247. int slab_unmergeable(struct kmem_cache *s)
  248. {
  249. if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
  250. return 1;
  251. if (!is_root_cache(s))
  252. return 1;
  253. if (s->ctor)
  254. return 1;
  255. if (s->usersize)
  256. return 1;
  257. /*
  258. * We may have set a slab to be unmergeable during bootstrap.
  259. */
  260. if (s->refcount < 0)
  261. return 1;
  262. return 0;
  263. }
  264. struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
  265. slab_flags_t flags, const char *name, void (*ctor)(void *))
  266. {
  267. struct kmem_cache *s;
  268. if (slab_nomerge)
  269. return NULL;
  270. if (ctor)
  271. return NULL;
  272. size = ALIGN(size, sizeof(void *));
  273. align = calculate_alignment(flags, align, size);
  274. size = ALIGN(size, align);
  275. flags = kmem_cache_flags(size, flags, name, NULL);
  276. if (flags & SLAB_NEVER_MERGE)
  277. return NULL;
  278. list_for_each_entry_reverse(s, &slab_root_caches, root_caches_node) {
  279. if (slab_unmergeable(s))
  280. continue;
  281. if (size > s->size)
  282. continue;
  283. if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
  284. continue;
  285. /*
  286. * Check if alignment is compatible.
  287. * Courtesy of Adrian Drzewiecki
  288. */
  289. if ((s->size & ~(align - 1)) != s->size)
  290. continue;
  291. if (s->size - size >= sizeof(void *))
  292. continue;
  293. if (IS_ENABLED(CONFIG_SLAB) && align &&
  294. (align > s->align || s->align % align))
  295. continue;
  296. return s;
  297. }
  298. return NULL;
  299. }
  300. static struct kmem_cache *create_cache(const char *name,
  301. unsigned int object_size, unsigned int align,
  302. slab_flags_t flags, unsigned int useroffset,
  303. unsigned int usersize, void (*ctor)(void *),
  304. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  305. {
  306. struct kmem_cache *s;
  307. int err;
  308. if (WARN_ON(useroffset + usersize > object_size))
  309. useroffset = usersize = 0;
  310. err = -ENOMEM;
  311. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  312. if (!s)
  313. goto out;
  314. s->name = name;
  315. s->size = s->object_size = object_size;
  316. s->align = align;
  317. s->ctor = ctor;
  318. s->useroffset = useroffset;
  319. s->usersize = usersize;
  320. err = init_memcg_params(s, memcg, root_cache);
  321. if (err)
  322. goto out_free_cache;
  323. err = __kmem_cache_create(s, flags);
  324. if (err)
  325. goto out_free_cache;
  326. s->refcount = 1;
  327. list_add(&s->list, &slab_caches);
  328. memcg_link_cache(s);
  329. out:
  330. if (err)
  331. return ERR_PTR(err);
  332. return s;
  333. out_free_cache:
  334. destroy_memcg_params(s);
  335. kmem_cache_free(kmem_cache, s);
  336. goto out;
  337. }
  338. /*
  339. * kmem_cache_create_usercopy - Create a cache.
  340. * @name: A string which is used in /proc/slabinfo to identify this cache.
  341. * @size: The size of objects to be created in this cache.
  342. * @align: The required alignment for the objects.
  343. * @flags: SLAB flags
  344. * @useroffset: Usercopy region offset
  345. * @usersize: Usercopy region size
  346. * @ctor: A constructor for the objects.
  347. *
  348. * Returns a ptr to the cache on success, NULL on failure.
  349. * Cannot be called within a interrupt, but can be interrupted.
  350. * The @ctor is run when new pages are allocated by the cache.
  351. *
  352. * The flags are
  353. *
  354. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  355. * to catch references to uninitialised memory.
  356. *
  357. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  358. * for buffer overruns.
  359. *
  360. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  361. * cacheline. This can be beneficial if you're counting cycles as closely
  362. * as davem.
  363. */
  364. struct kmem_cache *
  365. kmem_cache_create_usercopy(const char *name,
  366. unsigned int size, unsigned int align,
  367. slab_flags_t flags,
  368. unsigned int useroffset, unsigned int usersize,
  369. void (*ctor)(void *))
  370. {
  371. struct kmem_cache *s = NULL;
  372. const char *cache_name;
  373. int err;
  374. get_online_cpus();
  375. get_online_mems();
  376. memcg_get_cache_ids();
  377. mutex_lock(&slab_mutex);
  378. err = kmem_cache_sanity_check(name, size);
  379. if (err) {
  380. goto out_unlock;
  381. }
  382. /* Refuse requests with allocator specific flags */
  383. if (flags & ~SLAB_FLAGS_PERMITTED) {
  384. err = -EINVAL;
  385. goto out_unlock;
  386. }
  387. /*
  388. * Some allocators will constraint the set of valid flags to a subset
  389. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  390. * case, and we'll just provide them with a sanitized version of the
  391. * passed flags.
  392. */
  393. flags &= CACHE_CREATE_MASK;
  394. /* Fail closed on bad usersize of useroffset values. */
  395. if (WARN_ON(!usersize && useroffset) ||
  396. WARN_ON(size < usersize || size - usersize < useroffset))
  397. usersize = useroffset = 0;
  398. if (!usersize)
  399. s = __kmem_cache_alias(name, size, align, flags, ctor);
  400. if (s)
  401. goto out_unlock;
  402. cache_name = kstrdup_const(name, GFP_KERNEL);
  403. if (!cache_name) {
  404. err = -ENOMEM;
  405. goto out_unlock;
  406. }
  407. s = create_cache(cache_name, size,
  408. calculate_alignment(flags, align, size),
  409. flags, useroffset, usersize, ctor, NULL, NULL);
  410. if (IS_ERR(s)) {
  411. err = PTR_ERR(s);
  412. kfree_const(cache_name);
  413. }
  414. out_unlock:
  415. mutex_unlock(&slab_mutex);
  416. memcg_put_cache_ids();
  417. put_online_mems();
  418. put_online_cpus();
  419. if (err) {
  420. if (flags & SLAB_PANIC)
  421. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  422. name, err);
  423. else {
  424. pr_warn("kmem_cache_create(%s) failed with error %d\n",
  425. name, err);
  426. dump_stack();
  427. }
  428. return NULL;
  429. }
  430. return s;
  431. }
  432. EXPORT_SYMBOL(kmem_cache_create_usercopy);
  433. struct kmem_cache *
  434. kmem_cache_create(const char *name, unsigned int size, unsigned int align,
  435. slab_flags_t flags, void (*ctor)(void *))
  436. {
  437. return kmem_cache_create_usercopy(name, size, align, flags, 0, 0,
  438. ctor);
  439. }
  440. EXPORT_SYMBOL(kmem_cache_create);
  441. static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
  442. {
  443. LIST_HEAD(to_destroy);
  444. struct kmem_cache *s, *s2;
  445. /*
  446. * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the
  447. * @slab_caches_to_rcu_destroy list. The slab pages are freed
  448. * through RCU and and the associated kmem_cache are dereferenced
  449. * while freeing the pages, so the kmem_caches should be freed only
  450. * after the pending RCU operations are finished. As rcu_barrier()
  451. * is a pretty slow operation, we batch all pending destructions
  452. * asynchronously.
  453. */
  454. mutex_lock(&slab_mutex);
  455. list_splice_init(&slab_caches_to_rcu_destroy, &to_destroy);
  456. mutex_unlock(&slab_mutex);
  457. if (list_empty(&to_destroy))
  458. return;
  459. rcu_barrier();
  460. list_for_each_entry_safe(s, s2, &to_destroy, list) {
  461. #ifdef SLAB_SUPPORTS_SYSFS
  462. sysfs_slab_release(s);
  463. #else
  464. slab_kmem_cache_release(s);
  465. #endif
  466. }
  467. }
  468. static int shutdown_cache(struct kmem_cache *s)
  469. {
  470. /* free asan quarantined objects */
  471. kasan_cache_shutdown(s);
  472. if (__kmem_cache_shutdown(s) != 0)
  473. return -EBUSY;
  474. memcg_unlink_cache(s);
  475. list_del(&s->list);
  476. if (s->flags & SLAB_TYPESAFE_BY_RCU) {
  477. #ifdef SLAB_SUPPORTS_SYSFS
  478. sysfs_slab_unlink(s);
  479. #endif
  480. list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
  481. schedule_work(&slab_caches_to_rcu_destroy_work);
  482. } else {
  483. #ifdef SLAB_SUPPORTS_SYSFS
  484. sysfs_slab_unlink(s);
  485. sysfs_slab_release(s);
  486. #else
  487. slab_kmem_cache_release(s);
  488. #endif
  489. }
  490. return 0;
  491. }
  492. #ifdef CONFIG_MEMCG_KMEM
  493. /*
  494. * memcg_create_kmem_cache - Create a cache for a memory cgroup.
  495. * @memcg: The memory cgroup the new cache is for.
  496. * @root_cache: The parent of the new cache.
  497. *
  498. * This function attempts to create a kmem cache that will serve allocation
  499. * requests going from @memcg to @root_cache. The new cache inherits properties
  500. * from its parent.
  501. */
  502. void memcg_create_kmem_cache(struct mem_cgroup *memcg,
  503. struct kmem_cache *root_cache)
  504. {
  505. static char memcg_name_buf[NAME_MAX + 1]; /* protected by slab_mutex */
  506. struct cgroup_subsys_state *css = &memcg->css;
  507. struct memcg_cache_array *arr;
  508. struct kmem_cache *s = NULL;
  509. char *cache_name;
  510. int idx;
  511. get_online_cpus();
  512. get_online_mems();
  513. mutex_lock(&slab_mutex);
  514. /*
  515. * The memory cgroup could have been offlined while the cache
  516. * creation work was pending.
  517. */
  518. if (memcg->kmem_state != KMEM_ONLINE || root_cache->memcg_params.dying)
  519. goto out_unlock;
  520. idx = memcg_cache_id(memcg);
  521. arr = rcu_dereference_protected(root_cache->memcg_params.memcg_caches,
  522. lockdep_is_held(&slab_mutex));
  523. /*
  524. * Since per-memcg caches are created asynchronously on first
  525. * allocation (see memcg_kmem_get_cache()), several threads can try to
  526. * create the same cache, but only one of them may succeed.
  527. */
  528. if (arr->entries[idx])
  529. goto out_unlock;
  530. cgroup_name(css->cgroup, memcg_name_buf, sizeof(memcg_name_buf));
  531. cache_name = kasprintf(GFP_KERNEL, "%s(%llu:%s)", root_cache->name,
  532. css->serial_nr, memcg_name_buf);
  533. if (!cache_name)
  534. goto out_unlock;
  535. s = create_cache(cache_name, root_cache->object_size,
  536. root_cache->align,
  537. root_cache->flags & CACHE_CREATE_MASK,
  538. root_cache->useroffset, root_cache->usersize,
  539. root_cache->ctor, memcg, root_cache);
  540. /*
  541. * If we could not create a memcg cache, do not complain, because
  542. * that's not critical at all as we can always proceed with the root
  543. * cache.
  544. */
  545. if (IS_ERR(s)) {
  546. kfree(cache_name);
  547. goto out_unlock;
  548. }
  549. /*
  550. * Since readers won't lock (see cache_from_memcg_idx()), we need a
  551. * barrier here to ensure nobody will see the kmem_cache partially
  552. * initialized.
  553. */
  554. smp_wmb();
  555. arr->entries[idx] = s;
  556. out_unlock:
  557. mutex_unlock(&slab_mutex);
  558. put_online_mems();
  559. put_online_cpus();
  560. }
  561. static void kmemcg_deactivate_workfn(struct work_struct *work)
  562. {
  563. struct kmem_cache *s = container_of(work, struct kmem_cache,
  564. memcg_params.deact_work);
  565. get_online_cpus();
  566. get_online_mems();
  567. mutex_lock(&slab_mutex);
  568. s->memcg_params.deact_fn(s);
  569. mutex_unlock(&slab_mutex);
  570. put_online_mems();
  571. put_online_cpus();
  572. /* done, put the ref from slab_deactivate_memcg_cache_rcu_sched() */
  573. css_put(&s->memcg_params.memcg->css);
  574. }
  575. static void kmemcg_deactivate_rcufn(struct rcu_head *head)
  576. {
  577. struct kmem_cache *s = container_of(head, struct kmem_cache,
  578. memcg_params.deact_rcu_head);
  579. /*
  580. * We need to grab blocking locks. Bounce to ->deact_work. The
  581. * work item shares the space with the RCU head and can't be
  582. * initialized eariler.
  583. */
  584. INIT_WORK(&s->memcg_params.deact_work, kmemcg_deactivate_workfn);
  585. queue_work(memcg_kmem_cache_wq, &s->memcg_params.deact_work);
  586. }
  587. /**
  588. * slab_deactivate_memcg_cache_rcu_sched - schedule deactivation after a
  589. * sched RCU grace period
  590. * @s: target kmem_cache
  591. * @deact_fn: deactivation function to call
  592. *
  593. * Schedule @deact_fn to be invoked with online cpus, mems and slab_mutex
  594. * held after a sched RCU grace period. The slab is guaranteed to stay
  595. * alive until @deact_fn is finished. This is to be used from
  596. * __kmemcg_cache_deactivate().
  597. */
  598. void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s,
  599. void (*deact_fn)(struct kmem_cache *))
  600. {
  601. if (WARN_ON_ONCE(is_root_cache(s)) ||
  602. WARN_ON_ONCE(s->memcg_params.deact_fn))
  603. return;
  604. if (s->memcg_params.root_cache->memcg_params.dying)
  605. return;
  606. /* pin memcg so that @s doesn't get destroyed in the middle */
  607. css_get(&s->memcg_params.memcg->css);
  608. s->memcg_params.deact_fn = deact_fn;
  609. call_rcu_sched(&s->memcg_params.deact_rcu_head, kmemcg_deactivate_rcufn);
  610. }
  611. void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
  612. {
  613. int idx;
  614. struct memcg_cache_array *arr;
  615. struct kmem_cache *s, *c;
  616. idx = memcg_cache_id(memcg);
  617. get_online_cpus();
  618. get_online_mems();
  619. mutex_lock(&slab_mutex);
  620. list_for_each_entry(s, &slab_root_caches, root_caches_node) {
  621. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  622. lockdep_is_held(&slab_mutex));
  623. c = arr->entries[idx];
  624. if (!c)
  625. continue;
  626. __kmemcg_cache_deactivate(c);
  627. arr->entries[idx] = NULL;
  628. }
  629. mutex_unlock(&slab_mutex);
  630. put_online_mems();
  631. put_online_cpus();
  632. }
  633. void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
  634. {
  635. struct kmem_cache *s, *s2;
  636. get_online_cpus();
  637. get_online_mems();
  638. mutex_lock(&slab_mutex);
  639. list_for_each_entry_safe(s, s2, &memcg->kmem_caches,
  640. memcg_params.kmem_caches_node) {
  641. /*
  642. * The cgroup is about to be freed and therefore has no charges
  643. * left. Hence, all its caches must be empty by now.
  644. */
  645. BUG_ON(shutdown_cache(s));
  646. }
  647. mutex_unlock(&slab_mutex);
  648. put_online_mems();
  649. put_online_cpus();
  650. }
  651. static int shutdown_memcg_caches(struct kmem_cache *s)
  652. {
  653. struct memcg_cache_array *arr;
  654. struct kmem_cache *c, *c2;
  655. LIST_HEAD(busy);
  656. int i;
  657. BUG_ON(!is_root_cache(s));
  658. /*
  659. * First, shutdown active caches, i.e. caches that belong to online
  660. * memory cgroups.
  661. */
  662. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  663. lockdep_is_held(&slab_mutex));
  664. for_each_memcg_cache_index(i) {
  665. c = arr->entries[i];
  666. if (!c)
  667. continue;
  668. if (shutdown_cache(c))
  669. /*
  670. * The cache still has objects. Move it to a temporary
  671. * list so as not to try to destroy it for a second
  672. * time while iterating over inactive caches below.
  673. */
  674. list_move(&c->memcg_params.children_node, &busy);
  675. else
  676. /*
  677. * The cache is empty and will be destroyed soon. Clear
  678. * the pointer to it in the memcg_caches array so that
  679. * it will never be accessed even if the root cache
  680. * stays alive.
  681. */
  682. arr->entries[i] = NULL;
  683. }
  684. /*
  685. * Second, shutdown all caches left from memory cgroups that are now
  686. * offline.
  687. */
  688. list_for_each_entry_safe(c, c2, &s->memcg_params.children,
  689. memcg_params.children_node)
  690. shutdown_cache(c);
  691. list_splice(&busy, &s->memcg_params.children);
  692. /*
  693. * A cache being destroyed must be empty. In particular, this means
  694. * that all per memcg caches attached to it must be empty too.
  695. */
  696. if (!list_empty(&s->memcg_params.children))
  697. return -EBUSY;
  698. return 0;
  699. }
  700. static void flush_memcg_workqueue(struct kmem_cache *s)
  701. {
  702. mutex_lock(&slab_mutex);
  703. s->memcg_params.dying = true;
  704. mutex_unlock(&slab_mutex);
  705. /*
  706. * SLUB deactivates the kmem_caches through call_rcu_sched. Make
  707. * sure all registered rcu callbacks have been invoked.
  708. */
  709. if (IS_ENABLED(CONFIG_SLUB))
  710. rcu_barrier_sched();
  711. /*
  712. * SLAB and SLUB create memcg kmem_caches through workqueue and SLUB
  713. * deactivates the memcg kmem_caches through workqueue. Make sure all
  714. * previous workitems on workqueue are processed.
  715. */
  716. flush_workqueue(memcg_kmem_cache_wq);
  717. }
  718. #else
  719. static inline int shutdown_memcg_caches(struct kmem_cache *s)
  720. {
  721. return 0;
  722. }
  723. static inline void flush_memcg_workqueue(struct kmem_cache *s)
  724. {
  725. }
  726. #endif /* CONFIG_MEMCG_KMEM */
  727. void slab_kmem_cache_release(struct kmem_cache *s)
  728. {
  729. __kmem_cache_release(s);
  730. destroy_memcg_params(s);
  731. kfree_const(s->name);
  732. kmem_cache_free(kmem_cache, s);
  733. }
  734. void kmem_cache_destroy(struct kmem_cache *s)
  735. {
  736. int err;
  737. if (unlikely(!s))
  738. return;
  739. flush_memcg_workqueue(s);
  740. get_online_cpus();
  741. get_online_mems();
  742. mutex_lock(&slab_mutex);
  743. s->refcount--;
  744. if (s->refcount)
  745. goto out_unlock;
  746. err = shutdown_memcg_caches(s);
  747. if (!err)
  748. err = shutdown_cache(s);
  749. if (err) {
  750. pr_err("kmem_cache_destroy %s: Slab cache still has objects\n",
  751. s->name);
  752. dump_stack();
  753. }
  754. out_unlock:
  755. mutex_unlock(&slab_mutex);
  756. put_online_mems();
  757. put_online_cpus();
  758. }
  759. EXPORT_SYMBOL(kmem_cache_destroy);
  760. /**
  761. * kmem_cache_shrink - Shrink a cache.
  762. * @cachep: The cache to shrink.
  763. *
  764. * Releases as many slabs as possible for a cache.
  765. * To help debugging, a zero exit status indicates all slabs were released.
  766. */
  767. int kmem_cache_shrink(struct kmem_cache *cachep)
  768. {
  769. int ret;
  770. get_online_cpus();
  771. get_online_mems();
  772. kasan_cache_shrink(cachep);
  773. ret = __kmem_cache_shrink(cachep);
  774. put_online_mems();
  775. put_online_cpus();
  776. return ret;
  777. }
  778. EXPORT_SYMBOL(kmem_cache_shrink);
  779. bool slab_is_available(void)
  780. {
  781. return slab_state >= UP;
  782. }
  783. #ifndef CONFIG_SLOB
  784. /* Create a cache during boot when no slab services are available yet */
  785. void __init create_boot_cache(struct kmem_cache *s, const char *name,
  786. unsigned int size, slab_flags_t flags,
  787. unsigned int useroffset, unsigned int usersize)
  788. {
  789. int err;
  790. s->name = name;
  791. s->size = s->object_size = size;
  792. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  793. s->useroffset = useroffset;
  794. s->usersize = usersize;
  795. slab_init_memcg_params(s);
  796. err = __kmem_cache_create(s, flags);
  797. if (err)
  798. panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
  799. name, size, err);
  800. s->refcount = -1; /* Exempt from merging for now */
  801. }
  802. struct kmem_cache *__init create_kmalloc_cache(const char *name,
  803. unsigned int size, slab_flags_t flags,
  804. unsigned int useroffset, unsigned int usersize)
  805. {
  806. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  807. if (!s)
  808. panic("Out of memory when creating slab %s\n", name);
  809. create_boot_cache(s, name, size, flags, useroffset, usersize);
  810. list_add(&s->list, &slab_caches);
  811. memcg_link_cache(s);
  812. s->refcount = 1;
  813. return s;
  814. }
  815. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __ro_after_init;
  816. EXPORT_SYMBOL(kmalloc_caches);
  817. #ifdef CONFIG_ZONE_DMA
  818. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1] __ro_after_init;
  819. EXPORT_SYMBOL(kmalloc_dma_caches);
  820. #endif
  821. /*
  822. * Conversion table for small slabs sizes / 8 to the index in the
  823. * kmalloc array. This is necessary for slabs < 192 since we have non power
  824. * of two cache sizes there. The size of larger slabs can be determined using
  825. * fls.
  826. */
  827. static u8 size_index[24] __ro_after_init = {
  828. 3, /* 8 */
  829. 4, /* 16 */
  830. 5, /* 24 */
  831. 5, /* 32 */
  832. 6, /* 40 */
  833. 6, /* 48 */
  834. 6, /* 56 */
  835. 6, /* 64 */
  836. 1, /* 72 */
  837. 1, /* 80 */
  838. 1, /* 88 */
  839. 1, /* 96 */
  840. 7, /* 104 */
  841. 7, /* 112 */
  842. 7, /* 120 */
  843. 7, /* 128 */
  844. 2, /* 136 */
  845. 2, /* 144 */
  846. 2, /* 152 */
  847. 2, /* 160 */
  848. 2, /* 168 */
  849. 2, /* 176 */
  850. 2, /* 184 */
  851. 2 /* 192 */
  852. };
  853. static inline unsigned int size_index_elem(unsigned int bytes)
  854. {
  855. return (bytes - 1) / 8;
  856. }
  857. /*
  858. * Find the kmem_cache structure that serves a given size of
  859. * allocation
  860. */
  861. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  862. {
  863. unsigned int index;
  864. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  865. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  866. return NULL;
  867. }
  868. if (size <= 192) {
  869. if (!size)
  870. return ZERO_SIZE_PTR;
  871. index = size_index[size_index_elem(size)];
  872. } else
  873. index = fls(size - 1);
  874. #ifdef CONFIG_ZONE_DMA
  875. if (unlikely((flags & GFP_DMA)))
  876. return kmalloc_dma_caches[index];
  877. #endif
  878. return kmalloc_caches[index];
  879. }
  880. /*
  881. * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
  882. * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
  883. * kmalloc-67108864.
  884. */
  885. const struct kmalloc_info_struct kmalloc_info[] __initconst = {
  886. {NULL, 0}, {"kmalloc-96", 96},
  887. {"kmalloc-192", 192}, {"kmalloc-8", 8},
  888. {"kmalloc-16", 16}, {"kmalloc-32", 32},
  889. {"kmalloc-64", 64}, {"kmalloc-128", 128},
  890. {"kmalloc-256", 256}, {"kmalloc-512", 512},
  891. {"kmalloc-1024", 1024}, {"kmalloc-2048", 2048},
  892. {"kmalloc-4096", 4096}, {"kmalloc-8192", 8192},
  893. {"kmalloc-16384", 16384}, {"kmalloc-32768", 32768},
  894. {"kmalloc-65536", 65536}, {"kmalloc-131072", 131072},
  895. {"kmalloc-262144", 262144}, {"kmalloc-524288", 524288},
  896. {"kmalloc-1048576", 1048576}, {"kmalloc-2097152", 2097152},
  897. {"kmalloc-4194304", 4194304}, {"kmalloc-8388608", 8388608},
  898. {"kmalloc-16777216", 16777216}, {"kmalloc-33554432", 33554432},
  899. {"kmalloc-67108864", 67108864}
  900. };
  901. /*
  902. * Patch up the size_index table if we have strange large alignment
  903. * requirements for the kmalloc array. This is only the case for
  904. * MIPS it seems. The standard arches will not generate any code here.
  905. *
  906. * Largest permitted alignment is 256 bytes due to the way we
  907. * handle the index determination for the smaller caches.
  908. *
  909. * Make sure that nothing crazy happens if someone starts tinkering
  910. * around with ARCH_KMALLOC_MINALIGN
  911. */
  912. void __init setup_kmalloc_cache_index_table(void)
  913. {
  914. unsigned int i;
  915. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  916. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  917. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  918. unsigned int elem = size_index_elem(i);
  919. if (elem >= ARRAY_SIZE(size_index))
  920. break;
  921. size_index[elem] = KMALLOC_SHIFT_LOW;
  922. }
  923. if (KMALLOC_MIN_SIZE >= 64) {
  924. /*
  925. * The 96 byte size cache is not used if the alignment
  926. * is 64 byte.
  927. */
  928. for (i = 64 + 8; i <= 96; i += 8)
  929. size_index[size_index_elem(i)] = 7;
  930. }
  931. if (KMALLOC_MIN_SIZE >= 128) {
  932. /*
  933. * The 192 byte sized cache is not used if the alignment
  934. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  935. * instead.
  936. */
  937. for (i = 128 + 8; i <= 192; i += 8)
  938. size_index[size_index_elem(i)] = 8;
  939. }
  940. }
  941. static void __init new_kmalloc_cache(int idx, slab_flags_t flags)
  942. {
  943. kmalloc_caches[idx] = create_kmalloc_cache(kmalloc_info[idx].name,
  944. kmalloc_info[idx].size, flags, 0,
  945. kmalloc_info[idx].size);
  946. }
  947. /*
  948. * Create the kmalloc array. Some of the regular kmalloc arrays
  949. * may already have been created because they were needed to
  950. * enable allocations for slab creation.
  951. */
  952. void __init create_kmalloc_caches(slab_flags_t flags)
  953. {
  954. int i;
  955. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  956. if (!kmalloc_caches[i])
  957. new_kmalloc_cache(i, flags);
  958. /*
  959. * Caches that are not of the two-to-the-power-of size.
  960. * These have to be created immediately after the
  961. * earlier power of two caches
  962. */
  963. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  964. new_kmalloc_cache(1, flags);
  965. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  966. new_kmalloc_cache(2, flags);
  967. }
  968. /* Kmalloc array is now usable */
  969. slab_state = UP;
  970. #ifdef CONFIG_ZONE_DMA
  971. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  972. struct kmem_cache *s = kmalloc_caches[i];
  973. if (s) {
  974. unsigned int size = kmalloc_size(i);
  975. char *n = kasprintf(GFP_NOWAIT,
  976. "dma-kmalloc-%u", size);
  977. BUG_ON(!n);
  978. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  979. size, SLAB_CACHE_DMA | flags, 0, 0);
  980. }
  981. }
  982. #endif
  983. }
  984. #endif /* !CONFIG_SLOB */
  985. /*
  986. * To avoid unnecessary overhead, we pass through large allocation requests
  987. * directly to the page allocator. We use __GFP_COMP, because we will need to
  988. * know the allocation order to free the pages properly in kfree.
  989. */
  990. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  991. {
  992. void *ret;
  993. struct page *page;
  994. flags |= __GFP_COMP;
  995. page = alloc_pages(flags, order);
  996. ret = page ? page_address(page) : NULL;
  997. kmemleak_alloc(ret, size, 1, flags);
  998. kasan_kmalloc_large(ret, size, flags);
  999. return ret;
  1000. }
  1001. EXPORT_SYMBOL(kmalloc_order);
  1002. #ifdef CONFIG_TRACING
  1003. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  1004. {
  1005. void *ret = kmalloc_order(size, flags, order);
  1006. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  1007. return ret;
  1008. }
  1009. EXPORT_SYMBOL(kmalloc_order_trace);
  1010. #endif
  1011. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  1012. /* Randomize a generic freelist */
  1013. static void freelist_randomize(struct rnd_state *state, unsigned int *list,
  1014. unsigned int count)
  1015. {
  1016. unsigned int rand;
  1017. unsigned int i;
  1018. for (i = 0; i < count; i++)
  1019. list[i] = i;
  1020. /* Fisher-Yates shuffle */
  1021. for (i = count - 1; i > 0; i--) {
  1022. rand = prandom_u32_state(state);
  1023. rand %= (i + 1);
  1024. swap(list[i], list[rand]);
  1025. }
  1026. }
  1027. /* Create a random sequence per cache */
  1028. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  1029. gfp_t gfp)
  1030. {
  1031. struct rnd_state state;
  1032. if (count < 2 || cachep->random_seq)
  1033. return 0;
  1034. cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
  1035. if (!cachep->random_seq)
  1036. return -ENOMEM;
  1037. /* Get best entropy at this stage of boot */
  1038. prandom_seed_state(&state, get_random_long());
  1039. freelist_randomize(&state, cachep->random_seq, count);
  1040. return 0;
  1041. }
  1042. /* Destroy the per-cache random freelist sequence */
  1043. void cache_random_seq_destroy(struct kmem_cache *cachep)
  1044. {
  1045. kfree(cachep->random_seq);
  1046. cachep->random_seq = NULL;
  1047. }
  1048. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  1049. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
  1050. #ifdef CONFIG_SLAB
  1051. #define SLABINFO_RIGHTS (0600)
  1052. #else
  1053. #define SLABINFO_RIGHTS (0400)
  1054. #endif
  1055. static void print_slabinfo_header(struct seq_file *m)
  1056. {
  1057. /*
  1058. * Output format version, so at least we can change it
  1059. * without _too_ many complaints.
  1060. */
  1061. #ifdef CONFIG_DEBUG_SLAB
  1062. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  1063. #else
  1064. seq_puts(m, "slabinfo - version: 2.1\n");
  1065. #endif
  1066. seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
  1067. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  1068. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  1069. #ifdef CONFIG_DEBUG_SLAB
  1070. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  1071. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  1072. #endif
  1073. seq_putc(m, '\n');
  1074. }
  1075. void *slab_start(struct seq_file *m, loff_t *pos)
  1076. {
  1077. mutex_lock(&slab_mutex);
  1078. return seq_list_start(&slab_root_caches, *pos);
  1079. }
  1080. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  1081. {
  1082. return seq_list_next(p, &slab_root_caches, pos);
  1083. }
  1084. void slab_stop(struct seq_file *m, void *p)
  1085. {
  1086. mutex_unlock(&slab_mutex);
  1087. }
  1088. static void
  1089. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  1090. {
  1091. struct kmem_cache *c;
  1092. struct slabinfo sinfo;
  1093. if (!is_root_cache(s))
  1094. return;
  1095. for_each_memcg_cache(c, s) {
  1096. memset(&sinfo, 0, sizeof(sinfo));
  1097. get_slabinfo(c, &sinfo);
  1098. info->active_slabs += sinfo.active_slabs;
  1099. info->num_slabs += sinfo.num_slabs;
  1100. info->shared_avail += sinfo.shared_avail;
  1101. info->active_objs += sinfo.active_objs;
  1102. info->num_objs += sinfo.num_objs;
  1103. }
  1104. }
  1105. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  1106. {
  1107. struct slabinfo sinfo;
  1108. memset(&sinfo, 0, sizeof(sinfo));
  1109. get_slabinfo(s, &sinfo);
  1110. memcg_accumulate_slabinfo(s, &sinfo);
  1111. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  1112. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  1113. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  1114. seq_printf(m, " : tunables %4u %4u %4u",
  1115. sinfo.limit, sinfo.batchcount, sinfo.shared);
  1116. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  1117. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  1118. slabinfo_show_stats(m, s);
  1119. seq_putc(m, '\n');
  1120. }
  1121. static int slab_show(struct seq_file *m, void *p)
  1122. {
  1123. struct kmem_cache *s = list_entry(p, struct kmem_cache, root_caches_node);
  1124. if (p == slab_root_caches.next)
  1125. print_slabinfo_header(m);
  1126. cache_show(s, m);
  1127. return 0;
  1128. }
  1129. void dump_unreclaimable_slab(void)
  1130. {
  1131. struct kmem_cache *s, *s2;
  1132. struct slabinfo sinfo;
  1133. /*
  1134. * Here acquiring slab_mutex is risky since we don't prefer to get
  1135. * sleep in oom path. But, without mutex hold, it may introduce a
  1136. * risk of crash.
  1137. * Use mutex_trylock to protect the list traverse, dump nothing
  1138. * without acquiring the mutex.
  1139. */
  1140. if (!mutex_trylock(&slab_mutex)) {
  1141. pr_warn("excessive unreclaimable slab but cannot dump stats\n");
  1142. return;
  1143. }
  1144. pr_info("Unreclaimable slab info:\n");
  1145. pr_info("Name Used Total\n");
  1146. list_for_each_entry_safe(s, s2, &slab_caches, list) {
  1147. if (!is_root_cache(s) || (s->flags & SLAB_RECLAIM_ACCOUNT))
  1148. continue;
  1149. get_slabinfo(s, &sinfo);
  1150. if (sinfo.num_objs > 0)
  1151. pr_info("%-17s %10luKB %10luKB\n", cache_name(s),
  1152. (sinfo.active_objs * s->size) / 1024,
  1153. (sinfo.num_objs * s->size) / 1024);
  1154. }
  1155. mutex_unlock(&slab_mutex);
  1156. }
  1157. #if defined(CONFIG_MEMCG)
  1158. void *memcg_slab_start(struct seq_file *m, loff_t *pos)
  1159. {
  1160. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  1161. mutex_lock(&slab_mutex);
  1162. return seq_list_start(&memcg->kmem_caches, *pos);
  1163. }
  1164. void *memcg_slab_next(struct seq_file *m, void *p, loff_t *pos)
  1165. {
  1166. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  1167. return seq_list_next(p, &memcg->kmem_caches, pos);
  1168. }
  1169. void memcg_slab_stop(struct seq_file *m, void *p)
  1170. {
  1171. mutex_unlock(&slab_mutex);
  1172. }
  1173. int memcg_slab_show(struct seq_file *m, void *p)
  1174. {
  1175. struct kmem_cache *s = list_entry(p, struct kmem_cache,
  1176. memcg_params.kmem_caches_node);
  1177. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  1178. if (p == memcg->kmem_caches.next)
  1179. print_slabinfo_header(m);
  1180. cache_show(s, m);
  1181. return 0;
  1182. }
  1183. #endif
  1184. /*
  1185. * slabinfo_op - iterator that generates /proc/slabinfo
  1186. *
  1187. * Output layout:
  1188. * cache-name
  1189. * num-active-objs
  1190. * total-objs
  1191. * object size
  1192. * num-active-slabs
  1193. * total-slabs
  1194. * num-pages-per-slab
  1195. * + further values on SMP and with statistics enabled
  1196. */
  1197. static const struct seq_operations slabinfo_op = {
  1198. .start = slab_start,
  1199. .next = slab_next,
  1200. .stop = slab_stop,
  1201. .show = slab_show,
  1202. };
  1203. static int slabinfo_open(struct inode *inode, struct file *file)
  1204. {
  1205. return seq_open(file, &slabinfo_op);
  1206. }
  1207. static const struct file_operations proc_slabinfo_operations = {
  1208. .open = slabinfo_open,
  1209. .read = seq_read,
  1210. .write = slabinfo_write,
  1211. .llseek = seq_lseek,
  1212. .release = seq_release,
  1213. };
  1214. static int __init slab_proc_init(void)
  1215. {
  1216. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  1217. &proc_slabinfo_operations);
  1218. return 0;
  1219. }
  1220. module_init(slab_proc_init);
  1221. #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */
  1222. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  1223. gfp_t flags)
  1224. {
  1225. void *ret;
  1226. size_t ks = 0;
  1227. if (p)
  1228. ks = ksize(p);
  1229. if (ks >= new_size) {
  1230. kasan_krealloc((void *)p, new_size, flags);
  1231. return (void *)p;
  1232. }
  1233. ret = kmalloc_track_caller(new_size, flags);
  1234. if (ret && p)
  1235. memcpy(ret, p, ks);
  1236. return ret;
  1237. }
  1238. /**
  1239. * __krealloc - like krealloc() but don't free @p.
  1240. * @p: object to reallocate memory for.
  1241. * @new_size: how many bytes of memory are required.
  1242. * @flags: the type of memory to allocate.
  1243. *
  1244. * This function is like krealloc() except it never frees the originally
  1245. * allocated buffer. Use this if you don't want to free the buffer immediately
  1246. * like, for example, with RCU.
  1247. */
  1248. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  1249. {
  1250. if (unlikely(!new_size))
  1251. return ZERO_SIZE_PTR;
  1252. return __do_krealloc(p, new_size, flags);
  1253. }
  1254. EXPORT_SYMBOL(__krealloc);
  1255. /**
  1256. * krealloc - reallocate memory. The contents will remain unchanged.
  1257. * @p: object to reallocate memory for.
  1258. * @new_size: how many bytes of memory are required.
  1259. * @flags: the type of memory to allocate.
  1260. *
  1261. * The contents of the object pointed to are preserved up to the
  1262. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  1263. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  1264. * %NULL pointer, the object pointed to is freed.
  1265. */
  1266. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  1267. {
  1268. void *ret;
  1269. if (unlikely(!new_size)) {
  1270. kfree(p);
  1271. return ZERO_SIZE_PTR;
  1272. }
  1273. ret = __do_krealloc(p, new_size, flags);
  1274. if (ret && p != ret)
  1275. kfree(p);
  1276. return ret;
  1277. }
  1278. EXPORT_SYMBOL(krealloc);
  1279. /**
  1280. * kzfree - like kfree but zero memory
  1281. * @p: object to free memory of
  1282. *
  1283. * The memory of the object @p points to is zeroed before freed.
  1284. * If @p is %NULL, kzfree() does nothing.
  1285. *
  1286. * Note: this function zeroes the whole allocated buffer which can be a good
  1287. * deal bigger than the requested buffer size passed to kmalloc(). So be
  1288. * careful when using this function in performance sensitive code.
  1289. */
  1290. void kzfree(const void *p)
  1291. {
  1292. size_t ks;
  1293. void *mem = (void *)p;
  1294. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  1295. return;
  1296. ks = ksize(mem);
  1297. memset(mem, 0, ks);
  1298. kfree(mem);
  1299. }
  1300. EXPORT_SYMBOL(kzfree);
  1301. /* Tracepoints definitions. */
  1302. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  1303. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  1304. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  1305. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  1306. EXPORT_TRACEPOINT_SYMBOL(kfree);
  1307. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
  1308. int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
  1309. {
  1310. if (__should_failslab(s, gfpflags))
  1311. return -ENOMEM;
  1312. return 0;
  1313. }
  1314. ALLOW_ERROR_INJECTION(should_failslab, ERRNO);