slab_common.c 35 KB

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