PageRenderTime 68ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mm/slab.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 3825 lines | 3110 code | 628 blank | 87 comment | 502 complexity | 5dd169ce434a83938b6a2464e0584eca MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * linux/mm/slab.c
  3. * Written by Mark Hemment, 1996/97.
  4. * (markhe@nextd.demon.co.uk)
  5. *
  6. * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
  7. *
  8. * Major cleanup, different bufctl logic, per-cpu arrays
  9. * (c) 2000 Manfred Spraul
  10. *
  11. * Cleanup, make the head arrays unconditional, preparation for NUMA
  12. * (c) 2002 Manfred Spraul
  13. *
  14. * An implementation of the Slab Allocator as described in outline in;
  15. * UNIX Internals: The New Frontiers by Uresh Vahalia
  16. * Pub: Prentice Hall ISBN 0-13-101908-2
  17. * or with a little more detail in;
  18. * The Slab Allocator: An Object-Caching Kernel Memory Allocator
  19. * Jeff Bonwick (Sun Microsystems).
  20. * Presented at: USENIX Summer 1994 Technical Conference
  21. *
  22. * The memory is organized in caches, one cache for each object type.
  23. * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
  24. * Each cache consists out of many slabs (they are small (usually one
  25. * page long) and always contiguous), and each slab contains multiple
  26. * initialized objects.
  27. *
  28. * This means, that your constructor is used only for newly allocated
  29. * slabs and you must pass objects with the same initializations to
  30. * kmem_cache_free.
  31. *
  32. * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
  33. * normal). If you need a special memory type, then must create a new
  34. * cache for that memory type.
  35. *
  36. * In order to reduce fragmentation, the slabs are sorted in 3 groups:
  37. * full slabs with 0 free objects
  38. * partial slabs
  39. * empty slabs with no allocated objects
  40. *
  41. * If partial slabs exist, then new allocations come from these slabs,
  42. * otherwise from empty slabs or new slabs are allocated.
  43. *
  44. * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
  45. * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
  46. *
  47. * Each cache has a short per-cpu head array, most allocs
  48. * and frees go into that array, and if that array overflows, then 1/2
  49. * of the entries in the array are given back into the global cache.
  50. * The head array is strictly LIFO and should improve the cache hit rates.
  51. * On SMP, it additionally reduces the spinlock operations.
  52. *
  53. * The c_cpuarray may not be read with enabled local interrupts -
  54. * it's changed with a smp_call_function().
  55. *
  56. * SMP synchronization:
  57. * constructors and destructors are called without any locking.
  58. * Several members in struct kmem_cache and struct slab never change, they
  59. * are accessed without any locking.
  60. * The per-cpu arrays are never accessed from the wrong cpu, no locking,
  61. * and local interrupts are disabled so slab code is preempt-safe.
  62. * The non-constant members are protected with a per-cache irq spinlock.
  63. *
  64. * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
  65. * in 2000 - many ideas in the current implementation are derived from
  66. * his patch.
  67. *
  68. * Further notes from the original documentation:
  69. *
  70. * 11 April '97. Started multi-threading - markhe
  71. * The global cache-chain is protected by the mutex 'cache_chain_mutex'.
  72. * The sem is only needed when accessing/extending the cache-chain, which
  73. * can never happen inside an interrupt (kmem_cache_create(),
  74. * kmem_cache_shrink() and kmem_cache_reap()).
  75. *
  76. * At present, each engine can be growing a cache. This should be blocked.
  77. *
  78. * 15 March 2005. NUMA slab allocator.
  79. * Shai Fultheim <shai@scalex86.org>.
  80. * Shobhit Dayal <shobhit@calsoftinc.com>
  81. * Alok N Kataria <alokk@calsoftinc.com>
  82. * Christoph Lameter <christoph@lameter.com>
  83. *
  84. * Modified the slab allocator to be node aware on NUMA systems.
  85. * Each node has its own list of partial, free and full slabs.
  86. * All object allocations for a node occur from node specific slab lists.
  87. */
  88. #include <linux/slab.h>
  89. #include <linux/mm.h>
  90. #include <linux/poison.h>
  91. #include <linux/swap.h>
  92. #include <linux/cache.h>
  93. #include <linux/interrupt.h>
  94. #include <linux/init.h>
  95. #include <linux/compiler.h>
  96. #include <linux/cpuset.h>
  97. #include <linux/proc_fs.h>
  98. #include <linux/seq_file.h>
  99. #include <linux/notifier.h>
  100. #include <linux/kallsyms.h>
  101. #include <linux/cpu.h>
  102. #include <linux/sysctl.h>
  103. #include <linux/module.h>
  104. #include <linux/rcupdate.h>
  105. #include <linux/string.h>
  106. #include <linux/uaccess.h>
  107. #include <linux/nodemask.h>
  108. #include <linux/kmemleak.h>
  109. #include <linux/mempolicy.h>
  110. #include <linux/mutex.h>
  111. #include <linux/fault-inject.h>
  112. #include <linux/rtmutex.h>
  113. #include <linux/reciprocal_div.h>
  114. #include <linux/debugobjects.h>
  115. #include <linux/kmemcheck.h>
  116. #include <linux/memory.h>
  117. #include <linux/prefetch.h>
  118. #include <asm/cacheflush.h>
  119. #include <asm/tlbflush.h>
  120. #include <asm/page.h>
  121. #include <trace/events/kmem.h>
  122. #ifdef CONFIG_DEBUG_SLAB
  123. #define DEBUG 1
  124. #define STATS 1
  125. #define FORCED_DEBUG 1
  126. #else
  127. #define DEBUG 0
  128. #define STATS 0
  129. #define FORCED_DEBUG 0
  130. #endif
  131. #define BYTES_PER_WORD sizeof(void *)
  132. #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
  133. #ifndef ARCH_KMALLOC_FLAGS
  134. #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
  135. #endif
  136. #if DEBUG
  137. # define CREATE_MASK (SLAB_RED_ZONE | \
  138. SLAB_POISON | SLAB_HWCACHE_ALIGN | \
  139. SLAB_CACHE_DMA | \
  140. SLAB_STORE_USER | \
  141. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  142. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
  143. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
  144. #else
  145. # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
  146. SLAB_CACHE_DMA | \
  147. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  148. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
  149. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
  150. #endif
  151. typedef unsigned int kmem_bufctl_t;
  152. #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
  153. #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
  154. #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
  155. #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
  156. struct slab_rcu {
  157. struct rcu_head head;
  158. struct kmem_cache *cachep;
  159. void *addr;
  160. };
  161. struct slab {
  162. union {
  163. struct {
  164. struct list_head list;
  165. unsigned long colouroff;
  166. void *s_mem;
  167. unsigned int inuse;
  168. kmem_bufctl_t free;
  169. unsigned short nodeid;
  170. };
  171. struct slab_rcu __slab_cover_slab_rcu;
  172. };
  173. };
  174. struct array_cache {
  175. unsigned int avail;
  176. unsigned int limit;
  177. unsigned int batchcount;
  178. unsigned int touched;
  179. spinlock_t lock;
  180. void *entry[];
  181. };
  182. #define BOOT_CPUCACHE_ENTRIES 1
  183. struct arraycache_init {
  184. struct array_cache cache;
  185. void *entries[BOOT_CPUCACHE_ENTRIES];
  186. };
  187. struct kmem_list3 {
  188. struct list_head slabs_partial;
  189. struct list_head slabs_full;
  190. struct list_head slabs_free;
  191. unsigned long free_objects;
  192. unsigned int free_limit;
  193. unsigned int colour_next;
  194. spinlock_t list_lock;
  195. struct array_cache *shared;
  196. struct array_cache **alien;
  197. unsigned long next_reap;
  198. int free_touched;
  199. };
  200. #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
  201. static struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
  202. #define CACHE_CACHE 0
  203. #define SIZE_AC MAX_NUMNODES
  204. #define SIZE_L3 (2 * MAX_NUMNODES)
  205. static int drain_freelist(struct kmem_cache *cache,
  206. struct kmem_list3 *l3, int tofree);
  207. static void free_block(struct kmem_cache *cachep, void **objpp, int len,
  208. int node);
  209. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
  210. static void cache_reap(struct work_struct *unused);
  211. static __always_inline int index_of(const size_t size)
  212. {
  213. extern void __bad_size(void);
  214. if (__builtin_constant_p(size)) {
  215. int i = 0;
  216. #define CACHE(x) \
  217. if (size <=x) \
  218. return i; \
  219. else \
  220. i++;
  221. #include <linux/kmalloc_sizes.h>
  222. #undef CACHE
  223. __bad_size();
  224. } else
  225. __bad_size();
  226. return 0;
  227. }
  228. static int slab_early_init = 1;
  229. #define INDEX_AC index_of(sizeof(struct arraycache_init))
  230. #define INDEX_L3 index_of(sizeof(struct kmem_list3))
  231. static void kmem_list3_init(struct kmem_list3 *parent)
  232. {
  233. INIT_LIST_HEAD(&parent->slabs_full);
  234. INIT_LIST_HEAD(&parent->slabs_partial);
  235. INIT_LIST_HEAD(&parent->slabs_free);
  236. parent->shared = NULL;
  237. parent->alien = NULL;
  238. parent->colour_next = 0;
  239. spin_lock_init(&parent->list_lock);
  240. parent->free_objects = 0;
  241. parent->free_touched = 0;
  242. }
  243. #define MAKE_LIST(cachep, listp, slab, nodeid) \
  244. do { \
  245. INIT_LIST_HEAD(listp); \
  246. list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
  247. } while (0)
  248. #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
  249. do { \
  250. MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
  251. MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  252. MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
  253. } while (0)
  254. #define CFLGS_OFF_SLAB (0x80000000UL)
  255. #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
  256. #define BATCHREFILL_LIMIT 16
  257. #define REAPTIMEOUT_CPUC (2*HZ)
  258. #define REAPTIMEOUT_LIST3 (4*HZ)
  259. #if STATS
  260. #define STATS_INC_ACTIVE(x) ((x)->num_active++)
  261. #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
  262. #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
  263. #define STATS_INC_GROWN(x) ((x)->grown++)
  264. #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
  265. #define STATS_SET_HIGH(x) \
  266. do { \
  267. if ((x)->num_active > (x)->high_mark) \
  268. (x)->high_mark = (x)->num_active; \
  269. } while (0)
  270. #define STATS_INC_ERR(x) ((x)->errors++)
  271. #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
  272. #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
  273. #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
  274. #define STATS_SET_FREEABLE(x, i) \
  275. do { \
  276. if ((x)->max_freeable < i) \
  277. (x)->max_freeable = i; \
  278. } while (0)
  279. #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
  280. #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
  281. #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
  282. #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
  283. #else
  284. #define STATS_INC_ACTIVE(x) do { } while (0)
  285. #define STATS_DEC_ACTIVE(x) do { } while (0)
  286. #define STATS_INC_ALLOCED(x) do { } while (0)
  287. #define STATS_INC_GROWN(x) do { } while (0)
  288. #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
  289. #define STATS_SET_HIGH(x) do { } while (0)
  290. #define STATS_INC_ERR(x) do { } while (0)
  291. #define STATS_INC_NODEALLOCS(x) do { } while (0)
  292. #define STATS_INC_NODEFREES(x) do { } while (0)
  293. #define STATS_INC_ACOVERFLOW(x) do { } while (0)
  294. #define STATS_SET_FREEABLE(x, i) do { } while (0)
  295. #define STATS_INC_ALLOCHIT(x) do { } while (0)
  296. #define STATS_INC_ALLOCMISS(x) do { } while (0)
  297. #define STATS_INC_FREEHIT(x) do { } while (0)
  298. #define STATS_INC_FREEMISS(x) do { } while (0)
  299. #endif
  300. #if DEBUG
  301. static int obj_offset(struct kmem_cache *cachep)
  302. {
  303. return cachep->obj_offset;
  304. }
  305. static int obj_size(struct kmem_cache *cachep)
  306. {
  307. return cachep->obj_size;
  308. }
  309. static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
  310. {
  311. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  312. return (unsigned long long*) (objp + obj_offset(cachep) -
  313. sizeof(unsigned long long));
  314. }
  315. static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
  316. {
  317. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  318. if (cachep->flags & SLAB_STORE_USER)
  319. return (unsigned long long *)(objp + cachep->buffer_size -
  320. sizeof(unsigned long long) -
  321. REDZONE_ALIGN);
  322. return (unsigned long long *) (objp + cachep->buffer_size -
  323. sizeof(unsigned long long));
  324. }
  325. static void **dbg_userword(struct kmem_cache *cachep, void *objp)
  326. {
  327. BUG_ON(!(cachep->flags & SLAB_STORE_USER));
  328. return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
  329. }
  330. #else
  331. #define obj_offset(x) 0
  332. #define obj_size(cachep) (cachep->buffer_size)
  333. #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
  334. #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
  335. #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
  336. #endif
  337. #ifdef CONFIG_TRACING
  338. size_t slab_buffer_size(struct kmem_cache *cachep)
  339. {
  340. return cachep->buffer_size;
  341. }
  342. EXPORT_SYMBOL(slab_buffer_size);
  343. #endif
  344. #define SLAB_MAX_ORDER_HI 1
  345. #define SLAB_MAX_ORDER_LO 0
  346. static int slab_max_order = SLAB_MAX_ORDER_LO;
  347. static bool slab_max_order_set __initdata;
  348. static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
  349. {
  350. page->lru.next = (struct list_head *)cache;
  351. }
  352. static inline struct kmem_cache *page_get_cache(struct page *page)
  353. {
  354. page = compound_head(page);
  355. BUG_ON(!PageSlab(page));
  356. return (struct kmem_cache *)page->lru.next;
  357. }
  358. static inline void page_set_slab(struct page *page, struct slab *slab)
  359. {
  360. page->lru.prev = (struct list_head *)slab;
  361. }
  362. static inline struct slab *page_get_slab(struct page *page)
  363. {
  364. BUG_ON(!PageSlab(page));
  365. return (struct slab *)page->lru.prev;
  366. }
  367. static inline struct kmem_cache *virt_to_cache(const void *obj)
  368. {
  369. struct page *page = virt_to_head_page(obj);
  370. return page_get_cache(page);
  371. }
  372. static inline struct slab *virt_to_slab(const void *obj)
  373. {
  374. struct page *page = virt_to_head_page(obj);
  375. return page_get_slab(page);
  376. }
  377. static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
  378. unsigned int idx)
  379. {
  380. return slab->s_mem + cache->buffer_size * idx;
  381. }
  382. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  383. const struct slab *slab, void *obj)
  384. {
  385. u32 offset = (obj - slab->s_mem);
  386. return reciprocal_divide(offset, cache->reciprocal_buffer_size);
  387. }
  388. struct cache_sizes malloc_sizes[] = {
  389. #define CACHE(x) { .cs_size = (x) },
  390. #include <linux/kmalloc_sizes.h>
  391. CACHE(ULONG_MAX)
  392. #undef CACHE
  393. };
  394. EXPORT_SYMBOL(malloc_sizes);
  395. struct cache_names {
  396. char *name;
  397. char *name_dma;
  398. };
  399. static struct cache_names __initdata cache_names[] = {
  400. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
  401. #include <linux/kmalloc_sizes.h>
  402. {NULL,}
  403. #undef CACHE
  404. };
  405. static struct arraycache_init initarray_cache __initdata =
  406. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  407. static struct arraycache_init initarray_generic =
  408. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  409. static struct kmem_list3 *cache_cache_nodelists[MAX_NUMNODES];
  410. static struct kmem_cache cache_cache = {
  411. .nodelists = cache_cache_nodelists,
  412. .batchcount = 1,
  413. .limit = BOOT_CPUCACHE_ENTRIES,
  414. .shared = 1,
  415. .buffer_size = sizeof(struct kmem_cache),
  416. .name = "kmem_cache",
  417. };
  418. #define BAD_ALIEN_MAGIC 0x01020304ul
  419. static enum {
  420. NONE,
  421. PARTIAL_AC,
  422. PARTIAL_L3,
  423. EARLY,
  424. LATE,
  425. FULL
  426. } g_cpucache_up;
  427. int slab_is_available(void)
  428. {
  429. return g_cpucache_up >= EARLY;
  430. }
  431. #ifdef CONFIG_LOCKDEP
  432. static struct lock_class_key on_slab_l3_key;
  433. static struct lock_class_key on_slab_alc_key;
  434. static struct lock_class_key debugobj_l3_key;
  435. static struct lock_class_key debugobj_alc_key;
  436. static void slab_set_lock_classes(struct kmem_cache *cachep,
  437. struct lock_class_key *l3_key, struct lock_class_key *alc_key,
  438. int q)
  439. {
  440. struct array_cache **alc;
  441. struct kmem_list3 *l3;
  442. int r;
  443. l3 = cachep->nodelists[q];
  444. if (!l3)
  445. return;
  446. lockdep_set_class(&l3->list_lock, l3_key);
  447. alc = l3->alien;
  448. if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
  449. return;
  450. for_each_node(r) {
  451. if (alc[r])
  452. lockdep_set_class(&alc[r]->lock, alc_key);
  453. }
  454. }
  455. static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  456. {
  457. slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
  458. }
  459. static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  460. {
  461. int node;
  462. for_each_online_node(node)
  463. slab_set_debugobj_lock_classes_node(cachep, node);
  464. }
  465. static void init_node_lock_keys(int q)
  466. {
  467. struct cache_sizes *s = malloc_sizes;
  468. if (g_cpucache_up < LATE)
  469. return;
  470. for (s = malloc_sizes; s->cs_size != ULONG_MAX; s++) {
  471. struct kmem_list3 *l3;
  472. l3 = s->cs_cachep->nodelists[q];
  473. if (!l3 || OFF_SLAB(s->cs_cachep))
  474. continue;
  475. slab_set_lock_classes(s->cs_cachep, &on_slab_l3_key,
  476. &on_slab_alc_key, q);
  477. }
  478. }
  479. static inline void init_lock_keys(void)
  480. {
  481. int node;
  482. for_each_node(node)
  483. init_node_lock_keys(node);
  484. }
  485. #else
  486. static void init_node_lock_keys(int q)
  487. {
  488. }
  489. static inline void init_lock_keys(void)
  490. {
  491. }
  492. static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  493. {
  494. }
  495. static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  496. {
  497. }
  498. #endif
  499. static DEFINE_MUTEX(cache_chain_mutex);
  500. static struct list_head cache_chain;
  501. static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
  502. static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
  503. {
  504. return cachep->array[smp_processor_id()];
  505. }
  506. static inline struct kmem_cache *__find_general_cachep(size_t size,
  507. gfp_t gfpflags)
  508. {
  509. struct cache_sizes *csizep = malloc_sizes;
  510. #if DEBUG
  511. BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
  512. #endif
  513. if (!size)
  514. return ZERO_SIZE_PTR;
  515. while (size > csizep->cs_size)
  516. csizep++;
  517. #ifdef CONFIG_ZONE_DMA
  518. if (unlikely(gfpflags & GFP_DMA))
  519. return csizep->cs_dmacachep;
  520. #endif
  521. return csizep->cs_cachep;
  522. }
  523. static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
  524. {
  525. return __find_general_cachep(size, gfpflags);
  526. }
  527. static size_t slab_mgmt_size(size_t nr_objs, size_t align)
  528. {
  529. return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
  530. }
  531. static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  532. size_t align, int flags, size_t *left_over,
  533. unsigned int *num)
  534. {
  535. int nr_objs;
  536. size_t mgmt_size;
  537. size_t slab_size = PAGE_SIZE << gfporder;
  538. if (flags & CFLGS_OFF_SLAB) {
  539. mgmt_size = 0;
  540. nr_objs = slab_size / buffer_size;
  541. if (nr_objs > SLAB_LIMIT)
  542. nr_objs = SLAB_LIMIT;
  543. } else {
  544. nr_objs = (slab_size - sizeof(struct slab)) /
  545. (buffer_size + sizeof(kmem_bufctl_t));
  546. if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  547. > slab_size)
  548. nr_objs--;
  549. if (nr_objs > SLAB_LIMIT)
  550. nr_objs = SLAB_LIMIT;
  551. mgmt_size = slab_mgmt_size(nr_objs, align);
  552. }
  553. *num = nr_objs;
  554. *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
  555. }
  556. #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
  557. static void __slab_error(const char *function, struct kmem_cache *cachep,
  558. char *msg)
  559. {
  560. printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
  561. function, cachep->name, msg);
  562. dump_stack();
  563. }
  564. static int use_alien_caches __read_mostly = 1;
  565. static int __init noaliencache_setup(char *s)
  566. {
  567. use_alien_caches = 0;
  568. return 1;
  569. }
  570. __setup("noaliencache", noaliencache_setup);
  571. static int __init slab_max_order_setup(char *str)
  572. {
  573. get_option(&str, &slab_max_order);
  574. slab_max_order = slab_max_order < 0 ? 0 :
  575. min(slab_max_order, MAX_ORDER - 1);
  576. slab_max_order_set = true;
  577. return 1;
  578. }
  579. __setup("slab_max_order=", slab_max_order_setup);
  580. #ifdef CONFIG_NUMA
  581. static DEFINE_PER_CPU(unsigned long, slab_reap_node);
  582. static void init_reap_node(int cpu)
  583. {
  584. int node;
  585. node = next_node(cpu_to_mem(cpu), node_online_map);
  586. if (node == MAX_NUMNODES)
  587. node = first_node(node_online_map);
  588. per_cpu(slab_reap_node, cpu) = node;
  589. }
  590. static void next_reap_node(void)
  591. {
  592. int node = __this_cpu_read(slab_reap_node);
  593. node = next_node(node, node_online_map);
  594. if (unlikely(node >= MAX_NUMNODES))
  595. node = first_node(node_online_map);
  596. __this_cpu_write(slab_reap_node, node);
  597. }
  598. #else
  599. #define init_reap_node(cpu) do { } while (0)
  600. #define next_reap_node(void) do { } while (0)
  601. #endif
  602. static void __cpuinit start_cpu_timer(int cpu)
  603. {
  604. struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
  605. if (keventd_up() && reap_work->work.func == NULL) {
  606. init_reap_node(cpu);
  607. INIT_DELAYED_WORK_DEFERRABLE(reap_work, cache_reap);
  608. schedule_delayed_work_on(cpu, reap_work,
  609. __round_jiffies_relative(HZ, cpu));
  610. }
  611. }
  612. static struct array_cache *alloc_arraycache(int node, int entries,
  613. int batchcount, gfp_t gfp)
  614. {
  615. int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
  616. struct array_cache *nc = NULL;
  617. nc = kmalloc_node(memsize, gfp, node);
  618. kmemleak_no_scan(nc);
  619. if (nc) {
  620. nc->avail = 0;
  621. nc->limit = entries;
  622. nc->batchcount = batchcount;
  623. nc->touched = 0;
  624. spin_lock_init(&nc->lock);
  625. }
  626. return nc;
  627. }
  628. static int transfer_objects(struct array_cache *to,
  629. struct array_cache *from, unsigned int max)
  630. {
  631. int nr = min3(from->avail, max, to->limit - to->avail);
  632. if (!nr)
  633. return 0;
  634. memcpy(to->entry + to->avail, from->entry + from->avail -nr,
  635. sizeof(void *) *nr);
  636. from->avail -= nr;
  637. to->avail += nr;
  638. return nr;
  639. }
  640. #ifndef CONFIG_NUMA
  641. #define drain_alien_cache(cachep, alien) do { } while (0)
  642. #define reap_alien(cachep, l3) do { } while (0)
  643. static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
  644. {
  645. return (struct array_cache **)BAD_ALIEN_MAGIC;
  646. }
  647. static inline void free_alien_cache(struct array_cache **ac_ptr)
  648. {
  649. }
  650. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  651. {
  652. return 0;
  653. }
  654. static inline void *alternate_node_alloc(struct kmem_cache *cachep,
  655. gfp_t flags)
  656. {
  657. return NULL;
  658. }
  659. static inline void *____cache_alloc_node(struct kmem_cache *cachep,
  660. gfp_t flags, int nodeid)
  661. {
  662. return NULL;
  663. }
  664. #else
  665. static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
  666. static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
  667. static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
  668. {
  669. struct array_cache **ac_ptr;
  670. int memsize = sizeof(void *) * nr_node_ids;
  671. int i;
  672. if (limit > 1)
  673. limit = 12;
  674. ac_ptr = kzalloc_node(memsize, gfp, node);
  675. if (ac_ptr) {
  676. for_each_node(i) {
  677. if (i == node || !node_online(i))
  678. continue;
  679. ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
  680. if (!ac_ptr[i]) {
  681. for (i--; i >= 0; i--)
  682. kfree(ac_ptr[i]);
  683. kfree(ac_ptr);
  684. return NULL;
  685. }
  686. }
  687. }
  688. return ac_ptr;
  689. }
  690. static void free_alien_cache(struct array_cache **ac_ptr)
  691. {
  692. int i;
  693. if (!ac_ptr)
  694. return;
  695. for_each_node(i)
  696. kfree(ac_ptr[i]);
  697. kfree(ac_ptr);
  698. }
  699. static void __drain_alien_cache(struct kmem_cache *cachep,
  700. struct array_cache *ac, int node)
  701. {
  702. struct kmem_list3 *rl3 = cachep->nodelists[node];
  703. if (ac->avail) {
  704. spin_lock(&rl3->list_lock);
  705. if (rl3->shared)
  706. transfer_objects(rl3->shared, ac, ac->limit);
  707. free_block(cachep, ac->entry, ac->avail, node);
  708. ac->avail = 0;
  709. spin_unlock(&rl3->list_lock);
  710. }
  711. }
  712. static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
  713. {
  714. int node = __this_cpu_read(slab_reap_node);
  715. if (l3->alien) {
  716. struct array_cache *ac = l3->alien[node];
  717. if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
  718. __drain_alien_cache(cachep, ac, node);
  719. spin_unlock_irq(&ac->lock);
  720. }
  721. }
  722. }
  723. static void drain_alien_cache(struct kmem_cache *cachep,
  724. struct array_cache **alien)
  725. {
  726. int i = 0;
  727. struct array_cache *ac;
  728. unsigned long flags;
  729. for_each_online_node(i) {
  730. ac = alien[i];
  731. if (ac) {
  732. spin_lock_irqsave(&ac->lock, flags);
  733. __drain_alien_cache(cachep, ac, i);
  734. spin_unlock_irqrestore(&ac->lock, flags);
  735. }
  736. }
  737. }
  738. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  739. {
  740. struct slab *slabp = virt_to_slab(objp);
  741. int nodeid = slabp->nodeid;
  742. struct kmem_list3 *l3;
  743. struct array_cache *alien = NULL;
  744. int node;
  745. node = numa_mem_id();
  746. if (likely(slabp->nodeid == node))
  747. return 0;
  748. l3 = cachep->nodelists[node];
  749. STATS_INC_NODEFREES(cachep);
  750. if (l3->alien && l3->alien[nodeid]) {
  751. alien = l3->alien[nodeid];
  752. spin_lock(&alien->lock);
  753. if (unlikely(alien->avail == alien->limit)) {
  754. STATS_INC_ACOVERFLOW(cachep);
  755. __drain_alien_cache(cachep, alien, nodeid);
  756. }
  757. alien->entry[alien->avail++] = objp;
  758. spin_unlock(&alien->lock);
  759. } else {
  760. spin_lock(&(cachep->nodelists[nodeid])->list_lock);
  761. free_block(cachep, &objp, 1, nodeid);
  762. spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
  763. }
  764. return 1;
  765. }
  766. #endif
  767. static int init_cache_nodelists_node(int node)
  768. {
  769. struct kmem_cache *cachep;
  770. struct kmem_list3 *l3;
  771. const int memsize = sizeof(struct kmem_list3);
  772. list_for_each_entry(cachep, &cache_chain, next) {
  773. if (!cachep->nodelists[node]) {
  774. l3 = kmalloc_node(memsize, GFP_KERNEL, node);
  775. if (!l3)
  776. return -ENOMEM;
  777. kmem_list3_init(l3);
  778. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  779. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  780. cachep->nodelists[node] = l3;
  781. }
  782. spin_lock_irq(&cachep->nodelists[node]->list_lock);
  783. cachep->nodelists[node]->free_limit =
  784. (1 + nr_cpus_node(node)) *
  785. cachep->batchcount + cachep->num;
  786. spin_unlock_irq(&cachep->nodelists[node]->list_lock);
  787. }
  788. return 0;
  789. }
  790. static void __cpuinit cpuup_canceled(long cpu)
  791. {
  792. struct kmem_cache *cachep;
  793. struct kmem_list3 *l3 = NULL;
  794. int node = cpu_to_mem(cpu);
  795. const struct cpumask *mask = cpumask_of_node(node);
  796. list_for_each_entry(cachep, &cache_chain, next) {
  797. struct array_cache *nc;
  798. struct array_cache *shared;
  799. struct array_cache **alien;
  800. nc = cachep->array[cpu];
  801. cachep->array[cpu] = NULL;
  802. l3 = cachep->nodelists[node];
  803. if (!l3)
  804. goto free_array_cache;
  805. spin_lock_irq(&l3->list_lock);
  806. l3->free_limit -= cachep->batchcount;
  807. if (nc)
  808. free_block(cachep, nc->entry, nc->avail, node);
  809. if (!cpumask_empty(mask)) {
  810. spin_unlock_irq(&l3->list_lock);
  811. goto free_array_cache;
  812. }
  813. shared = l3->shared;
  814. if (shared) {
  815. free_block(cachep, shared->entry,
  816. shared->avail, node);
  817. l3->shared = NULL;
  818. }
  819. alien = l3->alien;
  820. l3->alien = NULL;
  821. spin_unlock_irq(&l3->list_lock);
  822. kfree(shared);
  823. if (alien) {
  824. drain_alien_cache(cachep, alien);
  825. free_alien_cache(alien);
  826. }
  827. free_array_cache:
  828. kfree(nc);
  829. }
  830. list_for_each_entry(cachep, &cache_chain, next) {
  831. l3 = cachep->nodelists[node];
  832. if (!l3)
  833. continue;
  834. drain_freelist(cachep, l3, l3->free_objects);
  835. }
  836. }
  837. static int __cpuinit cpuup_prepare(long cpu)
  838. {
  839. struct kmem_cache *cachep;
  840. struct kmem_list3 *l3 = NULL;
  841. int node = cpu_to_mem(cpu);
  842. int err;
  843. err = init_cache_nodelists_node(node);
  844. if (err < 0)
  845. goto bad;
  846. list_for_each_entry(cachep, &cache_chain, next) {
  847. struct array_cache *nc;
  848. struct array_cache *shared = NULL;
  849. struct array_cache **alien = NULL;
  850. nc = alloc_arraycache(node, cachep->limit,
  851. cachep->batchcount, GFP_KERNEL);
  852. if (!nc)
  853. goto bad;
  854. if (cachep->shared) {
  855. shared = alloc_arraycache(node,
  856. cachep->shared * cachep->batchcount,
  857. 0xbaadf00d, GFP_KERNEL);
  858. if (!shared) {
  859. kfree(nc);
  860. goto bad;
  861. }
  862. }
  863. if (use_alien_caches) {
  864. alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
  865. if (!alien) {
  866. kfree(shared);
  867. kfree(nc);
  868. goto bad;
  869. }
  870. }
  871. cachep->array[cpu] = nc;
  872. l3 = cachep->nodelists[node];
  873. BUG_ON(!l3);
  874. spin_lock_irq(&l3->list_lock);
  875. if (!l3->shared) {
  876. l3->shared = shared;
  877. shared = NULL;
  878. }
  879. #ifdef CONFIG_NUMA
  880. if (!l3->alien) {
  881. l3->alien = alien;
  882. alien = NULL;
  883. }
  884. #endif
  885. spin_unlock_irq(&l3->list_lock);
  886. kfree(shared);
  887. free_alien_cache(alien);
  888. if (cachep->flags & SLAB_DEBUG_OBJECTS)
  889. slab_set_debugobj_lock_classes_node(cachep, node);
  890. }
  891. init_node_lock_keys(node);
  892. return 0;
  893. bad:
  894. cpuup_canceled(cpu);
  895. return -ENOMEM;
  896. }
  897. static int __cpuinit cpuup_callback(struct notifier_block *nfb,
  898. unsigned long action, void *hcpu)
  899. {
  900. long cpu = (long)hcpu;
  901. int err = 0;
  902. switch (action) {
  903. case CPU_UP_PREPARE:
  904. case CPU_UP_PREPARE_FROZEN:
  905. mutex_lock(&cache_chain_mutex);
  906. err = cpuup_prepare(cpu);
  907. mutex_unlock(&cache_chain_mutex);
  908. break;
  909. case CPU_ONLINE:
  910. case CPU_ONLINE_FROZEN:
  911. start_cpu_timer(cpu);
  912. break;
  913. #ifdef CONFIG_HOTPLUG_CPU
  914. case CPU_DOWN_PREPARE:
  915. case CPU_DOWN_PREPARE_FROZEN:
  916. cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
  917. per_cpu(slab_reap_work, cpu).work.func = NULL;
  918. break;
  919. case CPU_DOWN_FAILED:
  920. case CPU_DOWN_FAILED_FROZEN:
  921. start_cpu_timer(cpu);
  922. break;
  923. case CPU_DEAD:
  924. case CPU_DEAD_FROZEN:
  925. #endif
  926. case CPU_UP_CANCELED:
  927. case CPU_UP_CANCELED_FROZEN:
  928. mutex_lock(&cache_chain_mutex);
  929. cpuup_canceled(cpu);
  930. mutex_unlock(&cache_chain_mutex);
  931. break;
  932. }
  933. return notifier_from_errno(err);
  934. }
  935. static struct notifier_block __cpuinitdata cpucache_notifier = {
  936. &cpuup_callback, NULL, 0
  937. };
  938. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  939. static int __meminit drain_cache_nodelists_node(int node)
  940. {
  941. struct kmem_cache *cachep;
  942. int ret = 0;
  943. list_for_each_entry(cachep, &cache_chain, next) {
  944. struct kmem_list3 *l3;
  945. l3 = cachep->nodelists[node];
  946. if (!l3)
  947. continue;
  948. drain_freelist(cachep, l3, l3->free_objects);
  949. if (!list_empty(&l3->slabs_full) ||
  950. !list_empty(&l3->slabs_partial)) {
  951. ret = -EBUSY;
  952. break;
  953. }
  954. }
  955. return ret;
  956. }
  957. static int __meminit slab_memory_callback(struct notifier_block *self,
  958. unsigned long action, void *arg)
  959. {
  960. struct memory_notify *mnb = arg;
  961. int ret = 0;
  962. int nid;
  963. nid = mnb->status_change_nid;
  964. if (nid < 0)
  965. goto out;
  966. switch (action) {
  967. case MEM_GOING_ONLINE:
  968. mutex_lock(&cache_chain_mutex);
  969. ret = init_cache_nodelists_node(nid);
  970. mutex_unlock(&cache_chain_mutex);
  971. break;
  972. case MEM_GOING_OFFLINE:
  973. mutex_lock(&cache_chain_mutex);
  974. ret = drain_cache_nodelists_node(nid);
  975. mutex_unlock(&cache_chain_mutex);
  976. break;
  977. case MEM_ONLINE:
  978. case MEM_OFFLINE:
  979. case MEM_CANCEL_ONLINE:
  980. case MEM_CANCEL_OFFLINE:
  981. break;
  982. }
  983. out:
  984. return notifier_from_errno(ret);
  985. }
  986. #endif
  987. static void __init init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
  988. int nodeid)
  989. {
  990. struct kmem_list3 *ptr;
  991. ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_NOWAIT, nodeid);
  992. BUG_ON(!ptr);
  993. memcpy(ptr, list, sizeof(struct kmem_list3));
  994. spin_lock_init(&ptr->list_lock);
  995. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  996. cachep->nodelists[nodeid] = ptr;
  997. }
  998. static void __init set_up_list3s(struct kmem_cache *cachep, int index)
  999. {
  1000. int node;
  1001. for_each_online_node(node) {
  1002. cachep->nodelists[node] = &initkmem_list3[index + node];
  1003. cachep->nodelists[node]->next_reap = jiffies +
  1004. REAPTIMEOUT_LIST3 +
  1005. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1006. }
  1007. }
  1008. void __init kmem_cache_init(void)
  1009. {
  1010. size_t left_over;
  1011. struct cache_sizes *sizes;
  1012. struct cache_names *names;
  1013. int i;
  1014. int order;
  1015. int node;
  1016. if (num_possible_nodes() == 1)
  1017. use_alien_caches = 0;
  1018. for (i = 0; i < NUM_INIT_LISTS; i++) {
  1019. kmem_list3_init(&initkmem_list3[i]);
  1020. if (i < MAX_NUMNODES)
  1021. cache_cache.nodelists[i] = NULL;
  1022. }
  1023. set_up_list3s(&cache_cache, CACHE_CACHE);
  1024. if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
  1025. slab_max_order = SLAB_MAX_ORDER_HI;
  1026. node = numa_mem_id();
  1027. INIT_LIST_HEAD(&cache_chain);
  1028. list_add(&cache_cache.next, &cache_chain);
  1029. cache_cache.colour_off = cache_line_size();
  1030. cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
  1031. cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE + node];
  1032. cache_cache.buffer_size = offsetof(struct kmem_cache, array[nr_cpu_ids]) +
  1033. nr_node_ids * sizeof(struct kmem_list3 *);
  1034. #if DEBUG
  1035. cache_cache.obj_size = cache_cache.buffer_size;
  1036. #endif
  1037. cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
  1038. cache_line_size());
  1039. cache_cache.reciprocal_buffer_size =
  1040. reciprocal_value(cache_cache.buffer_size);
  1041. for (order = 0; order < MAX_ORDER; order++) {
  1042. cache_estimate(order, cache_cache.buffer_size,
  1043. cache_line_size(), 0, &left_over, &cache_cache.num);
  1044. if (cache_cache.num)
  1045. break;
  1046. }
  1047. BUG_ON(!cache_cache.num);
  1048. cache_cache.gfporder = order;
  1049. cache_cache.colour = left_over / cache_cache.colour_off;
  1050. cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
  1051. sizeof(struct slab), cache_line_size());
  1052. sizes = malloc_sizes;
  1053. names = cache_names;
  1054. sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
  1055. sizes[INDEX_AC].cs_size,
  1056. ARCH_KMALLOC_MINALIGN,
  1057. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1058. NULL);
  1059. if (INDEX_AC != INDEX_L3) {
  1060. sizes[INDEX_L3].cs_cachep =
  1061. kmem_cache_create(names[INDEX_L3].name,
  1062. sizes[INDEX_L3].cs_size,
  1063. ARCH_KMALLOC_MINALIGN,
  1064. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1065. NULL);
  1066. }
  1067. slab_early_init = 0;
  1068. while (sizes->cs_size != ULONG_MAX) {
  1069. if (!sizes->cs_cachep) {
  1070. sizes->cs_cachep = kmem_cache_create(names->name,
  1071. sizes->cs_size,
  1072. ARCH_KMALLOC_MINALIGN,
  1073. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1074. NULL);
  1075. }
  1076. #ifdef CONFIG_ZONE_DMA
  1077. sizes->cs_dmacachep = kmem_cache_create(
  1078. names->name_dma,
  1079. sizes->cs_size,
  1080. ARCH_KMALLOC_MINALIGN,
  1081. ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
  1082. SLAB_PANIC,
  1083. NULL);
  1084. #endif
  1085. sizes++;
  1086. names++;
  1087. }
  1088. {
  1089. struct array_cache *ptr;
  1090. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1091. BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
  1092. memcpy(ptr, cpu_cache_get(&cache_cache),
  1093. sizeof(struct arraycache_init));
  1094. spin_lock_init(&ptr->lock);
  1095. cache_cache.array[smp_processor_id()] = ptr;
  1096. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1097. BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
  1098. != &initarray_generic.cache);
  1099. memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
  1100. sizeof(struct arraycache_init));
  1101. spin_lock_init(&ptr->lock);
  1102. malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
  1103. ptr;
  1104. }
  1105. {
  1106. int nid;
  1107. for_each_online_node(nid) {
  1108. init_list(&cache_cache, &initkmem_list3[CACHE_CACHE + nid], nid);
  1109. init_list(malloc_sizes[INDEX_AC].cs_cachep,
  1110. &initkmem_list3[SIZE_AC + nid], nid);
  1111. if (INDEX_AC != INDEX_L3) {
  1112. init_list(malloc_sizes[INDEX_L3].cs_cachep,
  1113. &initkmem_list3[SIZE_L3 + nid], nid);
  1114. }
  1115. }
  1116. }
  1117. g_cpucache_up = EARLY;
  1118. }
  1119. void __init kmem_cache_init_late(void)
  1120. {
  1121. struct kmem_cache *cachep;
  1122. g_cpucache_up = LATE;
  1123. init_lock_keys();
  1124. mutex_lock(&cache_chain_mutex);
  1125. list_for_each_entry(cachep, &cache_chain, next)
  1126. if (enable_cpucache(cachep, GFP_NOWAIT))
  1127. BUG();
  1128. mutex_unlock(&cache_chain_mutex);
  1129. g_cpucache_up = FULL;
  1130. register_cpu_notifier(&cpucache_notifier);
  1131. #ifdef CONFIG_NUMA
  1132. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  1133. #endif
  1134. }
  1135. static int __init cpucache_init(void)
  1136. {
  1137. int cpu;
  1138. for_each_online_cpu(cpu)
  1139. start_cpu_timer(cpu);
  1140. return 0;
  1141. }
  1142. __initcall(cpucache_init);
  1143. static noinline void
  1144. slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
  1145. {
  1146. struct kmem_list3 *l3;
  1147. struct slab *slabp;
  1148. unsigned long flags;
  1149. int node;
  1150. printk(KERN_WARNING
  1151. "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1152. nodeid, gfpflags);
  1153. printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
  1154. cachep->name, cachep->buffer_size, cachep->gfporder);
  1155. for_each_online_node(node) {
  1156. unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  1157. unsigned long active_slabs = 0, num_slabs = 0;
  1158. l3 = cachep->nodelists[node];
  1159. if (!l3)
  1160. continue;
  1161. spin_lock_irqsave(&l3->list_lock, flags);
  1162. list_for_each_entry(slabp, &l3->slabs_full, list) {
  1163. active_objs += cachep->num;
  1164. active_slabs++;
  1165. }
  1166. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  1167. active_objs += slabp->inuse;
  1168. active_slabs++;
  1169. }
  1170. list_for_each_entry(slabp, &l3->slabs_free, list)
  1171. num_slabs++;
  1172. free_objects += l3->free_objects;
  1173. spin_unlock_irqrestore(&l3->list_lock, flags);
  1174. num_slabs += active_slabs;
  1175. num_objs = num_slabs * cachep->num;
  1176. printk(KERN_WARNING
  1177. " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
  1178. node, active_slabs, num_slabs, active_objs, num_objs,
  1179. free_objects);
  1180. }
  1181. }
  1182. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1183. {
  1184. struct page *page;
  1185. int nr_pages;
  1186. int i;
  1187. #ifndef CONFIG_MMU
  1188. flags |= __GFP_COMP;
  1189. #endif
  1190. flags |= cachep->gfpflags;
  1191. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1192. flags |= __GFP_RECLAIMABLE;
  1193. page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
  1194. if (!page) {
  1195. if (!(flags & __GFP_NOWARN) && printk_ratelimit())
  1196. slab_out_of_memory(cachep, flags, nodeid);
  1197. return NULL;
  1198. }
  1199. nr_pages = (1 << cachep->gfporder);
  1200. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1201. add_zone_page_state(page_zone(page),
  1202. NR_SLAB_RECLAIMABLE, nr_pages);
  1203. else
  1204. add_zone_page_state(page_zone(page),
  1205. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1206. for (i = 0; i < nr_pages; i++)
  1207. __SetPageSlab(page + i);
  1208. if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  1209. kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  1210. if (cachep->ctor)
  1211. kmemcheck_mark_uninitialized_pages(page, nr_pages);
  1212. else
  1213. kmemcheck_mark_unallocated_pages(page, nr_pages);
  1214. }
  1215. return page_address(page);
  1216. }
  1217. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1218. {
  1219. unsigned long i = (1 << cachep->gfporder);
  1220. struct page *page = virt_to_page(addr);
  1221. const unsigned long nr_freed = i;
  1222. kmemcheck_free_shadow(page, cachep->gfporder);
  1223. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1224. sub_zone_page_state(page_zone(page),
  1225. NR_SLAB_RECLAIMABLE, nr_freed);
  1226. else
  1227. sub_zone_page_state(page_zone(page),
  1228. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1229. while (i--) {
  1230. BUG_ON(!PageSlab(page));
  1231. __ClearPageSlab(page);
  1232. page++;
  1233. }
  1234. if (current->reclaim_state)
  1235. current->reclaim_state->reclaimed_slab += nr_freed;
  1236. free_pages((unsigned long)addr, cachep->gfporder);
  1237. }
  1238. static void kmem_rcu_free(struct rcu_head *head)
  1239. {
  1240. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1241. struct kmem_cache *cachep = slab_rcu->cachep;
  1242. kmem_freepages(cachep, slab_rcu->addr);
  1243. if (OFF_SLAB(cachep))
  1244. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1245. }
  1246. #if DEBUG
  1247. #ifdef CONFIG_DEBUG_PAGEALLOC
  1248. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1249. unsigned long caller)
  1250. {
  1251. int size = obj_size(cachep);
  1252. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1253. if (size < 5 * sizeof(unsigned long))
  1254. return;
  1255. *addr++ = 0x12345678;
  1256. *addr++ = caller;
  1257. *addr++ = smp_processor_id();
  1258. size -= 3 * sizeof(unsigned long);
  1259. {
  1260. unsigned long *sptr = &caller;
  1261. unsigned long svalue;
  1262. while (!kstack_end(sptr)) {
  1263. svalue = *sptr++;
  1264. if (kernel_text_address(svalue)) {
  1265. *addr++ = svalue;
  1266. size -= sizeof(unsigned long);
  1267. if (size <= sizeof(unsigned long))
  1268. break;
  1269. }
  1270. }
  1271. }
  1272. *addr++ = 0x87654321;
  1273. }
  1274. #endif
  1275. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1276. {
  1277. int size = obj_size(cachep);
  1278. addr = &((char *)addr)[obj_offset(cachep)];
  1279. memset(addr, val, size);
  1280. *(unsigned char *)(addr + size - 1) = POISON_END;
  1281. }
  1282. static void dump_line(char *data, int offset, int limit)
  1283. {
  1284. int i;
  1285. unsigned char error = 0;
  1286. int bad_count = 0;
  1287. printk(KERN_ERR "%03x: ", offset);
  1288. for (i = 0; i < limit; i++) {
  1289. if (data[offset + i] != POISON_FREE) {
  1290. error = data[offset + i];
  1291. bad_count++;
  1292. }
  1293. }
  1294. print_hex_dump(KERN_CONT, "", 0, 16, 1,
  1295. &data[offset], limit, 1);
  1296. if (bad_count == 1) {
  1297. error ^= POISON_FREE;
  1298. if (!(error & (error - 1))) {
  1299. printk(KERN_ERR "Single bit error detected. Probably "
  1300. "bad RAM.\n");
  1301. #ifdef CONFIG_X86
  1302. printk(KERN_ERR "Run memtest86+ or a similar memory "
  1303. "test tool.\n");
  1304. #else
  1305. printk(KERN_ERR "Run a memory test tool.\n");
  1306. #endif
  1307. }
  1308. }
  1309. }
  1310. #endif
  1311. #if DEBUG
  1312. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1313. {
  1314. int i, size;
  1315. char *realobj;
  1316. if (cachep->flags & SLAB_RED_ZONE) {
  1317. printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
  1318. *dbg_redzone1(cachep, objp),
  1319. *dbg_redzone2(cachep, objp));
  1320. }
  1321. if (cachep->flags & SLAB_STORE_USER) {
  1322. printk(KERN_ERR "Last user: [<%p>]",
  1323. *dbg_userword(cachep, objp));
  1324. print_symbol("(%s)",
  1325. (unsigned long)*dbg_userword(cachep, objp));
  1326. printk("\n");
  1327. }
  1328. realobj = (char *)objp + obj_offset(cachep);
  1329. size = obj_size(cachep);
  1330. for (i = 0; i < size && lines; i += 16, lines--) {
  1331. int limit;
  1332. limit = 16;
  1333. if (i + limit > size)
  1334. limit = size - i;
  1335. dump_line(realobj, i, limit);
  1336. }
  1337. }
  1338. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1339. {
  1340. char *realobj;
  1341. int size, i;
  1342. int lines = 0;
  1343. realobj = (char *)objp + obj_offset(cachep);
  1344. size = obj_size(cachep);
  1345. for (i = 0; i < size; i++) {
  1346. char exp = POISON_FREE;
  1347. if (i == size - 1)
  1348. exp = POISON_END;
  1349. if (realobj[i] != exp) {
  1350. int limit;
  1351. if (lines == 0) {
  1352. printk(KERN_ERR
  1353. "Slab corruption (%s): %s start=%p, len=%d\n",
  1354. print_tainted(), cachep->name, realobj, size);
  1355. print_objinfo(cachep, objp, 0);
  1356. }
  1357. i = (i / 16) * 16;
  1358. limit = 16;
  1359. if (i + limit > size)
  1360. limit = size - i;
  1361. dump_line(realobj, i, limit);
  1362. i += 16;
  1363. lines++;
  1364. if (lines > 5)
  1365. break;
  1366. }
  1367. }
  1368. if (lines != 0) {
  1369. struct slab *slabp = virt_to_slab(objp);
  1370. unsigned int objnr;
  1371. objnr = obj_to_index(cachep, slabp, objp);
  1372. if (objnr) {
  1373. objp = index_to_obj(cachep, slabp, objnr - 1);
  1374. realobj = (char *)objp + obj_offset(cachep);
  1375. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1376. realobj, size);
  1377. print_objinfo(cachep, objp, 2);
  1378. }
  1379. if (objnr + 1 < cachep->num) {
  1380. objp = index_to_obj(cachep, slabp, objnr + 1);
  1381. realobj = (char *)objp + obj_offset(cachep);
  1382. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1383. realobj, size);
  1384. print_objinfo(cachep, objp, 2);
  1385. }
  1386. }
  1387. }
  1388. #endif
  1389. #if DEBUG
  1390. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1391. {
  1392. int i;
  1393. for (i = 0; i < cachep->num; i++) {
  1394. void *objp = index_to_obj(cachep, slabp, i);
  1395. if (cachep->flags & SLAB_POISON) {
  1396. #ifdef CONFIG_DEBUG_PAGEALLOC
  1397. if (cachep->buffer_size % PAGE_SIZE == 0 &&
  1398. OFF_SLAB(cachep))
  1399. kernel_map_pages(virt_to_page(objp),
  1400. cachep->buffer_size / PAGE_SIZE, 1);
  1401. else
  1402. check_poison_obj(cachep, objp);
  1403. #else
  1404. check_poison_obj(cachep, objp);
  1405. #endif
  1406. }
  1407. if (cachep->flags & SLAB_RED_ZONE) {
  1408. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1409. slab_error(cachep, "start of a freed object "
  1410. "was overwritten");
  1411. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1412. slab_error(cachep, "end of a freed object "
  1413. "was overwritten");
  1414. }
  1415. }
  1416. }
  1417. #else
  1418. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1419. {
  1420. }
  1421. #endif
  1422. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1423. {
  1424. void *addr = slabp->s_mem - slabp->colouroff;
  1425. slab_destroy_debugcheck(cachep, slabp);
  1426. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1427. struct slab_rcu *slab_rcu;
  1428. slab_rcu = (struct slab_rcu *)slabp;
  1429. slab_rcu->cachep = cachep;
  1430. slab_rcu->addr = addr;
  1431. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1432. } else {
  1433. kmem_freepages(cachep, addr);
  1434. if (OFF_SLAB(cachep))
  1435. kmem_cache_free(cachep->slabp_cache, slabp);
  1436. }
  1437. }
  1438. static void __kmem_cache_destroy(struct kmem_cache *cachep)
  1439. {
  1440. int i;
  1441. struct kmem_list3 *l3;
  1442. for_each_online_cpu(i)
  1443. kfree(cachep->array[i]);
  1444. for_each_online_node(i) {
  1445. l3 = cachep->nodelists[i];
  1446. if (l3) {
  1447. kfree(l3->shared);
  1448. free_alien_cache(l3->alien);
  1449. kfree(l3);
  1450. }
  1451. }
  1452. kmem_cache_free(&cache_cache, cachep);
  1453. }
  1454. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1455. size_t size, size_t align, unsigned long flags)
  1456. {
  1457. unsigned long offslab_limit;
  1458. size_t left_over = 0;
  1459. int gfporder;
  1460. for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
  1461. unsigned int num;
  1462. size_t remainder;
  1463. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1464. if (!num)
  1465. continue;
  1466. if (flags & CFLGS_OFF_SLAB) {
  1467. offslab_limit = size - sizeof(struct slab);
  1468. offslab_limit /= sizeof(kmem_bufctl_t);
  1469. if (num > offslab_limit)
  1470. break;
  1471. }
  1472. cachep->num = num;
  1473. cachep->gfporder = gfporder;
  1474. left_over = remainder;
  1475. if (flags & SLAB_RECLAIM_ACCOUNT)
  1476. break;
  1477. if (gfporder >= slab_max_order)
  1478. break;
  1479. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1480. break;
  1481. }
  1482. return left_over;
  1483. }
  1484. static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
  1485. {
  1486. if (g_cpucache_up == FULL)
  1487. return enable_cpucache(cachep, gfp);
  1488. if (g_cpucache_up == NONE) {
  1489. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1490. set_up_list3s(cachep, SIZE_AC);
  1491. if (INDEX_AC == INDEX_L3)
  1492. g_cpucache_up = PARTIAL_L3;
  1493. else
  1494. g_cpucache_up = PARTIAL_AC;
  1495. } else {
  1496. cachep->array[smp_processor_id()] =
  1497. kmalloc(sizeof(struct arraycache_init), gfp);
  1498. if (g_cpucache_up == PARTIAL_AC) {
  1499. set_up_list3s(cachep, SIZE_L3);
  1500. g_cpucache_up = PARTIAL_L3;
  1501. } else {
  1502. int node;
  1503. for_each_online_node(node) {
  1504. cachep->nodelists[node] =
  1505. kmalloc_node(sizeof(struct kmem_list3),
  1506. gfp, node);
  1507. BUG_ON(!cachep->nodelists[node]);
  1508. kmem_list3_init(cachep->nodelists[node]);
  1509. }
  1510. }
  1511. }
  1512. cachep->nodelists[numa_mem_id()]->next_reap =
  1513. jiffies + REAPTIMEOUT_LIST3 +
  1514. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1515. cpu_cache_get(cachep)->avail = 0;
  1516. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1517. cpu_cache_get(cachep)->batchcount = 1;
  1518. cpu_cache_get(cachep)->touched = 0;
  1519. cachep->batchcount = 1;
  1520. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1521. return 0;
  1522. }
  1523. struct kmem_cache *
  1524. kmem_cache_create (const char *name, size_t size, size_t align,
  1525. unsigned long flags, void (*ctor)(void *))
  1526. {
  1527. size_t left_over, slab_size, ralign;
  1528. struct kmem_cache *cachep = NULL, *pc;
  1529. gfp_t gfp;
  1530. if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
  1531. size > KMALLOC_MAX_SIZE) {
  1532. printk(KERN_ERR "%s: Early error in slab %s\n", __func__,
  1533. name);
  1534. BUG();
  1535. }
  1536. if (slab_is_available()) {
  1537. get_online_cpus();
  1538. mutex_lock(&cache_chain_mutex);
  1539. }
  1540. list_for_each_entry(pc, &cache_chain, next) {
  1541. char tmp;
  1542. int res;
  1543. res = probe_kernel_address(pc->name, tmp);
  1544. if (res) {
  1545. printk(KERN_ERR
  1546. "SLAB: cache with size %d has lost its name\n",
  1547. pc->buffer_size);
  1548. continue;
  1549. }
  1550. if (!strcmp(pc->name, name)) {
  1551. printk(KERN_ERR
  1552. "kmem_cache_create: duplicate cache %s\n", name);
  1553. dump_stack();
  1554. goto oops;
  1555. }
  1556. }
  1557. #if DEBUG
  1558. WARN_ON(strchr(name, ' '));
  1559. #if FORCED_DEBUG
  1560. if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  1561. 2 * sizeof(unsigned long long)))
  1562. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  1563. if (!(flags & SLAB_DESTROY_BY_RCU))
  1564. flags |= SLAB_POISON;
  1565. #endif
  1566. if (flags & SLAB_DESTROY_BY_RCU)
  1567. BUG_ON(flags & SLAB_POISON);
  1568. #endif
  1569. BUG_ON(flags & ~CREATE_MASK);
  1570. if (size & (BYTES_PER_WORD - 1)) {
  1571. size += (BYTES_PER_WORD - 1);
  1572. size &= ~(BYTES_PER_WORD - 1);
  1573. }
  1574. if (flags & SLAB_HWCACHE_ALIGN) {
  1575. ralign = cache_line_size();
  1576. while (size <= ralign / 2)
  1577. ralign /= 2;
  1578. } else {
  1579. ralign = BYTES_PER_WORD;
  1580. }
  1581. if (flags & SLAB_STORE_USER)
  1582. ralign = BYTES_PER_WORD;
  1583. if (flags & SLAB_RED_ZONE) {
  1584. ralign = REDZONE_ALIGN;
  1585. size += REDZONE_ALIGN - 1;
  1586. size &= ~(REDZONE_ALIGN - 1);
  1587. }
  1588. if (ralign < ARCH_SLAB_MINALIGN) {
  1589. ralign = ARCH_SLAB_MINALIGN;
  1590. }
  1591. if (ralign < align) {
  1592. ralign = align;
  1593. }
  1594. if (ralign > __alignof__(unsigned long long))
  1595. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1596. align = ralign;
  1597. if (slab_is_available())
  1598. gfp = GFP_KERNEL;
  1599. else
  1600. gfp = GFP_NOWAIT;
  1601. cachep = kmem_cache_zalloc(&cache_cache, gfp);
  1602. if (!cachep)
  1603. goto oops;
  1604. cachep->nodelists = (struct kmem_list3 **)&cachep->array[nr_cpu_ids];
  1605. #if DEBUG
  1606. cachep->obj_size = size;
  1607. if (flags & SLAB_RED_ZONE) {
  1608. cachep->obj_offset += sizeof(unsigned long long);
  1609. size += 2 * sizeof(unsigned long long);
  1610. }
  1611. if (flags & SLAB_STORE_USER) {
  1612. if (flags & SLAB_RED_ZONE)
  1613. size += REDZONE_ALIGN;
  1614. else
  1615. size += BYTES_PER_WORD;
  1616. }
  1617. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  1618. if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
  1619. && cachep->obj_size > cache_line_size() && ALIGN(size, align) < PAGE_SIZE) {
  1620. cachep->obj_offset += PAGE_SIZE - ALIGN(size, align);
  1621. size = PAGE_SIZE;
  1622. }
  1623. #endif
  1624. #endif
  1625. if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
  1626. !(flags & SLAB_NOLEAKTRACE))
  1627. flags |= CFLGS_OFF_SLAB;
  1628. size = ALIGN(size, align);
  1629. left_over = calculate_slab_order(cachep, size, align, flags);
  1630. if (!cachep->num) {
  1631. printk(KERN_ERR
  1632. "kmem_cache_create: couldn't create cache %s.\n", name);
  1633. kmem_cache_free(&cache_cache, cachep);
  1634. cachep = NULL;
  1635. goto oops;
  1636. }
  1637. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  1638. + sizeof(struct slab), align);
  1639. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  1640. flags &= ~CFLGS_OFF_SLAB;
  1641. left_over -= slab_size;
  1642. }
  1643. if (flags & CFLGS_OFF_SLAB) {
  1644. slab_size =
  1645. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  1646. #ifdef CONFIG_PAGE_POISONING
  1647. if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
  1648. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1649. #endif
  1650. }
  1651. cachep->colour_off = cache_line_size();
  1652. if (cachep->colour_off < align)
  1653. cachep->colour_off = align;
  1654. cachep->colour = left_over / cachep->colour_off;
  1655. cachep->slab_size = slab_size;
  1656. cachep->flags = flags;
  1657. cachep->gfpflags = 0;
  1658. if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
  1659. cachep->gfpflags |= GFP_DMA;
  1660. cachep->buffer_size = size;
  1661. cachep->reciprocal_buffer_size = reciprocal_value(size);
  1662. if (flags & CFLGS_OFF_SLAB) {
  1663. cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
  1664. BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
  1665. }
  1666. cachep->ctor = ctor;
  1667. cachep->name = name;
  1668. if (setup_cpu_cache(cachep, gfp)) {
  1669. __kmem_cache_destroy(cachep);
  1670. cachep = NULL;
  1671. goto oops;
  1672. }
  1673. if (flags & SLAB_DEBUG_OBJECTS) {
  1674. WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
  1675. slab_set_debugobj_lock_classes(cachep);
  1676. }
  1677. list_add(&cachep->next, &cache_chain);
  1678. oops:
  1679. if (!cachep && (flags & SLAB_PANIC))
  1680. panic("kmem_cache_create(): failed to create slab `%s'\n",
  1681. name);
  1682. if (slab_is_available()) {
  1683. mutex_unlock(&cache_chain_mutex);
  1684. put_online_cpus();
  1685. }
  1686. return cachep;
  1687. }
  1688. EXPORT_SYMBOL(kmem_cache_create);
  1689. #if DEBUG
  1690. static void check_irq_off(void)
  1691. {
  1692. BUG_ON(!irqs_disabled());
  1693. }
  1694. static void check_irq_on(void)
  1695. {
  1696. BUG_ON(irqs_disabled());
  1697. }
  1698. static void check_spinlock_acquired(struct kmem_cache *cachep)
  1699. {
  1700. #ifdef CONFIG_SMP
  1701. check_irq_off();
  1702. assert_spin_locked(&cachep->nodelists[numa_mem_id()]->list_lock);
  1703. #endif
  1704. }
  1705. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  1706. {
  1707. #ifdef CONFIG_SMP
  1708. check_irq_off();
  1709. assert_spin_locked(&cachep->nodelists[node]->list_lock);
  1710. #endif
  1711. }
  1712. #else
  1713. #define check_irq_off() do { } while(0)
  1714. #define check_irq_on() do { } while(0)
  1715. #define check_spinlock_acquired(x) do { } while(0)
  1716. #define check_spinlock_acquired_node(x, y) do { } while(0)
  1717. #endif
  1718. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  1719. struct array_cache *ac,
  1720. int force, int node);
  1721. static void do_drain(void *arg)
  1722. {
  1723. struct kmem_cache *cachep = arg;
  1724. struct array_cache *ac;
  1725. int node = numa_mem_id();
  1726. check_irq_off();
  1727. ac = cpu_cache_get(cachep);
  1728. spin_lock(&cachep->nodelists[node]->list_lock);
  1729. free_block(cachep, ac->entry, ac->avail, node);
  1730. spin_unlock(&cachep->nodelists[node]->list_lock);
  1731. ac->avail = 0;
  1732. }
  1733. static void drain_cpu_caches(struct kmem_cache *cachep)
  1734. {
  1735. struct kmem_list3 *l3;
  1736. int node;
  1737. on_each_cpu(do_drain, cachep, 1);
  1738. check_irq_on();
  1739. for_each_online_node(node) {
  1740. l3 = cachep->nodelists[node];
  1741. if (l3 && l3->alien)
  1742. drain_alien_cache(cachep, l3->alien);
  1743. }
  1744. for_each_online_node(node) {
  1745. l3 = cachep->nodelists[node];
  1746. if (l3)
  1747. drain_array(cachep, l3, l3->shared, 1, node);
  1748. }
  1749. }
  1750. static int drain_freelist(struct kmem_cache *cache,
  1751. struct kmem_list3 *l3, int tofree)
  1752. {
  1753. struct list_head *p;
  1754. int nr_freed;
  1755. struct slab *slabp;
  1756. nr_freed = 0;
  1757. while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
  1758. spin_lock_irq(&l3->list_lock);
  1759. p = l3->slabs_free.prev;
  1760. if (p == &l3->slabs_free) {
  1761. spin_unlock_irq(&l3->list_lock);
  1762. goto out;
  1763. }
  1764. slabp = list_entry(p, struct slab, list);
  1765. #if DEBUG
  1766. BUG_ON(slabp->inuse);
  1767. #endif
  1768. list_del(&slabp->list);
  1769. l3->free_objects -= cache->num;
  1770. spin_unlock_irq(&l3->list_lock);
  1771. slab_destroy(cache, slabp);
  1772. nr_freed++;
  1773. }
  1774. out:
  1775. return nr_freed;
  1776. }
  1777. static int __cache_shrink(struct kmem_cache *cachep)
  1778. {
  1779. int ret = 0, i = 0;
  1780. struct kmem_list3 *l3;
  1781. drain_cpu_caches(cachep);
  1782. check_irq_on();
  1783. for_each_online_node(i) {
  1784. l3 = cachep->nodelists[i];
  1785. if (!l3)
  1786. continue;
  1787. drain_freelist(cachep, l3, l3->free_objects);
  1788. ret += !list_empty(&l3->slabs_full) ||
  1789. !list_empty(&l3->slabs_partial);
  1790. }
  1791. return (ret ? 1 : 0);
  1792. }
  1793. int kmem_cache_shrink(struct kmem_cache *cachep)
  1794. {
  1795. int ret;
  1796. BUG_ON(!cachep || in_interrupt());
  1797. get_online_cpus();
  1798. mutex_lock(&cache_chain_mutex);
  1799. ret = __cache_shrink(cachep);
  1800. mutex_unlock(&cache_chain_mutex);
  1801. put_online_cpus();
  1802. return ret;
  1803. }
  1804. EXPORT_SYMBOL(kmem_cache_shrink);
  1805. void kmem_cache_destroy(struct kmem_cache *cachep)
  1806. {
  1807. BUG_ON(!cachep || in_interrupt());
  1808. get_online_cpus();
  1809. mutex_lock(&cache_chain_mutex);
  1810. list_del(&cachep->next);
  1811. if (__cache_shrink(cachep)) {
  1812. slab_error(cachep, "Can't free all objects");
  1813. list_add(&cachep->next, &cache_chain);
  1814. mutex_unlock(&cache_chain_mutex);
  1815. put_online_cpus();
  1816. return;
  1817. }
  1818. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  1819. rcu_barrier();
  1820. __kmem_cache_destroy(cachep);
  1821. mutex_unlock(&cache_chain_mutex);
  1822. put_online_cpus();
  1823. }
  1824. EXPORT_SYMBOL(kmem_cache_destroy);
  1825. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  1826. int colour_off, gfp_t local_flags,
  1827. int nodeid)
  1828. {
  1829. struct slab *slabp;
  1830. if (OFF_SLAB(cachep)) {
  1831. slabp = kmem_cache_alloc_node(cachep->slabp_cache,
  1832. local_flags, nodeid);
  1833. kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
  1834. local_flags);
  1835. if (!slabp)
  1836. return NULL;
  1837. } else {
  1838. slabp = objp + colour_off;
  1839. colour_off += cachep->slab_size;
  1840. }
  1841. slabp->inuse = 0;
  1842. slabp->colouroff = colour_off;
  1843. slabp->s_mem = objp + colour_off;
  1844. slabp->nodeid = nodeid;
  1845. slabp->free = 0;
  1846. return slabp;
  1847. }
  1848. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  1849. {
  1850. return (kmem_bufctl_t *) (slabp + 1);
  1851. }
  1852. static void cache_init_objs(struct kmem_cache *cachep,
  1853. struct slab *slabp)
  1854. {
  1855. int i;
  1856. for (i = 0; i < cachep->num; i++) {
  1857. void *objp = index_to_obj(cachep, slabp, i);
  1858. #if DEBUG
  1859. if (cachep->flags & SLAB_POISON)
  1860. poison_obj(cachep, objp, POISON_FREE);
  1861. if (cachep->flags & SLAB_STORE_USER)
  1862. *dbg_userword(cachep, objp) = NULL;
  1863. if (cachep->flags & SLAB_RED_ZONE) {
  1864. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  1865. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  1866. }
  1867. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  1868. cachep->ctor(objp + obj_offset(cachep));
  1869. if (cachep->flags & SLAB_RED_ZONE) {
  1870. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1871. slab_error(cachep, "constructor overwrote the"
  1872. " end of an object");
  1873. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1874. slab_error(cachep, "constructor overwrote the"
  1875. " start of an object");
  1876. }
  1877. if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
  1878. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  1879. kernel_map_pages(virt_to_page(objp),
  1880. cachep->buffer_size / PAGE_SIZE, 0);
  1881. #else
  1882. if (cachep->ctor)
  1883. cachep->ctor(objp);
  1884. #endif
  1885. slab_bufctl(slabp)[i] = i + 1;
  1886. }
  1887. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  1888. }
  1889. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  1890. {
  1891. if (CONFIG_ZONE_DMA_FLAG) {
  1892. if (flags & GFP_DMA)
  1893. BUG_ON(!(cachep->gfpflags & GFP_DMA));
  1894. else
  1895. BUG_ON(cachep->gfpflags & GFP_DMA);
  1896. }
  1897. }
  1898. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  1899. int nodeid)
  1900. {
  1901. void *objp = index_to_obj(cachep, slabp, slabp->free);
  1902. kmem_bufctl_t next;
  1903. slabp->inuse++;
  1904. next = slab_bufctl(slabp)[slabp->free];
  1905. #if DEBUG
  1906. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  1907. WARN_ON(slabp->nodeid != nodeid);
  1908. #endif
  1909. slabp->free = next;
  1910. return objp;
  1911. }
  1912. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  1913. void *objp, int nodeid)
  1914. {
  1915. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  1916. #if DEBUG
  1917. WARN_ON(slabp->nodeid != nodeid);
  1918. if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
  1919. printk(KERN_ERR "slab: double free detected in cache "
  1920. "'%s', objp %p\n", cachep->name, objp);
  1921. BUG();
  1922. }
  1923. #endif
  1924. slab_bufctl(slabp)[objnr] = slabp->free;
  1925. slabp->free = objnr;
  1926. slabp->inuse--;
  1927. }
  1928. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  1929. void *addr)
  1930. {
  1931. int nr_pages;
  1932. struct page *page;
  1933. page = virt_to_page(addr);
  1934. nr_pages = 1;
  1935. if (likely(!PageCompound(page)))
  1936. nr_pages <<= cache->gfporder;
  1937. do {
  1938. page_set_cache(page, cache);
  1939. page_set_slab(page, slab);
  1940. page++;
  1941. } while (--nr_pages);
  1942. }
  1943. static int cache_grow(struct kmem_cache *cachep,
  1944. gfp_t flags, int nodeid, void *objp)
  1945. {
  1946. struct slab *slabp;
  1947. size_t offset;
  1948. gfp_t local_flags;
  1949. struct kmem_list3 *l3;
  1950. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  1951. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  1952. check_irq_off();
  1953. l3 = cachep->nodelists[nodeid];
  1954. spin_lock(&l3->list_lock);
  1955. offset = l3->colour_next;
  1956. l3->colour_next++;
  1957. if (l3->colour_next >= cachep->colour)
  1958. l3->colour_next = 0;
  1959. spin_unlock(&l3->list_lock);
  1960. offset *= cachep->colour_off;
  1961. if (local_flags & __GFP_WAIT)
  1962. local_irq_enable();
  1963. kmem_flagcheck(cachep, flags);
  1964. if (!objp)
  1965. objp = kmem_getpages(cachep, local_flags, nodeid);
  1966. if (!objp)
  1967. goto failed;
  1968. slabp = alloc_slabmgmt(cachep, objp, offset,
  1969. local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
  1970. if (!slabp)
  1971. goto opps1;
  1972. slab_map_pages(cachep, slabp, objp);
  1973. cache_init_objs(cachep, slabp);
  1974. if (local_flags & __GFP_WAIT)
  1975. local_irq_disable();
  1976. check_irq_off();
  1977. spin_lock(&l3->list_lock);
  1978. list_add_tail(&slabp->list, &(l3->slabs_free));
  1979. STATS_INC_GROWN(cachep);
  1980. l3->free_objects += cachep->num;
  1981. spin_unlock(&l3->list_lock);
  1982. return 1;
  1983. opps1:
  1984. kmem_freepages(cachep, objp);
  1985. failed:
  1986. if (local_flags & __GFP_WAIT)
  1987. local_irq_disable();
  1988. return 0;
  1989. }
  1990. #if DEBUG
  1991. static void kfree_debugcheck(const void *objp)
  1992. {
  1993. if (!virt_addr_valid(objp)) {
  1994. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  1995. (unsigned long)objp);
  1996. BUG();
  1997. }
  1998. }
  1999. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2000. {
  2001. unsigned long long redzone1, redzone2;
  2002. redzone1 = *dbg_redzone1(cache, obj);
  2003. redzone2 = *dbg_redzone2(cache, obj);
  2004. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2005. return;
  2006. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2007. slab_error(cache, "double free detected");
  2008. else
  2009. slab_error(cache, "memory outside object was overwritten");
  2010. printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
  2011. obj, redzone1, redzone2);
  2012. }
  2013. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2014. void *caller)
  2015. {
  2016. struct page *page;
  2017. unsigned int objnr;
  2018. struct slab *slabp;
  2019. BUG_ON(virt_to_cache(objp) != cachep);
  2020. objp -= obj_offset(cachep);
  2021. kfree_debugcheck(objp);
  2022. page = virt_to_head_page(objp);
  2023. slabp = page_get_slab(page);
  2024. if (cachep->flags & SLAB_RED_ZONE) {
  2025. verify_redzone_free(cachep, objp);
  2026. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2027. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2028. }
  2029. if (cachep->flags & SLAB_STORE_USER)
  2030. *dbg_userword(cachep, objp) = caller;
  2031. objnr = obj_to_index(cachep, slabp, objp);
  2032. BUG_ON(objnr >= cachep->num);
  2033. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2034. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2035. slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  2036. #endif
  2037. if (cachep->flags & SLAB_POISON) {
  2038. #ifdef CONFIG_DEBUG_PAGEALLOC
  2039. if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2040. store_stackinfo(cachep, objp, (unsigned long)caller);
  2041. kernel_map_pages(virt_to_page(objp),
  2042. cachep->buffer_size / PAGE_SIZE, 0);
  2043. } else {
  2044. poison_obj(cachep, objp, POISON_FREE);
  2045. }
  2046. #else
  2047. poison_obj(cachep, objp, POISON_FREE);
  2048. #endif
  2049. }
  2050. return objp;
  2051. }
  2052. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2053. {
  2054. kmem_bufctl_t i;
  2055. int entries = 0;
  2056. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2057. entries++;
  2058. if (entries > cachep->num || i >= cachep->num)
  2059. goto bad;
  2060. }
  2061. if (entries != cachep->num - slabp->inuse) {
  2062. bad:
  2063. printk(KERN_ERR "slab: Internal list corruption detected in "
  2064. "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
  2065. cachep->name, cachep->num, slabp, slabp->inuse,
  2066. print_tainted());
  2067. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
  2068. sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
  2069. 1);
  2070. BUG();
  2071. }
  2072. }
  2073. #else
  2074. #define kfree_debugcheck(x) do { } while(0)
  2075. #define cache_free_debugcheck(x,objp,z) (objp)
  2076. #define check_slabp(x,y) do { } while(0)
  2077. #endif
  2078. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
  2079. {
  2080. int batchcount;
  2081. struct kmem_list3 *l3;
  2082. struct array_cache *ac;
  2083. int node;
  2084. retry:
  2085. check_irq_off();
  2086. node = numa_mem_id();
  2087. ac = cpu_cache_get(cachep);
  2088. batchcount = ac->batchcount;
  2089. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2090. batchcount = BATCHREFILL_LIMIT;
  2091. }
  2092. l3 = cachep->nodelists[node];
  2093. BUG_ON(ac->avail > 0 || !l3);
  2094. spin_lock(&l3->list_lock);
  2095. if (l3->shared && transfer_objects(ac, l3->shared, batchcount)) {
  2096. l3->shared->touched = 1;
  2097. goto alloc_done;
  2098. }
  2099. while (batchcount > 0) {
  2100. struct list_head *entry;
  2101. struct slab *slabp;
  2102. entry = l3->slabs_partial.next;
  2103. if (entry == &l3->slabs_partial) {
  2104. l3->free_touched = 1;
  2105. entry = l3->slabs_free.next;
  2106. if (entry == &l3->slabs_free)
  2107. goto must_grow;
  2108. }
  2109. slabp = list_entry(entry, struct slab, list);
  2110. check_slabp(cachep, slabp);
  2111. check_spinlock_acquired(cachep);
  2112. BUG_ON(slabp->inuse >= cachep->num);
  2113. while (slabp->inuse < cachep->num && batchcount--) {
  2114. STATS_INC_ALLOCED(cachep);
  2115. STATS_INC_ACTIVE(cachep);
  2116. STATS_SET_HIGH(cachep);
  2117. ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
  2118. node);
  2119. }
  2120. check_slabp(cachep, slabp);
  2121. list_del(&slabp->list);
  2122. if (slabp->free == BUFCTL_END)
  2123. list_add(&slabp->list, &l3->slabs_full);
  2124. else
  2125. list_add(&slabp->list, &l3->slabs_partial);
  2126. }
  2127. must_grow:
  2128. l3->free_objects -= ac->avail;
  2129. alloc_done:
  2130. spin_unlock(&l3->list_lock);
  2131. if (unlikely(!ac->avail)) {
  2132. int x;
  2133. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  2134. ac = cpu_cache_get(cachep);
  2135. if (!x && ac->avail == 0)
  2136. return NULL;
  2137. if (!ac->avail)
  2138. goto retry;
  2139. }
  2140. ac->touched = 1;
  2141. return ac->entry[--ac->avail];
  2142. }
  2143. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2144. gfp_t flags)
  2145. {
  2146. might_sleep_if(flags & __GFP_WAIT);
  2147. #if DEBUG
  2148. kmem_flagcheck(cachep, flags);
  2149. #endif
  2150. }
  2151. #if DEBUG
  2152. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2153. gfp_t flags, void *objp, void *caller)
  2154. {
  2155. if (!objp)
  2156. return objp;
  2157. if (cachep->flags & SLAB_POISON) {
  2158. #ifdef CONFIG_DEBUG_PAGEALLOC
  2159. if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2160. kernel_map_pages(virt_to_page(objp),
  2161. cachep->buffer_size / PAGE_SIZE, 1);
  2162. else
  2163. check_poison_obj(cachep, objp);
  2164. #else
  2165. check_poison_obj(cachep, objp);
  2166. #endif
  2167. poison_obj(cachep, objp, POISON_INUSE);
  2168. }
  2169. if (cachep->flags & SLAB_STORE_USER)
  2170. *dbg_userword(cachep, objp) = caller;
  2171. if (cachep->flags & SLAB_RED_ZONE) {
  2172. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2173. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2174. slab_error(cachep, "double free, or memory outside"
  2175. " object was overwritten");
  2176. printk(KERN_ERR
  2177. "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2178. objp, *dbg_redzone1(cachep, objp),
  2179. *dbg_redzone2(cachep, objp));
  2180. }
  2181. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2182. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2183. }
  2184. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2185. {
  2186. struct slab *slabp;
  2187. unsigned objnr;
  2188. slabp = page_get_slab(virt_to_head_page(objp));
  2189. objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
  2190. slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  2191. }
  2192. #endif
  2193. objp += obj_offset(cachep);
  2194. if (cachep->ctor && cachep->flags & SLAB_POISON)
  2195. cachep->ctor(objp);
  2196. if (ARCH_SLAB_MINALIGN &&
  2197. ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
  2198. printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2199. objp, (int)ARCH_SLAB_MINALIGN);
  2200. }
  2201. return objp;
  2202. }
  2203. #else
  2204. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2205. #endif
  2206. static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
  2207. {
  2208. if (cachep == &cache_cache)
  2209. return false;
  2210. return should_failslab(obj_size(cachep), flags, cachep->flags);
  2211. }
  2212. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2213. {
  2214. void *objp;
  2215. struct array_cache *ac;
  2216. check_irq_off();
  2217. ac = cpu_cache_get(cachep);
  2218. if (likely(ac->avail)) {
  2219. STATS_INC_ALLOCHIT(cachep);
  2220. ac->touched = 1;
  2221. objp = ac->entry[--ac->avail];
  2222. } else {
  2223. STATS_INC_ALLOCMISS(cachep);
  2224. objp = cache_alloc_refill(cachep, flags);
  2225. ac = cpu_cache_get(cachep);
  2226. }
  2227. if (objp)
  2228. kmemleak_erase(&ac->entry[ac->avail]);
  2229. return objp;
  2230. }
  2231. #ifdef CONFIG_NUMA
  2232. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2233. {
  2234. int nid_alloc, nid_here;
  2235. if (in_interrupt() || (flags & __GFP_THISNODE))
  2236. return NULL;
  2237. nid_alloc = nid_here = numa_mem_id();
  2238. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2239. nid_alloc = cpuset_slab_spread_node();
  2240. else if (current->mempolicy)
  2241. nid_alloc = slab_node(current->mempolicy);
  2242. if (nid_alloc != nid_here)
  2243. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2244. return NULL;
  2245. }
  2246. static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2247. {
  2248. struct zonelist *zonelist;
  2249. gfp_t local_flags;
  2250. struct zoneref *z;
  2251. struct zone *zone;
  2252. enum zone_type high_zoneidx = gfp_zone(flags);
  2253. void *obj = NULL;
  2254. int nid;
  2255. unsigned int cpuset_mems_cookie;
  2256. if (flags & __GFP_THISNODE)
  2257. return NULL;
  2258. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2259. retry_cpuset:
  2260. cpuset_mems_cookie = get_mems_allowed();
  2261. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  2262. retry:
  2263. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  2264. nid = zone_to_nid(zone);
  2265. if (cpuset_zone_allowed_hardwall(zone, flags) &&
  2266. cache->nodelists[nid] &&
  2267. cache->nodelists[nid]->free_objects) {
  2268. obj = ____cache_alloc_node(cache,
  2269. flags | GFP_THISNODE, nid);
  2270. if (obj)
  2271. break;
  2272. }
  2273. }
  2274. if (!obj) {
  2275. if (local_flags & __GFP_WAIT)
  2276. local_irq_enable();
  2277. kmem_flagcheck(cache, flags);
  2278. obj = kmem_getpages(cache, local_flags, numa_mem_id());
  2279. if (local_flags & __GFP_WAIT)
  2280. local_irq_disable();
  2281. if (obj) {
  2282. nid = page_to_nid(virt_to_page(obj));
  2283. if (cache_grow(cache, flags, nid, obj)) {
  2284. obj = ____cache_alloc_node(cache,
  2285. flags | GFP_THISNODE, nid);
  2286. if (!obj)
  2287. goto retry;
  2288. } else {
  2289. obj = NULL;
  2290. }
  2291. }
  2292. }
  2293. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
  2294. goto retry_cpuset;
  2295. return obj;
  2296. }
  2297. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  2298. int nodeid)
  2299. {
  2300. struct list_head *entry;
  2301. struct slab *slabp;
  2302. struct kmem_list3 *l3;
  2303. void *obj;
  2304. int x;
  2305. l3 = cachep->nodelists[nodeid];
  2306. BUG_ON(!l3);
  2307. retry:
  2308. check_irq_off();
  2309. spin_lock(&l3->list_lock);
  2310. entry = l3->slabs_partial.next;
  2311. if (entry == &l3->slabs_partial) {
  2312. l3->free_touched = 1;
  2313. entry = l3->slabs_free.next;
  2314. if (entry == &l3->slabs_free)
  2315. goto must_grow;
  2316. }
  2317. slabp = list_entry(entry, struct slab, list);
  2318. check_spinlock_acquired_node(cachep, nodeid);
  2319. check_slabp(cachep, slabp);
  2320. STATS_INC_NODEALLOCS(cachep);
  2321. STATS_INC_ACTIVE(cachep);
  2322. STATS_SET_HIGH(cachep);
  2323. BUG_ON(slabp->inuse == cachep->num);
  2324. obj = slab_get_obj(cachep, slabp, nodeid);
  2325. check_slabp(cachep, slabp);
  2326. l3->free_objects--;
  2327. list_del(&slabp->list);
  2328. if (slabp->free == BUFCTL_END)
  2329. list_add(&slabp->list, &l3->slabs_full);
  2330. else
  2331. list_add(&slabp->list, &l3->slabs_partial);
  2332. spin_unlock(&l3->list_lock);
  2333. goto done;
  2334. must_grow:
  2335. spin_unlock(&l3->list_lock);
  2336. x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
  2337. if (x)
  2338. goto retry;
  2339. return fallback_alloc(cachep, flags);
  2340. done:
  2341. return obj;
  2342. }
  2343. static __always_inline void *
  2344. __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
  2345. void *caller)
  2346. {
  2347. unsigned long save_flags;
  2348. void *ptr;
  2349. int slab_node = numa_mem_id();
  2350. flags &= gfp_allowed_mask;
  2351. lockdep_trace_alloc(flags);
  2352. if (slab_should_failslab(cachep, flags))
  2353. return NULL;
  2354. cache_alloc_debugcheck_before(cachep, flags);
  2355. local_irq_save(save_flags);
  2356. if (nodeid == NUMA_NO_NODE)
  2357. nodeid = slab_node;
  2358. if (unlikely(!cachep->nodelists[nodeid])) {
  2359. ptr = fallback_alloc(cachep, flags);
  2360. goto out;
  2361. }
  2362. if (nodeid == slab_node) {
  2363. ptr = ____cache_alloc(cachep, flags);
  2364. if (ptr)
  2365. goto out;
  2366. }
  2367. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  2368. out:
  2369. local_irq_restore(save_flags);
  2370. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  2371. kmemleak_alloc_recursive(ptr, obj_size(cachep), 1, cachep->flags,
  2372. flags);
  2373. if (likely(ptr))
  2374. kmemcheck_slab_alloc(cachep, flags, ptr, obj_size(cachep));
  2375. if (unlikely((flags & __GFP_ZERO) && ptr))
  2376. memset(ptr, 0, obj_size(cachep));
  2377. return ptr;
  2378. }
  2379. static __always_inline void *
  2380. __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  2381. {
  2382. void *objp;
  2383. if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
  2384. objp = alternate_node_alloc(cache, flags);
  2385. if (objp)
  2386. goto out;
  2387. }
  2388. objp = ____cache_alloc(cache, flags);
  2389. if (!objp)
  2390. objp = ____cache_alloc_node(cache, flags, numa_mem_id());
  2391. out:
  2392. return objp;
  2393. }
  2394. #else
  2395. static __always_inline void *
  2396. __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2397. {
  2398. return ____cache_alloc(cachep, flags);
  2399. }
  2400. #endif
  2401. static __always_inline void *
  2402. __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
  2403. {
  2404. unsigned long save_flags;
  2405. void *objp;
  2406. flags &= gfp_allowed_mask;
  2407. lockdep_trace_alloc(flags);
  2408. if (slab_should_failslab(cachep, flags))
  2409. return NULL;
  2410. cache_alloc_debugcheck_before(cachep, flags);
  2411. local_irq_save(save_flags);
  2412. objp = __do_cache_alloc(cachep, flags);
  2413. local_irq_restore(save_flags);
  2414. objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
  2415. kmemleak_alloc_recursive(objp, obj_size(cachep), 1, cachep->flags,
  2416. flags);
  2417. prefetchw(objp);
  2418. if (likely(objp))
  2419. kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
  2420. if (unlikely((flags & __GFP_ZERO) && objp))
  2421. memset(objp, 0, obj_size(cachep));
  2422. return objp;
  2423. }
  2424. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  2425. int node)
  2426. {
  2427. int i;
  2428. struct kmem_list3 *l3;
  2429. for (i = 0; i < nr_objects; i++) {
  2430. void *objp = objpp[i];
  2431. struct slab *slabp;
  2432. slabp = virt_to_slab(objp);
  2433. l3 = cachep->nodelists[node];
  2434. list_del(&slabp->list);
  2435. check_spinlock_acquired_node(cachep, node);
  2436. check_slabp(cachep, slabp);
  2437. slab_put_obj(cachep, slabp, objp, node);
  2438. STATS_DEC_ACTIVE(cachep);
  2439. l3->free_objects++;
  2440. check_slabp(cachep, slabp);
  2441. if (slabp->inuse == 0) {
  2442. if (l3->free_objects > l3->free_limit) {
  2443. l3->free_objects -= cachep->num;
  2444. slab_destroy(cachep, slabp);
  2445. } else {
  2446. list_add(&slabp->list, &l3->slabs_free);
  2447. }
  2448. } else {
  2449. list_add_tail(&slabp->list, &l3->slabs_partial);
  2450. }
  2451. }
  2452. }
  2453. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  2454. {
  2455. int batchcount;
  2456. struct kmem_list3 *l3;
  2457. int node = numa_mem_id();
  2458. batchcount = ac->batchcount;
  2459. #if DEBUG
  2460. BUG_ON(!batchcount || batchcount > ac->avail);
  2461. #endif
  2462. check_irq_off();
  2463. l3 = cachep->nodelists[node];
  2464. spin_lock(&l3->list_lock);
  2465. if (l3->shared) {
  2466. struct array_cache *shared_array = l3->shared;
  2467. int max = shared_array->limit - shared_array->avail;
  2468. if (max) {
  2469. if (batchcount > max)
  2470. batchcount = max;
  2471. memcpy(&(shared_array->entry[shared_array->avail]),
  2472. ac->entry, sizeof(void *) * batchcount);
  2473. shared_array->avail += batchcount;
  2474. goto free_done;
  2475. }
  2476. }
  2477. free_block(cachep, ac->entry, batchcount, node);
  2478. free_done:
  2479. #if STATS
  2480. {
  2481. int i = 0;
  2482. struct list_head *p;
  2483. p = l3->slabs_free.next;
  2484. while (p != &(l3->slabs_free)) {
  2485. struct slab *slabp;
  2486. slabp = list_entry(p, struct slab, list);
  2487. BUG_ON(slabp->inuse);
  2488. i++;
  2489. p = p->next;
  2490. }
  2491. STATS_SET_FREEABLE(cachep, i);
  2492. }
  2493. #endif
  2494. spin_unlock(&l3->list_lock);
  2495. ac->avail -= batchcount;
  2496. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  2497. }
  2498. static inline void __cache_free(struct kmem_cache *cachep, void *objp,
  2499. void *caller)
  2500. {
  2501. struct array_cache *ac = cpu_cache_get(cachep);
  2502. check_irq_off();
  2503. kmemleak_free_recursive(objp, cachep->flags);
  2504. objp = cache_free_debugcheck(cachep, objp, caller);
  2505. kmemcheck_slab_free(cachep, objp, obj_size(cachep));
  2506. if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
  2507. return;
  2508. if (likely(ac->avail < ac->limit)) {
  2509. STATS_INC_FREEHIT(cachep);
  2510. } else {
  2511. STATS_INC_FREEMISS(cachep);
  2512. cache_flusharray(cachep, ac);
  2513. }
  2514. ac->entry[ac->avail++] = objp;
  2515. }
  2516. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2517. {
  2518. void *ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
  2519. trace_kmem_cache_alloc(_RET_IP_, ret,
  2520. obj_size(cachep), cachep->buffer_size, flags);
  2521. return ret;
  2522. }
  2523. EXPORT_SYMBOL(kmem_cache_alloc);
  2524. #ifdef CONFIG_TRACING
  2525. void *
  2526. kmem_cache_alloc_trace(size_t size, struct kmem_cache *cachep, gfp_t flags)
  2527. {
  2528. void *ret;
  2529. ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
  2530. trace_kmalloc(_RET_IP_, ret,
  2531. size, slab_buffer_size(cachep), flags);
  2532. return ret;
  2533. }
  2534. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  2535. #endif
  2536. #ifdef CONFIG_NUMA
  2537. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  2538. {
  2539. void *ret = __cache_alloc_node(cachep, flags, nodeid,
  2540. __builtin_return_address(0));
  2541. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  2542. obj_size(cachep), cachep->buffer_size,
  2543. flags, nodeid);
  2544. return ret;
  2545. }
  2546. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2547. #ifdef CONFIG_TRACING
  2548. void *kmem_cache_alloc_node_trace(size_t size,
  2549. struct kmem_cache *cachep,
  2550. gfp_t flags,
  2551. int nodeid)
  2552. {
  2553. void *ret;
  2554. ret = __cache_alloc_node(cachep, flags, nodeid,
  2555. __builtin_return_address(0));
  2556. trace_kmalloc_node(_RET_IP_, ret,
  2557. size, slab_buffer_size(cachep),
  2558. flags, nodeid);
  2559. return ret;
  2560. }
  2561. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  2562. #endif
  2563. static __always_inline void *
  2564. __do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
  2565. {
  2566. struct kmem_cache *cachep;
  2567. cachep = kmem_find_general_cachep(size, flags);
  2568. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  2569. return cachep;
  2570. return kmem_cache_alloc_node_trace(size, cachep, flags, node);
  2571. }
  2572. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  2573. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2574. {
  2575. return __do_kmalloc_node(size, flags, node,
  2576. __builtin_return_address(0));
  2577. }
  2578. EXPORT_SYMBOL(__kmalloc_node);
  2579. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  2580. int node, unsigned long caller)
  2581. {
  2582. return __do_kmalloc_node(size, flags, node, (void *)caller);
  2583. }
  2584. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  2585. #else
  2586. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2587. {
  2588. return __do_kmalloc_node(size, flags, node, NULL);
  2589. }
  2590. EXPORT_SYMBOL(__kmalloc_node);
  2591. #endif
  2592. #endif
  2593. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  2594. void *caller)
  2595. {
  2596. struct kmem_cache *cachep;
  2597. void *ret;
  2598. cachep = __find_general_cachep(size, flags);
  2599. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  2600. return cachep;
  2601. ret = __cache_alloc(cachep, flags, caller);
  2602. trace_kmalloc((unsigned long) caller, ret,
  2603. size, cachep->buffer_size, flags);
  2604. return ret;
  2605. }
  2606. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  2607. void *__kmalloc(size_t size, gfp_t flags)
  2608. {
  2609. return __do_kmalloc(size, flags, __builtin_return_address(0));
  2610. }
  2611. EXPORT_SYMBOL(__kmalloc);
  2612. void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
  2613. {
  2614. return __do_kmalloc(size, flags, (void *)caller);
  2615. }
  2616. EXPORT_SYMBOL(__kmalloc_track_caller);
  2617. #else
  2618. void *__kmalloc(size_t size, gfp_t flags)
  2619. {
  2620. return __do_kmalloc(size, flags, NULL);
  2621. }
  2622. EXPORT_SYMBOL(__kmalloc);
  2623. #endif
  2624. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  2625. {
  2626. unsigned long flags;
  2627. local_irq_save(flags);
  2628. debug_check_no_locks_freed(objp, obj_size(cachep));
  2629. if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
  2630. debug_check_no_obj_freed(objp, obj_size(cachep));
  2631. __cache_free(cachep, objp, __builtin_return_address(0));
  2632. local_irq_restore(flags);
  2633. trace_kmem_cache_free(_RET_IP_, objp);
  2634. }
  2635. EXPORT_SYMBOL(kmem_cache_free);
  2636. void kfree(const void *objp)
  2637. {
  2638. struct kmem_cache *c;
  2639. unsigned long flags;
  2640. trace_kfree(_RET_IP_, objp);
  2641. if (unlikely(ZERO_OR_NULL_PTR(objp)))
  2642. return;
  2643. local_irq_save(flags);
  2644. kfree_debugcheck(objp);
  2645. c = virt_to_cache(objp);
  2646. debug_check_no_locks_freed(objp, obj_size(c));
  2647. debug_check_no_obj_freed(objp, obj_size(c));
  2648. __cache_free(c, (void *)objp, __builtin_return_address(0));
  2649. local_irq_restore(flags);
  2650. }
  2651. EXPORT_SYMBOL(kfree);
  2652. unsigned int kmem_cache_size(struct kmem_cache *cachep)
  2653. {
  2654. return obj_size(cachep);
  2655. }
  2656. EXPORT_SYMBOL(kmem_cache_size);
  2657. static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
  2658. {
  2659. int node;
  2660. struct kmem_list3 *l3;
  2661. struct array_cache *new_shared;
  2662. struct array_cache **new_alien = NULL;
  2663. for_each_online_node(node) {
  2664. if (use_alien_caches) {
  2665. new_alien = alloc_alien_cache(node, cachep->limit, gfp);
  2666. if (!new_alien)
  2667. goto fail;
  2668. }
  2669. new_shared = NULL;
  2670. if (cachep->shared) {
  2671. new_shared = alloc_arraycache(node,
  2672. cachep->shared*cachep->batchcount,
  2673. 0xbaadf00d, gfp);
  2674. if (!new_shared) {
  2675. free_alien_cache(new_alien);
  2676. goto fail;
  2677. }
  2678. }
  2679. l3 = cachep->nodelists[node];
  2680. if (l3) {
  2681. struct array_cache *shared = l3->shared;
  2682. spin_lock_irq(&l3->list_lock);
  2683. if (shared)
  2684. free_block(cachep, shared->entry,
  2685. shared->avail, node);
  2686. l3->shared = new_shared;
  2687. if (!l3->alien) {
  2688. l3->alien = new_alien;
  2689. new_alien = NULL;
  2690. }
  2691. l3->free_limit = (1 + nr_cpus_node(node)) *
  2692. cachep->batchcount + cachep->num;
  2693. spin_unlock_irq(&l3->list_lock);
  2694. kfree(shared);
  2695. free_alien_cache(new_alien);
  2696. continue;
  2697. }
  2698. l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);
  2699. if (!l3) {
  2700. free_alien_cache(new_alien);
  2701. kfree(new_shared);
  2702. goto fail;
  2703. }
  2704. kmem_list3_init(l3);
  2705. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  2706. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  2707. l3->shared = new_shared;
  2708. l3->alien = new_alien;
  2709. l3->free_limit = (1 + nr_cpus_node(node)) *
  2710. cachep->batchcount + cachep->num;
  2711. cachep->nodelists[node] = l3;
  2712. }
  2713. return 0;
  2714. fail:
  2715. if (!cachep->next.next) {
  2716. node--;
  2717. while (node >= 0) {
  2718. if (cachep->nodelists[node]) {
  2719. l3 = cachep->nodelists[node];
  2720. kfree(l3->shared);
  2721. free_alien_cache(l3->alien);
  2722. kfree(l3);
  2723. cachep->nodelists[node] = NULL;
  2724. }
  2725. node--;
  2726. }
  2727. }
  2728. return -ENOMEM;
  2729. }
  2730. struct ccupdate_struct {
  2731. struct kmem_cache *cachep;
  2732. struct array_cache *new[0];
  2733. };
  2734. static void do_ccupdate_local(void *info)
  2735. {
  2736. struct ccupdate_struct *new = info;
  2737. struct array_cache *old;
  2738. check_irq_off();
  2739. old = cpu_cache_get(new->cachep);
  2740. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  2741. new->new[smp_processor_id()] = old;
  2742. }
  2743. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  2744. int batchcount, int shared, gfp_t gfp)
  2745. {
  2746. struct ccupdate_struct *new;
  2747. int i;
  2748. new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
  2749. gfp);
  2750. if (!new)
  2751. return -ENOMEM;
  2752. for_each_online_cpu(i) {
  2753. new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
  2754. batchcount, gfp);
  2755. if (!new->new[i]) {
  2756. for (i--; i >= 0; i--)
  2757. kfree(new->new[i]);
  2758. kfree(new);
  2759. return -ENOMEM;
  2760. }
  2761. }
  2762. new->cachep = cachep;
  2763. on_each_cpu(do_ccupdate_local, (void *)new, 1);
  2764. check_irq_on();
  2765. cachep->batchcount = batchcount;
  2766. cachep->limit = limit;
  2767. cachep->shared = shared;
  2768. for_each_online_cpu(i) {
  2769. struct array_cache *ccold = new->new[i];
  2770. if (!ccold)
  2771. continue;
  2772. spin_lock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
  2773. free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
  2774. spin_unlock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
  2775. kfree(ccold);
  2776. }
  2777. kfree(new);
  2778. return alloc_kmemlist(cachep, gfp);
  2779. }
  2780. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
  2781. {
  2782. int err;
  2783. int limit, shared;
  2784. if (cachep->buffer_size > 131072)
  2785. limit = 1;
  2786. else if (cachep->buffer_size > PAGE_SIZE)
  2787. limit = 8;
  2788. else if (cachep->buffer_size > 1024)
  2789. limit = 24;
  2790. else if (cachep->buffer_size > 256)
  2791. limit = 54;
  2792. else
  2793. limit = 120;
  2794. shared = 0;
  2795. if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
  2796. shared = 8;
  2797. #if DEBUG
  2798. if (limit > 32)
  2799. limit = 32;
  2800. #endif
  2801. err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
  2802. if (err)
  2803. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  2804. cachep->name, -err);
  2805. return err;
  2806. }
  2807. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  2808. struct array_cache *ac, int force, int node)
  2809. {
  2810. int tofree;
  2811. if (!ac || !ac->avail)
  2812. return;
  2813. if (ac->touched && !force) {
  2814. ac->touched = 0;
  2815. } else {
  2816. spin_lock_irq(&l3->list_lock);
  2817. if (ac->avail) {
  2818. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  2819. if (tofree > ac->avail)
  2820. tofree = (ac->avail + 1) / 2;
  2821. free_block(cachep, ac->entry, tofree, node);
  2822. ac->avail -= tofree;
  2823. memmove(ac->entry, &(ac->entry[tofree]),
  2824. sizeof(void *) * ac->avail);
  2825. }
  2826. spin_unlock_irq(&l3->list_lock);
  2827. }
  2828. }
  2829. static void cache_reap(struct work_struct *w)
  2830. {
  2831. struct kmem_cache *searchp;
  2832. struct kmem_list3 *l3;
  2833. int node = numa_mem_id();
  2834. struct delayed_work *work = to_delayed_work(w);
  2835. if (!mutex_trylock(&cache_chain_mutex))
  2836. goto out;
  2837. list_for_each_entry(searchp, &cache_chain, next) {
  2838. check_irq_on();
  2839. l3 = searchp->nodelists[node];
  2840. reap_alien(searchp, l3);
  2841. drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
  2842. if (time_after(l3->next_reap, jiffies))
  2843. goto next;
  2844. l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
  2845. drain_array(searchp, l3, l3->shared, 0, node);
  2846. if (l3->free_touched)
  2847. l3->free_touched = 0;
  2848. else {
  2849. int freed;
  2850. freed = drain_freelist(searchp, l3, (l3->free_limit +
  2851. 5 * searchp->num - 1) / (5 * searchp->num));
  2852. STATS_ADD_REAPED(searchp, freed);
  2853. }
  2854. next:
  2855. cond_resched();
  2856. }
  2857. check_irq_on();
  2858. mutex_unlock(&cache_chain_mutex);
  2859. next_reap_node();
  2860. out:
  2861. schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
  2862. }
  2863. #ifdef CONFIG_SLABINFO
  2864. static void print_slabinfo_header(struct seq_file *m)
  2865. {
  2866. #if STATS
  2867. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  2868. #else
  2869. seq_puts(m, "slabinfo - version: 2.1\n");
  2870. #endif
  2871. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  2872. "<objperslab> <pagesperslab>");
  2873. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  2874. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  2875. #if STATS
  2876. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  2877. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  2878. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  2879. #endif
  2880. seq_putc(m, '\n');
  2881. }
  2882. static void *s_start(struct seq_file *m, loff_t *pos)
  2883. {
  2884. loff_t n = *pos;
  2885. mutex_lock(&cache_chain_mutex);
  2886. if (!n)
  2887. print_slabinfo_header(m);
  2888. return seq_list_start(&cache_chain, *pos);
  2889. }
  2890. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  2891. {
  2892. return seq_list_next(p, &cache_chain, pos);
  2893. }
  2894. static void s_stop(struct seq_file *m, void *p)
  2895. {
  2896. mutex_unlock(&cache_chain_mutex);
  2897. }
  2898. static int s_show(struct seq_file *m, void *p)
  2899. {
  2900. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
  2901. struct slab *slabp;
  2902. unsigned long active_objs;
  2903. unsigned long num_objs;
  2904. unsigned long active_slabs = 0;
  2905. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  2906. const char *name;
  2907. char *error = NULL;
  2908. int node;
  2909. struct kmem_list3 *l3;
  2910. active_objs = 0;
  2911. num_slabs = 0;
  2912. for_each_online_node(node) {
  2913. l3 = cachep->nodelists[node];
  2914. if (!l3)
  2915. continue;
  2916. check_irq_on();
  2917. spin_lock_irq(&l3->list_lock);
  2918. list_for_each_entry(slabp, &l3->slabs_full, list) {
  2919. if (slabp->inuse != cachep->num && !error)
  2920. error = "slabs_full accounting error";
  2921. active_objs += cachep->num;
  2922. active_slabs++;
  2923. }
  2924. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  2925. if (slabp->inuse == cachep->num && !error)
  2926. error = "slabs_partial inuse accounting error";
  2927. if (!slabp->inuse && !error)
  2928. error = "slabs_partial/inuse accounting error";
  2929. active_objs += slabp->inuse;
  2930. active_slabs++;
  2931. }
  2932. list_for_each_entry(slabp, &l3->slabs_free, list) {
  2933. if (slabp->inuse && !error)
  2934. error = "slabs_free/inuse accounting error";
  2935. num_slabs++;
  2936. }
  2937. free_objects += l3->free_objects;
  2938. if (l3->shared)
  2939. shared_avail += l3->shared->avail;
  2940. spin_unlock_irq(&l3->list_lock);
  2941. }
  2942. num_slabs += active_slabs;
  2943. num_objs = num_slabs * cachep->num;
  2944. if (num_objs - active_objs != free_objects && !error)
  2945. error = "free_objects accounting error";
  2946. name = cachep->name;
  2947. if (error)
  2948. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  2949. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  2950. name, active_objs, num_objs, cachep->buffer_size,
  2951. cachep->num, (1 << cachep->gfporder));
  2952. seq_printf(m, " : tunables %4u %4u %4u",
  2953. cachep->limit, cachep->batchcount, cachep->shared);
  2954. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  2955. active_slabs, num_slabs, shared_avail);
  2956. #if STATS
  2957. {
  2958. unsigned long high = cachep->high_mark;
  2959. unsigned long allocs = cachep->num_allocations;
  2960. unsigned long grown = cachep->grown;
  2961. unsigned long reaped = cachep->reaped;
  2962. unsigned long errors = cachep->errors;
  2963. unsigned long max_freeable = cachep->max_freeable;
  2964. unsigned long node_allocs = cachep->node_allocs;
  2965. unsigned long node_frees = cachep->node_frees;
  2966. unsigned long overflows = cachep->node_overflow;
  2967. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
  2968. "%4lu %4lu %4lu %4lu %4lu",
  2969. allocs, high, grown,
  2970. reaped, errors, max_freeable, node_allocs,
  2971. node_frees, overflows);
  2972. }
  2973. {
  2974. unsigned long allochit = atomic_read(&cachep->allochit);
  2975. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  2976. unsigned long freehit = atomic_read(&cachep->freehit);
  2977. unsigned long freemiss = atomic_read(&cachep->freemiss);
  2978. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  2979. allochit, allocmiss, freehit, freemiss);
  2980. }
  2981. #endif
  2982. seq_putc(m, '\n');
  2983. return 0;
  2984. }
  2985. static const struct seq_operations slabinfo_op = {
  2986. .start = s_start,
  2987. .next = s_next,
  2988. .stop = s_stop,
  2989. .show = s_show,
  2990. };
  2991. #define MAX_SLABINFO_WRITE 128
  2992. static ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  2993. size_t count, loff_t *ppos)
  2994. {
  2995. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  2996. int limit, batchcount, shared, res;
  2997. struct kmem_cache *cachep;
  2998. if (count > MAX_SLABINFO_WRITE)
  2999. return -EINVAL;
  3000. if (copy_from_user(&kbuf, buffer, count))
  3001. return -EFAULT;
  3002. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3003. tmp = strchr(kbuf, ' ');
  3004. if (!tmp)
  3005. return -EINVAL;
  3006. *tmp = '\0';
  3007. tmp++;
  3008. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3009. return -EINVAL;
  3010. mutex_lock(&cache_chain_mutex);
  3011. res = -EINVAL;
  3012. list_for_each_entry(cachep, &cache_chain, next) {
  3013. if (!strcmp(cachep->name, kbuf)) {
  3014. if (limit < 1 || batchcount < 1 ||
  3015. batchcount > limit || shared < 0) {
  3016. res = 0;
  3017. } else {
  3018. res = do_tune_cpucache(cachep, limit,
  3019. batchcount, shared,
  3020. GFP_KERNEL);
  3021. }
  3022. break;
  3023. }
  3024. }
  3025. mutex_unlock(&cache_chain_mutex);
  3026. if (res >= 0)
  3027. res = count;
  3028. return res;
  3029. }
  3030. static int slabinfo_open(struct inode *inode, struct file *file)
  3031. {
  3032. return seq_open(file, &slabinfo_op);
  3033. }
  3034. static const struct file_operations proc_slabinfo_operations = {
  3035. .open = slabinfo_open,
  3036. .read = seq_read,
  3037. .write = slabinfo_write,
  3038. .llseek = seq_lseek,
  3039. .release = seq_release,
  3040. };
  3041. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3042. static void *leaks_start(struct seq_file *m, loff_t *pos)
  3043. {
  3044. mutex_lock(&cache_chain_mutex);
  3045. return seq_list_start(&cache_chain, *pos);
  3046. }
  3047. static inline int add_caller(unsigned long *n, unsigned long v)
  3048. {
  3049. unsigned long *p;
  3050. int l;
  3051. if (!v)
  3052. return 1;
  3053. l = n[1];
  3054. p = n + 2;
  3055. while (l) {
  3056. int i = l/2;
  3057. unsigned long *q = p + 2 * i;
  3058. if (*q == v) {
  3059. q[1]++;
  3060. return 1;
  3061. }
  3062. if (*q > v) {
  3063. l = i;
  3064. } else {
  3065. p = q + 2;
  3066. l -= i + 1;
  3067. }
  3068. }
  3069. if (++n[1] == n[0])
  3070. return 0;
  3071. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  3072. p[0] = v;
  3073. p[1] = 1;
  3074. return 1;
  3075. }
  3076. static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  3077. {
  3078. void *p;
  3079. int i;
  3080. if (n[0] == n[1])
  3081. return;
  3082. for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
  3083. if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  3084. continue;
  3085. if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  3086. return;
  3087. }
  3088. }
  3089. static void show_symbol(struct seq_file *m, unsigned long address)
  3090. {
  3091. #ifdef CONFIG_KALLSYMS
  3092. unsigned long offset, size;
  3093. char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
  3094. if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
  3095. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  3096. if (modname[0])
  3097. seq_printf(m, " [%s]", modname);
  3098. return;
  3099. }
  3100. #endif
  3101. seq_printf(m, "%p", (void *)address);
  3102. }
  3103. static int leaks_show(struct seq_file *m, void *p)
  3104. {
  3105. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
  3106. struct slab *slabp;
  3107. struct kmem_list3 *l3;
  3108. const char *name;
  3109. unsigned long *n = m->private;
  3110. int node;
  3111. int i;
  3112. if (!(cachep->flags & SLAB_STORE_USER))
  3113. return 0;
  3114. if (!(cachep->flags & SLAB_RED_ZONE))
  3115. return 0;
  3116. n[1] = 0;
  3117. for_each_online_node(node) {
  3118. l3 = cachep->nodelists[node];
  3119. if (!l3)
  3120. continue;
  3121. check_irq_on();
  3122. spin_lock_irq(&l3->list_lock);
  3123. list_for_each_entry(slabp, &l3->slabs_full, list)
  3124. handle_slab(n, cachep, slabp);
  3125. list_for_each_entry(slabp, &l3->slabs_partial, list)
  3126. handle_slab(n, cachep, slabp);
  3127. spin_unlock_irq(&l3->list_lock);
  3128. }
  3129. name = cachep->name;
  3130. if (n[0] == n[1]) {
  3131. mutex_unlock(&cache_chain_mutex);
  3132. m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  3133. if (!m->private) {
  3134. m->private = n;
  3135. mutex_lock(&cache_chain_mutex);
  3136. return -ENOMEM;
  3137. }
  3138. *(unsigned long *)m->private = n[0] * 2;
  3139. kfree(n);
  3140. mutex_lock(&cache_chain_mutex);
  3141. m->count = m->size;
  3142. return 0;
  3143. }
  3144. for (i = 0; i < n[1]; i++) {
  3145. seq_printf(m, "%s: %lu ", name, n[2*i+3]);
  3146. show_symbol(m, n[2*i+2]);
  3147. seq_putc(m, '\n');
  3148. }
  3149. return 0;
  3150. }
  3151. static const struct seq_operations slabstats_op = {
  3152. .start = leaks_start,
  3153. .next = s_next,
  3154. .stop = s_stop,
  3155. .show = leaks_show,
  3156. };
  3157. static int slabstats_open(struct inode *inode, struct file *file)
  3158. {
  3159. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  3160. int ret = -ENOMEM;
  3161. if (n) {
  3162. ret = seq_open(file, &slabstats_op);
  3163. if (!ret) {
  3164. struct seq_file *m = file->private_data;
  3165. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  3166. m->private = n;
  3167. n = NULL;
  3168. }
  3169. kfree(n);
  3170. }
  3171. return ret;
  3172. }
  3173. static const struct file_operations proc_slabstats_operations = {
  3174. .open = slabstats_open,
  3175. .read = seq_read,
  3176. .llseek = seq_lseek,
  3177. .release = seq_release_private,
  3178. };
  3179. #endif
  3180. static int __init slab_proc_init(void)
  3181. {
  3182. proc_create("slabinfo",S_IWUSR|S_IRUSR,NULL,&proc_slabinfo_operations);
  3183. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3184. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  3185. #endif
  3186. return 0;
  3187. }
  3188. module_init(slab_proc_init);
  3189. #endif
  3190. size_t ksize(const void *objp)
  3191. {
  3192. BUG_ON(!objp);
  3193. if (unlikely(objp == ZERO_SIZE_PTR))
  3194. return 0;
  3195. return obj_size(virt_to_cache(objp));
  3196. }
  3197. EXPORT_SYMBOL(ksize);