slab_common.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Slab allocator functions that are independent of the allocator strategy
  4. *
  5. * (C) 2012 Christoph Lameter <cl@linux.com>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/mm.h>
  9. #include <linux/poison.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/memory.h>
  12. #include <linux/cache.h>
  13. #include <linux/compiler.h>
  14. #include <linux/module.h>
  15. #include <linux/cpu.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/proc_fs.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/page.h>
  22. #include <linux/memcontrol.h>
  23. #define CREATE_TRACE_POINTS
  24. #include <trace/events/kmem.h>
  25. #include "slab.h"
  26. enum slab_state slab_state;
  27. LIST_HEAD(slab_caches);
  28. DEFINE_MUTEX(slab_mutex);
  29. struct kmem_cache *kmem_cache;
  30. #ifdef CONFIG_HARDENED_USERCOPY
  31. bool usercopy_fallback __ro_after_init =
  32. IS_ENABLED(CONFIG_HARDENED_USERCOPY_FALLBACK);
  33. module_param(usercopy_fallback, bool, 0400);
  34. MODULE_PARM_DESC(usercopy_fallback,
  35. "WARN instead of reject usercopy whitelist violations");
  36. #endif
  37. static LIST_HEAD(slab_caches_to_rcu_destroy);
  38. static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
  39. static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
  40. slab_caches_to_rcu_destroy_workfn);
  41. /*
  42. * Set of flags that will prevent slab merging
  43. */
  44. #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  45. SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
  46. SLAB_FAILSLAB | SLAB_KASAN)
  47. #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
  48. SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
  49. /*
  50. * Merge control. If this is set then no merging of slab caches will occur.
  51. */
  52. static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);
  53. static int __init setup_slab_nomerge(char *str)
  54. {
  55. slab_nomerge = true;
  56. return 1;
  57. }
  58. #ifdef CONFIG_SLUB
  59. __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
  60. #endif
  61. __setup("slab_nomerge", setup_slab_nomerge);
  62. /*
  63. * Determine the size of a slab object
  64. */
  65. unsigned int kmem_cache_size(struct kmem_cache *s)
  66. {
  67. return s->object_size;
  68. }
  69. EXPORT_SYMBOL(kmem_cache_size);
  70. #ifdef CONFIG_DEBUG_VM
  71. static int kmem_cache_sanity_check(const char *name, unsigned int size)
  72. {
  73. if (!name || in_interrupt() || size < sizeof(void *) ||
  74. size > KMALLOC_MAX_SIZE) {
  75. pr_err("kmem_cache_create(%s) integrity check failed\n", name);
  76. return -EINVAL;
  77. }
  78. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  79. return 0;
  80. }
  81. #else
  82. static inline int kmem_cache_sanity_check(const char *name, unsigned int size)
  83. {
  84. return 0;
  85. }
  86. #endif
  87. void __kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
  88. {
  89. size_t i;
  90. for (i = 0; i < nr; i++) {
  91. if (s)
  92. kmem_cache_free(s, p[i]);
  93. else
  94. kfree(p[i]);
  95. }
  96. }
  97. int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
  98. void **p)
  99. {
  100. size_t i;
  101. for (i = 0; i < nr; i++) {
  102. void *x = p[i] = kmem_cache_alloc(s, flags);
  103. if (!x) {
  104. __kmem_cache_free_bulk(s, i, p);
  105. return 0;
  106. }
  107. }
  108. return i;
  109. }
  110. #ifdef CONFIG_MEMCG_KMEM
  111. LIST_HEAD(slab_root_caches);
  112. static DEFINE_SPINLOCK(memcg_kmem_wq_lock);
  113. void slab_init_memcg_params(struct kmem_cache *s)
  114. {
  115. s->memcg_params.root_cache = NULL;
  116. RCU_INIT_POINTER(s->memcg_params.memcg_caches, NULL);
  117. INIT_LIST_HEAD(&s->memcg_params.children);
  118. s->memcg_params.dying = false;
  119. }
  120. static int init_memcg_params(struct kmem_cache *s,
  121. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  122. {
  123. struct memcg_cache_array *arr;
  124. if (root_cache) {
  125. s->memcg_params.root_cache = root_cache;
  126. s->memcg_params.memcg = memcg;
  127. INIT_LIST_HEAD(&s->memcg_params.children_node);
  128. INIT_LIST_HEAD(&s->memcg_params.kmem_caches_node);
  129. return 0;
  130. }
  131. slab_init_memcg_params(s);
  132. if (!memcg_nr_cache_ids)
  133. return 0;
  134. arr = kvzalloc(sizeof(struct memcg_cache_array) +
  135. memcg_nr_cache_ids * sizeof(void *),
  136. GFP_KERNEL);
  137. if (!arr)
  138. return -ENOMEM;
  139. RCU_INIT_POINTER(s->memcg_params.memcg_caches, arr);
  140. return 0;
  141. }
  142. static void destroy_memcg_params(struct kmem_cache *s)
  143. {
  144. if (is_root_cache(s))
  145. kvfree(rcu_access_pointer(s->memcg_params.memcg_caches));
  146. }
  147. static void free_memcg_params(struct rcu_head *rcu)
  148. {
  149. struct memcg_cache_array *old;
  150. old = container_of(rcu, struct memcg_cache_array, rcu);
  151. kvfree(old);
  152. }
  153. static int update_memcg_params(struct kmem_cache *s, int new_array_size)
  154. {
  155. struct memcg_cache_array *old, *new;
  156. new = kvzalloc(sizeof(struct memcg_cache_array) +
  157. new_array_size * sizeof(void *), GFP_KERNEL);
  158. if (!new)
  159. return -ENOMEM;
  160. old = rcu_dereference_protected(s->memcg_params.memcg_caches,
  161. lockdep_is_held(&slab_mutex));
  162. if (old)
  163. memcpy(new->entries, old->entries,
  164. memcg_nr_cache_ids * sizeof(void *));
  165. rcu_assign_pointer(s->memcg_params.memcg_caches, new);
  166. if (old)
  167. call_rcu(&old->rcu, free_memcg_params);
  168. return 0;
  169. }
  170. int memcg_update_all_caches(int num_memcgs)
  171. {
  172. struct kmem_cache *s;
  173. int ret = 0;
  174. mutex_lock(&slab_mutex);
  175. list_for_each_entry(s, &slab_root_caches, root_caches_node) {
  176. ret = update_memcg_params(s, num_memcgs);
  177. /*
  178. * Instead of freeing the memory, we'll just leave the caches
  179. * up to this point in an updated state.
  180. */
  181. if (ret)
  182. break;
  183. }
  184. mutex_unlock(&slab_mutex);
  185. return ret;
  186. }
  187. void memcg_link_cache(struct kmem_cache *s)
  188. {
  189. if (is_root_cache(s)) {
  190. list_add(&s->root_caches_node, &slab_root_caches);
  191. } else {
  192. list_add(&s->memcg_params.children_node,
  193. &s->memcg_params.root_cache->memcg_params.children);
  194. list_add(&s->memcg_params.kmem_caches_node,
  195. &s->memcg_params.memcg->kmem_caches);
  196. }
  197. }
  198. static void memcg_unlink_cache(struct kmem_cache *s)
  199. {
  200. if (is_root_cache(s)) {
  201. list_del(&s->root_caches_node);
  202. } else {
  203. list_del(&s->memcg_params.children_node);
  204. list_del(&s->memcg_params.kmem_caches_node);
  205. }
  206. }
  207. #else
  208. static inline int init_memcg_params(struct kmem_cache *s,
  209. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  210. {
  211. return 0;
  212. }
  213. static inline void destroy_memcg_params(struct kmem_cache *s)
  214. {
  215. }
  216. static inline void memcg_unlink_cache(struct kmem_cache *s)
  217. {
  218. }
  219. #endif /* CONFIG_MEMCG_KMEM */
  220. /*
  221. * Figure out what the alignment of the objects will be given a set of
  222. * flags, a user specified alignment and the size of the objects.
  223. */
  224. static unsigned int calculate_alignment(slab_flags_t flags,
  225. unsigned int align, unsigned int size)
  226. {
  227. /*
  228. * If the user wants hardware cache aligned objects then follow that
  229. * suggestion if the object is sufficiently large.
  230. *
  231. * The hardware cache alignment cannot override the specified
  232. * alignment though. If that is greater then use it.
  233. */
  234. if (flags & SLAB_HWCACHE_ALIGN) {
  235. unsigned int ralign;
  236. ralign = cache_line_size();
  237. while (size <= ralign / 2)
  238. ralign /= 2;
  239. align = max(align, ralign);
  240. }
  241. if (align < ARCH_SLAB_MINALIGN)
  242. align = ARCH_SLAB_MINALIGN;
  243. return ALIGN(align, sizeof(void *));
  244. }
  245. /*
  246. * Find a mergeable slab cache
  247. */
  248. int slab_unmergeable(struct kmem_cache *s)
  249. {
  250. if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
  251. return 1;
  252. if (!is_root_cache(s))
  253. return 1;
  254. if (s->ctor)
  255. return 1;
  256. if (s->usersize)
  257. return 1;
  258. /*
  259. * We may have set a slab to be unmergeable during bootstrap.
  260. */
  261. if (s->refcount < 0)
  262. return 1;
  263. #ifdef CONFIG_MEMCG_KMEM
  264. /*
  265. * Skip the dying kmem_cache.
  266. */
  267. if (s->memcg_params.dying)
  268. return 1;
  269. #endif
  270. return 0;
  271. }
  272. struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
  273. slab_flags_t flags, const char *name, void (*ctor)(void *))
  274. {
  275. struct kmem_cache *s;
  276. if (slab_nomerge)
  277. return NULL;
  278. if (ctor)
  279. return NULL;
  280. size = ALIGN(size, sizeof(void *));
  281. align = calculate_alignment(flags, align, size);
  282. size = ALIGN(size, align);
  283. flags = kmem_cache_flags(size, flags, name, NULL);
  284. if (flags & SLAB_NEVER_MERGE)
  285. return NULL;
  286. list_for_each_entry_reverse(s, &slab_root_caches, root_caches_node) {
  287. if (slab_unmergeable(s))
  288. continue;
  289. if (size > s->size)
  290. continue;
  291. if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
  292. continue;
  293. /*
  294. * Check if alignment is compatible.
  295. * Courtesy of Adrian Drzewiecki
  296. */
  297. if ((s->size & ~(align - 1)) != s->size)
  298. continue;
  299. if (s->size - size >= sizeof(void *))
  300. continue;
  301. if (IS_ENABLED(CONFIG_SLAB) && align &&
  302. (align > s->align || s->align % align))
  303. continue;
  304. return s;
  305. }
  306. return NULL;
  307. }
  308. static struct kmem_cache *create_cache(const char *name,
  309. unsigned int object_size, unsigned int align,
  310. slab_flags_t flags, unsigned int useroffset,
  311. unsigned int usersize, void (*ctor)(void *),
  312. struct mem_cgroup *memcg, struct kmem_cache *root_cache)
  313. {
  314. struct kmem_cache *s;
  315. int err;
  316. if (WARN_ON(useroffset + usersize > object_size))
  317. useroffset = usersize = 0;
  318. err = -ENOMEM;
  319. s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
  320. if (!s)
  321. goto out;
  322. s->name = name;
  323. s->size = s->object_size = object_size;
  324. s->align = align;
  325. s->ctor = ctor;
  326. s->useroffset = useroffset;
  327. s->usersize = usersize;
  328. err = init_memcg_params(s, memcg, root_cache);
  329. if (err)
  330. goto out_free_cache;
  331. err = __kmem_cache_create(s, flags);
  332. if (err)
  333. goto out_free_cache;
  334. s->refcount = 1;
  335. list_add(&s->list, &slab_caches);
  336. memcg_link_cache(s);
  337. out:
  338. if (err)
  339. return ERR_PTR(err);
  340. return s;
  341. out_free_cache:
  342. destroy_memcg_params(s);
  343. kmem_cache_free(kmem_cache, s);
  344. goto out;
  345. }
  346. /*
  347. * kmem_cache_create_usercopy - Create a cache.
  348. * @name: A string which is used in /proc/slabinfo to identify this cache.
  349. * @size: The size of objects to be created in this cache.
  350. * @align: The required alignment for the objects.
  351. * @flags: SLAB flags
  352. * @useroffset: Usercopy region offset
  353. * @usersize: Usercopy region size
  354. * @ctor: A constructor for the objects.
  355. *
  356. * Returns a ptr to the cache on success, NULL on failure.
  357. * Cannot be called within a interrupt, but can be interrupted.
  358. * The @ctor is run when new pages are allocated by the cache.
  359. *
  360. * The flags are
  361. *
  362. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  363. * to catch references to uninitialised memory.
  364. *
  365. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  366. * for buffer overruns.
  367. *
  368. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  369. * cacheline. This can be beneficial if you're counting cycles as closely
  370. * as davem.
  371. */
  372. struct kmem_cache *
  373. kmem_cache_create_usercopy(const char *name,
  374. unsigned int size, unsigned int align,
  375. slab_flags_t flags,
  376. unsigned int useroffset, unsigned int usersize,
  377. void (*ctor)(void *))
  378. {
  379. struct kmem_cache *s = NULL;
  380. const char *cache_name;
  381. int err;
  382. get_online_cpus();
  383. get_online_mems();
  384. memcg_get_cache_ids();
  385. mutex_lock(&slab_mutex);
  386. err = kmem_cache_sanity_check(name, size);
  387. if (err) {
  388. goto out_unlock;
  389. }
  390. /* Refuse requests with allocator specific flags */
  391. if (flags & ~SLAB_FLAGS_PERMITTED) {
  392. err = -EINVAL;
  393. goto out_unlock;
  394. }
  395. /*
  396. * Some allocators will constraint the set of valid flags to a subset
  397. * of all flags. We expect them to define CACHE_CREATE_MASK in this
  398. * case, and we'll just provide them with a sanitized version of the
  399. * passed flags.
  400. */
  401. flags &= CACHE_CREATE_MASK;
  402. /* Fail closed on bad usersize of useroffset values. */
  403. if (WARN_ON(!usersize && useroffset) ||
  404. WARN_ON(size < usersize || size - usersize < useroffset))
  405. usersize = useroffset = 0;
  406. if (!usersize)
  407. s = __kmem_cache_alias(name, size, align, flags, ctor);
  408. if (s)
  409. goto out_unlock;
  410. cache_name = kstrdup_const(name, GFP_KERNEL);
  411. if (!cache_name) {
  412. err = -ENOMEM;
  413. goto out_unlock;
  414. }
  415. s = create_cache(cache_name, size,
  416. calculate_alignment(flags, align, size),
  417. flags, useroffset, usersize, ctor, NULL, NULL);
  418. if (IS_ERR(s)) {
  419. err = PTR_ERR(s);
  420. kfree_const(cache_name);
  421. }
  422. out_unlock:
  423. mutex_unlock(&slab_mutex);
  424. memcg_put_cache_ids();
  425. put_online_mems();
  426. put_online_cpus();
  427. if (err) {
  428. if (flags & SLAB_PANIC)
  429. panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
  430. name, err);
  431. else {
  432. pr_warn("kmem_cache_create(%s) failed with error %d\n",
  433. name, err);
  434. dump_stack();
  435. }
  436. return NULL;
  437. }
  438. return s;
  439. }
  440. EXPORT_SYMBOL(kmem_cache_create_usercopy);
  441. struct kmem_cache *
  442. kmem_cache_create(const char *name, unsigned int size, unsigned int align,
  443. slab_flags_t flags, void (*ctor)(void *))
  444. {
  445. return kmem_cache_create_usercopy(name, size, align, flags, 0, 0,
  446. ctor);
  447. }
  448. EXPORT_SYMBOL(kmem_cache_create);
  449. static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
  450. {
  451. LIST_HEAD(to_destroy);
  452. struct kmem_cache *s, *s2;
  453. /*
  454. * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the
  455. * @slab_caches_to_rcu_destroy list. The slab pages are freed
  456. * through RCU and and the associated kmem_cache are dereferenced
  457. * while freeing the pages, so the kmem_caches should be freed only
  458. * after the pending RCU operations are finished. As rcu_barrier()
  459. * is a pretty slow operation, we batch all pending destructions
  460. * asynchronously.
  461. */
  462. mutex_lock(&slab_mutex);
  463. list_splice_init(&slab_caches_to_rcu_destroy, &to_destroy);
  464. mutex_unlock(&slab_mutex);
  465. if (list_empty(&to_destroy))
  466. return;
  467. rcu_barrier();
  468. list_for_each_entry_safe(s, s2, &to_destroy, list) {
  469. #ifdef SLAB_SUPPORTS_SYSFS
  470. sysfs_slab_release(s);
  471. #else
  472. slab_kmem_cache_release(s);
  473. #endif
  474. }
  475. }
  476. static int shutdown_cache(struct kmem_cache *s)
  477. {
  478. /* free asan quarantined objects */
  479. kasan_cache_shutdown(s);
  480. if (__kmem_cache_shutdown(s) != 0)
  481. return -EBUSY;
  482. memcg_unlink_cache(s);
  483. list_del(&s->list);
  484. if (s->flags & SLAB_TYPESAFE_BY_RCU) {
  485. #ifdef SLAB_SUPPORTS_SYSFS
  486. sysfs_slab_unlink(s);
  487. #endif
  488. list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
  489. schedule_work(&slab_caches_to_rcu_destroy_work);
  490. } else {
  491. #ifdef SLAB_SUPPORTS_SYSFS
  492. sysfs_slab_unlink(s);
  493. sysfs_slab_release(s);
  494. #else
  495. slab_kmem_cache_release(s);
  496. #endif
  497. }
  498. return 0;
  499. }
  500. #ifdef CONFIG_MEMCG_KMEM
  501. /*
  502. * memcg_create_kmem_cache - Create a cache for a memory cgroup.
  503. * @memcg: The memory cgroup the new cache is for.
  504. * @root_cache: The parent of the new cache.
  505. *
  506. * This function attempts to create a kmem cache that will serve allocation
  507. * requests going from @memcg to @root_cache. The new cache inherits properties
  508. * from its parent.
  509. */
  510. void memcg_create_kmem_cache(struct mem_cgroup *memcg,
  511. struct kmem_cache *root_cache)
  512. {
  513. static char memcg_name_buf[NAME_MAX + 1]; /* protected by slab_mutex */
  514. struct cgroup_subsys_state *css = &memcg->css;
  515. struct memcg_cache_array *arr;
  516. struct kmem_cache *s = NULL;
  517. char *cache_name;
  518. int idx;
  519. get_online_cpus();
  520. get_online_mems();
  521. mutex_lock(&slab_mutex);
  522. /*
  523. * The memory cgroup could have been offlined while the cache
  524. * creation work was pending.
  525. */
  526. if (memcg->kmem_state != KMEM_ONLINE || root_cache->memcg_params.dying)
  527. goto out_unlock;
  528. idx = memcg_cache_id(memcg);
  529. arr = rcu_dereference_protected(root_cache->memcg_params.memcg_caches,
  530. lockdep_is_held(&slab_mutex));
  531. /*
  532. * Since per-memcg caches are created asynchronously on first
  533. * allocation (see memcg_kmem_get_cache()), several threads can try to
  534. * create the same cache, but only one of them may succeed.
  535. */
  536. if (arr->entries[idx])
  537. goto out_unlock;
  538. cgroup_name(css->cgroup, memcg_name_buf, sizeof(memcg_name_buf));
  539. cache_name = kasprintf(GFP_KERNEL, "%s(%llu:%s)", root_cache->name,
  540. css->serial_nr, memcg_name_buf);
  541. if (!cache_name)
  542. goto out_unlock;
  543. s = create_cache(cache_name, root_cache->object_size,
  544. root_cache->align,
  545. root_cache->flags & CACHE_CREATE_MASK,
  546. root_cache->useroffset, root_cache->usersize,
  547. root_cache->ctor, memcg, root_cache);
  548. /*
  549. * If we could not create a memcg cache, do not complain, because
  550. * that's not critical at all as we can always proceed with the root
  551. * cache.
  552. */
  553. if (IS_ERR(s)) {
  554. kfree(cache_name);
  555. goto out_unlock;
  556. }
  557. /*
  558. * Since readers won't lock (see cache_from_memcg_idx()), we need a
  559. * barrier here to ensure nobody will see the kmem_cache partially
  560. * initialized.
  561. */
  562. smp_wmb();
  563. arr->entries[idx] = s;
  564. out_unlock:
  565. mutex_unlock(&slab_mutex);
  566. put_online_mems();
  567. put_online_cpus();
  568. }
  569. static void kmemcg_deactivate_workfn(struct work_struct *work)
  570. {
  571. struct kmem_cache *s = container_of(work, struct kmem_cache,
  572. memcg_params.deact_work);
  573. get_online_cpus();
  574. get_online_mems();
  575. mutex_lock(&slab_mutex);
  576. s->memcg_params.deact_fn(s);
  577. mutex_unlock(&slab_mutex);
  578. put_online_mems();
  579. put_online_cpus();
  580. /* done, put the ref from slab_deactivate_memcg_cache_rcu_sched() */
  581. css_put(&s->memcg_params.memcg->css);
  582. }
  583. static void kmemcg_deactivate_rcufn(struct rcu_head *head)
  584. {
  585. struct kmem_cache *s = container_of(head, struct kmem_cache,
  586. memcg_params.deact_rcu_head);
  587. /*
  588. * We need to grab blocking locks. Bounce to ->deact_work. The
  589. * work item shares the space with the RCU head and can't be
  590. * initialized eariler.
  591. */
  592. INIT_WORK(&s->memcg_params.deact_work, kmemcg_deactivate_workfn);
  593. queue_work(memcg_kmem_cache_wq, &s->memcg_params.deact_work);
  594. }
  595. /**
  596. * slab_deactivate_memcg_cache_rcu_sched - schedule deactivation after a
  597. * sched RCU grace period
  598. * @s: target kmem_cache
  599. * @deact_fn: deactivation function to call
  600. *
  601. * Schedule @deact_fn to be invoked with online cpus, mems and slab_mutex
  602. * held after a sched RCU grace period. The slab is guaranteed to stay
  603. * alive until @deact_fn is finished. This is to be used from
  604. * __kmemcg_cache_deactivate().
  605. */
  606. void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s,
  607. void (*deact_fn)(struct kmem_cache *))
  608. {
  609. if (WARN_ON_ONCE(is_root_cache(s)) ||
  610. WARN_ON_ONCE(s->memcg_params.deact_fn))
  611. return;
  612. /*
  613. * memcg_kmem_wq_lock is used to synchronize memcg_params.dying
  614. * flag and make sure that no new kmem_cache deactivation tasks
  615. * are queued (see flush_memcg_workqueue() ).
  616. */
  617. spin_lock_irq(&memcg_kmem_wq_lock);
  618. if (s->memcg_params.root_cache->memcg_params.dying)
  619. goto unlock;
  620. /* pin memcg so that @s doesn't get destroyed in the middle */
  621. css_get(&s->memcg_params.memcg->css);
  622. s->memcg_params.deact_fn = deact_fn;
  623. call_rcu_sched(&s->memcg_params.deact_rcu_head, kmemcg_deactivate_rcufn);
  624. unlock:
  625. spin_unlock_irq(&memcg_kmem_wq_lock);
  626. }
  627. void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
  628. {
  629. int idx;
  630. struct memcg_cache_array *arr;
  631. struct kmem_cache *s, *c;
  632. idx = memcg_cache_id(memcg);
  633. get_online_cpus();
  634. get_online_mems();
  635. mutex_lock(&slab_mutex);
  636. list_for_each_entry(s, &slab_root_caches, root_caches_node) {
  637. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  638. lockdep_is_held(&slab_mutex));
  639. c = arr->entries[idx];
  640. if (!c)
  641. continue;
  642. __kmemcg_cache_deactivate(c);
  643. arr->entries[idx] = NULL;
  644. }
  645. mutex_unlock(&slab_mutex);
  646. put_online_mems();
  647. put_online_cpus();
  648. }
  649. void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
  650. {
  651. struct kmem_cache *s, *s2;
  652. get_online_cpus();
  653. get_online_mems();
  654. mutex_lock(&slab_mutex);
  655. list_for_each_entry_safe(s, s2, &memcg->kmem_caches,
  656. memcg_params.kmem_caches_node) {
  657. /*
  658. * The cgroup is about to be freed and therefore has no charges
  659. * left. Hence, all its caches must be empty by now.
  660. */
  661. BUG_ON(shutdown_cache(s));
  662. }
  663. mutex_unlock(&slab_mutex);
  664. put_online_mems();
  665. put_online_cpus();
  666. }
  667. static int shutdown_memcg_caches(struct kmem_cache *s)
  668. {
  669. struct memcg_cache_array *arr;
  670. struct kmem_cache *c, *c2;
  671. LIST_HEAD(busy);
  672. int i;
  673. BUG_ON(!is_root_cache(s));
  674. /*
  675. * First, shutdown active caches, i.e. caches that belong to online
  676. * memory cgroups.
  677. */
  678. arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
  679. lockdep_is_held(&slab_mutex));
  680. for_each_memcg_cache_index(i) {
  681. c = arr->entries[i];
  682. if (!c)
  683. continue;
  684. if (shutdown_cache(c))
  685. /*
  686. * The cache still has objects. Move it to a temporary
  687. * list so as not to try to destroy it for a second
  688. * time while iterating over inactive caches below.
  689. */
  690. list_move(&c->memcg_params.children_node, &busy);
  691. else
  692. /*
  693. * The cache is empty and will be destroyed soon. Clear
  694. * the pointer to it in the memcg_caches array so that
  695. * it will never be accessed even if the root cache
  696. * stays alive.
  697. */
  698. arr->entries[i] = NULL;
  699. }
  700. /*
  701. * Second, shutdown all caches left from memory cgroups that are now
  702. * offline.
  703. */
  704. list_for_each_entry_safe(c, c2, &s->memcg_params.children,
  705. memcg_params.children_node)
  706. shutdown_cache(c);
  707. list_splice(&busy, &s->memcg_params.children);
  708. /*
  709. * A cache being destroyed must be empty. In particular, this means
  710. * that all per memcg caches attached to it must be empty too.
  711. */
  712. if (!list_empty(&s->memcg_params.children))
  713. return -EBUSY;
  714. return 0;
  715. }
  716. static void memcg_set_kmem_cache_dying(struct kmem_cache *s)
  717. {
  718. spin_lock_irq(&memcg_kmem_wq_lock);
  719. s->memcg_params.dying = true;
  720. spin_unlock_irq(&memcg_kmem_wq_lock);
  721. }
  722. static void flush_memcg_workqueue(struct kmem_cache *s)
  723. {
  724. /*
  725. * SLUB deactivates the kmem_caches through call_rcu_sched. Make
  726. * sure all registered rcu callbacks have been invoked.
  727. */
  728. if (IS_ENABLED(CONFIG_SLUB))
  729. rcu_barrier_sched();
  730. /*
  731. * SLAB and SLUB create memcg kmem_caches through workqueue and SLUB
  732. * deactivates the memcg kmem_caches through workqueue. Make sure all
  733. * previous workitems on workqueue are processed.
  734. */
  735. if (likely(memcg_kmem_cache_wq))
  736. flush_workqueue(memcg_kmem_cache_wq);
  737. }
  738. #else
  739. static inline int shutdown_memcg_caches(struct kmem_cache *s)
  740. {
  741. return 0;
  742. }
  743. #endif /* CONFIG_MEMCG_KMEM */
  744. void slab_kmem_cache_release(struct kmem_cache *s)
  745. {
  746. __kmem_cache_release(s);
  747. destroy_memcg_params(s);
  748. kfree_const(s->name);
  749. kmem_cache_free(kmem_cache, s);
  750. }
  751. void kmem_cache_destroy(struct kmem_cache *s)
  752. {
  753. int err;
  754. if (unlikely(!s))
  755. return;
  756. get_online_cpus();
  757. get_online_mems();
  758. mutex_lock(&slab_mutex);
  759. s->refcount--;
  760. if (s->refcount)
  761. goto out_unlock;
  762. #ifdef CONFIG_MEMCG_KMEM
  763. memcg_set_kmem_cache_dying(s);
  764. mutex_unlock(&slab_mutex);
  765. put_online_mems();
  766. put_online_cpus();
  767. flush_memcg_workqueue(s);
  768. get_online_cpus();
  769. get_online_mems();
  770. mutex_lock(&slab_mutex);
  771. #endif
  772. err = shutdown_memcg_caches(s);
  773. if (!err)
  774. err = shutdown_cache(s);
  775. if (err) {
  776. pr_err("kmem_cache_destroy %s: Slab cache still has objects\n",
  777. s->name);
  778. dump_stack();
  779. }
  780. out_unlock:
  781. mutex_unlock(&slab_mutex);
  782. put_online_mems();
  783. put_online_cpus();
  784. }
  785. EXPORT_SYMBOL(kmem_cache_destroy);
  786. /**
  787. * kmem_cache_shrink - Shrink a cache.
  788. * @cachep: The cache to shrink.
  789. *
  790. * Releases as many slabs as possible for a cache.
  791. * To help debugging, a zero exit status indicates all slabs were released.
  792. */
  793. int kmem_cache_shrink(struct kmem_cache *cachep)
  794. {
  795. int ret;
  796. get_online_cpus();
  797. get_online_mems();
  798. kasan_cache_shrink(cachep);
  799. ret = __kmem_cache_shrink(cachep);
  800. put_online_mems();
  801. put_online_cpus();
  802. return ret;
  803. }
  804. EXPORT_SYMBOL(kmem_cache_shrink);
  805. bool slab_is_available(void)
  806. {
  807. return slab_state >= UP;
  808. }
  809. #ifndef CONFIG_SLOB
  810. /* Create a cache during boot when no slab services are available yet */
  811. void __init create_boot_cache(struct kmem_cache *s, const char *name,
  812. unsigned int size, slab_flags_t flags,
  813. unsigned int useroffset, unsigned int usersize)
  814. {
  815. int err;
  816. s->name = name;
  817. s->size = s->object_size = size;
  818. s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
  819. s->useroffset = useroffset;
  820. s->usersize = usersize;
  821. slab_init_memcg_params(s);
  822. err = __kmem_cache_create(s, flags);
  823. if (err)
  824. panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
  825. name, size, err);
  826. s->refcount = -1; /* Exempt from merging for now */
  827. }
  828. struct kmem_cache *__init create_kmalloc_cache(const char *name,
  829. unsigned int size, slab_flags_t flags,
  830. unsigned int useroffset, unsigned int usersize)
  831. {
  832. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  833. if (!s)
  834. panic("Out of memory when creating slab %s\n", name);
  835. create_boot_cache(s, name, size, flags, useroffset, usersize);
  836. list_add(&s->list, &slab_caches);
  837. memcg_link_cache(s);
  838. s->refcount = 1;
  839. return s;
  840. }
  841. struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __ro_after_init;
  842. EXPORT_SYMBOL(kmalloc_caches);
  843. #ifdef CONFIG_ZONE_DMA
  844. struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1] __ro_after_init;
  845. EXPORT_SYMBOL(kmalloc_dma_caches);
  846. #endif
  847. /*
  848. * Conversion table for small slabs sizes / 8 to the index in the
  849. * kmalloc array. This is necessary for slabs < 192 since we have non power
  850. * of two cache sizes there. The size of larger slabs can be determined using
  851. * fls.
  852. */
  853. static u8 size_index[24] __ro_after_init = {
  854. 3, /* 8 */
  855. 4, /* 16 */
  856. 5, /* 24 */
  857. 5, /* 32 */
  858. 6, /* 40 */
  859. 6, /* 48 */
  860. 6, /* 56 */
  861. 6, /* 64 */
  862. 1, /* 72 */
  863. 1, /* 80 */
  864. 1, /* 88 */
  865. 1, /* 96 */
  866. 7, /* 104 */
  867. 7, /* 112 */
  868. 7, /* 120 */
  869. 7, /* 128 */
  870. 2, /* 136 */
  871. 2, /* 144 */
  872. 2, /* 152 */
  873. 2, /* 160 */
  874. 2, /* 168 */
  875. 2, /* 176 */
  876. 2, /* 184 */
  877. 2 /* 192 */
  878. };
  879. static inline unsigned int size_index_elem(unsigned int bytes)
  880. {
  881. return (bytes - 1) / 8;
  882. }
  883. /*
  884. * Find the kmem_cache structure that serves a given size of
  885. * allocation
  886. */
  887. struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
  888. {
  889. unsigned int index;
  890. if (size <= 192) {
  891. if (!size)
  892. return ZERO_SIZE_PTR;
  893. index = size_index[size_index_elem(size)];
  894. } else {
  895. if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
  896. WARN_ON(1);
  897. return NULL;
  898. }
  899. index = fls(size - 1);
  900. }
  901. #ifdef CONFIG_ZONE_DMA
  902. if (unlikely((flags & GFP_DMA)))
  903. return kmalloc_dma_caches[index];
  904. #endif
  905. return kmalloc_caches[index];
  906. }
  907. /*
  908. * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
  909. * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
  910. * kmalloc-67108864.
  911. */
  912. const struct kmalloc_info_struct kmalloc_info[] __initconst = {
  913. {NULL, 0}, {"kmalloc-96", 96},
  914. {"kmalloc-192", 192}, {"kmalloc-8", 8},
  915. {"kmalloc-16", 16}, {"kmalloc-32", 32},
  916. {"kmalloc-64", 64}, {"kmalloc-128", 128},
  917. {"kmalloc-256", 256}, {"kmalloc-512", 512},
  918. {"kmalloc-1024", 1024}, {"kmalloc-2048", 2048},
  919. {"kmalloc-4096", 4096}, {"kmalloc-8192", 8192},
  920. {"kmalloc-16384", 16384}, {"kmalloc-32768", 32768},
  921. {"kmalloc-65536", 65536}, {"kmalloc-131072", 131072},
  922. {"kmalloc-262144", 262144}, {"kmalloc-524288", 524288},
  923. {"kmalloc-1048576", 1048576}, {"kmalloc-2097152", 2097152},
  924. {"kmalloc-4194304", 4194304}, {"kmalloc-8388608", 8388608},
  925. {"kmalloc-16777216", 16777216}, {"kmalloc-33554432", 33554432},
  926. {"kmalloc-67108864", 67108864}
  927. };
  928. /*
  929. * Patch up the size_index table if we have strange large alignment
  930. * requirements for the kmalloc array. This is only the case for
  931. * MIPS it seems. The standard arches will not generate any code here.
  932. *
  933. * Largest permitted alignment is 256 bytes due to the way we
  934. * handle the index determination for the smaller caches.
  935. *
  936. * Make sure that nothing crazy happens if someone starts tinkering
  937. * around with ARCH_KMALLOC_MINALIGN
  938. */
  939. void __init setup_kmalloc_cache_index_table(void)
  940. {
  941. unsigned int i;
  942. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  943. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  944. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  945. unsigned int elem = size_index_elem(i);
  946. if (elem >= ARRAY_SIZE(size_index))
  947. break;
  948. size_index[elem] = KMALLOC_SHIFT_LOW;
  949. }
  950. if (KMALLOC_MIN_SIZE >= 64) {
  951. /*
  952. * The 96 byte size cache is not used if the alignment
  953. * is 64 byte.
  954. */
  955. for (i = 64 + 8; i <= 96; i += 8)
  956. size_index[size_index_elem(i)] = 7;
  957. }
  958. if (KMALLOC_MIN_SIZE >= 128) {
  959. /*
  960. * The 192 byte sized cache is not used if the alignment
  961. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  962. * instead.
  963. */
  964. for (i = 128 + 8; i <= 192; i += 8)
  965. size_index[size_index_elem(i)] = 8;
  966. }
  967. }
  968. static void __init new_kmalloc_cache(int idx, slab_flags_t flags)
  969. {
  970. kmalloc_caches[idx] = create_kmalloc_cache(kmalloc_info[idx].name,
  971. kmalloc_info[idx].size, flags, 0,
  972. kmalloc_info[idx].size);
  973. }
  974. /*
  975. * Create the kmalloc array. Some of the regular kmalloc arrays
  976. * may already have been created because they were needed to
  977. * enable allocations for slab creation.
  978. */
  979. void __init create_kmalloc_caches(slab_flags_t flags)
  980. {
  981. int i;
  982. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
  983. if (!kmalloc_caches[i])
  984. new_kmalloc_cache(i, flags);
  985. /*
  986. * Caches that are not of the two-to-the-power-of size.
  987. * These have to be created immediately after the
  988. * earlier power of two caches
  989. */
  990. if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
  991. new_kmalloc_cache(1, flags);
  992. if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
  993. new_kmalloc_cache(2, flags);
  994. }
  995. /* Kmalloc array is now usable */
  996. slab_state = UP;
  997. #ifdef CONFIG_ZONE_DMA
  998. for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
  999. struct kmem_cache *s = kmalloc_caches[i];
  1000. if (s) {
  1001. unsigned int size = kmalloc_size(i);
  1002. char *n = kasprintf(GFP_NOWAIT,
  1003. "dma-kmalloc-%u", size);
  1004. BUG_ON(!n);
  1005. kmalloc_dma_caches[i] = create_kmalloc_cache(n,
  1006. size, SLAB_CACHE_DMA | flags, 0, 0);
  1007. }
  1008. }
  1009. #endif
  1010. }
  1011. #endif /* !CONFIG_SLOB */
  1012. /*
  1013. * To avoid unnecessary overhead, we pass through large allocation requests
  1014. * directly to the page allocator. We use __GFP_COMP, because we will need to
  1015. * know the allocation order to free the pages properly in kfree.
  1016. */
  1017. void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
  1018. {
  1019. void *ret;
  1020. struct page *page;
  1021. flags |= __GFP_COMP;
  1022. page = alloc_pages(flags, order);
  1023. ret = page ? page_address(page) : NULL;
  1024. kmemleak_alloc(ret, size, 1, flags);
  1025. kasan_kmalloc_large(ret, size, flags);
  1026. return ret;
  1027. }
  1028. EXPORT_SYMBOL(kmalloc_order);
  1029. #ifdef CONFIG_TRACING
  1030. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  1031. {
  1032. void *ret = kmalloc_order(size, flags, order);
  1033. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  1034. return ret;
  1035. }
  1036. EXPORT_SYMBOL(kmalloc_order_trace);
  1037. #endif
  1038. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  1039. /* Randomize a generic freelist */
  1040. static void freelist_randomize(struct rnd_state *state, unsigned int *list,
  1041. unsigned int count)
  1042. {
  1043. unsigned int rand;
  1044. unsigned int i;
  1045. for (i = 0; i < count; i++)
  1046. list[i] = i;
  1047. /* Fisher-Yates shuffle */
  1048. for (i = count - 1; i > 0; i--) {
  1049. rand = prandom_u32_state(state);
  1050. rand %= (i + 1);
  1051. swap(list[i], list[rand]);
  1052. }
  1053. }
  1054. /* Create a random sequence per cache */
  1055. int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
  1056. gfp_t gfp)
  1057. {
  1058. struct rnd_state state;
  1059. if (count < 2 || cachep->random_seq)
  1060. return 0;
  1061. cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
  1062. if (!cachep->random_seq)
  1063. return -ENOMEM;
  1064. /* Get best entropy at this stage of boot */
  1065. prandom_seed_state(&state, get_random_long());
  1066. freelist_randomize(&state, cachep->random_seq, count);
  1067. return 0;
  1068. }
  1069. /* Destroy the per-cache random freelist sequence */
  1070. void cache_random_seq_destroy(struct kmem_cache *cachep)
  1071. {
  1072. kfree(cachep->random_seq);
  1073. cachep->random_seq = NULL;
  1074. }
  1075. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  1076. #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
  1077. #ifdef CONFIG_SLAB
  1078. #define SLABINFO_RIGHTS (0600)
  1079. #else
  1080. #define SLABINFO_RIGHTS (0400)
  1081. #endif
  1082. static void print_slabinfo_header(struct seq_file *m)
  1083. {
  1084. /*
  1085. * Output format version, so at least we can change it
  1086. * without _too_ many complaints.
  1087. */
  1088. #ifdef CONFIG_DEBUG_SLAB
  1089. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  1090. #else
  1091. seq_puts(m, "slabinfo - version: 2.1\n");
  1092. #endif
  1093. seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
  1094. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  1095. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  1096. #ifdef CONFIG_DEBUG_SLAB
  1097. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  1098. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  1099. #endif
  1100. seq_putc(m, '\n');
  1101. }
  1102. void *slab_start(struct seq_file *m, loff_t *pos)
  1103. {
  1104. mutex_lock(&slab_mutex);
  1105. return seq_list_start(&slab_root_caches, *pos);
  1106. }
  1107. void *slab_next(struct seq_file *m, void *p, loff_t *pos)
  1108. {
  1109. return seq_list_next(p, &slab_root_caches, pos);
  1110. }
  1111. void slab_stop(struct seq_file *m, void *p)
  1112. {
  1113. mutex_unlock(&slab_mutex);
  1114. }
  1115. static void
  1116. memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
  1117. {
  1118. struct kmem_cache *c;
  1119. struct slabinfo sinfo;
  1120. if (!is_root_cache(s))
  1121. return;
  1122. for_each_memcg_cache(c, s) {
  1123. memset(&sinfo, 0, sizeof(sinfo));
  1124. get_slabinfo(c, &sinfo);
  1125. info->active_slabs += sinfo.active_slabs;
  1126. info->num_slabs += sinfo.num_slabs;
  1127. info->shared_avail += sinfo.shared_avail;
  1128. info->active_objs += sinfo.active_objs;
  1129. info->num_objs += sinfo.num_objs;
  1130. }
  1131. }
  1132. static void cache_show(struct kmem_cache *s, struct seq_file *m)
  1133. {
  1134. struct slabinfo sinfo;
  1135. memset(&sinfo, 0, sizeof(sinfo));
  1136. get_slabinfo(s, &sinfo);
  1137. memcg_accumulate_slabinfo(s, &sinfo);
  1138. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  1139. cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
  1140. sinfo.objects_per_slab, (1 << sinfo.cache_order));
  1141. seq_printf(m, " : tunables %4u %4u %4u",
  1142. sinfo.limit, sinfo.batchcount, sinfo.shared);
  1143. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  1144. sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
  1145. slabinfo_show_stats(m, s);
  1146. seq_putc(m, '\n');
  1147. }
  1148. static int slab_show(struct seq_file *m, void *p)
  1149. {
  1150. struct kmem_cache *s = list_entry(p, struct kmem_cache, root_caches_node);
  1151. if (p == slab_root_caches.next)
  1152. print_slabinfo_header(m);
  1153. cache_show(s, m);
  1154. return 0;
  1155. }
  1156. void dump_unreclaimable_slab(void)
  1157. {
  1158. struct kmem_cache *s, *s2;
  1159. struct slabinfo sinfo;
  1160. /*
  1161. * Here acquiring slab_mutex is risky since we don't prefer to get
  1162. * sleep in oom path. But, without mutex hold, it may introduce a
  1163. * risk of crash.
  1164. * Use mutex_trylock to protect the list traverse, dump nothing
  1165. * without acquiring the mutex.
  1166. */
  1167. if (!mutex_trylock(&slab_mutex)) {
  1168. pr_warn("excessive unreclaimable slab but cannot dump stats\n");
  1169. return;
  1170. }
  1171. pr_info("Unreclaimable slab info:\n");
  1172. pr_info("Name Used Total\n");
  1173. list_for_each_entry_safe(s, s2, &slab_caches, list) {
  1174. if (!is_root_cache(s) || (s->flags & SLAB_RECLAIM_ACCOUNT))
  1175. continue;
  1176. get_slabinfo(s, &sinfo);
  1177. if (sinfo.num_objs > 0)
  1178. pr_info("%-17s %10luKB %10luKB\n", cache_name(s),
  1179. (sinfo.active_objs * s->size) / 1024,
  1180. (sinfo.num_objs * s->size) / 1024);
  1181. }
  1182. mutex_unlock(&slab_mutex);
  1183. }
  1184. #if defined(CONFIG_MEMCG)
  1185. void *memcg_slab_start(struct seq_file *m, loff_t *pos)
  1186. {
  1187. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  1188. mutex_lock(&slab_mutex);
  1189. return seq_list_start(&memcg->kmem_caches, *pos);
  1190. }
  1191. void *memcg_slab_next(struct seq_file *m, void *p, loff_t *pos)
  1192. {
  1193. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  1194. return seq_list_next(p, &memcg->kmem_caches, pos);
  1195. }
  1196. void memcg_slab_stop(struct seq_file *m, void *p)
  1197. {
  1198. mutex_unlock(&slab_mutex);
  1199. }
  1200. int memcg_slab_show(struct seq_file *m, void *p)
  1201. {
  1202. struct kmem_cache *s = list_entry(p, struct kmem_cache,
  1203. memcg_params.kmem_caches_node);
  1204. struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
  1205. if (p == memcg->kmem_caches.next)
  1206. print_slabinfo_header(m);
  1207. cache_show(s, m);
  1208. return 0;
  1209. }
  1210. #endif
  1211. /*
  1212. * slabinfo_op - iterator that generates /proc/slabinfo
  1213. *
  1214. * Output layout:
  1215. * cache-name
  1216. * num-active-objs
  1217. * total-objs
  1218. * object size
  1219. * num-active-slabs
  1220. * total-slabs
  1221. * num-pages-per-slab
  1222. * + further values on SMP and with statistics enabled
  1223. */
  1224. static const struct seq_operations slabinfo_op = {
  1225. .start = slab_start,
  1226. .next = slab_next,
  1227. .stop = slab_stop,
  1228. .show = slab_show,
  1229. };
  1230. static int slabinfo_open(struct inode *inode, struct file *file)
  1231. {
  1232. return seq_open(file, &slabinfo_op);
  1233. }
  1234. static const struct file_operations proc_slabinfo_operations = {
  1235. .open = slabinfo_open,
  1236. .read = seq_read,
  1237. .write = slabinfo_write,
  1238. .llseek = seq_lseek,
  1239. .release = seq_release,
  1240. };
  1241. static int __init slab_proc_init(void)
  1242. {
  1243. proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
  1244. &proc_slabinfo_operations);
  1245. return 0;
  1246. }
  1247. module_init(slab_proc_init);
  1248. #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */
  1249. static __always_inline void *__do_krealloc(const void *p, size_t new_size,
  1250. gfp_t flags)
  1251. {
  1252. void *ret;
  1253. size_t ks = 0;
  1254. if (p)
  1255. ks = ksize(p);
  1256. if (ks >= new_size) {
  1257. kasan_krealloc((void *)p, new_size, flags);
  1258. return (void *)p;
  1259. }
  1260. ret = kmalloc_track_caller(new_size, flags);
  1261. if (ret && p)
  1262. memcpy(ret, p, ks);
  1263. return ret;
  1264. }
  1265. /**
  1266. * __krealloc - like krealloc() but don't free @p.
  1267. * @p: object to reallocate memory for.
  1268. * @new_size: how many bytes of memory are required.
  1269. * @flags: the type of memory to allocate.
  1270. *
  1271. * This function is like krealloc() except it never frees the originally
  1272. * allocated buffer. Use this if you don't want to free the buffer immediately
  1273. * like, for example, with RCU.
  1274. */
  1275. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  1276. {
  1277. if (unlikely(!new_size))
  1278. return ZERO_SIZE_PTR;
  1279. return __do_krealloc(p, new_size, flags);
  1280. }
  1281. EXPORT_SYMBOL(__krealloc);
  1282. /**
  1283. * krealloc - reallocate memory. The contents will remain unchanged.
  1284. * @p: object to reallocate memory for.
  1285. * @new_size: how many bytes of memory are required.
  1286. * @flags: the type of memory to allocate.
  1287. *
  1288. * The contents of the object pointed to are preserved up to the
  1289. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  1290. * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
  1291. * %NULL pointer, the object pointed to is freed.
  1292. */
  1293. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  1294. {
  1295. void *ret;
  1296. if (unlikely(!new_size)) {
  1297. kfree(p);
  1298. return ZERO_SIZE_PTR;
  1299. }
  1300. ret = __do_krealloc(p, new_size, flags);
  1301. if (ret && p != ret)
  1302. kfree(p);
  1303. return ret;
  1304. }
  1305. EXPORT_SYMBOL(krealloc);
  1306. /**
  1307. * kzfree - like kfree but zero memory
  1308. * @p: object to free memory of
  1309. *
  1310. * The memory of the object @p points to is zeroed before freed.
  1311. * If @p is %NULL, kzfree() does nothing.
  1312. *
  1313. * Note: this function zeroes the whole allocated buffer which can be a good
  1314. * deal bigger than the requested buffer size passed to kmalloc(). So be
  1315. * careful when using this function in performance sensitive code.
  1316. */
  1317. void kzfree(const void *p)
  1318. {
  1319. size_t ks;
  1320. void *mem = (void *)p;
  1321. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  1322. return;
  1323. ks = ksize(mem);
  1324. memzero_explicit(mem, ks);
  1325. kfree(mem);
  1326. }
  1327. EXPORT_SYMBOL(kzfree);
  1328. /* Tracepoints definitions. */
  1329. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  1330. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  1331. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  1332. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  1333. EXPORT_TRACEPOINT_SYMBOL(kfree);
  1334. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
  1335. int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
  1336. {
  1337. if (__should_failslab(s, gfpflags))
  1338. return -ENOMEM;
  1339. return 0;
  1340. }
  1341. ALLOW_ERROR_INJECTION(should_failslab, ERRNO);