slab_common.c 24 KB

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