~ubuntu-branches/ubuntu/trusty/linux-armadaxp/trusty

« back to all changes in this revision

Viewing changes to mm/slub.c

  • Committer: Package Import Robot
  • Author(s): Michael Casadevall, Bryan Wu, Dann Frazier, Michael Casadeall
  • Date: 2012-03-10 15:00:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120310150054-flugb39zon8vvgwe
Tags: 3.2.0-1600.1
[ Bryan Wu ]
* UBUNTU: import debian/debian.env and debian.armadaxp

[ Dann Frazier ]
* ARM: Armada XP: remove trailing '/' in dirnames in mvRules.mk

[ Michael Casadeall ]
* tools: add some tools for Marvell Armada XP processor
* kernel: timer tick hacking from Marvell
* kernel: Sheeva Errata: add delay on Sheeva when powering down
* net: add Marvell NFP netfilter
* net: socket and skb modifications made by Marvell
* miscdevice: add minor IDs for some Marvell Armada drivers
* fs: introduce memory pool for splice()
* video: EDID detection updates from Marvell Armada XP patchset
* video: backlight: add Marvell Dove LCD backlight driver
* video: display: add THS8200 display driver
* video: framebuffer: add Marvell Dove and Armada XP processor onchip LCD controller driver
* usbtest: add Interrupt transfer testing by Marvell Armada XP code
* usb: ehci: add support for Marvell EHCI controler
* tty/serial: 8250: add support for Marvell Armada XP processor and DeviceTree work
* rtc: add support for Marvell Armada XP onchip RTC controller
* net: pppoe: add Marvell ethernet NFP hook in PPPoE networking driver
* mtd: nand: add support for Marvell Armada XP Nand Flash Controller
* mtd: maps: add Marvell Armada XP specific map driver
* mmc: add support for Marvell Armada XP MMC/SD host controller
* i2c: add support for Marvell Armada XP onchip i2c bus controller
* hwmon: add Kconfig option for Armada XP onchip thermal sensor driver
* dmaengine: add Net DMA support for splice and update Marvell XOR DMA engine driver
* ata: add support for Marvell Armada XP SATA controller and update some quirks
* ARM: add Marvell Armada XP machine to mach-types
* ARM: oprofile: add support for Marvell PJ4B core
* ARM: mm: more ARMv6 switches for Marvell Armada XP
* ARM: remove static declaration to allow compilation
* ARM: alignment access fault trick
* ARM: mm: skip some fault fixing when run on NONE SMP ARMv6 mode during early abort event
* ARM: mm: add Marvell Sheeva CPU Architecture for PJ4B
* ARM: introduce optimized copy operation for Marvell Armada XP
* ARM: SAUCE: hardware breakpoint trick for Marvell Armada XP
* ARM: big endian and little endian tricks for Marvell Armada XP
* ARM: SAUCE: Add Marvell Armada XP build rules to arch/arm/kernel/Makefile
* ARM: vfp: add special handling for Marvell Armada XP
* ARM: add support for Marvell U-Boot
* ARM: add mv_controller_num for ARM PCI drivers
* ARM: add support for local PMUs, general SMP tweaks and cache flushing
* ARM: add Marvell device identifies in glue-proc.h
* ARM: add IPC driver support for Marvell platforms
* ARM: add DMA mapping for Marvell platforms
* ARM: add Sheeva errata and PJ4B code for booting
* ARM: update Kconfig and Makefile to include Marvell Armada XP platforms
* ARM: Armada XP: import LSP from Marvell for Armada XP 3.2 kernel enablement

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * SLUB: A slab allocator that limits cache line use instead of queuing
3
3
 * objects in per cpu and per node lists.
4
4
 *
5
 
 * The allocator synchronizes using per slab locks and only
6
 
 * uses a centralized lock to manage a pool of partial slabs.
 
5
 * The allocator synchronizes using per slab locks or atomic operatios
 
6
 * and only uses a centralized lock to manage a pool of partial slabs.
7
7
 *
8
8
 * (C) 2007 SGI, Christoph Lameter
 
9
 * (C) 2011 Linux Foundation, Christoph Lameter
9
10
 */
10
11
 
11
12
#include <linux/mm.h>
27
28
#include <linux/memory.h>
28
29
#include <linux/math64.h>
29
30
#include <linux/fault-inject.h>
 
31
#include <linux/stacktrace.h>
30
32
 
31
33
#include <trace/events/kmem.h>
32
34
 
33
35
/*
34
36
 * Lock order:
35
 
 *   1. slab_lock(page)
36
 
 *   2. slab->list_lock
37
 
 *
38
 
 *   The slab_lock protects operations on the object of a particular
39
 
 *   slab and its metadata in the page struct. If the slab lock
40
 
 *   has been taken then no allocations nor frees can be performed
41
 
 *   on the objects in the slab nor can the slab be added or removed
42
 
 *   from the partial or full lists since this would mean modifying
43
 
 *   the page_struct of the slab.
 
37
 *   1. slub_lock (Global Semaphore)
 
38
 *   2. node->list_lock
 
39
 *   3. slab_lock(page) (Only on some arches and for debugging)
 
40
 *
 
41
 *   slub_lock
 
42
 *
 
43
 *   The role of the slub_lock is to protect the list of all the slabs
 
44
 *   and to synchronize major metadata changes to slab cache structures.
 
45
 *
 
46
 *   The slab_lock is only used for debugging and on arches that do not
 
47
 *   have the ability to do a cmpxchg_double. It only protects the second
 
48
 *   double word in the page struct. Meaning
 
49
 *      A. page->freelist       -> List of object free in a page
 
50
 *      B. page->counters       -> Counters of objects
 
51
 *      C. page->frozen         -> frozen state
 
52
 *
 
53
 *   If a slab is frozen then it is exempt from list management. It is not
 
54
 *   on any list. The processor that froze the slab is the one who can
 
55
 *   perform list operations on the page. Other processors may put objects
 
56
 *   onto the freelist but the processor that froze the slab is the only
 
57
 *   one that can retrieve the objects from the page's freelist.
44
58
 *
45
59
 *   The list_lock protects the partial and full list on each node and
46
60
 *   the partial slab counter. If taken then no new slabs may be added or
53
67
 *   slabs, operations can continue without any centralized lock. F.e.
54
68
 *   allocating a long series of objects that fill up slabs does not require
55
69
 *   the list lock.
56
 
 *
57
 
 *   The lock order is sometimes inverted when we are trying to get a slab
58
 
 *   off a list. We take the list_lock and then look for a page on the list
59
 
 *   to use. While we do that objects in the slabs may be freed. We can
60
 
 *   only operate on the slab if we have also taken the slab_lock. So we use
61
 
 *   a slab_trylock() on the slab. If trylock was successful then no frees
62
 
 *   can occur anymore and we can use the slab for allocations etc. If the
63
 
 *   slab_trylock() does not succeed then frees are in progress in the slab and
64
 
 *   we must stay away from it for a while since we may cause a bouncing
65
 
 *   cacheline if we try to acquire the lock. So go onto the next slab.
66
 
 *   If all pages are busy then we may allocate a new slab instead of reusing
67
 
 *   a partial slab. A new slab has no one operating on it and thus there is
68
 
 *   no danger of cacheline contention.
69
 
 *
70
70
 *   Interrupts are disabled during allocation and deallocation in order to
71
71
 *   make the slab allocator safe to use in the context of an irq. In addition
72
72
 *   interrupts are disabled to ensure that the processor does not change
131
131
/* Enable to test recovery from slab corruption on boot */
132
132
#undef SLUB_RESILIENCY_TEST
133
133
 
 
134
/* Enable to log cmpxchg failures */
 
135
#undef SLUB_DEBUG_CMPXCHG
 
136
 
134
137
/*
135
138
 * Mininum number of partial slabs. These will be left on the partial
136
139
 * lists even if they are empty. kmem_cache_shrink may reclaim them.
166
169
 
167
170
#define OO_SHIFT        16
168
171
#define OO_MASK         ((1 << OO_SHIFT) - 1)
169
 
#define MAX_OBJS_PER_PAGE       65535 /* since page.objects is u16 */
 
172
#define MAX_OBJS_PER_PAGE       32767 /* since page.objects is u15 */
170
173
 
171
174
/* Internal SLUB flags */
172
175
#define __OBJECT_POISON         0x80000000UL /* Poison object */
 
176
#define __CMPXCHG_DOUBLE        0x40000000UL /* Use cmpxchg_double */
173
177
 
174
178
static int kmem_size = sizeof(struct kmem_cache);
175
179
 
191
195
/*
192
196
 * Tracking user of a slab.
193
197
 */
 
198
#define TRACK_ADDRS_COUNT 16
194
199
struct track {
195
200
        unsigned long addr;     /* Called from address */
 
201
#ifdef CONFIG_STACKTRACE
 
202
        unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
 
203
#endif
196
204
        int cpu;                /* Was running on cpu */
197
205
        int pid;                /* Pid context */
198
206
        unsigned long when;     /* When did the operation occur */
338
346
        return x.x & OO_MASK;
339
347
}
340
348
 
 
349
/*
 
350
 * Per slab locking using the pagelock
 
351
 */
 
352
static __always_inline void slab_lock(struct page *page)
 
353
{
 
354
        bit_spin_lock(PG_locked, &page->flags);
 
355
}
 
356
 
 
357
static __always_inline void slab_unlock(struct page *page)
 
358
{
 
359
        __bit_spin_unlock(PG_locked, &page->flags);
 
360
}
 
361
 
 
362
/* Interrupts must be disabled (for the fallback code to work right) */
 
363
static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 
364
                void *freelist_old, unsigned long counters_old,
 
365
                void *freelist_new, unsigned long counters_new,
 
366
                const char *n)
 
