aboutsummaryrefslogtreecommitdiffstats
path: root/mm/slub.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/slub.c')
-rw-r--r--mm/slub.c308
1 files changed, 77 insertions, 231 deletions
diff --git a/mm/slub.c b/mm/slub.c
index cab5288736c8..3525a4ec9794 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -218,10 +218,10 @@ static inline void sysfs_slab_remove(struct kmem_cache *s)
218 218
219#endif 219#endif
220 220
221static inline void stat(struct kmem_cache_cpu *c, enum stat_item si) 221static inline void stat(struct kmem_cache *s, enum stat_item si)
222{ 222{
223#ifdef CONFIG_SLUB_STATS 223#ifdef CONFIG_SLUB_STATS
224 c->stat[si]++; 224 __this_cpu_inc(s->cpu_slab->stat[si]);
225#endif 225#endif
226} 226}
227 227
@@ -243,15 +243,6 @@ static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
243#endif 243#endif
244} 244}
245 245
246static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
247{
248#ifdef CONFIG_SMP
249 return s->cpu_slab[cpu];
250#else
251 return &s->cpu_slab;
252#endif
253}
254
255/* Verify that a pointer has an address that is valid within a slab page */ 246/* Verify that a pointer has an address that is valid within a slab page */
256static inline int check_valid_pointer(struct kmem_cache *s, 247static inline int check_valid_pointer(struct kmem_cache *s,
257 struct page *page, const void *object) 248 struct page *page, const void *object)
@@ -270,13 +261,6 @@ static inline int check_valid_pointer(struct kmem_cache *s,
270 return 1; 261 return 1;
271} 262}
272 263
273/*
274 * Slow version of get and set free pointer.
275 *
276 * This version requires touching the cache lines of kmem_cache which
277 * we avoid to do in the fast alloc free paths. There we obtain the offset
278 * from the page struct.
279 */
280static inline void *get_freepointer(struct kmem_cache *s, void *object) 264static inline void *get_freepointer(struct kmem_cache *s, void *object)
281{ 265{
282 return *(void **)(object + s->offset); 266 return *(void **)(object + s->offset);
@@ -1128,7 +1112,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
1128 if (!page) 1112 if (!page)
1129 return NULL; 1113 return NULL;
1130 1114
1131 stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK); 1115 stat(s, ORDER_FALLBACK);
1132 } 1116 }
1133 1117
1134 if (kmemcheck_enabled 1118 if (kmemcheck_enabled
@@ -1426,23 +1410,22 @@ static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
1426static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail) 1410static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1427{ 1411{
1428 struct kmem_cache_node *n = get_node(s, page_to_nid(page)); 1412 struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1429 struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
1430 1413
1431 __ClearPageSlubFrozen(page); 1414 __ClearPageSlubFrozen(page);
1432 if (page->inuse) { 1415 if (page->inuse) {
1433 1416
1434 if (page->freelist) { 1417 if (page->freelist) {
1435 add_partial(n, page, tail); 1418 add_partial(n, page, tail);
1436 stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD); 1419 stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1437 } else { 1420 } else {
1438 stat(c, DEACTIVATE_FULL); 1421 stat(s, DEACTIVATE_FULL);
1439 if (SLABDEBUG && PageSlubDebug(page) && 1422 if (SLABDEBUG && PageSlubDebug(page) &&
1440 (s->flags & SLAB_STORE_USER)) 1423 (s->flags & SLAB_STORE_USER))
1441 add_full(n, page); 1424 add_full(n, page);
1442 } 1425 }
1443 slab_unlock(page); 1426 slab_unlock(page);
1444 } else { 1427 } else {
1445 stat(c, DEACTIVATE_EMPTY); 1428 stat(s, DEACTIVATE_EMPTY);
1446 if (n->nr_partial < s->min_partial) { 1429 if (n->nr_partial < s->min_partial) {
1447 /* 1430 /*
1448 * Adding an empty slab to the partial slabs in order 1431 * Adding an empty slab to the partial slabs in order
@@ -1458,7 +1441,7 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1458 slab_unlock(page); 1441 slab_unlock(page);
1459 } else { 1442 } else {
1460 slab_unlock(page); 1443 slab_unlock(page);
1461 stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB); 1444 stat(s, FREE_SLAB);
1462 discard_slab(s, page); 1445 discard_slab(s, page);
1463 } 1446 }
1464 } 1447 }
@@ -1473,7 +1456,7 @@ static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1473 int tail = 1; 1456 int tail = 1;
1474 1457
1475 if (page->freelist) 1458 if (page->freelist)
1476 stat(c, DEACTIVATE_REMOTE_FREES); 1459 stat(s, DEACTIVATE_REMOTE_FREES);
1477 /* 1460 /*
1478 * Merge cpu freelist into slab freelist. Typically we get here 1461 * Merge cpu freelist into slab freelist. Typically we get here
1479 * because both freelists are empty. So this is unlikely 1462 * because both freelists are empty. So this is unlikely
@@ -1486,10 +1469,10 @@ static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1486 1469
1487 /* Retrieve object from cpu_freelist */ 1470 /* Retrieve object from cpu_freelist */
1488 object = c->freelist; 1471 object = c->freelist;
1489 c->freelist = c->freelist[c->offset]; 1472 c->freelist = get_freepointer(s, c->freelist);
1490 1473
1491 /* And put onto the regular freelist */ 1474 /* And put onto the regular freelist */
1492 object[c->offset] = page->freelist; 1475 set_freepointer(s, object, page->freelist);
1493 page->freelist = object; 1476 page->freelist = object;
1494 page->inuse--; 1477 page->inuse--;
1495 } 1478 }
@@ -1499,7 +1482,7 @@ static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1499 1482
1500static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c) 1483static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1501{ 1484{
1502 stat(c, CPUSLAB_FLUSH); 1485 stat(s, CPUSLAB_FLUSH);
1503 slab_lock(c->page); 1486 slab_lock(c->page);
1504 deactivate_slab(s, c); 1487 deactivate_slab(s, c);
1505} 1488}
@@ -1511,7 +1494,7 @@ static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1511 */ 1494 */
1512static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu) 1495static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
1513{ 1496{
1514 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu); 1497 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
1515 1498
1516 if (likely(c && c->page)) 1499 if (likely(c && c->page))
1517 flush_slab(s, c); 1500 flush_slab(s, c);
@@ -1639,7 +1622,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
1639 if (unlikely(!node_match(c, node))) 1622 if (unlikely(!node_match(c, node)))
1640 goto another_slab; 1623 goto another_slab;
1641 1624
1642 stat(c, ALLOC_REFILL); 1625 stat(s, ALLOC_REFILL);
1643 1626
1644load_freelist: 1627load_freelist:
1645 object = c->page->freelist; 1628 object = c->page->freelist;
@@ -1648,13 +1631,13 @@ load_freelist:
1648 if (unlikely(SLABDEBUG && PageSlubDebug(c->page))) 1631 if (unlikely(SLABDEBUG && PageSlubDebug(c->page)))
1649 goto debug; 1632 goto debug;
1650 1633
1651 c->freelist = object[c->offset]; 1634 c->freelist = get_freepointer(s, object);
1652 c->page->inuse = c->page->objects; 1635 c->page->inuse = c->page->objects;
1653 c->page->freelist = NULL; 1636 c->page->freelist = NULL;
1654 c->node = page_to_nid(c->page); 1637 c->node = page_to_nid(c->page);
1655unlock_out: 1638unlock_out:
1656 slab_unlock(c->page); 1639 slab_unlock(c->page);
1657 stat(c, ALLOC_SLOWPATH); 1640 stat(s, ALLOC_SLOWPATH);
1658 return object; 1641 return object;
1659 1642
1660another_slab: 1643another_slab:
@@ -1664,7 +1647,7 @@ new_slab:
1664 new = get_partial(s, gfpflags, node); 1647 new = get_partial(s, gfpflags, node);
1665 if (new) { 1648 if (new) {
1666 c->page = new; 1649 c->page = new;
1667 stat(c, ALLOC_FROM_PARTIAL); 1650 stat(s, ALLOC_FROM_PARTIAL);
1668 goto load_freelist; 1651 goto load_freelist;
1669 } 1652 }
1670 1653
@@ -1677,8 +1660,8 @@ new_slab:
1677 local_irq_disable(); 1660 local_irq_disable();
1678 1661
1679 if (new) { 1662 if (new) {
1680 c = get_cpu_slab(s, smp_processor_id()); 1663 c = __this_cpu_ptr(s->cpu_slab);
1681 stat(c, ALLOC_SLAB); 1664 stat(s, ALLOC_SLAB);
1682 if (c->page) 1665 if (c->page)
1683 flush_slab(s, c); 1666 flush_slab(s, c);
1684 slab_lock(new); 1667 slab_lock(new);
@@ -1694,7 +1677,7 @@ debug:
1694 goto another_slab; 1677 goto another_slab;
1695 1678
1696 c->page->inuse++; 1679 c->page->inuse++;
1697 c->page->freelist = object[c->offset]; 1680 c->page->freelist = get_freepointer(s, object);
1698 c->node = -1; 1681 c->node = -1;
1699 goto unlock_out; 1682 goto unlock_out;
1700} 1683}
@@ -1715,7 +1698,6 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
1715 void **object; 1698 void **object;
1716 struct kmem_cache_cpu *c; 1699 struct kmem_cache_cpu *c;
1717 unsigned long flags; 1700 unsigned long flags;
1718 unsigned int objsize;
1719 1701
1720 gfpflags &= gfp_allowed_mask; 1702 gfpflags &= gfp_allowed_mask;
1721 1703
@@ -1726,24 +1708,23 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
1726 return NULL; 1708 return NULL;
1727 1709
1728 local_irq_save(flags); 1710 local_irq_save(flags);
1729 c = get_cpu_slab(s, smp_processor_id()); 1711 c = __this_cpu_ptr(s->cpu_slab);
1730 objsize = c->objsize; 1712 object = c->freelist;
1731 if (unlikely(!c->freelist || !node_match(c, node))) 1713 if (unlikely(!object || !node_match(c, node)))
1732 1714
1733 object = __slab_alloc(s, gfpflags, node, addr, c); 1715 object = __slab_alloc(s, gfpflags, node, addr, c);
1734 1716
1735 else { 1717 else {
1736 object = c->freelist; 1718 c->freelist = get_freepointer(s, object);
1737 c->freelist = object[c->offset]; 1719 stat(s, ALLOC_FASTPATH);
1738 stat(c, ALLOC_FASTPATH);
1739 } 1720 }
1740 local_irq_restore(flags); 1721 local_irq_restore(flags);
1741 1722
1742 if (unlikely(gfpflags & __GFP_ZERO) && object) 1723 if (unlikely(gfpflags & __GFP_ZERO) && object)
1743 memset(object, 0, objsize); 1724 memset(object, 0, s->objsize);
1744 1725
1745 kmemcheck_slab_alloc(s, gfpflags, object, c->objsize); 1726 kmemcheck_slab_alloc(s, gfpflags, object, s->objsize);
1746 kmemleak_alloc_recursive(object, objsize, 1, s->flags, gfpflags); 1727 kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, gfpflags);
1747 1728
1748 return object; 1729 return object;
1749} 1730}
@@ -1798,26 +1779,25 @@ EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
1798 * handling required then we can return immediately. 1779 * handling required then we can return immediately.
1799 */ 1780 */
1800static void __slab_free(struct kmem_cache *s, struct page *page, 1781static void __slab_free(struct kmem_cache *s, struct page *page,
1801 void *x, unsigned long addr, unsigned int offset) 1782 void *x, unsigned long addr)
1802{ 1783{
1803 void *prior; 1784 void *prior;
1804 void **object = (void *)x; 1785 void **object = (void *)x;
1805 struct kmem_cache_cpu *c;
1806 1786
1807 c = get_cpu_slab(s, raw_smp_processor_id()); 1787 stat(s, FREE_SLOWPATH);
1808 stat(c, FREE_SLOWPATH);
1809 slab_lock(page); 1788 slab_lock(page);
1810 1789
1811 if (unlikely(SLABDEBUG && PageSlubDebug(page))) 1790 if (unlikely(SLABDEBUG && PageSlubDebug(page)))
1812 goto debug; 1791 goto debug;
1813 1792
1814checks_ok: 1793checks_ok:
1815 prior = object[offset] = page->freelist; 1794 prior = page->freelist;
1795 set_freepointer(s, object, prior);
1816 page->freelist = object; 1796 page->freelist = object;
1817 page->inuse--; 1797 page->inuse--;
1818 1798
1819 if (unlikely(PageSlubFrozen(page))) { 1799 if (unlikely(PageSlubFrozen(page))) {
1820 stat(c, FREE_FROZEN); 1800 stat(s, FREE_FROZEN);
1821 goto out_unlock; 1801 goto out_unlock;
1822 } 1802 }
1823 1803
@@ -1830,7 +1810,7 @@ checks_ok:
1830 */ 1810 */
1831 if (unlikely(!prior)) { 1811 if (unlikely(!prior)) {
1832 add_partial(get_node(s, page_to_nid(page)), page, 1); 1812 add_partial(get_node(s, page_to_nid(page)), page, 1);
1833 stat(c, FREE_ADD_PARTIAL); 1813 stat(s, FREE_ADD_PARTIAL);
1834 } 1814 }
1835 1815
1836out_unlock: 1816out_unlock:
@@ -1843,10 +1823,10 @@ slab_empty:
1843 * Slab still on the partial list. 1823 * Slab still on the partial list.
1844 */ 1824 */
1845 remove_partial(s, page); 1825 remove_partial(s, page);
1846 stat(c, FREE_REMOVE_PARTIAL); 1826 stat(s, FREE_REMOVE_PARTIAL);
1847 } 1827 }
1848 slab_unlock(page); 1828 slab_unlock(page);
1849 stat(c, FREE_SLAB); 1829 stat(s, FREE_SLAB);
1850 discard_slab(s, page); 1830 discard_slab(s, page);
1851 return; 1831 return;
1852 1832
@@ -1876,17 +1856,17 @@ static __always_inline void slab_free(struct kmem_cache *s,
1876 1856
1877 kmemleak_free_recursive(x, s->flags); 1857 kmemleak_free_recursive(x, s->flags);
1878 local_irq_save(flags); 1858 local_irq_save(flags);
1879 c = get_cpu_slab(s, smp_processor_id()); 1859 c = __this_cpu_ptr(s->cpu_slab);
1880 kmemcheck_slab_free(s, object, c->objsize); 1860 kmemcheck_slab_free(s, object, s->objsize);
1881 debug_check_no_locks_freed(object, c->objsize); 1861 debug_check_no_locks_freed(object, s->objsize);
1882 if (!(s->flags & SLAB_DEBUG_OBJECTS)) 1862 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1883 debug_check_no_obj_freed(object, c->objsize); 1863 debug_check_no_obj_freed(object, s->objsize);
1884 if (likely(page == c->page && c->node >= 0)) { 1864 if (likely(page == c->page && c->node >= 0)) {
1885 object[c->offset] = c->freelist; 1865 set_freepointer(s, object, c->freelist);
1886 c->freelist = object; 1866 c->freelist = object;
1887 stat(c, FREE_FASTPATH); 1867 stat(s, FREE_FASTPATH);
1888 } else 1868 } else
1889 __slab_free(s, page, x, addr, c->offset); 1869 __slab_free(s, page, x, addr);
1890 1870
1891 local_irq_restore(flags); 1871 local_irq_restore(flags);
1892} 1872}
@@ -2073,19 +2053,6 @@ static unsigned long calculate_alignment(unsigned long flags,
2073 return ALIGN(align, sizeof(void *)); 2053 return ALIGN(align, sizeof(void *));
2074} 2054}
2075 2055
2076static void init_kmem_cache_cpu(struct kmem_cache *s,
2077 struct kmem_cache_cpu *c)
2078{
2079 c->page = NULL;
2080 c->freelist = NULL;
2081 c->node = 0;
2082 c->offset = s->offset / sizeof(void *);
2083 c->objsize = s->objsize;
2084#ifdef CONFIG_SLUB_STATS
2085 memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
2086#endif
2087}
2088
2089static void 2056static void
2090init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s) 2057init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
2091{ 2058{
@@ -2099,130 +2066,24 @@ init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
2099#endif 2066#endif
2100} 2067}
2101 2068
2102#ifdef CONFIG_SMP 2069static DEFINE_PER_CPU(struct kmem_cache_cpu, kmalloc_percpu[KMALLOC_CACHES]);
2103/*
2104 * Per cpu array for per cpu structures.
2105 *
2106 * The per cpu array places all kmem_cache_cpu structures from one processor
2107 * close together meaning that it becomes possible that multiple per cpu
2108 * structures are contained in one cacheline. This may be particularly
2109 * beneficial for the kmalloc caches.
2110 *
2111 * A desktop system typically has around 60-80 slabs. With 100 here we are
2112 * likely able to get per cpu structures for all caches from the array defined
2113 * here. We must be able to cover all kmalloc caches during bootstrap.
2114 *
2115 * If the per cpu array is exhausted then fall back to kmalloc
2116 * of individual cachelines. No sharing is possible then.
2117 */
2118#define NR_KMEM_CACHE_CPU 100
2119
2120static DEFINE_PER_CPU(struct kmem_cache_cpu [NR_KMEM_CACHE_CPU],
2121 kmem_cache_cpu);
2122
2123static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
2124static DECLARE_BITMAP(kmem_cach_cpu_free_init_once, CONFIG_NR_CPUS);
2125
2126static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
2127 int cpu, gfp_t flags)
2128{
2129 struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
2130
2131 if (c)
2132 per_cpu(kmem_cache_cpu_free, cpu) =
2133 (void *)c->freelist;
2134 else {
2135 /* Table overflow: So allocate ourselves */
2136 c = kmalloc_node(
2137 ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
2138 flags, cpu_to_node(cpu));
2139 if (!c)
2140 return NULL;
2141 }
2142
2143 init_kmem_cache_cpu(s, c);
2144 return c;
2145}
2146
2147static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
2148{
2149 if (c < per_cpu(kmem_cache_cpu, cpu) ||
2150 c >= per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
2151 kfree(c);
2152 return;
2153 }
2154 c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
2155 per_cpu(kmem_cache_cpu_free, cpu) = c;
2156}
2157
2158static void free_kmem_cache_cpus(struct kmem_cache *s)
2159{
2160 int cpu;
2161
2162 for_each_online_cpu(cpu) {
2163 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2164
2165 if (c) {
2166 s->cpu_slab[cpu] = NULL;
2167 free_kmem_cache_cpu(c, cpu);
2168 }
2169 }
2170}
2171
2172static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2173{
2174 int cpu;
2175
2176 for_each_online_cpu(cpu) {
2177 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
2178 2070
2179 if (c) 2071static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2180 continue;
2181
2182 c = alloc_kmem_cache_cpu(s, cpu, flags);
2183 if (!c) {
2184 free_kmem_cache_cpus(s);
2185 return 0;
2186 }
2187 s->cpu_slab[cpu] = c;
2188 }
2189 return 1;
2190}
2191
2192/*
2193 * Initialize the per cpu array.
2194 */
2195static void init_alloc_cpu_cpu(int cpu)
2196{
2197 int i;
2198
2199 if (cpumask_test_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once)))
2200 return;
2201
2202 for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
2203 free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
2204
2205 cpumask_set_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once));
2206}
2207
2208static void __init init_alloc_cpu(void)
2209{ 2072{
2210 int cpu; 2073 if (s < kmalloc_caches + KMALLOC_CACHES && s >= kmalloc_caches)
2211 2074 /*
2212 for_each_online_cpu(cpu) 2075 * Boot time creation of the kmalloc array. Use static per cpu data
2213 init_alloc_cpu_cpu(cpu); 2076 * since the per cpu allocator is not available yet.
2214 } 2077 */
2078 s->cpu_slab = per_cpu_var(kmalloc_percpu) + (s - kmalloc_caches);
2079 else
2080 s->cpu_slab = alloc_percpu(struct kmem_cache_cpu);
2215 2081
2216#else 2082 if (!s->cpu_slab)
2217static inline void free_kmem_cache_cpus(struct kmem_cache *s) {} 2083 return 0;
2218static inline void init_alloc_cpu(void) {}
2219 2084
2220static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
2221{
2222 init_kmem_cache_cpu(s, &s->cpu_slab);
2223 return 1; 2085 return 1;
2224} 2086}
2225#endif
2226 2087
2227#ifdef CONFIG_NUMA 2088#ifdef CONFIG_NUMA
2228/* 2089/*
@@ -2291,7 +2152,8 @@ static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2291 int node; 2152 int node;
2292 int local_node; 2153 int local_node;
2293 2154
2294 if (slab_state >= UP) 2155 if (slab_state >= UP && (s < kmalloc_caches ||
2156 s > kmalloc_caches + KMALLOC_CACHES))
2295 local_node = page_to_nid(virt_to_page(s)); 2157 local_node = page_to_nid(virt_to_page(s));
2296 else 2158 else
2297 local_node = 0; 2159 local_node = 0;
@@ -2506,6 +2368,7 @@ static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2506 2368
2507 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA)) 2369 if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
2508 return 1; 2370 return 1;
2371
2509 free_kmem_cache_nodes(s); 2372 free_kmem_cache_nodes(s);
2510error: 2373error:
2511 if (flags & SLAB_PANIC) 2374 if (flags & SLAB_PANIC)
@@ -2613,9 +2476,8 @@ static inline int kmem_cache_close(struct kmem_cache *s)
2613 int node; 2476 int node;
2614 2477
2615 flush_all(s); 2478 flush_all(s);
2616 2479 free_percpu(s->cpu_slab);
2617 /* Attempt to free all objects */ 2480 /* Attempt to free all objects */
2618 free_kmem_cache_cpus(s);
2619 for_each_node_state(node, N_NORMAL_MEMORY) { 2481 for_each_node_state(node, N_NORMAL_MEMORY) {
2620 struct kmem_cache_node *n = get_node(s, node); 2482 struct kmem_cache_node *n = get_node(s, node);
2621 2483
@@ -2655,7 +2517,7 @@ EXPORT_SYMBOL(kmem_cache_destroy);
2655 * Kmalloc subsystem 2517 * Kmalloc subsystem
2656 *******************************************************************/ 2518 *******************************************************************/
2657 2519
2658struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned; 2520struct kmem_cache kmalloc_caches[KMALLOC_CACHES] __cacheline_aligned;
2659EXPORT_SYMBOL(kmalloc_caches); 2521EXPORT_SYMBOL(kmalloc_caches);
2660 2522
2661static int __init setup_slub_min_order(char *str) 2523static int __init setup_slub_min_order(char *str)
@@ -2745,6 +2607,7 @@ static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2745 char *text; 2607 char *text;
2746 size_t realsize; 2608 size_t realsize;
2747 unsigned long slabflags; 2609 unsigned long slabflags;
2610 int i;
2748 2611
2749 s = kmalloc_caches_dma[index]; 2612 s = kmalloc_caches_dma[index];
2750 if (s) 2613 if (s)
@@ -2764,7 +2627,14 @@ static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2764 realsize = kmalloc_caches[index].objsize; 2627 realsize = kmalloc_caches[index].objsize;
2765 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", 2628 text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
2766 (unsigned int)realsize); 2629 (unsigned int)realsize);
2767 s = kmalloc(kmem_size, flags & ~SLUB_DMA); 2630
2631 s = NULL;
2632 for (i = 0; i < KMALLOC_CACHES; i++)
2633 if (!kmalloc_caches[i].size)
2634 break;
2635
2636 BUG_ON(i >= KMALLOC_CACHES);
2637 s = kmalloc_caches + i;
2768 2638
2769 /* 2639 /*
2770 * Must defer sysfs creation to a workqueue because we don't know 2640 * Must defer sysfs creation to a workqueue because we don't know
@@ -2776,9 +2646,9 @@ static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
2776 if (slab_state >= SYSFS) 2646 if (slab_state >= SYSFS)
2777 slabflags |= __SYSFS_ADD_DEFERRED; 2647 slabflags |= __SYSFS_ADD_DEFERRED;
2778 2648
2779 if (!s || !text || !kmem_cache_open(s, flags, text, 2649 if (!text || !kmem_cache_open(s, flags, text,
2780 realsize, ARCH_KMALLOC_MINALIGN, slabflags, NULL)) { 2650 realsize, ARCH_KMALLOC_MINALIGN, slabflags, NULL)) {
2781 kfree(s); 2651 s->size = 0;
2782 kfree(text); 2652 kfree(text);
2783 goto unlock_out; 2653 goto unlock_out;
2784 } 2654 }
@@ -3180,8 +3050,6 @@ void __init kmem_cache_init(void)
3180 int i; 3050 int i;
3181 int caches = 0; 3051 int caches = 0;
3182 3052
3183 init_alloc_cpu();
3184
3185#ifdef CONFIG_NUMA 3053#ifdef CONFIG_NUMA
3186 /* 3054 /*
3187 * Must first have the slab cache available for the allocations of the 3055 * Must first have the slab cache available for the allocations of the
@@ -3265,8 +3133,10 @@ void __init kmem_cache_init(void)
3265 3133
3266#ifdef CONFIG_SMP 3134#ifdef CONFIG_SMP
3267 register_cpu_notifier(&slab_notifier); 3135 register_cpu_notifier(&slab_notifier);
3268 kmem_size = offsetof(struct kmem_cache, cpu_slab) + 3136#endif
3269 nr_cpu_ids * sizeof(struct kmem_cache_cpu *); 3137#ifdef CONFIG_NUMA
3138 kmem_size = offsetof(struct kmem_cache, node) +
3139 nr_node_ids * sizeof(struct kmem_cache_node *);
3270#else 3140#else
3271 kmem_size = sizeof(struct kmem_cache); 3141 kmem_size = sizeof(struct kmem_cache);
3272#endif 3142#endif
@@ -3355,22 +3225,12 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
3355 down_write(&slub_lock); 3225 down_write(&slub_lock);
3356 s = find_mergeable(size, align, flags, name, ctor); 3226 s = find_mergeable(size, align, flags, name, ctor);
3357 if (s) { 3227 if (s) {
3358 int cpu;
3359
3360 s->refcount++; 3228 s->refcount++;
3361 /* 3229 /*
3362 * Adjust the object sizes so that we clear 3230 * Adjust the object sizes so that we clear
3363 * the complete object on kzalloc. 3231 * the complete object on kzalloc.
3364 */ 3232 */
3365 s->objsize = max(s->objsize, (int)size); 3233 s->objsize = max(s->objsize, (int)size);
3366
3367 /*
3368 * And then we need to update the object size in the
3369 * per cpu structures
3370 */
3371 for_each_online_cpu(cpu)
3372 get_cpu_slab(s, cpu)->objsize = s->objsize;
3373
3374 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *))); 3234 s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
3375 up_write(&slub_lock); 3235 up_write(&slub_lock);
3376 3236
@@ -3424,29 +3284,15 @@ static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
3424 unsigned long flags; 3284 unsigned long flags;
3425 3285
3426 switch (action) { 3286 switch (action) {
3427 case CPU_UP_PREPARE:
3428 case CPU_UP_PREPARE_FROZEN:
3429 init_alloc_cpu_cpu(cpu);
3430 down_read(&slub_lock);
3431 list_for_each_entry(s, &slab_caches, list)
3432 s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
3433 GFP_KERNEL);
3434 up_read(&slub_lock);
3435 break;
3436
3437 case CPU_UP_CANCELED: 3287 case CPU_UP_CANCELED:
3438 case CPU_UP_CANCELED_FROZEN: 3288 case CPU_UP_CANCELED_FROZEN:
3439 case CPU_DEAD: 3289 case CPU_DEAD:
3440 case CPU_DEAD_FROZEN: 3290 case CPU_DEAD_FROZEN:
3441 down_read(&slub_lock); 3291 down_read(&slub_lock);
3442 list_for_each_entry(s, &slab_caches, list) { 3292 list_for_each_entry(s, &slab_caches, list) {
3443 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
3444
3445 local_irq_save(flags); 3293 local_irq_save(flags);
3446 __flush_cpu_slab(s, cpu); 3294 __flush_cpu_slab(s, cpu);
3447 local_irq_restore(flags); 3295 local_irq_restore(flags);
3448 free_kmem_cache_cpu(c, cpu);
3449 s->cpu_slab[cpu] = NULL;
3450 } 3296 }
3451 up_read(&slub_lock); 3297 up_read(&slub_lock);
3452 break; 3298 break;
@@ -3932,7 +3778,7 @@ static ssize_t show_slab_objects(struct kmem_cache *s,
3932 int cpu; 3778 int cpu;
3933 3779
3934 for_each_possible_cpu(cpu) { 3780 for_each_possible_cpu(cpu) {
3935 struct kmem_cache_cpu *c = get_cpu_slab(s, cpu); 3781 struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
3936 3782
3937 if (!c || c->node < 0) 3783 if (!c || c->node < 0)
3938 continue; 3784 continue;
@@ -4374,7 +4220,7 @@ static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
4374 return -ENOMEM; 4220 return -ENOMEM;
4375 4221
4376 for_each_online_cpu(cpu) { 4222 for_each_online_cpu(cpu) {
4377 unsigned x = get_cpu_slab(s, cpu)->stat[si]; 4223 unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
4378 4224
4379 data[cpu] = x; 4225 data[cpu] = x;
4380 sum += x; 4226 sum += x;
@@ -4397,7 +4243,7 @@ static void clear_stat(struct kmem_cache *s, enum stat_item si)
4397 int cpu; 4243 int cpu;
4398 4244
4399 for_each_online_cpu(cpu) 4245 for_each_online_cpu(cpu)
4400 get_cpu_slab(s, cpu)->stat[si] = 0; 4246 per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
4401} 4247}
4402 4248
4403#define STAT_ATTR(si, text) \ 4249#define STAT_ATTR(si, text) \