slab_common.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. int __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 0;
  105. }
  106. }
  107. return i;
  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 *create_cache(const char *name,
  265. size_t object_size, size_t size, size_t align,
  266. 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 = NULL;
  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. goto out_unlock;
  335. }
  336. /*
  337. * Some allocators will constraint the set of valid flags to a subset
  338. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  339. * case, and we'll just provide them with a sanitized version of the
  340. * passed flags.
  341. */
  342. flags &= CACHE_CREATE_MASK;
  343. s = __kmem_cache_alias(name, size, align, flags, ctor);
  344. if (s)
  345. goto out_unlock;
  346. cache_name = kstrdup_const(name, GFP_KERNEL);
  347. if (!cache_name) {
  348. err = -ENOMEM;
  349. goto out_unlock;
  350. }
  351. s = create_cache(cache_name, size, size,
  352. calculate_alignment(flags, align, size),
  353. flags, ctor, NULL, NULL);
  354. if (IS_ERR(s)) {
  355. err = PTR_ERR(s);
  356. kfree_const(cache_name);
  357. }
  358. out_unlock:
  359. mutex_unlock(&slab_mutex);
  360. memcg_put_cache_ids();
  361. put_online_mems();
  362. put_online_cpus();
  363. if (err) {
  364. if (flags & SLAB_PANIC)
  365. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  366. name, err);
  367. else {
  368. printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
  369. name, err);
  370. dump_stack();
  371. }
  372. return NULL;
  373. }
  374. return s;
  375. }
  376. EXPORT_SYMBOL(kmem_cache_create);
  377. static int shutdown_cache(struct kmem_cache *s,
  378. struct list_head *release, bool *need_rcu_barrier)
  379. {
  380. if (__kmem_cache_shutdown(s) != 0)
  381. return -EBUSY;
  382. if (s->flags & SLAB_DESTROY_BY_RCU)
  383. *need_rcu_barrier = true;
  384. list_move(&s->list, release);
  385. return 0;
  386. }
  387. static void release_caches(struct list_head *release, bool need_rcu_barrier)
  388. {
  389. struct kmem_cache *s, *s2;
  390. if (need_rcu_barrier)
  391. rcu_barrier();
  392. list_for_each_entry_safe(s, s2, release, list) {
  393. #ifdef SLAB_SUPPORTS_SYSFS
  394. sysfs_slab_remove(s);
  395. #else
  396. slab_kmem_cache_release(s);
  397. #endif
  398. }
  399. }
  400. #ifdef CONFIG_MEMCG_KMEM
  401. /*
  402. * memcg_create_kmem_cache - Create a cache for a memory cgroup.
  403. * @memcg: The memory cgroup the new cache is for.
  404. * @root_cache: The parent of the new cache.
  405. *
  406. * This function attempts to create a kmem cache that will serve allocation
  407. * requests going from @memcg to @root_cache. The new cache inherits properties
  408. * from its parent.
  409. */
  410. void memcg_create_kmem_cache(struct mem_cgroup *memcg,
  411. struct kmem_cache *root_cache)
  412. {
  413. static char memcg_name_buf[NAME_MAX + 1]; /* protected by slab_mutex */
  414. struct cgroup_subsys_state *css = &memcg->css;
  415. struct memcg_cache_array *arr;
  416. struct kmem_cache *s = NULL;
  417. char *cache_name;
  418. int idx;
  419. get_online_cpus();
  420. get_online_mems();
  421. mutex_lock(&slab_mutex);
  422. /*
  423. * The memory cgroup could have been deactivated while the cache
  424. * creation work was pending.
  425. */
  426. if (!memcg_kmem_is_active(memcg))
  427. goto out_unlock;
  428. idx = memcg_cache_id(memcg);
  429. arr = rcu_dereference_protected(root_cache->memcg_params.memcg_caches,
  430. lockdep_is_held(&slab_mutex));
  431. /*
  432. * Since per-memcg caches are created asynchronously on first
  433. * allocation (see memcg_kmem_get_cache()), several threads can try to
  434. * create the same cache, but only one of them may succeed.
  435. */
  436. if (arr->entries[idx])
  437. goto out_unlock;
  438. cgroup_name(css->cgroup, memcg_name_buf, sizeof(memcg_name_buf));
  439. cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
  440. css->id, memcg_name_buf);
  441. if (!cache_name)
  442. goto out_unlock;
  443. s = create_cache(cache_name, root_cache->object_size,
  444. root_cache->size, root_cache->align,
  445. root_cache->flags, root_cache->ctor,
  446. memcg, root_cache);
  447. /*
  448. * If we could not create a memcg cache, do not complain, because
  449. * that's not critical at all as we can always proceed with the root
  450. * cache.
  451. */
  452. if (IS_ERR(s)) {
  453. kfree(cache_name);
  454. goto out_unlock;
  455. }
  456. list_add(&s->memcg_params.list, &root_cache->memcg_params.list);
  457. /*
  458. * Since readers won't lock (see cache_from_memcg_idx()), we need a
  459. * barrier here to ensure nobody will see the kmem_cache partially
  460. * initialized.
  461. */
  462. smp_wmb();
  463. arr->entries[idx] = s;
  464. out_unlock:
  465. mutex_unlock(&slab_mutex);
  466. put_online_mems();
  467. put_online_cpus();
  468. }
  469. void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
  470. {
  471. int idx;
  472. struct memcg_cache_array *arr;
  473. struct kmem_cache *s, *c;
  474. idx = memcg_cache_id(memcg);
  475. get_online_cpus();
  476. get_online_mems();
  477. mutex_lock(&slab_mutex);
  478. list_for_each_entry(s, &slab_caches, list) {
  479. if (!is_root_cache(s))
  480. continue;
  481. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  482. lockdep_is_held(&slab_mutex));
  483. c = arr->entries[idx];
  484. if (!c)
  485. continue;
  486. __kmem_cache_shrink(c, true);
  487. arr->entries[idx] = NULL;
  488. }
  489. mutex_unlock(&slab_mutex);
  490. put_online_mems();
  491. put_online_cpus();
  492. }
  493. static int __shutdown_memcg_cache(struct kmem_cache *s,
  494. struct list_head *release, bool *need_rcu_barrier)
  495. {
  496. BUG_ON(is_root_cache(s));
  497. if (shutdown_cache(s, release, need_rcu_barrier))
  498. return -EBUSY;
  499. list_del(&s->memcg_params.list);
  500. return 0;
  501. }
  502. void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
  503. {
  504. LIST_HEAD(release);
  505. bool need_rcu_barrier = false;
  506. struct kmem_cache *s, *s2;
  507. get_online_cpus();
  508. get_online_mems();
  509. mutex_lock(&slab_mutex);
  510. list_for_each_entry_safe(s, s2, &slab_caches, list) {
  511. if (is_root_cache(s) || s->memcg_params.memcg != memcg)
  512. continue;
  513. /*
  514. * The cgroup is about to be freed and therefore has no charges
  515. * left. Hence, all its caches must be empty by now.
  516. */
  517. BUG_ON(__shutdown_memcg_cache(s, &release, &need_rcu_barrier));
  518. }
  519. mutex_unlock(&slab_mutex);
  520. put_online_mems();
  521. put_online_cpus();
  522. release_caches(&release, need_rcu_barrier);
  523. }
  524. static int shutdown_memcg_caches(struct kmem_cache *s,
  525. struct list_head *release, bool *need_rcu_barrier)
  526. {
  527. struct memcg_cache_array *arr;
  528. struct kmem_cache *c, *c2;
  529. LIST_HEAD(busy);
  530. int i;
  531. BUG_ON(!is_root_cache(s));
  532. /*
  533. * First, shutdown active caches, i.e. caches that belong to online
  534. * memory cgroups.
  535. */
  536. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  537. lockdep_is_held(&slab_mutex));
  538. for_each_memcg_cache_index(i) {
  539. c = arr->entries[i];
  540. if (!c)
  541. continue;
  542. if (__shutdown_memcg_cache(c, release, need_rcu_barrier))
  543. /*
  544. * The cache still has objects. Move it to a temporary
  545. * list so as not to try to destroy it for a second
  546. * time while iterating over inactive caches below.
  547. */
  548. list_move(&c->memcg_params.list, &busy);
  549. else
  550. /*
  551. * The cache is empty and will be destroyed soon. Clear
  552. * the pointer to it in the memcg_caches array so that
  553. * it will never be accessed even if the root cache
  554. * stays alive.
  555. */
  556. arr->entries[i] = NULL;
  557. }
  558. /*
  559. * Second, shutdown all caches left from memory cgroups that are now
  560. * offline.
  561. */
  562. list_for_each_entry_safe(c, c2, &s->memcg_params.list,
  563. memcg_params.list)
  564. __shutdown_memcg_cache(c, release, need_rcu_barrier);
  565. list_splice(&busy, &s->memcg_params.list);
  566. /*
  567. * A cache being destroyed must be empty. In particular, this means
  568. * that all per memcg caches attached to it must be empty too.
  569. */
  570. if (!list_empty(&s->memcg_params.list))
  571. return -EBUSY;
  572. return 0;
  573. }
  574. #else
  575. static inline int shutdown_memcg_caches(struct kmem_cache *s,
  576. struct list_head *release, bool *need_rcu_barrier)
  577. {
  578. return 0;
  579. }
  580. #endif /* CONFIG_MEMCG_KMEM */
  581. void slab_kmem_cache_release(struct kmem_cache *s)
  582. {
  583. destroy_memcg_params(s);
  584. kfree_const(s->name);
  585. kmem_cache_free(kmem_cache, s);
  586. }
  587. void kmem_cache_destroy(struct kmem_cache *s)
  588. {
  589. LIST_HEAD(release);
  590. bool need_rcu_barrier = false;
  591. int err;
  592. if (unlikely(!s))
  593. return;
  594. get_online_cpus();
  595. get_online_mems();
  596. mutex_lock(&slab_mutex);
  597. s->refcount--;
  598. if (s->refcount)
  599. goto out_unlock;
  600. err = shutdown_memcg_caches(s, &release, &need_rcu_barrier);
  601. if (!err)
  602. err = shutdown_cache(s, &release, &need_rcu_barrier);
  603. if (err) {
  604. pr_err("kmem_cache_destroy %s: "
  605. "Slab cache still has objects\n", s->name);
  606. dump_stack();
  607. }
  608. out_unlock:
  609. mutex_unlock(&slab_mutex);
  610. put_online_mems();
  611. put_online_cpus();
  612. release_caches(&release, need_rcu_barrier);
  613. }
  614. EXPORT_SYMBOL(kmem_cache_destroy);
  615. /**
  616. * kmem_cache_shrink - Shrink a cache.
  617. * @cachep: The cache to shrink.
  618. *
  619. * Releases as many slabs as possible for a cache.
  620. * To help debugging, a zero exit status indicates all slabs were released.
  621. */
  622. int kmem_cache_shrink(struct kmem_cache *cachep)
  623. {
  624. int ret;
  625. get_online_cpus();
  626. get_online_mems();
  627. ret = __kmem_cache_shrink(cachep, false);
  628. put_online_mems();
  629. put_online_cpus();
  630. return ret;
  631. }
  632. EXPORT_SYMBOL(kmem_cache_shrink);
  633. bool slab_is_available(void)
  634. {
  635. return slab_state >= UP;
  636. }
  637. #ifndef CONFIG_SLOB
  638. /* Create a cache during boot when no slab services are available yet */
  639. void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
  640. unsigned long flags)
  641. {
  642. int err;
  643. s->name = name;
  644. s->size = s->object_size = size;
  645. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  646. slab_init_memcg_params(s);
  647. err = __kmem_cache_create(s, flags);
  648. if (err)
  649. panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
  650. name, size, err);
  651. s->refcount = -1; /* Exempt from merging for now */
  652. }
  653. struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
  654. unsigned long flags)
  655. {
  656. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  657. if (!s)
  658. panic("Out of memory when creating slab %s\n", name);
  659. create_boot_cache(s, name, size, flags);
  660. list_add(&s->list, &slab_caches);
  661. s->refcount = 1;
  662. return s;
  663. }
  664. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
  665. EXPORT_SYMBOL(kmalloc_caches);
  666. #ifdef CONFIG_ZONE_DMA
  667. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
  668. EXPORT_SYMBOL(kmalloc_dma_caches);
  669. #endif
  670. /*
  671. * Conversion table for small slabs sizes / 8 to the index in the
  672. * kmalloc array. This is necessary for slabs < 192 since we have non power
  673. * of two cache sizes there. The size of larger slabs can be determined using
  674. * fls.
  675. */
  676. static s8 size_index[24] = {
  677. 3, /* 8 */
  678. 4, /* 16 */
  679. 5, /* 24 */
  680. 5, /* 32 */
  681. 6, /* 40 */
  682. 6, /* 48 */
  683. 6, /* 56 */
  684. 6, /* 64 */
  685. 1, /* 72 */
  686. 1, /* 80 */
  687. 1, /* 88 */
  688. 1, /* 96 */
  689. 7, /* 104 */
  690. 7, /* 112 */
  691. 7, /* 120 */
  692. 7, /* 128 */
  693. 2, /* 136 */
  694. 2, /* 144 */
  695. 2, /* 152 */
  696. 2, /* 160 */
  697. 2, /* 168 */
  698. 2, /* 176 */
  699. 2, /* 184 */
  700. 2 /* 192 */
  701. };
  702. static inline int size_index_elem(size_t bytes)
  703. {
  704. return (bytes - 1) / 8;
  705. }
  706. /*
  707. * Find the kmem_cache structure that serves a given size of
  708. * allocation
  709. */
  710. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  711. {
  712. int index;
  713. if (unlikely(size > KMALLOC_MAX_SIZE)) {
  714. WARN_ON_ONCE(!(flags & __GFP_NOWARN));
  715. return NULL;
  716. }
  717. if (size <= 192) {
  718. if (!size)
  719. return ZERO_SIZE_PTR;
  720. index = size_index[size_index_elem(size)];
  721. } else
  722. index = fls(size - 1);
  723. #ifdef CONFIG_ZONE_DMA
  724. if (unlikely((flags & GFP_DMA)))
  725. return kmalloc_dma_caches[index];
  726. #endif
  727. return kmalloc_caches[index];
  728. }
  729. /*
  730. * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
  731. * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
  732. * kmalloc-67108864.
  733. */
  734. static struct {
  735. const char *name;
  736. unsigned long size;
  737. } const kmalloc_info[] __initconst = {
  738. {NULL, 0}, {"kmalloc-96", 96},
  739. {"kmalloc-192", 192}, {"kmalloc-8", 8},
  740. {"kmalloc-16", 16}, {"kmalloc-32", 32},
  741. {"kmalloc-64", 64}, {"kmalloc-128", 128},
  742. {"kmalloc-256", 256}, {"kmalloc-512", 512},
  743. {"kmalloc-1024", 1024}, {"kmalloc-2048", 2048},
  744. {"kmalloc-4096", 4096}, {"kmalloc-8192", 8192},
  745. {"kmalloc-16384", 16384}, {"kmalloc-32768", 32768},
  746. {"kmalloc-65536", 65536}, {"kmalloc-131072", 131072},
  747. {"kmalloc-262144", 262144}, {"kmalloc-524288", 524288},
  748. {"kmalloc-1048576", 1048576}, {"kmalloc-2097152", 2097152},
  749. {"kmalloc-4194304", 4194304}, {"kmalloc-8388608", 8388608},
  750. {"kmalloc-16777216", 16777216}, {"kmalloc-33554432", 33554432},
  751. {"kmalloc-67108864", 67108864}
  752. };
  753. /*
  754. * Patch up the size_index table if we have strange large alignment
  755. * requirements for the kmalloc array. This is only the case for
  756. * MIPS it seems. The standard arches will not generate any code here.
  757. *
  758. * Largest permitted alignment is 256 bytes due to the way we
  759. * handle the index determination for the smaller caches.
  760. *
  761. * Make sure that nothing crazy happens if someone starts tinkering
  762. * around with ARCH_KMALLOC_MINALIGN
  763. */
  764. void __init setup_kmalloc_cache_index_table(void)
  765. {
  766. int i;
  767. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  768. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  769. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  770. int elem = size_index_elem(i);
  771. if (elem >= ARRAY_SIZE(size_index))
  772. break;
  773. size_index[elem] = KMALLOC_SHIFT_LOW;
  774. }
  775. if (KMALLOC_MIN_SIZE >= 64) {
  776. /*
  777. * The 96 byte size cache is not used if the alignment
  778. * is 64 byte.
  779. */
  780. for (i = 64 + 8; i <= 96; i += 8)
  781. size_index[size_index_elem(i)] = 7;
  782. }
  783. if (KMALLOC_MIN_SIZE >= 128) {
  784. /*
  785. * The 192 byte sized cache is not used if the alignment
  786. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  787. * instead.
  788. */
  789. for (i = 128 + 8; i <= 192; i += 8)
  790. size_index[size_index_elem(i)] = 8;
  791. }
  792. }
  793. static void __init new_kmalloc_cache(int idx, unsigned long flags)
  794. {
  795. kmalloc_caches[idx] = create_kmalloc_cache(kmalloc_info[idx].name,
  796. kmalloc_info[idx].size, flags);
  797. }
  798. /*
  799. * Create the kmalloc array. Some of the regular kmalloc arrays
  800. * may already have been created because they were needed to
  801. * enable allocations for slab creation.
  802. */
  803. void __init create_kmalloc_caches(unsigned long flags)
  804. {
  805. int i;
  806. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  807. if (!kmalloc_caches[i])
  808. new_kmalloc_cache(i, flags);
  809. /*
  810. * Caches that are not of the two-to-the-power-of size.
  811. * These have to be created immediately after the
  812. * earlier power of two caches
  813. */
  814. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  815. new_kmalloc_cache(1, flags);
  816. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  817. new_kmalloc_cache(2, flags);
  818. }
  819. /* Kmalloc array is now usable */
  820. slab_state = UP;
  821. #ifdef CONFIG_ZONE_DMA
  822. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  823. struct kmem_cache *s = kmalloc_caches[i];
  824. if (s) {
  825. int size = kmalloc_size(i);
  826. char *n = kasprintf(GFP_NOWAIT,
  827. "dma-kmalloc-%d", size);
  828. BUG_ON(!n);
  829. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  830. size, SLAB_CACHE_DMA | flags);
  831. }
  832. }
  833. #endif
  834. }
  835. #endif /* !CONFIG_SLOB */
  836. /*
  837. * To avoid unnecessary overhead, we pass through large allocation requests
  838. * directly to the page allocator. We use __GFP_COMP, because we will need to
  839. * know the allocation order to free the pages properly in kfree.
  840. */
  841. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  842. {
  843. void *ret;
  844. struct page *page;
  845. flags |= __GFP_COMP;
  846. page = alloc_kmem_pages(flags, order);
  847. ret = page ? page_address(page) : NULL;
  848. kmemleak_alloc(ret, size, 1, flags);
  849. kasan_kmalloc_large(ret, size);
  850. return ret;
  851. }
  852. EXPORT_SYMBOL(kmalloc_order);
  853. #ifdef CONFIG_TRACING
  854. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  855. {
  856. void *ret = kmalloc_order(size, flags, order);
  857. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  858. return ret;
  859. }
  860. EXPORT_SYMBOL(kmalloc_order_trace);
  861. #endif
  862. #ifdef CONFIG_SLABINFO
  863. #ifdef CONFIG_SLAB
  864. #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
  865. #else
  866. #define SLABINFO_RIGHTS S_IRUSR
  867. #endif
  868. static void print_slabinfo_header(struct seq_file *m)
  869. {
  870. /*
  871. * Output format version, so at least we can change it
  872. * without _too_ many complaints.
  873. */
  874. #ifdef CONFIG_DEBUG_SLAB
  875. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  876. #else
  877. seq_puts(m, "slabinfo - version: 2.1\n");
  878. #endif
  879. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  880. "<objperslab> <pagesperslab>");
  881. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  882. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  883. #ifdef CONFIG_DEBUG_SLAB
  884. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  885. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  886. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  887. #endif
  888. seq_putc(m, '\n');
  889. }
  890. void *slab_start(struct seq_file *m, loff_t *pos)
  891. {
  892. mutex_lock(&slab_mutex);
  893. return seq_list_start(&slab_caches, *pos);
  894. }
  895. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  896. {
  897. return seq_list_next(p, &slab_caches, pos);
  898. }
  899. void slab_stop(struct seq_file *m, void *p)
  900. {
  901. mutex_unlock(&slab_mutex);
  902. }
  903. static void
  904. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  905. {
  906. struct kmem_cache *c;
  907. struct slabinfo sinfo;
  908. if (!is_root_cache(s))
  909. return;
  910. for_each_memcg_cache(c, s) {
  911. memset(&sinfo, 0, sizeof(sinfo));
  912. get_slabinfo(c, &sinfo);
  913. info->active_slabs += sinfo.active_slabs;
  914. info->num_slabs += sinfo.num_slabs;
  915. info->shared_avail += sinfo.shared_avail;
  916. info->active_objs += sinfo.active_objs;
  917. info->num_objs += sinfo.num_objs;
  918. }
  919. }
  920. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  921. {
  922. struct slabinfo sinfo;
  923. memset(&sinfo, 0, sizeof(sinfo));
  924. get_slabinfo(s, &sinfo);
  925. memcg_accumulate_slabinfo(s, &sinfo);
  926. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  927. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  928. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  929. seq_printf(m, " : tunables %4u %4u %4u",
  930. sinfo.limit, sinfo.batchcount, sinfo.shared);
  931. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  932. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  933. slabinfo_show_stats(m, s);
  934. seq_putc(m, '\n');
  935. }
  936. static int slab_show(struct seq_file *m, void *p)
  937. {
  938. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  939. if (p == slab_caches.next)
  940. print_slabinfo_header(m);
  941. if (is_root_cache(s))
  942. cache_show(s, m);
  943. return 0;
  944. }
  945. #ifdef CONFIG_MEMCG_KMEM
  946. int memcg_slab_show(struct seq_file *m, void *p)
  947. {
  948. struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
  949. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  950. if (p == slab_caches.next)
  951. print_slabinfo_header(m);
  952. if (!is_root_cache(s) && s->memcg_params.memcg == memcg)
  953. cache_show(s, m);
  954. return 0;
  955. }
  956. #endif
  957. /*
  958. * slabinfo_op - iterator that generates /proc/slabinfo
  959. *
  960. * Output layout:
  961. * cache-name
  962. * num-active-objs
  963. * total-objs
  964. * object size
  965. * num-active-slabs
  966. * total-slabs
  967. * num-pages-per-slab
  968. * + further values on SMP and with statistics enabled
  969. */
  970. static const struct seq_operations slabinfo_op = {
  971. .start = slab_start,
  972. .next = slab_next,
  973. .stop = slab_stop,
  974. .show = slab_show,
  975. };
  976. static int slabinfo_open(struct inode *inode, struct file *file)
  977. {
  978. return seq_open(file, &slabinfo_op);
  979. }
  980. static const struct file_operations proc_slabinfo_operations = {
  981. .open = slabinfo_open,
  982. .read = seq_read,
  983. .write = slabinfo_write,
  984. .llseek = seq_lseek,
  985. .release = seq_release,
  986. };
  987. static int __init slab_proc_init(void)
  988. {
  989. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  990. &proc_slabinfo_operations);
  991. return 0;
  992. }
  993. module_init(slab_proc_init);
  994. #endif /* CONFIG_SLABINFO */
  995. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  996. gfp_t flags)
  997. {
  998. void *ret;
  999. size_t ks = 0;
  1000. if (p)
  1001. ks = ksize(p);
  1002. if (ks >= new_size) {
  1003. kasan_krealloc((void *)p, new_size);
  1004. return (void *)p;
  1005. }
  1006. ret = kmalloc_track_caller(new_size, flags);
  1007. if (ret && p)
  1008. memcpy(ret, p, ks);
  1009. return ret;
  1010. }
  1011. /**
  1012. * __krealloc - like krealloc() but don't free @p.
  1013. * @p: object to reallocate memory for.
  1014. * @new_size: how many bytes of memory are required.
  1015. * @flags: the type of memory to allocate.
  1016. *
  1017. * This function is like krealloc() except it never frees the originally
  1018. * allocated buffer. Use this if you don't want to free the buffer immediately
  1019. * like, for example, with RCU.
  1020. */
  1021. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  1022. {
  1023. if (unlikely(!new_size))
  1024. return ZERO_SIZE_PTR;
  1025. return __do_krealloc(p, new_size, flags);
  1026. }
  1027. EXPORT_SYMBOL(__krealloc);
  1028. /**
  1029. * krealloc - reallocate memory. The contents will remain unchanged.
  1030. * @p: object to reallocate memory for.
  1031. * @new_size: how many bytes of memory are required.
  1032. * @flags: the type of memory to allocate.
  1033. *
  1034. * The contents of the object pointed to are preserved up to the
  1035. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  1036. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  1037. * %NULL pointer, the object pointed to is freed.
  1038. */
  1039. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  1040. {
  1041. void *ret;
  1042. if (unlikely(!new_size)) {
  1043. kfree(p);
  1044. return ZERO_SIZE_PTR;
  1045. }
  1046. ret = __do_krealloc(p, new_size, flags);
  1047. if (ret && p != ret)
  1048. kfree(p);
  1049. return ret;
  1050. }
  1051. EXPORT_SYMBOL(krealloc);
  1052. /**
  1053. * kzfree - like kfree but zero memory
  1054. * @p: object to free memory of
  1055. *
  1056. * The memory of the object @p points to is zeroed before freed.
  1057. * If @p is %NULL, kzfree() does nothing.
  1058. *
  1059. * Note: this function zeroes the whole allocated buffer which can be a good
  1060. * deal bigger than the requested buffer size passed to kmalloc(). So be
  1061. * careful when using this function in performance sensitive code.
  1062. */
  1063. void kzfree(const void *p)
  1064. {
  1065. size_t ks;
  1066. void *mem = (void *)p;
  1067. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  1068. return;
  1069. ks = ksize(mem);
  1070. memset(mem, 0, ks);
  1071. kfree(mem);
  1072. }
  1073. EXPORT_SYMBOL(kzfree);
  1074. /* Tracepoints definitions. */
  1075. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  1076. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  1077. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  1078. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  1079. EXPORT_TRACEPOINT_SYMBOL(kfree);
  1080. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);