367
{
 
368
        VM_BUG_ON(!irqs_disabled());
 
369
#ifdef CONFIG_CMPXCHG_DOUBLE
 
370
        if (s->flags & __CMPXCHG_DOUBLE) {
 
371
                if (cmpxchg_double(&page->freelist,
 
372
                        freelist_old, counters_old,
 
373
                        freelist_new, counters_new))
 
374
                return 1;
 
375
        } else
 
376
#endif
 
377
        {
 
378
                slab_lock(page);
 
379
                if (page->freelist == freelist_old && page->counters == counters_old) {
 
380
                        page->freelist = freelist_new;
 
381
                        page->counters = counters_new;
 
382
                        slab_unlock(page);
 
383
                        return 1;
 
384
                }
 
385
                slab_unlock(page);
 
386
        }
 
387
 
 
388
        cpu_relax();
 
389
        stat(s, CMPXCHG_DOUBLE_FAIL);
 
390
 
 
391
#ifdef SLUB_DEBUG_CMPXCHG
 
392
        printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
 
393
#endif
 
394
 
 
395
        return 0;
 
396
}
 
397
 
 
398
static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 
399
                void *freelist_old, unsigned long counters_old,
 
400
                void *freelist_new, unsigned long counters_new,
 
401
                const char *n)
 
402
{
 
403
#ifdef CONFIG_CMPXCHG_DOUBLE
 
404
        if (s->flags & __CMPXCHG_DOUBLE) {
 
405
                if (cmpxchg_double(&page->freelist,
 
406
                        freelist_old, counters_old,
 
407
                        freelist_new, counters_new))
 
408
                return 1;
 
409
        } else
 
410
#endif
 
411
        {
 
412
                unsigned long flags;
 
413
 
 
414
                local_irq_save(flags);
 
415
                slab_lock(page);
 
416
                if (page->freelist == freelist_old && page->counters == counters_old) {
 
417
                        page->freelist = freelist_new;
 
418
                        page->counters = counters_new;
 
419
                        slab_unlock(page);
 
420
                        local_irq_restore(flags);
 
421
                        return 1;
 
422
                }
 
423
                slab_unlock(page);
 
424
                local_irq_restore(flags);
 
425
        }
 
426
 
 
427
        cpu_relax();
 
428
        stat(s, CMPXCHG_DOUBLE_FAIL);
 
429
 
 
430
#ifdef SLUB_DEBUG_CMPXCHG
 
431
        printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
 
432
#endif
 
433
 
 
434
        return 0;
 
435
}
 
436
 
341
437
#ifdef CONFIG_SLUB_DEBUG
342
438
/*
343
439
 * Determine a map of object in use on a page.
344
440
 *
345
 
 * Slab lock or node listlock must be held to guarantee that the page does
 
441
 * Node listlock must be held to guarantee that the page does
346
442
 * not vanish from under us.
347
443
 */
348
444
static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
371
467
 */
372
468
static void print_section(char *text, u8 *addr, unsigned int length)
373
469
{
374
 
        int i, offset;
375
 
        int newline = 1;
376
 
        char ascii[17];
377
 
 
378
 
        ascii[16] = 0;
379
 
 
380
 
        for (i = 0; i < length; i++) {
381
 
                if (newline) {
382
 
                        printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
383
 
                        newline = 0;
384
 
                }
385
 
                printk(KERN_CONT " %02x", addr[i]);
386
 
                offset = i % 16;
387
 
                ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
388
 
                if (offset == 15) {
389
 
                        printk(KERN_CONT " %s\n", ascii);
390
 
                        newline = 1;
391
 
                }
392
 
        }
393
 
        if (!newline) {
394
 
                i %= 16;
395
 
                while (i < 16) {
396
 
                        printk(KERN_CONT "   ");
397
 
                        ascii[i] = ' ';
398
 
                        i++;
399
 
                }
400
 
                printk(KERN_CONT " %s\n", ascii);
401
 
        }
 
470
        print_hex_dump(KERN_ERR, text, DUMP_PREFIX_ADDRESS, 16, 1, addr,
 
471
                        length, 1);
402
472
}
403
473
 
404
474
static struct track *get_track(struct kmem_cache *s, void *object,
420
490
        struct track *p = get_track(s, object, alloc);
421
491
 
422
492
        if (addr) {
 
493
#ifdef CONFIG_STACKTRACE
 
494
                struct stack_trace trace;
 
495
                int i;
 
496
 
 
497
                trace.nr_entries = 0;
 
498
                trace.max_entries = TRACK_ADDRS_COUNT;
 
499
                trace.entries = p->addrs;
 
500
                trace.skip = 3;
 
501
                save_stack_trace(&trace);
 
502
 
 
503
                /* See rant in lockdep.c */
 
504
                if (trace.nr_entries != 0 &&
 
505
                    trace.entries[trace.nr_entries - 1] == ULONG_MAX)
 
506
                        trace.nr_entries--;
 
507
 
 
508
                for (i = trace.nr_entries; i < TRACK_ADDRS_COUNT; i++)
 
509
                        p->addrs[i] = 0;
 
510
#endif
423
511
                p->addr = addr;
424
512
                p->cpu = smp_processor_id();
425
513
                p->pid = current->pid;
444
532
 
445
533
        printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
446
534
                s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
 
535
#ifdef CONFIG_STACKTRACE
 
536
        {
 
537
                int i;
 
538
                for (i = 0; i < TRACK_ADDRS_COUNT; i++)
 
539
                        if (t->addrs[i])
 
540
                                printk(KERN_ERR "\t%pS\n", (void *)t->addrs[i]);
 
541
                        else
 
542
                                break;
 
543
        }
 
544
#endif
447
545
}
448
546
 
449
547
static void print_tracking(struct kmem_cache *s, void *object)
501
599
                        p, p - addr, get_freepointer(s, p));
502
600
 
503
601
        if (p > addr + 16)
504
 
                print_section("Bytes b4", p - 16, 16);
505
 
 
506
 
        print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
507
 
 
 
602
                print_section("Bytes b4 ", p - 16, 16);
 
603
 
 
604
        print_section("Object ", p, min_t(unsigned long, s->objsize,
 
605
                                PAGE_SIZE));
508
606
        if (s->flags & SLAB_RED_ZONE)
509
 
                print_section("Redzone", p + s->objsize,
 
607
                print_section("Redzone ", p + s->objsize,
510
608
                        s->inuse - s->objsize);
511
609
 
512
610
        if (s->offset)
519
617
 
520
618
        if (off != s->size)
521
619
                /* Beginning of the filler is the free pointer */
522
 
                print_section("Padding", p + off, s->size - off);
 
620
                print_section("Padding ", p + off, s->size - off);
523
621
 
524
622
        dump_stack();
525
623
}
557
655
                memset(p + s->objsize, val, s->inuse - s->objsize);
558
656
}
559
657
 
560
 
static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
561
 
{
562
 
        while (bytes) {
563
 
                if (*start != (u8)value)
564
 
                        return start;
565
 
                start++;
566
 
                bytes--;
567
 
        }
568
 
        return NULL;
569
 
}
570
 
 
571
658
static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
572
659
                                                void *from, void *to)
