slab_common.c 28 KB

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