forked from hardkernel/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslqb.c
executable file
·3816 lines (3198 loc) · 84.8 KB
/
slqb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SLQB: A slab allocator that focuses on per-CPU scaling, and good performance
* with order-0 allocations. Fastpaths emphasis is placed on local allocaiton
* and freeing, but with a secondary goal of good remote freeing (freeing on
* another CPU from that which allocated).
*
* Using ideas and code from mm/slab.c, mm/slob.c, and mm/slub.c.
*/
#include <linux/mm.h>
#include <linux/swap.h> /* struct reclaim_state */
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include <linux/cpu.h>
#include <linux/cpuset.h>
#include <linux/mempolicy.h>
#include <linux/ctype.h>
#include <linux/kallsyms.h>
#include <linux/memory.h>
#include <linux/fault-inject.h>
/*
* TODO
* - fix up releasing of offlined data structures. Not a big deal because
* they don't get cumulatively leaked with successive online/offline cycles
* - allow OOM conditions to flush back per-CPU pages to common lists to be
* reused by other CPUs.
* - investiage performance with memoryless nodes. Perhaps CPUs can be given
* a default closest home node via which it can use fastpath functions.
* Perhaps it is not a big problem.
*/
/*
* slqb_page overloads struct page, and is used to manage some slob allocation
* aspects, however to avoid the horrible mess in include/linux/mm_types.h,
* we'll just define our own struct slqb_page type variant here.
*/
struct slqb_page {
union {
struct {
unsigned long flags; /* mandatory */
atomic_t _count; /* mandatory */
unsigned int inuse; /* Nr of objects */
struct kmem_cache_list *list; /* Pointer to list */
void **freelist; /* LIFO freelist */
union {
struct list_head lru; /* misc. list */
struct rcu_head rcu_head; /* for rcu freeing */
};
};
struct page page;
};
};
static inline void struct_slqb_page_wrong_size(void)
{ BUILD_BUG_ON(sizeof(struct slqb_page) != sizeof(struct page)); }
#define PG_SLQB_BIT (1 << PG_slab)
/*
* slqb_min_order: minimum allocation order for slabs
*/
static int slqb_min_order;
/*
* slqb_min_objects: minimum number of objects per slab. Increasing this
* will increase the allocation order for slabs with larger objects
*/
static int slqb_min_objects = 1;
#ifdef CONFIG_NUMA
static inline int slab_numa(struct kmem_cache *s)
{
return s->flags & SLAB_NUMA;
}
#else
static inline int slab_numa(struct kmem_cache *s)
{
return 0;
}
#endif
static inline int slab_hiwater(struct kmem_cache *s)
{
return s->hiwater;
}
static inline int slab_freebatch(struct kmem_cache *s)
{
return s->freebatch;
}
/*
* Lock order:
* kmem_cache_node->list_lock
* kmem_cache_remote_free->lock
*
* Data structures:
* SLQB is primarily per-cpu. For each kmem_cache, each CPU has:
*
* - A LIFO list of node-local objects. Allocation and freeing of node local
* objects goes first to this list.
*
* - 2 Lists of slab pages, free and partial pages. If an allocation misses
* the object list, it tries from the partial list, then the free list.
* After freeing an object to the object list, if it is over a watermark,
* some objects are freed back to pages. If an allocation misses these lists,
* a new slab page is allocated from the page allocator. If the free list
* reaches a watermark, some of its pages are returned to the page allocator.
*
* - A remote free queue, where objects freed that did not come from the local
* node are queued to. When this reaches a watermark, the objects are
* flushed.
*
* - A remotely freed queue, where objects allocated from this CPU are flushed
* to from other CPUs' remote free queues. kmem_cache_remote_free->lock is
* used to protect access to this queue.
*
* When the remotely freed queue reaches a watermark, a flag is set to tell
* the owner CPU to check it. The owner CPU will then check the queue on the
* next allocation that misses the object list. It will move all objects from
* this list onto the object list and then allocate one.
*
* This system of remote queueing is intended to reduce lock and remote
* cacheline acquisitions, and give a cooling off period for remotely freed
* objects before they are re-allocated.
*
* node specific allocations from somewhere other than the local node are
* handled by a per-node list which is the same as the above per-CPU data
* structures except for the following differences:
*
* - kmem_cache_node->list_lock is used to protect access for multiple CPUs to
* allocate from a given node.
*
* - There is no remote free queue. Nodes don't free objects, CPUs do.
*/
static inline void slqb_stat_inc(struct kmem_cache_list *list,
enum stat_item si)
{
#ifdef CONFIG_SLQB_STATS
list->stats[si]++;
#endif
}
static inline void slqb_stat_add(struct kmem_cache_list *list,
enum stat_item si, unsigned long nr)
{
#ifdef CONFIG_SLQB_STATS
list->stats[si] += nr;
#endif
}
static inline int slqb_page_to_nid(struct slqb_page *page)
{
return page_to_nid(&page->page);
}
static inline void *slqb_page_address(struct slqb_page *page)
{
return page_address(&page->page);
}
static inline struct zone *slqb_page_zone(struct slqb_page *page)
{
return page_zone(&page->page);
}
static inline int virt_to_nid(const void *addr)
{
return page_to_nid(virt_to_page(addr));
}
static inline struct slqb_page *virt_to_head_slqb_page(const void *addr)
{
struct page *p;
p = virt_to_head_page(addr);
return (struct slqb_page *)p;
}
static inline void __free_slqb_pages(struct slqb_page *page, unsigned int order,
int pages)
{
struct page *p = &page->page;
reset_page_mapcount(p);
p->mapping = NULL;
VM_BUG_ON(!(p->flags & PG_SLQB_BIT));
p->flags &= ~PG_SLQB_BIT;
if (current->reclaim_state)
current->reclaim_state->reclaimed_slab += pages;
__free_pages(p, order);
}
#ifdef CONFIG_SLQB_DEBUG
static inline int slab_debug(struct kmem_cache *s)
{
return s->flags &
(SLAB_DEBUG_FREE |
SLAB_RED_ZONE |
SLAB_POISON |
SLAB_STORE_USER |
SLAB_TRACE);
}
static inline int slab_poison(struct kmem_cache *s)
{
return s->flags & SLAB_POISON;
}
#else
static inline int slab_debug(struct kmem_cache *s)
{
return 0;
}
static inline int slab_poison(struct kmem_cache *s)
{
return 0;
}
#endif
#define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
SLAB_POISON | SLAB_STORE_USER)
/* Internal SLQB flags */
#define __OBJECT_POISON 0x80000000 /* Poison object */
/* Not all arches define cache_line_size */
#ifndef cache_line_size
#define cache_line_size() L1_CACHE_BYTES
#endif
#ifdef CONFIG_SMP
static struct notifier_block slab_notifier;
#endif
/*
* slqb_lock protects slab_caches list and serialises hotplug operations.
* hotplug operations take lock for write, other operations can hold off
* hotplug by taking it for read (or write).
*/
static DECLARE_RWSEM(slqb_lock);
/*
* A list of all slab caches on the system
*/
static LIST_HEAD(slab_caches);
/*
* Tracking user of a slab.
*/
struct track {
unsigned long addr; /* Called from address */
int cpu; /* Was running on cpu */
int pid; /* Pid context */
unsigned long when; /* When did the operation occur */
};
enum track_item { TRACK_ALLOC, TRACK_FREE };
static struct kmem_cache kmem_cache_cache;
#ifdef CONFIG_SLQB_SYSFS
static int sysfs_slab_add(struct kmem_cache *s);
static void sysfs_slab_remove(struct kmem_cache *s);
#else
static inline int sysfs_slab_add(struct kmem_cache *s)
{
return 0;
}
static inline void sysfs_slab_remove(struct kmem_cache *s)
{
kmem_cache_free(&kmem_cache_cache, s);
}
#endif
/********************************************************************
* Core slab cache functions
*******************************************************************/
static int __slab_is_available __read_mostly;
int slab_is_available(void)
{
return __slab_is_available;
}
static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
{
#ifdef CONFIG_SMP
VM_BUG_ON(!s->cpu_slab[cpu]);
return s->cpu_slab[cpu];
#else
return &s->cpu_slab;
#endif
}
static inline int check_valid_pointer(struct kmem_cache *s,
struct slqb_page *page, const void *object)
{
void *base;
base = slqb_page_address(page);
if (object < base || object >= base + s->objects * s->size ||
(object - base) % s->size) {
return 0;
}
return 1;
}
static inline void *get_freepointer(struct kmem_cache *s, void *object)
{
return *(void **)(object + s->offset);
}
static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
{
*(void **)(object + s->offset) = fp;
}
/* Loop over all objects in a slab */
#define for_each_object(__p, __s, __addr) \
for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
__p += (__s)->size)
/* Scan freelist */
#define for_each_free_object(__p, __s, __free) \
for (__p = (__free); (__p) != NULL; __p = get_freepointer((__s),\
__p))
#ifdef CONFIG_SLQB_DEBUG
/*
* Debug settings:
*/
#ifdef CONFIG_SLQB_DEBUG_ON
static int slqb_debug __read_mostly = DEBUG_DEFAULT_FLAGS;
#else
static int slqb_debug __read_mostly;
#endif
static char *slqb_debug_slabs;
/*
* Object debugging
*/
static void print_section(char *text, u8 *addr, unsigned int length)
{
int i, offset;
int newline = 1;
char ascii[17];
ascii[16] = 0;
for (i = 0; i < length; i++) {
if (newline) {
printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
newline = 0;
}
printk(KERN_CONT " %02x", addr[i]);
offset = i % 16;
ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
if (offset == 15) {
printk(KERN_CONT " %s\n", ascii);
newline = 1;
}
}
if (!newline) {
i %= 16;
while (i < 16) {
printk(KERN_CONT " ");
ascii[i] = ' ';
i++;
}
printk(KERN_CONT " %s\n", ascii);
}
}
static struct track *get_track(struct kmem_cache *s, void *object,
enum track_item alloc)
{
struct track *p;
if (s->offset)
p = object + s->offset + sizeof(void *);
else
p = object + s->inuse;
return p + alloc;
}
static void set_track(struct kmem_cache *s, void *object,
enum track_item alloc, unsigned long addr)
{
struct track *p;
if (s->offset)
p = object + s->offset + sizeof(void *);
else
p = object + s->inuse;
p += alloc;
if (addr) {
p->addr = addr;
p->cpu = raw_smp_processor_id();
p->pid = current ? current->pid : -1;
p->when = jiffies;
} else
memset(p, 0, sizeof(struct track));
}
static void init_tracking(struct kmem_cache *s, void *object)
{
if (!(s->flags & SLAB_STORE_USER))
return;
set_track(s, object, TRACK_FREE, 0UL);
set_track(s, object, TRACK_ALLOC, 0UL);
}
static void print_track(const char *s, struct track *t)
{
if (!t->addr)
return;
printk(KERN_ERR "INFO: %s in ", s);
__print_symbol("%s", (unsigned long)t->addr);
printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
}
static void print_tracking(struct kmem_cache *s, void *object)
{
if (!(s->flags & SLAB_STORE_USER))
return;
print_track("Allocated", get_track(s, object, TRACK_ALLOC));
print_track("Freed", get_track(s, object, TRACK_FREE));
}
static void print_page_info(struct slqb_page *page)
{
printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
page, page->inuse, page->freelist, page->flags);
}
#define MAX_ERR_STR 100
static void slab_bug(struct kmem_cache *s, char *fmt, ...)
{
va_list args;
char buf[MAX_ERR_STR];
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
printk(KERN_ERR "========================================"
"=====================================\n");
printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
printk(KERN_ERR "----------------------------------------"
"-------------------------------------\n\n");
}
static void slab_fix(struct kmem_cache *s, char *fmt, ...)
{
va_list args;
char buf[100];
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
}
static void print_trailer(struct kmem_cache *s, struct slqb_page *page, u8 *p)
{
unsigned int off; /* Offset of last byte */
u8 *addr = slqb_page_address(page);
print_tracking(s, p);
print_page_info(page);
printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
p, p - addr, get_freepointer(s, p));
if (p > addr + 16)
print_section("Bytes b4", p - 16, 16);
print_section("Object", p, min(s->objsize, 128));
if (s->flags & SLAB_RED_ZONE)
print_section("Redzone", p + s->objsize, s->inuse - s->objsize);
if (s->offset)
off = s->offset + sizeof(void *);
else
off = s->inuse;
if (s->flags & SLAB_STORE_USER)
off += 2 * sizeof(struct track);
if (off != s->size) {
/* Beginning of the filler is the free pointer */
print_section("Padding", p + off, s->size - off);
}
dump_stack();
}
static void object_err(struct kmem_cache *s, struct slqb_page *page,
u8 *object, char *reason)
{
slab_bug(s, reason);
print_trailer(s, page, object);
}
static void slab_err(struct kmem_cache *s, struct slqb_page *page,
char *fmt, ...)
{
slab_bug(s, fmt);
print_page_info(page);
dump_stack();
}
static void init_object(struct kmem_cache *s, void *object, int active)
{
u8 *p = object;
if (s->flags & __OBJECT_POISON) {
memset(p, POISON_FREE, s->objsize - 1);
p[s->objsize - 1] = POISON_END;
}
if (s->flags & SLAB_RED_ZONE) {
memset(p + s->objsize,
active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
s->inuse - s->objsize);
}
}
static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
{
while (bytes) {
if (*start != (u8)value)
return start;
start++;
bytes--;
}
return NULL;
}
static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
void *from, void *to)
{
slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
memset(from, data, to - from);
}
static int check_bytes_and_report(struct kmem_cache *s, struct slqb_page *page,
u8 *object, char *what,
u8 *start, unsigned int value, unsigned int bytes)
{
u8 *fault;
u8 *end;
fault = check_bytes(start, value, bytes);
if (!fault)
return 1;
end = start + bytes;
while (end > fault && end[-1] == value)
end--;
slab_bug(s, "%s overwritten", what);
printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
fault, end - 1, fault[0], value);
print_trailer(s, page, object);
restore_bytes(s, what, value, fault, end);
return 0;
}
/*
* Object layout:
*
* object address
* Bytes of the object to be managed.
* If the freepointer may overlay the object then the free
* pointer is the first word of the object.
*
* Poisoning uses 0x6b (POISON_FREE) and the last byte is
* 0xa5 (POISON_END)
*
* object + s->objsize
* Padding to reach word boundary. This is also used for Redzoning.
* Padding is extended by another word if Redzoning is enabled and
* objsize == inuse.
*
* We fill with 0xbb (RED_INACTIVE) for inactive objects and with
* 0xcc (RED_ACTIVE) for objects in use.
*
* object + s->inuse
* Meta data starts here.
*
* A. Free pointer (if we cannot overwrite object on free)
* B. Tracking data for SLAB_STORE_USER
* C. Padding to reach required alignment boundary or at mininum
* one word if debuggin is on to be able to detect writes
* before the word boundary.
*
* Padding is done using 0x5a (POISON_INUSE)
*
* object + s->size
* Nothing is used beyond s->size.
*/
static int check_pad_bytes(struct kmem_cache *s, struct slqb_page *page, u8 *p)
{
unsigned long off = s->inuse; /* The end of info */
if (s->offset) {
/* Freepointer is placed after the object. */
off += sizeof(void *);
}
if (s->flags & SLAB_STORE_USER) {
/* We also have user information there */
off += 2 * sizeof(struct track);
}
if (s->size == off)
return 1;
return check_bytes_and_report(s, page, p, "Object padding",
p + off, POISON_INUSE, s->size - off);
}
static int slab_pad_check(struct kmem_cache *s, struct slqb_page *page)
{
u8 *start;
u8 *fault;
u8 *end;
int length;
int remainder;
if (!(s->flags & SLAB_POISON))
return 1;
start = slqb_page_address(page);
end = start + (PAGE_SIZE << s->order);
length = s->objects * s->size;
remainder = end - (start + length);
if (!remainder)
return 1;
fault = check_bytes(start + length, POISON_INUSE, remainder);
if (!fault)
return 1;
while (end > fault && end[-1] == POISON_INUSE)
end--;
slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
print_section("Padding", start, length);
restore_bytes(s, "slab padding", POISON_INUSE, start, end);
return 0;
}
static int check_object(struct kmem_cache *s, struct slqb_page *page,
void *object, int active)
{
u8 *p = object;
u8 *endobject = object + s->objsize;
if (s->flags & SLAB_RED_ZONE) {
unsigned int red =
active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
if (!check_bytes_and_report(s, page, object, "Redzone",
endobject, red, s->inuse - s->objsize))
return 0;
} else {
if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
check_bytes_and_report(s, page, p, "Alignment padding",
endobject, POISON_INUSE, s->inuse - s->objsize);
}
}
if (s->flags & SLAB_POISON) {
if (!active && (s->flags & __OBJECT_POISON)) {
if (!check_bytes_and_report(s, page, p, "Poison", p,
POISON_FREE, s->objsize - 1))
return 0;
if (!check_bytes_and_report(s, page, p, "Poison",
p + s->objsize - 1, POISON_END, 1))
return 0;
}
/*
* check_pad_bytes cleans up on its own.
*/
check_pad_bytes(s, page, p);
}
return 1;
}
static int check_slab(struct kmem_cache *s, struct slqb_page *page)
{
if (!(page->flags & PG_SLQB_BIT)) {
slab_err(s, page, "Not a valid slab page");
return 0;
}
if (page->inuse == 0) {
slab_err(s, page, "inuse before free / after alloc", s->name);
return 0;
}
if (page->inuse > s->objects) {
slab_err(s, page, "inuse %u > max %u",
s->name, page->inuse, s->objects);
return 0;
}
/* Slab_pad_check fixes things up after itself */
slab_pad_check(s, page);
return 1;
}
static void trace(struct kmem_cache *s, struct slqb_page *page,
void *object, int alloc)
{
if (s->flags & SLAB_TRACE) {
printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
s->name,
alloc ? "alloc" : "free",
object, page->inuse,
page->freelist);
if (!alloc)
print_section("Object", (void *)object, s->objsize);
dump_stack();
}
}
static void setup_object_debug(struct kmem_cache *s, struct slqb_page *page,
void *object)
{
if (!slab_debug(s))
return;
if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
return;
init_object(s, object, 0);
init_tracking(s, object);
}
static int alloc_debug_processing(struct kmem_cache *s,
void *object, unsigned long addr)
{
struct slqb_page *page;
page = virt_to_head_slqb_page(object);
if (!check_slab(s, page))
goto bad;
if (!check_valid_pointer(s, page, object)) {
object_err(s, page, object, "Freelist Pointer check fails");
goto bad;
}
if (object && !check_object(s, page, object, 0))
goto bad;
/* Success perform special debug activities for allocs */
if (s->flags & SLAB_STORE_USER)
set_track(s, object, TRACK_ALLOC, addr);
trace(s, page, object, 1);
init_object(s, object, 1);
return 1;
bad:
return 0;
}
static int free_debug_processing(struct kmem_cache *s,
void *object, unsigned long addr)
{
struct slqb_page *page;
page = virt_to_head_slqb_page(object);
if (!check_slab(s, page))
goto fail;
if (!check_valid_pointer(s, page, object)) {
slab_err(s, page, "Invalid object pointer 0x%p", object);
goto fail;
}
if (!check_object(s, page, object, 1))
return 0;
/* Special debug activities for freeing objects */
if (s->flags & SLAB_STORE_USER)
set_track(s, object, TRACK_FREE, addr);
trace(s, page, object, 0);
init_object(s, object, 0);
return 1;
fail:
slab_fix(s, "Object at 0x%p not freed", object);
return 0;
}
static int __init setup_slqb_debug(char *str)
{
slqb_debug = DEBUG_DEFAULT_FLAGS;
if (*str++ != '=' || !*str) {
/*
* No options specified. Switch on full debugging.
*/
goto out;
}
if (*str == ',') {
/*
* No options but restriction on slabs. This means full
* debugging for slabs matching a pattern.
*/
goto check_slabs;
}
slqb_debug = 0;
if (*str == '-') {
/*
* Switch off all debugging measures.
*/
goto out;
}
/*
* Determine which debug features should be switched on
*/
for (; *str && *str != ','; str++) {
switch (tolower(*str)) {
case 'f':
slqb_debug |= SLAB_DEBUG_FREE;
break;
case 'z':
slqb_debug |= SLAB_RED_ZONE;
break;
case 'p':
slqb_debug |= SLAB_POISON;
break;
case 'u':
slqb_debug |= SLAB_STORE_USER;
break;
case 't':
slqb_debug |= SLAB_TRACE;
break;
case 'a':
slqb_debug |= SLAB_FAILSLAB;
break;
default:
printk(KERN_ERR "slqb_debug option '%c' "
"unknown. skipped\n", *str);
}
}
check_slabs:
if (*str == ',')
slqb_debug_slabs = str + 1;
out:
return 1;
}
__setup("slqb_debug", setup_slqb_debug);
static int __init setup_slqb_min_order(char *str)
{
get_option(&str, &slqb_min_order);
slqb_min_order = min(slqb_min_order, MAX_ORDER - 1);
return 1;
}
__setup("slqb_min_order=", setup_slqb_min_order);
static int __init setup_slqb_min_objects(char *str)
{
get_option(&str, &slqb_min_objects);
return 1;
}
__setup("slqb_min_objects=", setup_slqb_min_objects);
static unsigned long kmem_cache_flags(unsigned long objsize,
unsigned long flags, const char *name,
void (*ctor)(void *))
{
/*
* Enable debugging if selected on the kernel commandline.
*/
if (slqb_debug && (!slqb_debug_slabs ||
strncmp(slqb_debug_slabs, name,
strlen(slqb_debug_slabs)) == 0))
flags |= slqb_debug;
if (num_possible_nodes() > 1)
flags |= SLAB_NUMA;
return flags;
}
#else
static inline void setup_object_debug(struct kmem_cache *s,
struct slqb_page *page, void *object)
{
}
static inline int alloc_debug_processing(struct kmem_cache *s,
void *object, unsigned long addr)
{
return 0;
}
static inline int free_debug_processing(struct kmem_cache *s,
void *object, unsigned long addr)
{
return 0;
}
static inline int slab_pad_check(struct kmem_cache *s, struct slqb_page *page)
{
return 1;
}
static inline int check_object(struct kmem_cache *s, struct slqb_page *page,
void *object, int active)
{
return 1;
}
static inline void add_full(struct kmem_cache_node *n, struct slqb_page *page)
{
}
static inline unsigned long kmem_cache_flags(unsigned long objsize,
unsigned long flags, const char *name, void (*ctor)(void *))
{
if (num_possible_nodes() > 1)
flags |= SLAB_NUMA;
return flags;
}
static const int slqb_debug;
#endif
/*
* allocate a new slab (return its corresponding struct slqb_page)
*/
static struct slqb_page *allocate_slab(struct kmem_cache *s,
gfp_t flags, int node)
{
struct slqb_page *page;
int pages = 1 << s->order;
flags |= s->allocflags;
page = (struct slqb_page *)alloc_pages_node(node, flags, s->order);
if (!page)
return NULL;
mod_zone_page_state(slqb_page_zone(page),
(s->flags & SLAB_RECLAIM_ACCOUNT) ?
NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
pages);
return page;
}
/*
* Called once for each object on a new slab page
*/
static void setup_object(struct kmem_cache *s,
struct slqb_page *page, void *object)
{
setup_object_debug(s, page, object);
if (unlikely(s->ctor))
s->ctor(object);
}
/*
* Allocate a new slab, set up its object list.
*/
static struct slqb_page *new_slab_page(struct kmem_cache *s,
gfp_t flags, int node, unsigned int colour)
{
struct slqb_page *page;
void *start;