573
660
{
582
669
        u8 *fault;
583
670
        u8 *end;
584
671
 
585
 
        fault = check_bytes(start, value, bytes);
 
672
        fault = memchr_inv(start, value, bytes);
586
673
        if (!fault)
587
674
                return 1;
588
675
 
675
762
        if (!remainder)
676
763
                return 1;
677
764
 
678
 
        fault = check_bytes(end - remainder, POISON_INUSE, remainder);
 
765
        fault = memchr_inv(end - remainder, POISON_INUSE, remainder);
679
766
        if (!fault)
680
767
                return 1;
681
768
        while (end > fault && end[-1] == POISON_INUSE)
682
769
                end--;
683
770
 
684
771
        slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
685
 
        print_section("Padding", end - remainder, remainder);
 
772
        print_section("Padding ", end - remainder, remainder);
686
773
 
687
774
        restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
688
775
        return 0;
773
860
static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
774
861
{
775
862
        int nr = 0;
776
 
        void *fp = page->freelist;
 
863
        void *fp;
777
864
        void *object = NULL;
778
865
        unsigned long max_objects;
779
866
 
 
867
        fp = page->freelist;
780
868
        while (fp && nr <= page->objects) {
781
869
                if (fp == search)
782
870
                        return 1;
830
918
                        page->freelist);
831
919
 
832
920
                if (!alloc)
833
 
                        print_section("Object", (void *)object, s->objsize);
 
921
                        print_section("Object ", (void *)object, s->objsize);
834
922
 
835
923
                dump_stack();
836
924
        }
881
969
 
882
970
/*
883
971
 * Tracking of fully allocated slabs for debugging purposes.
 
972
 *
 
973
 * list_lock must be held.
884
974
 */
885
 
static void add_full(struct kmem_cache_node *n, struct page *page)
 
975
static void add_full(struct kmem_cache *s,
 
976
        struct kmem_cache_node *n, struct page *page)
886
977
{
887
 
        spin_lock(&n->list_lock);
 
978
        if (!(s->flags & SLAB_STORE_USER))
 
979
                return;
 
980
 
888
981
        list_add(&page->lru, &n->full);
889
 
        spin_unlock(&n->list_lock);
890
982
}
891
983
 
 
984
/*
 
985
 * list_lock must be held.
 
986
 */
892
987
static void remove_full(struct kmem_cache *s, struct page *page)
893
988
{
894
 
        struct kmem_cache_node *n;
895
 
 
896
989
        if (!(s->flags & SLAB_STORE_USER))
897
990
                return;
898
991
 
899
 
        n = get_node(s, page_to_nid(page));
900
 
 
901
 
        spin_lock(&n->list_lock);
902
992
        list_del(&page->lru);
903
 
        spin_unlock(&n->list_lock);
904
993
}
905
994
 
906
995
/* Tracking of the number of slabs for debugging purposes */
956
1045
        if (!check_slab(s, page))
957
1046
                goto bad;
958
1047
 
959
 
        if (!on_freelist(s, page, object)) {
960
 
                object_err(s, page, object, "Object already allocated");
961
 
                goto bad;
962
 
        }
963
 
 
964
1048
        if (!check_valid_pointer(s, page, object)) {
965
1049
                object_err(s, page, object, "Freelist Pointer check fails");
966
1050
                goto bad;
993
1077
static noinline int free_debug_processing(struct kmem_cache *s,
994
1078
                 struct page *page, void *object, unsigned long addr)
995
1079
{
 
1080
        unsigned long flags;
 
1081
        int rc = 0;
 
1082
 
 
1083
        local_irq_save(flags);
 
1084
        slab_lock(page);
 
1085
 
996
1086
        if (!check_slab(s, page))
997
1087
                goto fail;
998
1088
 
1007
1097
        }
1008
1098
 
1009
1099
        if (!check_object(s, page, object, SLUB_RED_ACTIVE))
1010
 
                return 0;
 
1100
                goto out;
1011
1101
 
1012
1102
        if (unlikely(s != page->slab)) {
1013
1103
                if (!PageSlab(page)) {
1024
1114
                goto fail;
1025
1115
        }
1026
1116
 
1027
 
        /* Special debug activities for freeing objects */
1028
 
        if (!PageSlubFrozen(page) && !page->freelist)
1029
 
                remove_full(s, page);
1030
1117
        if (s->flags & SLAB_STORE_USER)
1031
1118
                set_track(s, object, TRACK_FREE, addr);
1032
1119
        trace(s, page, object, 0);
1033
1120
        init_object(s, object, SLUB_RED_INACTIVE);
1034
 
        return 1;
 
1121
        rc = 1;
 
1122
out:
 
1123
        slab_unlock(page);
 
1124
        local_irq_restore(flags);
 
1125
        return rc;
1035
1126
 
1036
1127
fail:
1037
1128
        slab_fix(s, "Object at 0x%p not freed", object);
1038
 
        return 0;
 
1129
        goto out;
1039
1130
}
1040
1131
 
1041
1132
static int __init setup_slub_debug(char *str)
1135
1226
                        { return 1; }
1136
1227
static inline int check_object(struct kmem_cache *s, struct page *page,
1137
1228
                        void *object, u8 val) { return 1; }
1138
 
static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
 
1229
static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
 
1230
                                        struct page *page) {}
 
1231
static inline void remove_full(struct kmem_cache *s, struct page *page) {}
1139
1232
static inline unsigned long kmem_cache_flags(unsigned long objsize,
1140
1233
        unsigned long flags, const char *name,
1141
1234
        void (*ctor)(void *))
1187
1280
        struct kmem_cache_order_objects oo = s->oo;
1188
1281
        gfp_t alloc_gfp;
1189
1282
 
 
1283
        flags &= gfp_allowed_mask;
 
1284
 
 
1285
        if (flags & __GFP_WAIT)
 
1286
                local_irq_enable();
 
1287
 
1190
1288
        flags |= s->allocflags;
1191
1289
 
1192
1290
        /*
1203
1301
                 * Try a lower order alloc if possible
1204
1302
                 */
1205
1303
                page = alloc_slab_page(flags, node, oo);
1206
 
                if (!page)
1207
 
                        return NULL;
1208
1304
 
1209
 
                stat(s, ORDER_FALLBACK);
 
1305
                if (page)
 
1306
                        stat(s, ORDER_FALLBACK);
1210
1307
        }
1211
1308
 
 
1309
        if (flags & __GFP_WAIT)
 
1310
                local_irq_disable();
 
1311
 
 
1312
        if (!page)
 
1313
                return NULL;
 
1314
 
1212
1315
        if (kmemcheck_enabled
1213
1316
                && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
1214
1317
                int pages = 1 << oo_order(oo);
1275
1378
        set_freepointer(s, last, NULL);
1276
1379
 
1277
1380
        page->freelist = start;
1278
 
        page->inuse = 0;
 
1381
        page->inuse = page->objects;
 
1382
        page->frozen = 1;
1279
1383
out:
1280
1384
        return page;
1281
1385
}
1353
1457
}
1354
1458
 
1355
1459
/*
1356
 
 * Per slab locking using the pagelock
1357
 
 */
1358
 
static __always_inline void slab_lock(struct page *page)
1359
 
{
1360
 
        bit_spin_lock(PG_locked, &page->flags);
1361
 
}
1362
 
 
1363
 
static __always_inline void slab_unlock(struct page *page)
1364
 
{
1365
 
        __bit_spin_unlock(PG_locked, &page->flags);
1366
 
}
1367
 
 
1368
 
static __always_inline int slab_trylock(struct page *page)
1369
 
{
1370
 
        int rc = 1;
1371
 
 
1372
 
        rc = bit_spin_trylock(PG_locked, &page->flags);
1373
 
        return rc;
1374
 
}
1375
 
 
1376
 
/*
1377
 
 * Management of partially allocated slabs
1378
 
 */
1379
 
static void add_partial(struct kmem_cache_node *n,
 
1460
 * Management of partially allocated slabs.
 
1461
 *
 
1462
 * list_lock must be held.
 
1463
 */
 
1464
static inline void add_partial(struct kmem_cache_node *n,
1380
1465
                                struct page *page, int tail)
1381
1466
{
1382
 
        spin_lock(&n->list_lock);
1383
1467
        n->nr_partial++;
1384
 
        if (tail)
 
1468
        if (tail == DEACTIVATE_TO_TAIL)
1385
1469
                list_add_tail(&page->lru, &n->partial);
1386
1470
        else
1387
1471
                list_add(&page->lru, &n->partial);
1388
 
        spin_unlock(&n->list_lock);
1389
1472
}
1390
1473
 
1391
 
static inline void __remove_partial(struct kmem_cache_node *n,
 
1474
/*
 
1475
 * list_lock must be held.
 
1476
 */
 
1477
static inline void remove_partial(struct kmem_cache_node *n,
1392
1478
                                        struct page *page)
1393
1479
{
1394
1480
        list_del(&page->lru);
1395
1481
        n->nr_partial--;
1396
1482
}
1397
1483
 
1398
 
static void remove_partial(struct kmem_cache *s, struct page *page)
1399
 
{
1400
 
        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1401
 
 
1402
 
        spin_lock(&n->list_lock);
1403
 
        __remove_partial(n, page);
1404
 
        spin_unlock(&n->list_lock);
1405
 
}
1406
 
 
1407
1484
/*
1408
 
 * Lock slab and remove from the partial list.
 
1485
 * Lock slab, remove from the partial list and put the object into the
 
1486
 * per cpu freelist.
 
1487
 *
 
1488
 * Returns a list of objects or NULL if it fails.
1409
1489
 *
1410
1490
 * Must hold list_lock.
1411
1491
 */
1412
 
static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
1413
 
                                                        struct page *page)
 
1492
static inline void *acquire_slab(struct kmem_cache *s,
 
1493
                struct kmem_cache_node *n, struct page *page,
 
1494
                int mode)
1414
1495
{
1415
 
        if (slab_trylock(page)) {
1416
 
                __remove_partial(n, page);
1417
 
                __SetPageSlubFrozen(page);
1418
 
                return 1;
1419
 
        }
1420
 
        return 0;
 
1496
        void *freelist;
 
1497
        unsigned long counters;
 
1498
        struct page new;
 
1499
 
 
1500
        /*
 
1501
         * Zap the freelist and set the frozen bit.
 
1502
         * The old freelist is the list of objects for the
 
1503
         * per cpu allocation list.
 
1504
         */
 
1505
        do {
 
1506
                freelist = page->freelist;
 
1507
                counters = page->counters;
 
1508
                new.counters = counters;
 
1509
                if (mode)
 
1510
                        new.inuse = page->objects;
 
1511
 
 
1512
                VM_BUG_ON(new.frozen);
 
1513
                new.frozen = 1;
 
1514
 
 
1515
        } while (!__cmpxchg_double_slab(s, page,
 
1516
                        freelist, counters,
 
1517
                        NULL, new.counters,
 
1518
                        "lock and freeze"));
 
1519
 
 
1520
        remove_partial(n, page);
 
1521
        return freelist;
1421
1522
}
1422
1523
 
 
1524
static int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
 
1525
 
1423
1526
/*
1424
1527
 * Try to allocate a partial slab from a specific node.
1425
1528
 */
1426
 
static struct page *get_partial_node(struct kmem_cache_node *n)
 
1529
static void *get_partial_node(struct kmem_cache *s,
 
1530
                struct kmem_cache_node *n, struct kmem_cache_cpu *c)
1427
1531
{
1428
 
        struct page *page;
 
1532
        struct page *page, *page2;
 
1533
        void *object = NULL;
1429
1534
 
1430
1535
        /*
1431
1536
         * Racy check. If we mistakenly see no partial slabs then we
1437
1542
                return NULL;
1438
1543
 
1439
1544
        spin_lock(&n->list_lock);
1440
 
        list_for_each_entry(page, &n->partial, lru)
1441
 
                if (lock_and_freeze_slab(n, page))
1442
 
                        goto out;
1443
 
        page = NULL;
1444
 
out:
 
1545
        list_for_each_entry_safe(page, page2, &n->partial, lru) {
 
1546
                void *t = acquire_slab(s, n, page, object == NULL);
 
1547
                int available;
 
1548
 
 
1549
                if (!t)
 
1550
                        break;
 
1551
 
 
1552
                if (!object) {
 
1553
                        c->page = page;
 
1554
                        c->node = page_to_nid(page);
 
1555
                        stat(s, ALLOC_FROM_PARTIAL);
 
1556
                        object = t;
 
1557
                        available =  page->objects - page->inuse;
 
1558
                } else {
 
1559
                        page->freelist = t;
 
1560
                        available = put_cpu_partial(s, page, 0);
 
1561
                }
 
1562
                if (kmem_cache_debug(s) || available > s->cpu_partial / 2)
 
1563
                        break;
 
1564
 
 
1565
        }
1445
1566
        spin_unlock(&n->list_lock);
1446
 
        return page;
 
1567
        return object;
1447
1568
}
1448
1569
 
1449
1570
/*
1450
1571
 * Get a page from somewhere. Search in increasing NUMA distances.
1451
1572
 */
1452
 
static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
 
1573
static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags,
 
1574
                struct kmem_cache_cpu *c)
1453
1575
{
1454
1576
#ifdef CONFIG_NUMA
1455
1577
        struct zonelist *zonelist;
1456
1578
        struct zoneref *z;
1457
1579
        struct zone *zone;
1458
1580
        enum zone_type high_zoneidx = gfp_zone(flags);
1459
 
        struct page *page;
 
1581
        void *object;
1460
1582
 
1461
1583
        /*
1462
1584
         * The defrag ratio allows a configuration of the tradeoffs between
1489
1611
 
1490
1612
                if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1491
1613
                                n->nr_partial > s->min_partial) {
1492
 
                        page = get_partial_node(n);
1493
 
                        if (page) {
 
1614
                        object = get_partial_node(s, n, c);
 
1615
                        if (object) {
1494
1616
                                put_mems_allowed();
1495
 
                                return page;
 
1617
                                return object;
1496
1618
                        }
1497
1619
                }
1498
1620
        }
1504
1626
/*
1505
1627
 * Get a partial page, lock it and return it.
1506
1628
 */
1507
 
static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
 
1629
static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
 
1630
                struct kmem_cache_cpu *c)
1508
1631
{
1509
 
        struct page *page;
 
1632
        void *object;
1510
1633
        int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
1511
1634
 
1512
 
        page = get_partial_node(get_node(s, searchnode));
1513
 
        if (page || node != NUMA_NO_NODE)
1514
 
                return page;
1515
 
 
1516
 
        return get_any_partial(s, flags);
1517
 
}
1518
 
 
1519
 
/*
1520
 
 * Move a page back to the lists.
1521
 
 *
1522
 
 * Must be called with the slab lock held.
1523
 
 *
1524
 
 * On exit the slab lock will have been dropped.
1525
 
 */
1526
 
static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1527
 
        __releases(bitlock)
1528
 
{
1529
 
        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
1530
 
 
1531
 
        __ClearPageSlubFrozen(page);
1532
 
        if (page->inuse) {
1533
 
 
1534
 
                if (page->freelist) {
1535
 
                        add_partial(n, page, tail);
1536
 
                        stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
1537
 
                } else {
1538
 
                        stat(s, DEACTIVATE_FULL);
1539
 
                        if (kmem_cache_debug(s) && (s->flags & SLAB_STORE_USER))
1540
 
                                add_full(n, page);
1541
 
                }
1542
 
                slab_unlock(page);
1543
 
        } else {
1544
 
                stat(s, DEACTIVATE_EMPTY);
1545
 
                if (n->nr_partial < s->min_partial) {
1546
 
                        /*
1547
 
                         * Adding an empty slab to the partial slabs in order
1548
 
                         * to avoid page allocator overhead. This slab needs
1549
 
                         * to come after the other slabs with objects in
1550
 
                         * so that the others get filled first. That way the
1551
 
                         * size of the partial list stays small.
1552
 
                         *
1553
 
                         * kmem_cache_shrink can reclaim any empty slabs from
1554
 
                         * the partial list.
1555
 
                         */
1556
 
                        add_partial(n, page, 1);
1557
 
                        slab_unlock(page);
1558
 
                } else {
1559
 
                        slab_unlock(page);
1560
 
                        stat(s, FREE_SLAB);
1561
 
                        discard_slab(s, page);
1562
 
                }
1563
 
        }
 
1635
        object = get_partial_node(s, get_node(s, searchnode), c);
 
1636
        if (object || node != NUMA_NO_NODE)
 
1637
                return object;
 
1638
 
 
1639
        return get_any_partial(s, flags, c);
1564
1640
}
1565
1641
 
1566
1642
#ifdef CONFIG_PREEMPT
1629
1705
        for_each_possible_cpu(cpu)
1630
1706
                per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
1631
1707
}
 
