slab_common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. #ifdef CONFIG_DEBUG_VM
  29. static int kmem_cache_sanity_check(const char *name, size_t size)
  30. {
  31. struct kmem_cache *s = NULL;
  32. if (!name || in_interrupt() || size < sizeof(void *) ||
  33. size > KMALLOC_MAX_SIZE) {
  34. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  35. return -EINVAL;
  36. }
  37. list_for_each_entry(s, &slab_caches, list) {
  38. char tmp;
  39. int res;
  40. /*
  41. * This happens when the module gets unloaded and doesn't
  42. * destroy its slab cache and no-one else reuses the vmalloc
  43. * area of the module. Print a warning.
  44. */
  45. res = probe_kernel_address(s->name, tmp);
  46. if (res) {
  47. pr_err("Slab cache with size %d has lost its name\n",
  48. s->object_size);
  49. continue;
  50. }
  51. #if !defined(CONFIG_SLUB)
  52. if (!strcmp(s->name, name)) {
  53. pr_err("%s (%s): Cache name already exists.\n",
  54. __func__, name);
  55. dump_stack();
  56. s = NULL;
  57. return -EINVAL;
  58. }
  59. #endif
  60. }
  61. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  62. return 0;
  63. }
  64. #else
  65. static inline int kmem_cache_sanity_check(const char *name, size_t size)
  66. {
  67. return 0;
  68. }
  69. #endif
  70. #ifdef CONFIG_MEMCG_KMEM
  71. int memcg_update_all_caches(int num_memcgs)
  72. {
  73. struct kmem_cache *s;
  74. int ret = 0;
  75. mutex_lock(&slab_mutex);
  76. list_for_each_entry(s, &slab_caches, list) {
  77. if (!is_root_cache(s))
  78. continue;
  79. ret = memcg_update_cache_size(s, num_memcgs);
  80. /*
  81. * See comment in memcontrol.c, memcg_update_cache_size:
  82. * Instead of freeing the memory, we'll just leave the caches
  83. * up to this point in an updated state.
  84. */
  85. if (ret)
  86. goto out;
  87. }
  88. memcg_update_array_size(num_memcgs);
  89. out:
  90. mutex_unlock(&slab_mutex);
  91. return ret;
  92. }
  93. #endif
  94. /*
  95. * Figure out what the alignment of the objects will be given a set of
  96. * flags, a user specified alignment and the size of the objects.
  97. */
  98. unsigned long calculate_alignment(unsigned long flags,
  99. unsigned long align, unsigned long size)
  100. {
  101. /*
  102. * If the user wants hardware cache aligned objects then follow that
  103. * suggestion if the object is sufficiently large.
  104. *
  105. * The hardware cache alignment cannot override the specified
  106. * alignment though. If that is greater then use it.
  107. */
  108. if (flags & SLAB_HWCACHE_ALIGN) {
  109. unsigned long ralign = cache_line_size();
  110. while (size <= ralign / 2)
  111. ralign /= 2;
  112. align = max(align, ralign);
  113. }
  114. if (align < ARCH_SLAB_MINALIGN)
  115. align = ARCH_SLAB_MINALIGN;
  116. return ALIGN(align, sizeof(void *));
  117. }
  118. static struct kmem_cache *
  119. do_kmem_cache_create(char *name, size_t object_size, size_t size, size_t align,
  120. unsigned long flags, void (*ctor)(void *),
  121. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  122. {
  123. struct kmem_cache *s;
  124. int err;
  125. err = -ENOMEM;
  126. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  127. if (!s)
  128. goto out;
  129. s->name = name;
  130. s->object_size = object_size;
  131. s->size = size;
  132. s->align = align;
  133. s->ctor = ctor;
  134. err = memcg_alloc_cache_params(memcg, s, root_cache);
  135. if (err)
  136. goto out_free_cache;
  137. err = __kmem_cache_create(s, flags);
  138. if (err)
  139. goto out_free_cache;
  140. s->refcount = 1;
  141. list_add(&s->list, &slab_caches);
  142. out:
  143. if (err)
  144. return ERR_PTR(err);
  145. return s;
  146. out_free_cache:
  147. memcg_free_cache_params(s);
  148. kfree(s);
  149. goto out;
  150. }
  151. /*
  152. * kmem_cache_create - Create a cache.
  153. * @name: A string which is used in /proc/slabinfo to identify this cache.
  154. * @size: The size of objects to be created in this cache.
  155. * @align: The required alignment for the objects.
  156. * @flags: SLAB flags
  157. * @ctor: A constructor for the objects.
  158. *
  159. * Returns a ptr to the cache on success, NULL on failure.
  160. * Cannot be called within a interrupt, but can be interrupted.
  161. * The @ctor is run when new pages are allocated by the cache.
  162. *
  163. * The flags are
  164. *
  165. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  166. * to catch references to uninitialised memory.
  167. *
  168. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  169. * for buffer overruns.
  170. *
  171. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  172. * cacheline. This can be beneficial if you're counting cycles as closely
  173. * as davem.
  174. */
  175. struct kmem_cache *
  176. kmem_cache_create(const char *name, size_t size, size_t align,
  177. unsigned long flags, void (*ctor)(void *))
  178. {
  179. struct kmem_cache *s;
  180. char *cache_name;
  181. int err;
  182. get_online_cpus();
  183. get_online_mems();
  184. mutex_lock(&slab_mutex);
  185. err = kmem_cache_sanity_check(name, size);
  186. if (err)
  187. goto out_unlock;
  188. /*
  189. * Some allocators will constraint the set of valid flags to a subset
  190. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  191. * case, and we'll just provide them with a sanitized version of the
  192. * passed flags.
  193. */
  194. flags &= CACHE_CREATE_MASK;
  195. s = __kmem_cache_alias(name, size, align, flags, ctor);
  196. if (s)
  197. goto out_unlock;
  198. cache_name = kstrdup(name, GFP_KERNEL);
  199. if (!cache_name) {
  200. err = -ENOMEM;
  201. goto out_unlock;
  202. }
  203. s = do_kmem_cache_create(cache_name, size, size,
  204. calculate_alignment(flags, align, size),
  205. flags, ctor, NULL, NULL);
  206. if (IS_ERR(s)) {
  207. err = PTR_ERR(s);
  208. kfree(cache_name);
  209. }
  210. out_unlock:
  211. mutex_unlock(&slab_mutex);
  212. put_online_mems();
  213. put_online_cpus();
  214. if (err) {
  215. if (flags & SLAB_PANIC)
  216. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  217. name, err);
  218. else {
  219. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  220. name, err);
  221. dump_stack();
  222. }
  223. return NULL;
  224. }
  225. return s;
  226. }
  227. EXPORT_SYMBOL(kmem_cache_create);
  228. #ifdef CONFIG_MEMCG_KMEM
  229. /*
  230. * memcg_create_kmem_cache - Create a cache for a memory cgroup.
  231. * @memcg: The memory cgroup the new cache is for.
  232. * @root_cache: The parent of the new cache.
  233. * @memcg_name: The name of the memory cgroup (used for naming the new cache).
  234. *
  235. * This function attempts to create a kmem cache that will serve allocation
  236. * requests going from @memcg to @root_cache. The new cache inherits properties
  237. * from its parent.
  238. */
  239. struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
  240. struct kmem_cache *root_cache,
  241. const char *memcg_name)
  242. {
  243. struct kmem_cache *s = NULL;
  244. char *cache_name;
  245. get_online_cpus();
  246. get_online_mems();
  247. mutex_lock(&slab_mutex);
  248. cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
  249. memcg_cache_id(memcg), memcg_name);
  250. if (!cache_name)
  251. goto out_unlock;
  252. s = do_kmem_cache_create(cache_name, root_cache->object_size,
  253. root_cache->size, root_cache->align,
  254. root_cache->flags, root_cache->ctor,
  255. memcg, root_cache);
  256. if (IS_ERR(s)) {
  257. kfree(cache_name);
  258. s = NULL;
  259. }
  260. out_unlock:
  261. mutex_unlock(&slab_mutex);
  262. put_online_mems();
  263. put_online_cpus();
  264. return s;
  265. }
  266. static int memcg_cleanup_cache_params(struct kmem_cache *s)
  267. {
  268. int rc;
  269. if (!s->memcg_params ||
  270. !s->memcg_params->is_root_cache)
  271. return 0;
  272. mutex_unlock(&slab_mutex);
  273. rc = __memcg_cleanup_cache_params(s);
  274. mutex_lock(&slab_mutex);
  275. return rc;
  276. }
  277. #else
  278. static int memcg_cleanup_cache_params(struct kmem_cache *s)
  279. {
  280. return 0;
  281. }
  282. #endif /* CONFIG_MEMCG_KMEM */
  283. void slab_kmem_cache_release(struct kmem_cache *s)
  284. {
  285. kfree(s->name);
  286. kmem_cache_free(kmem_cache, s);
  287. }
  288. void kmem_cache_destroy(struct kmem_cache *s)
  289. {
  290. get_online_cpus();
  291. get_online_mems();
  292. mutex_lock(&slab_mutex);
  293. s->refcount--;
  294. if (s->refcount)
  295. goto out_unlock;
  296. if (memcg_cleanup_cache_params(s) != 0)
  297. goto out_unlock;
  298. if (__kmem_cache_shutdown(s) != 0) {
  299. printk(KERN_ERR "kmem_cache_destroy %s: "
  300. "Slab cache still has objects\n", s->name);
  301. dump_stack();
  302. goto out_unlock;
  303. }
  304. list_del(&s->list);
  305. mutex_unlock(&slab_mutex);
  306. if (s->flags & SLAB_DESTROY_BY_RCU)
  307. rcu_barrier();
  308. memcg_free_cache_params(s);
  309. #ifdef SLAB_SUPPORTS_SYSFS
  310. sysfs_slab_remove(s);
  311. #else
  312. slab_kmem_cache_release(s);
  313. #endif
  314. goto out;
  315. out_unlock:
  316. mutex_unlock(&slab_mutex);
  317. out:
  318. put_online_mems();
  319. put_online_cpus();
  320. }
  321. EXPORT_SYMBOL(kmem_cache_destroy);
  322. /**
  323. * kmem_cache_shrink - Shrink a cache.
  324. * @cachep: The cache to shrink.
  325. *
  326. * Releases as many slabs as possible for a cache.
  327. * To help debugging, a zero exit status indicates all slabs were released.
  328. */
  329. int kmem_cache_shrink(struct kmem_cache *cachep)
  330. {
  331. int ret;
  332. get_online_cpus();
  333. get_online_mems();
  334. ret = __kmem_cache_shrink(cachep);
  335. put_online_mems();
  336. put_online_cpus();
  337. return ret;
  338. }
  339. EXPORT_SYMBOL(kmem_cache_shrink);
  340. int slab_is_available(void)
  341. {
  342. return slab_state >= UP;
  343. }
  344. #ifndef CONFIG_SLOB
  345. /* Create a cache during boot when no slab services are available yet */
  346. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  347. unsigned long flags)
  348. {
  349. int err;
  350. s->name = name;
  351. s->size = s->object_size = size;
  352. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  353. err = __kmem_cache_create(s, flags);
  354. if (err)
  355. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  356. name, size, err);
  357. s->refcount = -1; /* Exempt from merging for now */
  358. }
  359. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  360. unsigned long flags)
  361. {
  362. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  363. if (!s)
  364. panic("Out of memory when creating slab %s\n", name);
  365. create_boot_cache(s, name, size, flags);
  366. list_add(&s->list, &slab_caches);
  367. s->refcount = 1;
  368. return s;
  369. }
  370. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  371. EXPORT_SYMBOL(kmalloc_caches);
  372. #ifdef CONFIG_ZONE_DMA
  373. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  374. EXPORT_SYMBOL(kmalloc_dma_caches);
  375. #endif
  376. /*
  377. * Conversion table for small slabs sizes / 8 to the index in the
  378. * kmalloc array. This is necessary for slabs < 192 since we have non power
  379. * of two cache sizes there. The size of larger slabs can be determined using
  380. * fls.
  381. */
  382. static s8 size_index[24] = {
  383. 3, /* 8 */
  384. 4, /* 16 */
  385. 5, /* 24 */
  386. 5, /* 32 */
  387. 6, /* 40 */
  388. 6, /* 48 */
  389. 6, /* 56 */
  390. 6, /* 64 */
  391. 1, /* 72 */
  392. 1, /* 80 */
  393. 1, /* 88 */
  394. 1, /* 96 */
  395. 7, /* 104 */
  396. 7, /* 112 */
  397. 7, /* 120 */
  398. 7, /* 128 */
  399. 2, /* 136 */
  400. 2, /* 144 */
  401. 2, /* 152 */
  402. 2, /* 160 */
  403. 2, /* 168 */
  404. 2, /* 176 */
  405. 2, /* 184 */
  406. 2 /* 192 */
  407. };
  408. static inline int size_index_elem(size_t bytes)
  409. {
  410. return (bytes - 1) / 8;
  411. }
  412. /*
  413. * Find the kmem_cache structure that serves a given size of
  414. * allocation
  415. */
  416. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  417. {
  418. int index;
  419. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  420. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  421. return NULL;
  422. }
  423. if (size <= 192) {
  424. if (!size)
  425. return ZERO_SIZE_PTR;
  426. index = size_index[size_index_elem(size)];
  427. } else
  428. index = fls(size - 1);
  429. #ifdef CONFIG_ZONE_DMA
  430. if (unlikely((flags & GFP_DMA)))
  431. return kmalloc_dma_caches[index];
  432. #endif
  433. return kmalloc_caches[index];
  434. }
  435. /*
  436. * Create the kmalloc array. Some of the regular kmalloc arrays
  437. * may already have been created because they were needed to
  438. * enable allocations for slab creation.
  439. */
  440. void __init create_kmalloc_caches(unsigned long flags)
  441. {
  442. int i;
  443. /*
  444. * Patch up the size_index table if we have strange large alignment
  445. * requirements for the kmalloc array. This is only the case for
  446. * MIPS it seems. The standard arches will not generate any code here.
  447. *
  448. * Largest permitted alignment is 256 bytes due to the way we
  449. * handle the index determination for the smaller caches.
  450. *
  451. * Make sure that nothing crazy happens if someone starts tinkering
  452. * around with ARCH_KMALLOC_MINALIGN
  453. */
  454. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  455. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  456. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  457. int elem = size_index_elem(i);
  458. if (elem >= ARRAY_SIZE(size_index))
  459. break;
  460. size_index[elem] = KMALLOC_SHIFT_LOW;
  461. }
  462. if (KMALLOC_MIN_SIZE >= 64) {
  463. /*
  464. * The 96 byte size cache is not used if the alignment
  465. * is 64 byte.
  466. */
  467. for (i = 64 + 8; i <= 96; i += 8)
  468. size_index[size_index_elem(i)] = 7;
  469. }
  470. if (KMALLOC_MIN_SIZE >= 128) {
  471. /*
  472. * The 192 byte sized cache is not used if the alignment
  473. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  474. * instead.
  475. */
  476. for (i = 128 + 8; i <= 192; i += 8)
  477. size_index[size_index_elem(i)] = 8;
  478. }
  479. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  480. if (!kmalloc_caches[i]) {
  481. kmalloc_caches[i] = create_kmalloc_cache(NULL,
  482. 1 << i, flags);
  483. }
  484. /*
  485. * Caches that are not of the two-to-the-power-of size.
  486. * These have to be created immediately after the
  487. * earlier power of two caches
  488. */
  489. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  490. kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
  491. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  492. kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
  493. }
  494. /* Kmalloc array is now usable */
  495. slab_state = UP;
  496. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  497. struct kmem_cache *s = kmalloc_caches[i];
  498. char *n;
  499. if (s) {
  500. n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
  501. BUG_ON(!n);
  502. s->name = n;
  503. }
  504. }
  505. #ifdef CONFIG_ZONE_DMA
  506. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  507. struct kmem_cache *s = kmalloc_caches[i];
  508. if (s) {
  509. int size = kmalloc_size(i);
  510. char *n = kasprintf(GFP_NOWAIT,
  511. "dma-kmalloc-%d", size);
  512. BUG_ON(!n);
  513. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  514. size, SLAB_CACHE_DMA | flags);
  515. }
  516. }
  517. #endif
  518. }
  519. #endif /* !CONFIG_SLOB */
  520. /*
  521. * To avoid unnecessary overhead, we pass through large allocation requests
  522. * directly to the page allocator. We use __GFP_COMP, because we will need to
  523. * know the allocation order to free the pages properly in kfree.
  524. */
  525. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  526. {
  527. void *ret;
  528. struct page *page;
  529. flags |= __GFP_COMP;
  530. page = alloc_kmem_pages(flags, order);
  531. ret = page ? page_address(page) : NULL;
  532. kmemleak_alloc(ret, size, 1, flags);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL(kmalloc_order);
  536. #ifdef CONFIG_TRACING
  537. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  538. {
  539. void *ret = kmalloc_order(size, flags, order);
  540. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL(kmalloc_order_trace);
  544. #endif
  545. #ifdef CONFIG_SLABINFO
  546. #ifdef CONFIG_SLAB
  547. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  548. #else
  549. #define SLABINFO_RIGHTS S_IRUSR
  550. #endif
  551. void print_slabinfo_header(struct seq_file *m)
  552. {
  553. /*
  554. * Output format version, so at least we can change it
  555. * without _too_ many complaints.
  556. */
  557. #ifdef CONFIG_DEBUG_SLAB
  558. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  559. #else
  560. seq_puts(m, "slabinfo - version: 2.1\n");
  561. #endif
  562. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  563. "<objperslab> <pagesperslab>");
  564. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  565. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  566. #ifdef CONFIG_DEBUG_SLAB
  567. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  568. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  569. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  570. #endif
  571. seq_putc(m, '\n');
  572. }
  573. static void *s_start(struct seq_file *m, loff_t *pos)
  574. {
  575. loff_t n = *pos;
  576. mutex_lock(&slab_mutex);
  577. if (!n)
  578. print_slabinfo_header(m);
  579. return seq_list_start(&slab_caches, *pos);
  580. }
  581. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  582. {
  583. return seq_list_next(p, &slab_caches, pos);
  584. }
  585. void slab_stop(struct seq_file *m, void *p)
  586. {
  587. mutex_unlock(&slab_mutex);
  588. }
  589. static void
  590. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  591. {
  592. struct kmem_cache *c;
  593. struct slabinfo sinfo;
  594. int i;
  595. if (!is_root_cache(s))
  596. return;
  597. for_each_memcg_cache_index(i) {
  598. c = cache_from_memcg_idx(s, i);
  599. if (!c)
  600. continue;
  601. memset(&sinfo, 0, sizeof(sinfo));
  602. get_slabinfo(c, &sinfo);
  603. info->active_slabs += sinfo.active_slabs;
  604. info->num_slabs += sinfo.num_slabs;
  605. info->shared_avail += sinfo.shared_avail;
  606. info->active_objs += sinfo.active_objs;
  607. info->num_objs += sinfo.num_objs;
  608. }
  609. }
  610. int cache_show(struct kmem_cache *s, struct seq_file *m)
  611. {
  612. struct slabinfo sinfo;
  613. memset(&sinfo, 0, sizeof(sinfo));
  614. get_slabinfo(s, &sinfo);
  615. memcg_accumulate_slabinfo(s, &sinfo);
  616. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  617. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  618. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  619. seq_printf(m, " : tunables %4u %4u %4u",
  620. sinfo.limit, sinfo.batchcount, sinfo.shared);
  621. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  622. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  623. slabinfo_show_stats(m, s);
  624. seq_putc(m, '\n');
  625. return 0;
  626. }
  627. static int s_show(struct seq_file *m, void *p)
  628. {
  629. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  630. if (!is_root_cache(s))
  631. return 0;
  632. return cache_show(s, m);
  633. }
  634. /*
  635. * slabinfo_op - iterator that generates /proc/slabinfo
  636. *
  637. * Output layout:
  638. * cache-name
  639. * num-active-objs
  640. * total-objs
  641. * object size
  642. * num-active-slabs
  643. * total-slabs
  644. * num-pages-per-slab
  645. * + further values on SMP and with statistics enabled
  646. */
  647. static const struct seq_operations slabinfo_op = {
  648. .start = s_start,
  649. .next = slab_next,
  650. .stop = slab_stop,
  651. .show = s_show,
  652. };
  653. static int slabinfo_open(struct inode *inode, struct file *file)
  654. {
  655. return seq_open(file, &slabinfo_op);
  656. }
  657. static const struct file_operations proc_slabinfo_operations = {
  658. .open = slabinfo_open,
  659. .read = seq_read,
  660. .write = slabinfo_write,
  661. .llseek = seq_lseek,
  662. .release = seq_release,
  663. };
  664. static int __init slab_proc_init(void)
  665. {
  666. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  667. &proc_slabinfo_operations);
  668. return 0;
  669. }
  670. module_init(slab_proc_init);
  671. #endif /* CONFIG_SLABINFO */
  672. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  673. gfp_t flags)
  674. {
  675. void *ret;
  676. size_t ks = 0;
  677. if (p)
  678. ks = ksize(p);
  679. if (ks >= new_size)
  680. return (void *)p;
  681. ret = kmalloc_track_caller(new_size, flags);
  682. if (ret && p)
  683. memcpy(ret, p, ks);
  684. return ret;
  685. }
  686. /**
  687. * __krealloc - like krealloc() but don't free @p.
  688. * @p: object to reallocate memory for.
  689. * @new_size: how many bytes of memory are required.
  690. * @flags: the type of memory to allocate.
  691. *
  692. * This function is like krealloc() except it never frees the originally
  693. * allocated buffer. Use this if you don't want to free the buffer immediately
  694. * like, for example, with RCU.
  695. */
  696. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  697. {
  698. if (unlikely(!new_size))
  699. return ZERO_SIZE_PTR;
  700. return __do_krealloc(p, new_size, flags);
  701. }
  702. EXPORT_SYMBOL(__krealloc);
  703. /**
  704. * krealloc - reallocate memory. The contents will remain unchanged.
  705. * @p: object to reallocate memory for.
  706. * @new_size: how many bytes of memory are required.
  707. * @flags: the type of memory to allocate.
  708. *
  709. * The contents of the object pointed to are preserved up to the
  710. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  711. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  712. * %NULL pointer, the object pointed to is freed.
  713. */
  714. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  715. {
  716. void *ret;
  717. if (unlikely(!new_size)) {
  718. kfree(p);
  719. return ZERO_SIZE_PTR;
  720. }
  721. ret = __do_krealloc(p, new_size, flags);
  722. if (ret && p != ret)
  723. kfree(p);
  724. return ret;
  725. }
  726. EXPORT_SYMBOL(krealloc);
  727. /**
  728. * kzfree - like kfree but zero memory
  729. * @p: object to free memory of
  730. *
  731. * The memory of the object @p points to is zeroed before freed.
  732. * If @p is %NULL, kzfree() does nothing.
  733. *
  734. * Note: this function zeroes the whole allocated buffer which can be a good
  735. * deal bigger than the requested buffer size passed to kmalloc(). So be
  736. * careful when using this function in performance sensitive code.
  737. */
  738. void kzfree(const void *p)
  739. {
  740. size_t ks;
  741. void *mem = (void *)p;
  742. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  743. return;
  744. ks = ksize(mem);
  745. memset(mem, 0, ks);
  746. kfree(mem);
  747. }
  748. EXPORT_SYMBOL(kzfree);
  749. /* Tracepoints definitions. */
  750. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  751. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  752. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  753. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  754. EXPORT_TRACEPOINT_SYMBOL(kfree);
  755. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);