slab_common.c 30 KB

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