1708
 
1632
1709
/*
1633
1710
 * Remove the cpu slab
1634
1711
 */
1635
1712
static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1636
 
        __releases(bitlock)
1637
1713
{
 
1714
        enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
1638
1715
        struct page *page = c->page;
1639
 
        int tail = 1;
 
1716
        struct kmem_cache_node *n = get_node(s, page_to_nid(page));
 
1717
        int lock = 0;
 
1718
        enum slab_modes l = M_NONE, m = M_NONE;
 
1719
        void *freelist;
 
1720
        void *nextfree;
 
1721
        int tail = DEACTIVATE_TO_HEAD;
 
1722
        struct page new;
 
1723
        struct page old;
1640
1724
 
1641
 
        if (page->freelist)
 
1725
        if (page->freelist) {
1642
1726
                stat(s, DEACTIVATE_REMOTE_FREES);
1643
 
        /*
1644
 
         * Merge cpu freelist into slab freelist. Typically we get here
1645
 
         * because both freelists are empty. So this is unlikely
1646
 
         * to occur.
1647
 
         */
1648
 
        while (unlikely(c->freelist)) {
1649
 
                void **object;
1650
 
 
1651
 
                tail = 0;       /* Hot objects. Put the slab first */
1652
 
 
1653
 
                /* Retrieve object from cpu_freelist */
1654
 
                object = c->freelist;
1655
 
                c->freelist = get_freepointer(s, c->freelist);
1656
 
 
1657
 
                /* And put onto the regular freelist */
1658
 
                set_freepointer(s, object, page->freelist);
1659
 
                page->freelist = object;
1660
 
                page->inuse--;
 
1727
                tail = DEACTIVATE_TO_TAIL;
1661
1728
        }
 
1729
 
 
1730
        c->tid = next_tid(c->tid);
1662
1731
        c->page = NULL;
1663
 
        c->tid = next_tid(c->tid);
1664
 
        unfreeze_slab(s, page, tail);
 
1732
        freelist = c->freelist;
 
1733
        c->freelist = NULL;
 
1734
 
 
1735
        /*
 
1736
         * Stage one: Free all available per cpu objects back
 
1737
         * to the page freelist while it is still frozen. Leave the
 
1738
         * last one.
 
1739
         *
 
1740
         * There is no need to take the list->lock because the page
 
1741
         * is still frozen.
 
1742
         */
 
1743
        while (freelist && (nextfree = get_freepointer(s, freelist))) {
 
1744
                void *prior;
 
1745
                unsigned long counters;
 
1746
 
 
1747
                do {
 
1748
                        prior = page->freelist;
 
1749
                        counters = page->counters;
 
1750
                        set_freepointer(s, freelist, prior);
 
1751
                        new.counters = counters;
 
1752
                        new.inuse--;
 
1753
                        VM_BUG_ON(!new.frozen);
 
1754
 
 
1755
                } while (!__cmpxchg_double_slab(s, page,
 
1756
                        prior, counters,
 
1757
                        freelist, new.counters,
 
1758
                        "drain percpu freelist"));
 
1759
 
 
1760
                freelist = nextfree;
 
1761
        }
 
1762
 
 
1763
        /*
 
1764
         * Stage two: Ensure that the page is unfrozen while the
 
1765
         * list presence reflects the actual number of objects
 
1766
         * during unfreeze.
 
1767
         *
 
1768
         * We setup the list membership and then perform a cmpxchg
 
1769
         * with the count. If there is a mismatch then the page
 
1770
         * is not unfrozen but the page is on the wrong list.
 
1771
         *
 
1772
         * Then we restart the process which may have to remove
 
1773
         * the page from the list that we just put it on again
 
1774
         * because the number of objects in the slab may have
 
1775
         * changed.
 
1776
         */
 
1777
redo:
 
1778
 
 
1779
        old.freelist = page->freelist;
 
1780
        old.counters = page->counters;
 
1781
        VM_BUG_ON(!old.frozen);
 
1782
 
 
1783
        /* Determine target state of the slab */
 
1784
        new.counters = old.counters;
 
1785
        if (freelist) {
 
1786
                new.inuse--;
 
1787
                set_freepointer(s, freelist, old.freelist);
 
1788
                new.freelist = freelist;
 
1789
        } else
 
1790
                new.freelist = old.freelist;
 
1791
 
 
1792
        new.frozen = 0;
 
1793
 
 
1794
        if (!new.inuse && n->nr_partial > s->min_partial)
 
1795
                m = M_FREE;
 
1796
        else if (new.freelist) {
 
1797
                m = M_PARTIAL;
 
1798
                if (!lock) {
 
1799
                        lock = 1;
 
1800
                        /*
 
1801
                         * Taking the spinlock removes the possiblity
 
1802
                         * that acquire_slab() will see a slab page that
 
1803
                         * is frozen
 
1804
                         */
 
1805
                        spin_lock(&n->list_lock);
 
1806
                }
 
1807
        } else {
 
1808
                m = M_FULL;
 
1809
                if (kmem_cache_debug(s) && !lock) {
 
1810
                        lock = 1;
 
1811
                        /*
 
1812
                         * This also ensures that the scanning of full
 
1813
                         * slabs from diagnostic functions will not see
 
1814
                         * any frozen slabs.
 
1815
                         */
 
1816
                        spin_lock(&n->list_lock);
 
1817
                }
 
1818
        }
 
1819
 
 
1820
        if (l != m) {
 
1821
 
 
1822
                if (l == M_PARTIAL)
 
1823
 
 
1824
                        remove_partial(n, page);
 
1825
 
 
1826
                else if (l == M_FULL)
 
1827
 
 
1828
                        remove_full(s, page);
 
1829
 
 
1830
                if (m == M_PARTIAL) {
 
1831
 
 
1832
                        add_partial(n, page, tail);
 
1833
                        stat(s, tail);
 
1834
 
 
1835
                } else if (m == M_FULL) {
 
1836
 
 
1837
                        stat(s, DEACTIVATE_FULL);
 
1838
                        add_full(s, n, page);
 
1839
 
 
1840
                }
 
1841
        }
 
1842
 
 
1843
        l = m;
 
1844
        if (!__cmpxchg_double_slab(s, page,
 
1845
                                old.freelist, old.counters,
 
1846
                                new.freelist, new.counters,
 
1847
                                "unfreezing slab"))
 
1848
                goto redo;
 
1849
 
 
1850
        if (lock)
 
1851
                spin_unlock(&n->list_lock);
 
1852
 
 
1853
        if (m == M_FREE) {
 
1854
                stat(s, DEACTIVATE_EMPTY);
 
1855
                discard_slab(s, page);
 
1856
                stat(s, FREE_SLAB);
 
1857
        }
 
1858
}
 
1859
 
 
1860
/* Unfreeze all the cpu partial slabs */
 
1861
static void unfreeze_partials(struct kmem_cache *s)
 
1862
{
 
1863
        struct kmem_cache_node *n = NULL;
 
1864
        struct kmem_cache_cpu *c = this_cpu_ptr(s->cpu_slab);
 
1865
        struct page *page, *discard_page = NULL;
 
1866
 
 
1867
        while ((page = c->partial)) {
 
1868
                enum slab_modes { M_PARTIAL, M_FREE };
 
1869
                enum slab_modes l, m;
 
1870
                struct page new;
 
1871
                struct page old;
 
1872
 
 
1873
                c->partial = page->next;
 
1874
                l = M_FREE;
 
1875
 
 
1876
                do {
 
1877
 
 
1878
                        old.freelist = page->freelist;
 
1879
                        old.counters = page->counters;
 
1880
                        VM_BUG_ON(!old.frozen);
 
1881
 
 
1882
                        new.counters = old.counters;
 
1883
                        new.freelist = old.freelist;
 
1884
 
 
1885
                        new.frozen = 0;
 
1886
 
 
1887
                        if (!new.inuse && (!n || n->nr_partial > s->min_partial))
 
1888
                                m = M_FREE;
 
1889
                        else {
 
1890
                                struct kmem_cache_node *n2 = get_node(s,
 
1891
                                                        page_to_nid(page));
 
1892
 
 
1893
                                m = M_PARTIAL;
 
1894
                                if (n != n2) {
 
1895
                                        if (n)
 
1896
                                                spin_unlock(&n->list_lock);
 
1897
 
 
1898
                                        n = n2;
 
1899
                                        spin_lock(&n->list_lock);
 
1900
                                }
 
1901
                        }
 
1902
 
 
1903
                        if (l != m) {
 
1904
                                if (l == M_PARTIAL)
 
1905
                                        remove_partial(n, page);
 
1906
                                else
 
1907
                                        add_partial(n, page,
 
1908
                                                DEACTIVATE_TO_TAIL);
 
1909
 
 
1910
                                l = m;
 
1911
                        }
 
1912
 
 
1913
                } while (!cmpxchg_double_slab(s, page,
 
1914
                                old.freelist, old.counters,
 
1915
                                new.freelist, new.counters,
 
1916
                                "unfreezing slab"));
 
1917
 
 
1918
                if (m == M_FREE) {
 
1919
                        page->next = discard_page;
 
1920
                        discard_page = page;
 
1921
                }
 
1922
        }
 
1923
 
 
1924
        if (n)
 
1925
                spin_unlock(&n->list_lock);
 
1926
 
 
1927
        while (discard_page) {
 
1928
                page = discard_page;
 
1929
                discard_page = discard_page->next;
 
1930
 
 
1931
                stat(s, DEACTIVATE_EMPTY);
 
1932
                discard_slab(s, page);
 
1933
                stat(s, FREE_SLAB);
 
1934
        }
 
1935
}
 
1936
 
 
1937
/*
 
1938
 * Put a page that was just frozen (in __slab_free) into a partial page
 
1939
 * slot if available. This is done without interrupts disabled and without
 
1940
 * preemption disabled. The cmpxchg is racy and may put the partial page
 
1941
 * onto a random cpus partial slot.
 
1942
 *
 
1943
 * If we did not find a slot then simply move all the partials to the
 
1944
 * per node partial list.
 
1945
 */
 
1946
int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 
1947
{
 
1948
        struct page *oldpage;
 
1949
        int pages;
 
1950
        int pobjects;
 
1951
 
 
1952
        do {
 
1953
                pages = 0;
 
1954
                pobjects = 0;
 
1955
                oldpage = this_cpu_read(s->cpu_slab->partial);
 
1956
 
 
1957
                if (oldpage) {
 
1958
                        pobjects = oldpage->pobjects;
 
1959
                        pages = oldpage->pages;
 
1960
                        if (drain && pobjects > s->cpu_partial) {
 
1961
                                unsigned long flags;
 
1962
                                /*
 
1963
                                 * partial array is full. Move the existing
 
1964
                                 * set to the per node partial list.
 
1965
                                 */
 
1966
                                local_irq_save(flags);
 
1967
                                unfreeze_partials(s);
 
1968
                                local_irq_restore(flags);
 
1969
                                pobjects = 0;
 
1970
                                pages = 0;
 
1971
                        }
 
1972
                }
 
1973
 
 
1974
                pages++;
 
1975
                pobjects += page->objects - page->inuse;
 
1976
 
 
1977
                page->pages = pages;
 
1978
                page->pobjects = pobjects;
 
1979
                page->next = oldpage;
 
1980
 
 
1981
        } while (irqsafe_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page) != oldpage);
 
1982
        stat(s, CPU_PARTIAL_FREE);
 
1983
        return pobjects;
1665
1984
}
1666
1985
 
