slab_common.c 34 KB

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