···37373838struct kmem_cache_cpu {3939 void **freelist; /* Pointer to next available object */4040-#ifdef CONFIG_CMPXCHG_LOCAL4140 unsigned long tid; /* Globally unique transaction id */4242-#endif4341 struct page *page; /* The slab from which we are allocating */4442 int node; /* The node of the page (or -1 for debug) */4543#ifdef CONFIG_SLUB_STATS···177179 if (size <= 4 * 1024) return 12;178180/*179181 * The following is only needed to support architectures with a larger page180180- * size than 4k.182182+ * size than 4k. We need to support 2 * PAGE_SIZE here. So for a 64k page183183+ * size we would have to go up to 128k.181184 */182185 if (size <= 8 * 1024) return 13;183186 if (size <= 16 * 1024) return 14;···189190 if (size <= 512 * 1024) return 19;190191 if (size <= 1024 * 1024) return 20;191192 if (size <= 2 * 1024 * 1024) return 21;192192- return -1;193193+ BUG();194194+ return -1; /* Will never be reached */193195194196/*195197 * What we really wanted to do and cannot do because of compiler issues is:
+65-100
mm/slub.c
···261261 return *(void **)(object + s->offset);262262}263263264264+static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)265265+{266266+ void *p;267267+268268+#ifdef CONFIG_DEBUG_PAGEALLOC269269+ probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));270270+#else271271+ p = get_freepointer(s, object);272272+#endif273273+ return p;274274+}275275+264276static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)265277{266278 *(void **)(object + s->offset) = fp;···282270#define for_each_object(__p, __s, __addr, __objects) \283271 for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\284272 __p += (__s)->size)285285-286286-/* Scan freelist */287287-#define for_each_free_object(__p, __s, __free) \288288- for (__p = (__free); __p; __p = get_freepointer((__s), __p))289273290274/* Determine object index from a given position */291275static inline int slab_index(void *p, struct kmem_cache *s, void *addr)···339331}340332341333#ifdef CONFIG_SLUB_DEBUG334334+/*335335+ * Determine a map of object in use on a page.336336+ *337337+ * Slab lock or node listlock must be held to guarantee that the page does338338+ * not vanish from under us.339339+ */340340+static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)341341+{342342+ void *p;343343+ void *addr = page_address(page);344344+345345+ for (p = page->freelist; p; p = get_freepointer(s, p))346346+ set_bit(slab_index(p, s, addr), map);347347+}348348+342349/*343350 * Debug settings:344351 */···15101487 int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;1511148815121489 page = get_partial_node(get_node(s, searchnode));15131513- if (page || node != -1)14901490+ if (page || node != NUMA_NO_NODE)15141491 return page;1515149215161493 return get_any_partial(s, flags);···15631540 }15641541}1565154215661566-#ifdef CONFIG_CMPXCHG_LOCAL15671543#ifdef CONFIG_PREEMPT15681544/*15691545 * Calculate the next globally unique transaction for disambiguiation···16221600 stat(s, CMPXCHG_DOUBLE_CPU_FAIL);16231601}1624160216251625-#endif16261626-16271603void init_kmem_cache_cpus(struct kmem_cache *s)16281604{16291629-#ifdef CONFIG_CMPXCHG_LOCAL16301605 int cpu;1631160616321607 for_each_possible_cpu(cpu)16331608 per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);16341634-#endif16351635-16361609}16371610/*16381611 * Remove the cpu slab···16601643 page->inuse--;16611644 }16621645 c->page = NULL;16631663-#ifdef CONFIG_CMPXCHG_LOCAL16641646 c->tid = next_tid(c->tid);16651665-#endif16661647 unfreeze_slab(s, page, tail);16671648}16681649···17941779 unsigned long addr, struct kmem_cache_cpu *c)17951780{17961781 void **object;17971797- struct page *new;17981798-#ifdef CONFIG_CMPXCHG_LOCAL17821782+ struct page *page;17991783 unsigned long flags;1800178418011785 local_irq_save(flags);···18061792 */18071793 c = this_cpu_ptr(s->cpu_slab);18081794#endif18091809-#endif1810179518111796 /* We handle __GFP_ZERO in the caller */18121797 gfpflags &= ~__GFP_ZERO;1813179818141814- if (!c->page)17991799+ page = c->page;18001800+ if (!page)18151801 goto new_slab;1816180218171817- slab_lock(c->page);18031803+ slab_lock(page);18181804 if (unlikely(!node_match(c, node)))18191805 goto another_slab;1820180618211807 stat(s, ALLOC_REFILL);1822180818231809load_freelist:18241824- object = c->page->freelist;18101810+ object = page->freelist;18251811 if (unlikely(!object))18261812 goto another_slab;18271813 if (kmem_cache_debug(s))18281814 goto debug;1829181518301816 c->freelist = get_freepointer(s, object);18311831- c->page->inuse = c->page->objects;18321832- c->page->freelist = NULL;18331833- c->node = page_to_nid(c->page);18171817+ page->inuse = page->objects;18181818+ page->freelist = NULL;18191819+18341820unlock_out:18351835- slab_unlock(c->page);18361836-#ifdef CONFIG_CMPXCHG_LOCAL18211821+ slab_unlock(page);18371822 c->tid = next_tid(c->tid);18381823 local_irq_restore(flags);18391839-#endif18401824 stat(s, ALLOC_SLOWPATH);18411825 return object;18421826···18421830 deactivate_slab(s, c);1843183118441832new_slab:18451845- new = get_partial(s, gfpflags, node);18461846- if (new) {18471847- c->page = new;18331833+ page = get_partial(s, gfpflags, node);18341834+ if (page) {18481835 stat(s, ALLOC_FROM_PARTIAL);18361836+ c->node = page_to_nid(page);18371837+ c->page = page;18491838 goto load_freelist;18501839 }18511840···18541841 if (gfpflags & __GFP_WAIT)18551842 local_irq_enable();1856184318571857- new = new_slab(s, gfpflags, node);18441844+ page = new_slab(s, gfpflags, node);1858184518591846 if (gfpflags & __GFP_WAIT)18601847 local_irq_disable();1861184818621862- if (new) {18491849+ if (page) {18631850 c = __this_cpu_ptr(s->cpu_slab);18641851 stat(s, ALLOC_SLAB);18651852 if (c->page)18661853 flush_slab(s, c);18671867- slab_lock(new);18681868- __SetPageSlubFrozen(new);18691869- c->page = new;18541854+18551855+ slab_lock(page);18561856+ __SetPageSlubFrozen(page);18571857+ c->node = page_to_nid(page);18581858+ c->page = page;18701859 goto load_freelist;18711860 }18721861 if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())18731862 slab_out_of_memory(s, gfpflags, node);18741874-#ifdef CONFIG_CMPXCHG_LOCAL18751863 local_irq_restore(flags);18761876-#endif18771864 return NULL;18781865debug:18791879- if (!alloc_debug_processing(s, c->page, object, addr))18661866+ if (!alloc_debug_processing(s, page, object, addr))18801867 goto another_slab;1881186818821882- c->page->inuse++;18831883- c->page->freelist = get_freepointer(s, object);18691869+ page->inuse++;18701870+ page->freelist = get_freepointer(s, object);18711871+ deactivate_slab(s, c);18721872+ c->page = NULL;18841873 c->node = NUMA_NO_NODE;18851874 goto unlock_out;18861875}···19021887{19031888 void **object;19041889 struct kmem_cache_cpu *c;19051905-#ifdef CONFIG_CMPXCHG_LOCAL19061890 unsigned long tid;19071907-#else19081908- unsigned long flags;19091909-#endif1910189119111892 if (slab_pre_alloc_hook(s, gfpflags))19121893 return NULL;1913189419141914-#ifndef CONFIG_CMPXCHG_LOCAL19151915- local_irq_save(flags);19161916-#else19171895redo:19181918-#endif1919189619201897 /*19211898 * Must read kmem_cache cpu data via this cpu ptr. Preemption is···19171910 */19181911 c = __this_cpu_ptr(s->cpu_slab);1919191219201920-#ifdef CONFIG_CMPXCHG_LOCAL19211913 /*19221914 * The transaction ids are globally unique per cpu and per operation on19231915 * a per cpu queue. Thus they can be guarantee that the cmpxchg_double···19251919 */19261920 tid = c->tid;19271921 barrier();19281928-#endif1929192219301923 object = c->freelist;19311924 if (unlikely(!object || !node_match(c, node)))···19321927 object = __slab_alloc(s, gfpflags, node, addr, c);1933192819341929 else {19351935-#ifdef CONFIG_CMPXCHG_LOCAL19361930 /*19371931 * The cmpxchg will only match if there was no additional19381932 * operation and if we are on the right processor.···19471943 if (unlikely(!irqsafe_cpu_cmpxchg_double(19481944 s->cpu_slab->freelist, s->cpu_slab->tid,19491945 object, tid,19501950- get_freepointer(s, object), next_tid(tid)))) {19461946+ get_freepointer_safe(s, object), next_tid(tid)))) {1951194719521948 note_cmpxchg_failure("slab_alloc", s, tid);19531949 goto redo;19541950 }19551955-#else19561956- c->freelist = get_freepointer(s, object);19571957-#endif19581951 stat(s, ALLOC_FASTPATH);19591952 }19601960-19611961-#ifndef CONFIG_CMPXCHG_LOCAL19621962- local_irq_restore(flags);19631963-#endif1964195319651954 if (unlikely(gfpflags & __GFP_ZERO) && object)19661955 memset(object, 0, s->objsize);···20312034{20322035 void *prior;20332036 void **object = (void *)x;20342034-#ifdef CONFIG_CMPXCHG_LOCAL20352037 unsigned long flags;2036203820372039 local_irq_save(flags);20382038-#endif20392040 slab_lock(page);20402041 stat(s, FREE_SLOWPATH);2041204220422042- if (kmem_cache_debug(s))20432043- goto debug;20432043+ if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))20442044+ goto out_unlock;2044204520452045-checks_ok:20462046 prior = page->freelist;20472047 set_freepointer(s, object, prior);20482048 page->freelist = object;···2064207020652071out_unlock:20662072 slab_unlock(page);20672067-#ifdef CONFIG_CMPXCHG_LOCAL20682073 local_irq_restore(flags);20692069-#endif20702074 return;2071207520722076slab_empty:···20762084 stat(s, FREE_REMOVE_PARTIAL);20772085 }20782086 slab_unlock(page);20792079-#ifdef CONFIG_CMPXCHG_LOCAL20802087 local_irq_restore(flags);20812081-#endif20822088 stat(s, FREE_SLAB);20832089 discard_slab(s, page);20842084- return;20852085-20862086-debug:20872087- if (!free_debug_processing(s, page, x, addr))20882088- goto out_unlock;20892089- goto checks_ok;20902090}2091209120922092/*···20972113{20982114 void **object = (void *)x;20992115 struct kmem_cache_cpu *c;21002100-#ifdef CONFIG_CMPXCHG_LOCAL21012116 unsigned long tid;21022102-#else21032103- unsigned long flags;21042104-#endif2105211721062118 slab_free_hook(s, x);2107211921082108-#ifndef CONFIG_CMPXCHG_LOCAL21092109- local_irq_save(flags);21102110-21112111-#else21122120redo:21132113-#endif2114212121152122 /*21162123 * Determine the currently cpus per cpu slab.···21112136 */21122137 c = __this_cpu_ptr(s->cpu_slab);2113213821142114-#ifdef CONFIG_CMPXCHG_LOCAL21152139 tid = c->tid;21162140 barrier();21172117-#endif2118214121192119- if (likely(page == c->page && c->node != NUMA_NO_NODE)) {21422142+ if (likely(page == c->page)) {21202143 set_freepointer(s, object, c->freelist);2121214421222122-#ifdef CONFIG_CMPXCHG_LOCAL21232145 if (unlikely(!irqsafe_cpu_cmpxchg_double(21242146 s->cpu_slab->freelist, s->cpu_slab->tid,21252147 c->freelist, tid,···21252153 note_cmpxchg_failure("slab_free", s, tid);21262154 goto redo;21272155 }21282128-#else21292129- c->freelist = object;21302130-#endif21312156 stat(s, FREE_FASTPATH);21322157 } else21332158 __slab_free(s, page, x, addr);2134215921352135-#ifndef CONFIG_CMPXCHG_LOCAL21362136- local_irq_restore(flags);21372137-#endif21382160}2139216121402162void kmem_cache_free(struct kmem_cache *s, void *x)···26392673 return;26402674 slab_err(s, page, "%s", text);26412675 slab_lock(page);26422642- for_each_free_object(p, s, page->freelist)26432643- set_bit(slab_index(p, s, addr), map);2644267626772677+ get_map(s, page, map);26452678 for_each_object(p, s, addr, page->objects) {2646267926472680 if (!test_bit(slab_index(p, s, addr), map)) {···31683203 list_for_each_entry(p, &n->partial, lru)31693204 p->slab = s;3170320531713171-#ifdef CONFIG_SLAB_DEBUG32063206+#ifdef CONFIG_SLUB_DEBUG31723207 list_for_each_entry(p, &n->full, lru)31733208 p->slab = s;31743209#endif···35753610 /* Now we know that a valid freelist exists */35763611 bitmap_zero(map, page->objects);3577361235783578- for_each_free_object(p, s, page->freelist) {35793579- set_bit(slab_index(p, s, addr), map);35803580- if (!check_object(s, page, p, SLUB_RED_INACTIVE))35813581- return 0;36133613+ get_map(s, page, map);36143614+ for_each_object(p, s, addr, page->objects) {36153615+ if (test_bit(slab_index(p, s, addr), map))36163616+ if (!check_object(s, page, p, SLUB_RED_INACTIVE))36173617+ return 0;35823618 }3583361935843620 for_each_object(p, s, addr, page->objects)···37873821 void *p;3788382237893823 bitmap_zero(map, page->objects);37903790- for_each_free_object(p, s, page->freelist)37913791- set_bit(slab_index(p, s, addr), map);38243824+ get_map(s, page, map);3792382537933826 for_each_object(p, s, addr, page->objects)37943827 if (!test_bit(slab_index(p, s, addr), map))