1667
1986
static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
1668
1987
{
1669
1988
        stat(s, CPUSLAB_FLUSH);
1670
 
        slab_lock(c->page);
1671
1989
        deactivate_slab(s, c);
1672
1990
}
1673
1991
 
1680
1998
{
1681
1999
        struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
1682
2000
 
1683
 
        if (likely(c && c->page))
1684
 
                flush_slab(s, c);
 
2001
        if (likely(c)) {
 
2002
                if (c->page)
 
2003
                        flush_slab(s, c);
 
2004
 
 
2005
                unfreeze_partials(s);
 
2006
        }
1685
2007
}
1686
2008
 
1687
2009
static void flush_cpu_slab(void *d)
1772
2094
        }
1773
2095
}
1774
2096
 
 
2097
static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
 
2098
                        int node, struct kmem_cache_cpu **pc)
 
2099
{
 
2100
        void *object;
 
2101
        struct kmem_cache_cpu *c;
 
2102
        struct page *page = new_slab(s, flags, node);
 
2103
 
 
2104
        if (page) {
 
2105
                c = __this_cpu_ptr(s->cpu_slab);
 
2106
                if (c->page)
 
2107
                        flush_slab(s, c);
 
2108
 
 
2109
                /*
 
2110
                 * No other reference to the page yet so we can
 
2111
                 * muck around with it freely without cmpxchg
 
2112
                 */
 
2113
                object = page->freelist;
 
2114
                page->freelist = NULL;
 
2115
 
 
2116
                stat(s, ALLOC_SLAB);
 
2117
                c->node = page_to_nid(page);
 
2118
                c->page = page;
 
2119
                *pc = c;
 
2120
        } else
 
2121
                object = NULL;
 
2122
 
 
2123
        return object;
 
2124
}
 
2125
 
1775
2126
/*
1776
2127
 * Slow path. The lockless freelist is empty or we need to perform
1777
2128
 * debugging duties.
1778
2129
 *
1779
 
 * Interrupts are disabled.
1780
 
 *
1781
2130
 * Processing is still very fast if new objects have been freed to the
1782
2131
 * regular freelist. In that case we simply take over the regular freelist
1783
2132
 * as the lockless freelist and zap the regular freelist.
1794
2143
                          unsigned long addr, struct kmem_cache_cpu *c)
1795
2144
{
1796
2145
        void **object;
1797
 
        struct page *page;
1798
2146
        unsigned long flags;
 
2147
        struct page new;
 
2148
        unsigned long counters;
1799
2149
 
1800
2150
        local_irq_save(flags);
1801
2151
#ifdef CONFIG_PREEMPT
1807
2157
        c = this_cpu_ptr(s->cpu_slab);
1808
2158
#endif
1809
2159
 
1810
 
        /* We handle __GFP_ZERO in the caller */
1811
 
        gfpflags &= ~__GFP_ZERO;
1812
 
 
1813
 
        page = c->page;
1814
 
        if (!page)
1815
 
                goto new_slab;
1816
 
 
1817
 
        slab_lock(page);
1818
 
        if (unlikely(!node_match(c, node)))
1819
 
                goto another_slab;
 
2160
        if (!c->page)
 
2161
                goto new_slab;
 
2162
redo:
 
2163
        if (unlikely(!node_match(c, node))) {
 
2164
                stat(s, ALLOC_NODE_MISMATCH);
 
2165
                deactivate_slab(s, c);
 
2166
                goto new_slab;
 
2167
        }
 
2168
 
 
2169
        /* must check again c->freelist in case of cpu migration or IRQ */
 
2170
        object = c->freelist;
 
2171
        if (object)
 
2172
                goto load_freelist;
 
2173
 
 
2174
        stat(s, ALLOC_SLOWPATH);
 
