slab_common.c 30 KB

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