2175
 
 
2176
        do {
 
2177
                object = c->page->freelist;
 
2178
                counters = c->page->counters;
 
2179
                new.counters = counters;
 
2180
                VM_BUG_ON(!new.frozen);
 
2181
 
 
2182
                /*
 
2183
                 * If there is no object left then we use this loop to
 
2184
                 * deactivate the slab which is simple since no objects
 
2185
                 * are left in the slab and therefore we do not need to
 
2186
                 * put the page back onto the partial list.
 
2187
                 *
 
2188
                 * If there are objects left then we retrieve them
 
2189
                 * and use them to refill the per cpu queue.
 
2190
                 */
 
2191
 
 
2192
                new.inuse = c->page->objects;
 
2193
                new.frozen = object != NULL;
 
2194
 
 
2195
        } while (!__cmpxchg_double_slab(s, c->page,
 
2196
                        object, counters,
 
2197
                        NULL, new.counters,
 
2198
                        "__slab_alloc"));
 
2199
 
 
2200
        if (!object) {
 
2201
                c->page = NULL;
 
2202
                stat(s, DEACTIVATE_BYPASS);
 
2203
                goto new_slab;
 
2204
        }
1820
2205
 
1821
2206
        stat(s, ALLOC_REFILL);
1822
2207
 
1823
2208
load_freelist:
1824
 
        object = page->freelist;
1825
 
        if (unlikely(!object))
1826
 
                goto another_slab;
1827
 
        if (kmem_cache_debug(s))
1828
 
                goto debug;
1829
 
 
1830
2209
        c->freelist = get_freepointer(s, object);
1831
 
        page->inuse = page->objects;
1832
 
        page->freelist = NULL;
1833
 
 
1834
 
        slab_unlock(page);
1835
2210
        c->tid = next_tid(c->tid);
1836
2211
        local_irq_restore(flags);
1837
 
        stat(s, ALLOC_SLOWPATH);
1838
2212
        return object;
1839
2213
 
1840
 
another_slab:
1841
 
        deactivate_slab(s, c);
1842
 
 
1843
2214
new_slab:
1844
 
        page = get_partial(s, gfpflags, node);
1845
 
        if (page) {
1846
 
                stat(s, ALLOC_FROM_PARTIAL);
1847
 
                c->node = page_to_nid(page);
1848
 
                c->page = page;
1849
 
                goto load_freelist;
1850
 
        }
1851
 
 
1852
 
        gfpflags &= gfp_allowed_mask;
1853
 
        if (gfpflags & __GFP_WAIT)
1854
 
                local_irq_enable();
1855
 
 
1856
 
        page = new_slab(s, gfpflags, node);
1857
 
 
1858
 
        if (gfpflags & __GFP_WAIT)
1859
 
                local_irq_disable();
1860
 
 
1861
 
        if (page) {
1862
 
                c = __this_cpu_ptr(s->cpu_slab);
1863
 
                stat(s, ALLOC_SLAB);
1864
 
                if (c->page)
1865
 
                        flush_slab(s, c);
1866
 
 
1867
 
                slab_lock(page);
1868
 
                __SetPageSlubFrozen(page);
1869
 
                c->node = page_to_nid(page);
1870
 
                c->page = page;
1871
 
                goto load_freelist;
1872
 
        }
1873
 
        if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
1874
 
                slab_out_of_memory(s, gfpflags, node);
1875
 
        local_irq_restore(flags);
1876
 
        return NULL;
1877
 
debug:
1878
 
        if (!alloc_debug_processing(s, page, object, addr))
1879
 
                goto another_slab;
1880
 
 
1881
 
        page->inuse++;
1882
 
        page->freelist = get_freepointer(s, object);
 
2215
 
 
2216
        if (c->partial) {
 
2217
                c->page = c->partial;
 
2218
                c->partial = c->page->next;
 
2219
                c->node = page_to_nid(c->page);
 
2220
                stat(s, CPU_PARTIAL_ALLOC);
 
2221
                c->freelist = NULL;
 
2222
                goto redo;
 
2223
        }
 
2224
 
 
2225
        /* Then do expensive stuff like retrieving pages from the partial lists */
 
2226
        object = get_partial(s, gfpflags, node, c);
 
2227
 
 
2228
        if (unlikely(!object)) {
 
2229
 
 
2230
                object = new_slab_objects(s, gfpflags, node, &c);
 
2231
 
 
2232
                if (unlikely(!object)) {
 
2233
                        if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
 
2234
                                slab_out_of_memory(s, gfpflags, node);
 
2235
 
 
2236
                        local_irq_restore(flags);
 
2237
                        return NULL;
 
2238
                }
 
2239
        }
 
2240
 
 
2241
        if (likely(!kmem_cache_debug(s)))
 
2242
                goto load_freelist;
 
2243
 
 
2244
        /* Only entered in the debug case */
 
2245
        if (!alloc_debug_processing(s, c->page, object, addr))
 
2246
                goto new_slab;  /* Slab failed checks. Next slab needed */
 
2247
 
 
2248
        c->freelist = get_freepointer(s, object);
1883
2249
        deactivate_slab(s, c);
1884
 
        c->page = NULL;
1885
2250
        c->node = NUMA_NO_NODE;
1886
2251
        local_irq_restore(flags);
1887
2252
        return object;
2031
2396
{
2032
2397
        void *prior;
2033
2398
        void **object = (void *)x;
2034
 
        unsigned long flags;
 
2399
        int was_frozen;
 
2400
        int inuse;
 
2401
        struct page new;
 
2402
        unsigned long counters;
 
2403
        struct kmem_cache_node *n = NULL;
 
2404
        unsigned long uninitialized_var(flags);
2035
2405
 
2036
 
        local_irq_save(flags);
2037
 
        slab_lock(page);
2038
2406
        stat(s, FREE_SLOWPATH);
2039
2407
 
2040
2408
        if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
2041
 
                goto out_unlock;
2042
 
 
2043
 
        prior = page->freelist;
2044
 
        set_freepointer(s, object, prior);
2045
 
        page->freelist = object;
2046
 
        page->inuse--;
2047
 
 
2048
 
        if (unlikely(PageSlubFrozen(page))) {
 
2409
                return;
 
2410
 
 
2411
        do {
 
2412
                prior = page->freelist;
 
2413
                counters = page->counters;
 
2414
                set_freepointer(s, object, prior);
 
2415
                new.counters = counters;
 
2416
                was_frozen = new.frozen;
 
2417
                new.inuse--;
 
2418
                if ((!new.inuse || !prior) && !was_frozen && !n) {
 
2419
 
 
2420
                        if (!kmem_cache_debug(s) && !prior)
 
2421
 
 
2422
                                /*
 
2423
                                 * Slab was on no list before and will be partially empty
 
2424
                                 * We can defer the list move and instead freeze it.
 
2425
                                 */
 
2426
                                new.frozen = 1;
 
2427
 
 
2428
                        else { /* Needs to be taken off a list */
 
2429
 
 
2430
                                n = get_node(s, page_to_nid(page));
 
2431
                                /*
 
2432
                                 * Speculatively acquire the list_lock.
 
2433
                                 * If the cmpxchg does not succeed then we may
 
2434
                                 * drop the list_lock without any processing.
 
2435
                                 *
 
2436
                                 * Otherwise the list_lock will synchronize with
 
2437
                                 * other processors updating the list of slabs.
 
2438
                                 */
 
2439
                                spin_lock_irqsave(&n->list_lock, flags);
 
2440
 
 
2441
                        }
 
2442
                }
 
2443
                inuse = new.inuse;
 
2444
 
 
2445
        } while (!cmpxchg_double_slab(s, page,
 
2446
                prior, counters,
 
2447
                object, new.counters,
 
2448
                "__slab_free"));
 
2449
 
 
2450
        if (likely(!n)) {
 
2451
 
 
2452
                /*
 
2453
                 * If we just froze the page then put it onto the
 
2454
                 * per cpu partial list.
 
2455
                 */
 
2456
                if (new.frozen && !was_frozen)
 
2457
                        put_cpu_partial(s, page, 1);
 
2458
 
 
2459
                /*
 
2460
                 * The list lock was not taken therefore no list
 
2461
                 * activity can be necessary.
 
2462
                 */
 
2463
                if (was_frozen)
 
2464
                        stat(s, FREE_FROZEN);
 
2465
                return;
 
2466
        }
 
2467
 
 
2468
        /*
 
2469
         * was_frozen may have been set after we acquired the list_lock in
 
2470
         * an earlier loop. So we need to check it here again.
 
2471
         */
 
2472
        if (was_frozen)
2049
2473
                stat(s, FREE_FROZEN);
2050
 
                goto out_unlock;
2051
 
        }
2052
 
 
2053
 
        if (unlikely(!page->inuse))
2054
 
                goto slab_empty;
2055
 
 
2056
 
        /*
2057
 
         * Objects left in the slab. If it was not on the partial list before
2058
 
         * then add it.
2059
 
         */
2060
 
        if (unlikely(!prior)) {
2061
 
                add_partial(get_node(s, page_to_nid(page)), page, 1);
2062
 
                stat(s, FREE_ADD_PARTIAL);
2063
 
        }
2064
 
 
2065
 
out_unlock:
2066
 
        slab_unlock(page);
2067
 
        local_irq_restore(flags);
 
2474
        else {
 
2475
                if (unlikely(!inuse && n->nr_partial > s->min_partial))
 
2476
                        goto slab_empty;
 
2477
 
 
2478
                /*
 
2479
                 * Objects left in the slab. If it was not on the partial list before
 
2480
                 * then add it.
 
2481
                 */
 
2482
                if (unlikely(!prior)) {
 
2483
                        remove_full(s, page);
 
2484
                        add_partial(n, page, DEACTIVATE_TO_TAIL);
 
2485
                        stat(s, FREE_ADD_PARTIAL);
 
2486
                }
 
2487
        }
 
2488
        spin_unlock_irqrestore(&n->list_lock, flags);
2068
2489
        return;
2069
2490
 
2070
2491
slab_empty:
2071
2492
        if (prior) {
2072
2493
                /*
2073
 
                 * Slab still on the partial list.
 
2494
                 * Slab on the partial list.
2074
2495
                 */
2075
 
                remove_partial(s, page);
 
2496
                remove_partial(n, page);
2076
2497
                stat(s, FREE_REMOVE_PARTIAL);
2077
 
        }
2078
 
        slab_unlock(page);
2079
 
        local_irq_restore(flags);
 
2498
        } else
 
2499
                /* Slab must be on the full list */
 
2500
                remove_full(s, page);
 
2501
 
 
2502
        spin_unlock_irqrestore(&n->list_lock, flags);
2080
2503
        stat(s, FREE_SLAB);
2081
2504
        discard_slab(s, page);
2082
2505
}
2102
2525
        slab_free_hook(s, x);
2103
2526
 
2104
2527
redo:
2105
 
 
2106
2528
        /*
2107
2529
         * Determine the currently cpus per cpu slab.
2108
2530
         * The cpu may change afterward. However that does not matter since
2350
2772
{
2351
2773
        struct page *page;
2352
2774
        struct kmem_cache_node *n;
2353
 
        unsigned long flags;
2354
2775
 
2355
2776
        BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
2356
2777
 
2367
2788
        n = page->freelist;
2368
2789
        BUG_ON(!n);
2369
2790
        page->freelist = get_freepointer(kmem_cache_node, n);
2370
 
        page->inuse++;
 
2791
        page->inuse = 1;
 
2792
        page->frozen = 0;
2371
2793
        kmem_cache_node->node[node] = n;
2372
2794
#ifdef CONFIG_SLUB_DEBUG
2373
2795
        init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
2376
2798
        init_kmem_cache_node(n, kmem_cache_node);
2377
2799
        inc_slabs_node(kmem_cache_node, node, page->objects);
2378
2800
 
2379
 
        /*
2380
 
         * lockdep requires consistent irq usage for each lock
2381
 
         * so even though there cannot be a race this early in
2382
 
         * the boot sequence, we still disable irqs.
2383
 
         */
2384
 
        local_irq_save(flags);
2385
 
        add_partial(n, page, 0);
2386
 
        local_irq_restore(flags);
 
2801
        add_partial(n, page, DEACTIVATE_TO_HEAD);
2387
2802
}
2388
2803
 
2389
2804
static void free_kmem_cache_nodes(struct kmem_cache *s)
2589
3004
                }
2590
3005
        }
2591
3006
 
 
3007
#ifdef CONFIG_CMPXCHG_DOUBLE
 
3008
        if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
 
3009
                /* Enable fast mode */
 
3010
                s->flags |= __CMPXCHG_DOUBLE;
 
3011
#endif
 
3012
 
2592
3013
        /*
2593
3014
         * The larger the object size is, the more pages we want on the partial
2594
3015
         * list to avoid pounding the page allocator excessively.
2595
3016
         */
2596
 
        set_min_partial(s, ilog2(s->size));
 
3017
        set_min_partial(s, ilog2(s->size) / 2);
 
3018
 
 
3019
        /*
 
3020
         * cpu_partial determined the maximum number of objects kept in the
 
3021
         * per cpu partial lists of a processor.
 
3022
         *
 
3023
         * Per cpu partial lists mainly contain slabs that just have one
 
3024
         * object freed. If they are used for allocation then they can be
 
3025
         * filled up again with minimal effort. The slab will never hit the
 
3026
         * per node partial lists and therefore no locking will be required.
 
3027
         *
 
3028
         * This setting also determines
 
3029
         *
 
3030
         * A) The number of objects from per cpu partial slabs dumped to the
 
3031
         *    per node list when we reach the limit.
 
3032
         * B) The number of objects in cpu partial slabs to extract from the
 
3033
         *    per node list when we run out of per cpu objects. We only fetch 50%
 
3034
         *    to keep some capacity around for frees.
 
3035
         */
 
3036
        if (s->size >= PAGE_SIZE)
 
3037
                s->cpu_partial = 2;
 
3038
        else if (s->size >= 1024)
 
3039
                s->cpu_partial = 6;
 
3040
        else if (s->size >= 256)
 
3041
                s->cpu_partial = 13;
 
3042
        else
 
3043
                s->cpu_partial = 30;
 
3044
 
2597
3045
        s->refcount = 1;
2598
3046
#ifdef CONFIG_NUMA
2599
3047
        s->remote_node_defrag_ratio = 1000;
2652
3100
 
2653
3101
/*
2654
3102
 * Attempt to free all partial slabs on a node.
 
3103
 * This is called from kmem_cache_close(). We must be the last thread
 
3104
 * using the cache and therefore we do not need to lock anymore.
2655
3105
 */
2656
3106
static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
2657
3107
{
2658
 
        unsigned long flags;
2659
3108
        struct page *page, *h;
2660
3109
 
2661
 
        spin_lock_irqsave(&n->list_lock, flags);
2662
3110
        list_for_each_entry_safe(page, h, &n->partial, lru) {
2663
3111
                if (!page->inuse) {
2664
 
                        __remove_partial(n, page);
 
3112
                        remove_partial(n, page);
2665
3113
                        discard_slab(s, page);
2666
3114
                } else {
2667
3115
                        list_slab_objects(s, page,
2668
3116
                                "Objects remaining on kmem_cache_close()");
2669
3117
                }
2670
3118
        }
2671
 
        spin_unlock_irqrestore(&n->list_lock, flags);
2672
3119
}
2673
3120
 
2674
3121
/*
2702
3149
        s->refcount--;
2703
3150
        if (!s->refcount) {
2704
3151
                list_del(&s->list);
 
3152
                up_write(&slub_lock);
2705
3153
                if (kmem_cache_close(s)) {
2706
3154
                        printk(KERN_ERR "SLUB %s: %s called for cache that "
2707
3155
                                "still has objects.\n", s->name, __func__);
2710
3158
                if (s->flags & SLAB_DESTROY_BY_RCU)
2711
3159
                        rcu_barrier();
2712
3160
                sysfs_slab_remove(s);
2713
 
        }
2714
 
        up_write(&slub_lock);
 
3161
        } else
 
3162
                up_write(&slub_lock);
2715
3163
}
2716
3164
EXPORT_SYMBOL(kmem_cache_destroy);
2717
3165
 
2928
3376
}
2929
3377
EXPORT_SYMBOL(ksize);
2930
3378
 
 
3379
#ifdef CONFIG_SLUB_DEBUG
 
3380
bool verify_mem_not_deleted(const void *x)
 
3381
{
 
3382
        struct page *page;
 
3383
        void *object = (void *)x;
 
3384
        unsigned long flags;
 
3385
        bool rv;
 
3386
 
 
3387
        if (unlikely(ZERO_OR_NULL_PTR(x)))
 
3388
                return false;
 
3389
 
 
3390
        local_irq_save(flags);
 
3391
 
 
3392
        page = virt_to_head_page(x);
 
3393
        if (unlikely(!PageSlab(page))) {
 
3394
                /* maybe it was from stack? */
 
3395
                rv = true;
 
3396
                goto out_unlock;
 
3397
        }
 
3398
 
 
3399
        slab_lock(page);
 
3400
        if (on_freelist(page->slab, page, object)) {
 
3401
                object_err(page->slab, page, object, "Object is on free-list");
 
3402
                rv = false;
 
3403
        } else {
 
3404
                rv = true;
 
3405
        }
 
3406
        slab_unlock(page);
 
3407
 
 
3408
out_unlock:
 
3409
        local_irq_restore(flags);
 
3410
        return rv;
 
3411
}
 
3412
EXPORT_SYMBOL(verify_mem_not_deleted);
 
3413
#endif
 
3414
 
2931
3415
void kfree(const void *x)
2932
3416
{
2933
3417
        struct page *page;
2993
3477
                 * list_lock. page->inuse here is the upper limit.
2994
3478
                 */
2995
3479
                list_for_each_entry_safe(page, t, &n->partial, lru) {
2996
 
                        if (!page->inuse && slab_trylock(page)) {
2997
 
                                /*
2998
 
                                 * Must hold slab lock here because slab_free
2999
 
                                 * may have freed the last object and be
3000
 
                                 * waiting to release the slab.
3001
 
                                 */
3002
 
                                __remove_partial(n, page);
3003
 
                                slab_unlock(page);
3004
 
                                discard_slab(s, page);
3005
 
                        } else {
3006
 
                                list_move(&page->lru,
3007
 
                                slabs_by_inuse + page->inuse);
3008
 
                        }
 
3480
                        list_move(&page->lru, slabs_by_inuse + page->inuse);
 
3481
                        if (!page->inuse)
 
3482
                                n->nr_partial--;
3009
3483
                }
3010
3484
 
3011
3485
                /*
3012
3486
                 * Rebuild the partial list with the slabs filled up most
3013
3487
                 * first and the least used slabs at the end.
3014
3488
                 */
3015
 
                for (i = objects - 1; i >= 0; i--)
 
3489
                for (i = objects - 1; i > 0; i--)
3016
3490
                        list_splice(slabs_by_inuse + i, n->partial.prev);
3017
3491
 
3018
3492
                spin_unlock_irqrestore(&n->list_lock, flags);
 
3493
 
 
3494
                /* Release empty slabs */
 
3495
                list_for_each_entry_safe(page, t, slabs_by_inuse, lru)
 
3496
                        discard_slab(s, page);
3019
3497
        }
3020
3498
 
3021
3499
        kfree(slabs_by_inuse);
3588
4066
static void validate_slab_slab(struct kmem_cache *s, struct page *page,
3589
4067
                                                unsigned long *map)
3590
4068
{
3591
 
        if (slab_trylock(page)) {
3592
 
                validate_slab(s, page, map);
3593
 
                slab_unlock(page);
3594
 
        } else
3595
 
                printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
3596
 
                        s->name, page);
 
4069
        slab_lock(page);
 
4070
        validate_slab(s, page, map);
 
4071
        slab_unlock(page);
3597
4072
}
3598
4073
 
3599
4074
static int validate_slab_node(struct kmem_cache *s,
3974
4449
 
3975
4450
                for_each_possible_cpu(cpu) {
3976
4451
                        struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
 
4452
                        int node = ACCESS_ONCE(c->node);
 
4453
                        struct page *page;
3977
4454
 
3978
 
                        if (!c || c->node < 0)
 
4455
                        if (node < 0)
3979
4456
                                continue;
3980
 
 
3981
 
                        if (c->page) {
3982
 
                                        if (flags & SO_TOTAL)
3983
 
                                                x = c->page->objects;
 
4457
                        page = ACCESS_ONCE(c->page);
 
4458
                        if (page) {
 
4459
                                if (flags & SO_TOTAL)
 
4460
                                        x = page->objects;
3984
4461
                                else if (flags & SO_OBJECTS)
3985
 
                                        x = c->page->inuse;
 
4462
                                        x = page->inuse;
3986
4463
                                else
3987
4464
                                        x = 1;
3988
4465
 
3989
4466
                                total += x;
3990
 
                                nodes[c->node] += x;
3991
 
                        }
3992
 
                        per_cpu[c->node]++;
 
4467
                                nodes[node] += x;
 
4468
                        }
 
4469
                        page = c->partial;
 
4470
 
 
4471
                        if (page) {
 
4472
                                x = page->pobjects;
 
4473
                                total += x;
 
4474
                                nodes[node] += x;
 
4475
                        }
 
4476
                        per_cpu[node]++;
3993
4477
                }
3994
4478
        }
3995
4479
 
4058
4542
#endif
4059
4543
 
4060
4544
#define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
4061
 
#define to_slab(n) container_of(n, struct kmem_cache, kobj);
 
4545
#define to_slab(n) container_of(n, struct kmem_cache, kobj)
4062
4546
 
4063
4547
struct slab_attribute {
4064
4548
        struct attribute attr;
4067
4551
};
4068
4552
 
4069
4553
#define SLAB_ATTR_RO(_name) \
4070
 
        static struct slab_attribute _name##_attr = __ATTR_RO(_name)
 
4554
        static struct slab_attribute _name##_attr = \
 
4555
        __ATTR(_name, 0400, _name##_show, NULL)
4071
4556
 
4072
4557
#define SLAB_ATTR(_name) \
4073
4558
        static struct slab_attribute _name##_attr =  \
4074
 
        __ATTR(_name, 0644, _name##_show, _name##_store)
 
4559
        __ATTR(_name, 0600, _name##_show, _name##_store)
4075
4560
 
4076
4561
static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
4077
4562
{
4140
4625
}
4141
4626
SLAB_ATTR(min_partial);
4142
4627
 
 
4628
static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
 
4629
{
 
4630
        return sprintf(buf, "%u\n", s->cpu_partial);
 
4631
}
 
4632
 
 
4633
static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
 
4634
                                 size_t length)
 
4635
{
 
4636
        unsigned long objects;
 
4637
        int err;
 
4638
 
 
4639
        err = strict_strtoul(buf, 10, &objects);
 
4640
        if (err)
 
4641
                return err;
 
4642
 
 
4643
        s->cpu_partial = objects;
 
4644
        flush_all(s);
 
4645
        return length;
 
4646
}
 
4647
SLAB_ATTR(cpu_partial);
 
4648
 
4143
4649
static ssize_t ctor_show(struct kmem_cache *s, char *buf)
4144
4650
{
4145
4651
        if (!s->ctor)
4178
4684
}
4179
4685
SLAB_ATTR_RO(objects_partial);
4180
4686
 
 
4687
static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
 
4688
{
 
4689
        int objects = 0;
 
4690
        int pages = 0;
 
4691
        int cpu;
 
4692
        int len;
 
4693
 
 
4694
        for_each_online_cpu(cpu) {
 
4695
                struct page *page = per_cpu_ptr(s->cpu_slab, cpu)->partial;
 
4696
 
 
4697
                if (page) {
 
4698
                        pages += page->pages;
 
4699
                        objects += page->pobjects;
 
4700
                }
 
4701
        }
 
4702
 
 
4703
        len = sprintf(buf, "%d(%d)", objects, pages);
 
4704
 
 
4705
#ifdef CONFIG_SMP
 
4706
        for_each_online_cpu(cpu) {
 
4707
                struct page *page = per_cpu_ptr(s->cpu_slab, cpu) ->partial;
 
4708
 
 
4709
                if (page && len < PAGE_SIZE - 20)
 
4710
                        len += sprintf(buf + len, " C%d=%d(%d)", cpu,
 
4711
                                page->pobjects, page->pages);
 
4712
        }
 
4713
#endif
 
4714
        return len + sprintf(buf + len, "\n");
 
4715
}
 
4716
SLAB_ATTR_RO(slabs_cpu_partial);
 
4717
 
4181
4718
static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
4182
4719
{
4183
4720
        return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
4241
4778
                                const char *buf, size_t length)
4242
4779
{
4243
4780
        s->flags &= ~SLAB_DEBUG_FREE;
4244
 
        if (buf[0] == '1')
 
4781
        if (buf[0] == '1') {
 
4782
                s->flags &= ~__CMPXCHG_DOUBLE;
4245
4783
                s->flags |= SLAB_DEBUG_FREE;
 
4784
        }
4246
4785
        return length;
4247
4786
}
4248
4787
SLAB_ATTR(sanity_checks);
4256
4795
                                                        size_t length)
4257
4796
{
4258
4797
        s->flags &= ~SLAB_TRACE;
4259
 
        if (buf[0] == '1')
 
4798
        if (buf[0] == '1') {
 
4799
                s->flags &= ~__CMPXCHG_DOUBLE;
4260
4800
                s->flags |= SLAB_TRACE;
 
4801
        }
4261
4802
        return length;
4262
4803
}
4263
4804
SLAB_ATTR(trace);
4274
4815
                return -EBUSY;
4275
4816
 
4276
4817
        s->flags &= ~SLAB_RED_ZONE;
4277
 
        if (buf[0] == '1')
 
4818
        if (buf[0] == '1') {
 
4819
                s->flags &= ~__CMPXCHG_DOUBLE;
4278
4820
                s->flags |= SLAB_RED_ZONE;
 
4821
        }
4279
4822
        calculate_sizes(s, -1);
4280
4823
        return length;
4281
4824
}
4293
4836
                return -EBUSY;
4294
4837
 
4295
4838
        s->flags &= ~SLAB_POISON;
4296
 
        if (buf[0] == '1')
 
4839
        if (buf[0] == '1') {
 
4840
                s->flags &= ~__CMPXCHG_DOUBLE;
4297
4841
                s->flags |= SLAB_POISON;
 
4842
        }
4298
4843
        calculate_sizes(s, -1);
4299
4844
        return length;
4300
4845
}
4312
4857
                return -EBUSY;
4313
4858
 
4314
4859
        s->flags &= ~SLAB_STORE_USER;
4315
 
        if (buf[0] == '1')
 
4860
        if (buf[0] == '1') {
 
4861
                s->flags &= ~__CMPXCHG_DOUBLE;
4316
4862
                s->flags |= SLAB_STORE_USER;
 
4863
        }
4317
4864
        calculate_sizes(s, -1);
4318
4865
        return length;
4319
4866
}
4478
5025
STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
4479
5026
STAT_ATTR(ALLOC_SLAB, alloc_slab);
4480
5027
STAT_ATTR(ALLOC_REFILL, alloc_refill);
 
5028
STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
4481
5029
STAT_ATTR(FREE_SLAB, free_slab);
4482
5030
STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
4483
5031
STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
4485
5033
STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
4486
5034
STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
4487
5035
STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
 
5036
STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
4488
5037
STAT_ATTR(ORDER_FALLBACK, order_fallback);
 
5038
STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
 
5039
STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
 
5040
STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
 
5041
STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
4489
5042
#endif
4490
5043
 
4491
5044
static struct attribute *slab_attrs[] = {
4494
5047
        &objs_per_slab_attr.attr,
4495
5048
        &order_attr.attr,
4496
5049
        &min_partial_attr.attr,
 
5050
        &cpu_partial_attr.attr,
4497
5051
        &objects_attr.attr,
4498
5052
        &objects_partial_attr.attr,
4499
5053
        &partial_attr.attr,
4506
5060
        &destroy_by_rcu_attr.attr,
4507
5061
        &shrink_attr.attr,
4508
5062
        &reserved_attr.attr,
 
5063
        &slabs_cpu_partial_attr.attr,
4509
5064
#ifdef CONFIG_SLUB_DEBUG
4510
5065
        &total_objects_attr.attr,
4511
5066
        &slabs_attr.attr,
4535
5090
        &alloc_from_partial_attr.attr,
4536
5091
        &alloc_slab_attr.attr,
4537
5092
        &alloc_refill_attr.attr,
 
5093
        &alloc_node_mismatch_attr.attr,
4538
5094
        &free_slab_attr.attr,
4539
5095
        &cpuslab_flush_attr.attr,
4540
5096
        &deactivate_full_attr.attr,
4542
5098
        &deactivate_to_head_attr.attr,
4543
5099
        &deactivate_to_tail_attr.attr,
4544
5100
        &deactivate_remote_frees_attr.attr,
 
5101
        &deactivate_bypass_attr.attr,
4545
5102
        &order_fallback_attr.attr,
 
5103
        &cmpxchg_double_fail_attr.attr,
 
5104
        &cmpxchg_double_cpu_fail_attr.attr,
 
5105
        &cpu_partial_alloc_attr.attr,
 
5106
        &cpu_partial_free_attr.attr,
4546
5107
#endif
4547
5108
#ifdef CONFIG_FAILSLAB
4548
5109
        &failslab_attr.attr,
4894
5455
 
4895
5456
static int __init slab_proc_init(void)
4896
5457
{
4897
 
        proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
 
5458
        proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
4898
5459
        return 0;
4899
5460
}
4900
5461
module_init(slab_proc_init);