-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0001-Add-UKSM.patch
6969 lines (6926 loc) · 175 KB
/
0001-Add-UKSM.patch
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
From 3b7936e189c614c6914557258163e7afbca234f9 Mon Sep 17 00:00:00 2001
From: Piotr Rogowski <[email protected]>
Date: Fri, 26 Mar 2021 08:28:31 +0100
Subject: [PATCH] Add UKSM
---
Documentation/vm/uksm.txt | 61 +
fs/exec.c | 1 +
fs/proc/meminfo.c | 4 +
include/linux/ksm.h | 43 +-
include/linux/mm_types.h | 3 +
include/linux/mmzone.h | 3 +
include/linux/pgtable.h | 17 +-
include/linux/sradix-tree.h | 77 +
include/linux/uksm.h | 149 +
kernel/fork.c | 2 +-
lib/Makefile | 2 +-
lib/sradix-tree.c | 476 +++
mm/Kconfig | 26 +
mm/Makefile | 3 +-
mm/ksm.c | 11 -
mm/memory.c | 33 +-
mm/mmap.c | 37 +
mm/uksm.c | 5614 +++++++++++++++++++++++++++++++++++
mm/vmstat.c | 3 +
19 files changed, 6539 insertions(+), 26 deletions(-)
create mode 100644 Documentation/vm/uksm.txt
create mode 100644 include/linux/sradix-tree.h
create mode 100644 include/linux/uksm.h
create mode 100644 lib/sradix-tree.c
create mode 100644 mm/uksm.c
diff --git a/Documentation/vm/uksm.txt b/Documentation/vm/uksm.txt
new file mode 100644
index 000000000000..be19a3127001
--- /dev/null
+++ b/Documentation/vm/uksm.txt
@@ -0,0 +1,61 @@
+The Ultra Kernel Samepage Merging feature
+----------------------------------------------
+/*
+ * Ultra KSM. Copyright (C) 2011-2012 Nai Xia
+ *
+ * This is an improvement upon KSM. Some basic data structures and routines
+ * are borrowed from ksm.c .
+ *
+ * Its new features:
+ * 1. Full system scan:
+ * It automatically scans all user processes' anonymous VMAs. Kernel-user
+ * interaction to submit a memory area to KSM is no longer needed.
+ *
+ * 2. Rich area detection:
+ * It automatically detects rich areas containing abundant duplicated
+ * pages based. Rich areas are given a full scan speed. Poor areas are
+ * sampled at a reasonable speed with very low CPU consumption.
+ *
+ * 3. Ultra Per-page scan speed improvement:
+ * A new hash algorithm is proposed. As a result, on a machine with
+ * Core(TM)2 Quad Q9300 CPU in 32-bit mode and 800MHZ DDR2 main memory, it
+ * can scan memory areas that does not contain duplicated pages at speed of
+ * 627MB/sec ~ 2445MB/sec and can merge duplicated areas at speed of
+ * 477MB/sec ~ 923MB/sec.
+ *
+ * 4. Thrashing area avoidance:
+ * Thrashing area(an VMA that has frequent Ksm page break-out) can be
+ * filtered out. My benchmark shows it's more efficient than KSM's per-page
+ * hash value based volatile page detection.
+ *
+ *
+ * 5. Misc changes upon KSM:
+ * * It has a fully x86-opitmized memcmp dedicated for 4-byte-aligned page
+ * comparison. It's much faster than default C version on x86.
+ * * rmap_item now has an struct *page member to loosely cache a
+ * address-->page mapping, which reduces too much time-costly
+ * follow_page().
+ * * The VMA creation/exit procedures are hooked to let the Ultra KSM know.
+ * * try_to_merge_two_pages() now can revert a pte if it fails. No break_
+ * ksm is needed for this case.
+ *
+ * 6. Full Zero Page consideration(contributed by Figo Zhang)
+ * Now uksmd consider full zero pages as special pages and merge them to an
+ * special unswappable uksm zero page.
+ */
+
+ChangeLog:
+
+2012-05-05 The creation of this Doc
+2012-05-08 UKSM 0.1.1.1 libc crash bug fix, api clean up, doc clean up.
+2012-05-28 UKSM 0.1.1.2 bug fix release
+2012-06-26 UKSM 0.1.2-beta1 first beta release for 0.1.2
+2012-07-2 UKSM 0.1.2-beta2
+2012-07-10 UKSM 0.1.2-beta3
+2012-07-26 UKSM 0.1.2 Fine grained speed control, more scan optimization.
+2012-10-13 UKSM 0.1.2.1 Bug fixes.
+2012-12-31 UKSM 0.1.2.2 Minor bug fixes.
+2014-07-02 UKSM 0.1.2.3 Fix a " __this_cpu_read() in preemptible bug".
+2015-04-22 UKSM 0.1.2.4 Fix a race condition that can sometimes trigger anonying warnings.
+2016-09-10 UKSM 0.1.2.5 Fix a bug in dedup ratio calculation.
+2017-02-26 UKSM 0.1.2.6 Fix a bug in hugetlbpage handling and a race bug with page migration.
diff --git a/fs/exec.c b/fs/exec.c
index 5d4d52039105..86f9f65262a6 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -65,6 +65,7 @@
#include <linux/vmalloc.h>
#include <linux/io_uring.h>
#include <linux/syscall_user_dispatch.h>
+#include <linux/ksm.h>
#include <linux/uaccess.h>
#include <asm/mmu_context.h>
diff --git a/fs/proc/meminfo.c b/fs/proc/meminfo.c
index d6fc74619625..e173b259fb52 100644
--- a/fs/proc/meminfo.c
+++ b/fs/proc/meminfo.c
@@ -108,6 +108,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
#endif
show_val_kb(m, "PageTables: ",
global_node_page_state(NR_PAGETABLE));
+#ifdef CONFIG_UKSM
+ show_val_kb(m, "KsmZeroPages: ",
+ global_zone_page_state(NR_UKSM_ZERO_PAGES));
+#endif
show_val_kb(m, "NFS_Unstable: ", 0);
show_val_kb(m, "Bounce: ",
diff --git a/include/linux/ksm.h b/include/linux/ksm.h
index 161e8164abcf..f0dbdf3c986a 100644
--- a/include/linux/ksm.h
+++ b/include/linux/ksm.h
@@ -21,20 +21,16 @@ struct mem_cgroup;
#ifdef CONFIG_KSM
int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
unsigned long end, int advice, unsigned long *vm_flags);
-int __ksm_enter(struct mm_struct *mm);
-void __ksm_exit(struct mm_struct *mm);
-static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
+static inline struct stable_node *page_stable_node(struct page *page)
{
- if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
- return __ksm_enter(mm);
- return 0;
+ return PageKsm(page) ? page_rmapping(page) : NULL;
}
-static inline void ksm_exit(struct mm_struct *mm)
+static inline void set_page_stable_node(struct page *page,
+ struct stable_node *stable_node)
{
- if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
- __ksm_exit(mm);
+ page->mapping = (void *)((unsigned long)stable_node | PAGE_MAPPING_KSM);
}
/*
@@ -54,6 +50,33 @@ struct page *ksm_might_need_to_copy(struct page *page,
void rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc);
void ksm_migrate_page(struct page *newpage, struct page *oldpage);
+#ifdef CONFIG_KSM_LEGACY
+int __ksm_enter(struct mm_struct *mm);
+void __ksm_exit(struct mm_struct *mm);
+static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
+{
+ if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))
+ return __ksm_enter(mm);
+ return 0;
+}
+
+static inline void ksm_exit(struct mm_struct *mm)
+{
+ if (test_bit(MMF_VM_MERGEABLE, &mm->flags))
+ __ksm_exit(mm);
+}
+
+#elif defined(CONFIG_UKSM)
+static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
+{
+ return 0;
+}
+
+static inline void ksm_exit(struct mm_struct *mm)
+{
+}
+#endif /* !CONFIG_UKSM */
+
#else /* !CONFIG_KSM */
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
@@ -89,4 +112,6 @@ static inline void ksm_migrate_page(struct page *newpage, struct page *oldpage)
#endif /* CONFIG_MMU */
#endif /* !CONFIG_KSM */
+#include <linux/uksm.h>
+
#endif /* __LINUX_KSM_H */
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 07d9acb5b19c..858a2f712354 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -369,6 +369,9 @@ struct vm_area_struct {
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
#endif
struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
+#ifdef CONFIG_UKSM
+ struct vma_slot *uksm_vma_slot;
+#endif
} __randomize_layout;
struct core_thread {
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index b593316bff3d..90d5b5a430f6 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -157,6 +157,9 @@ enum zone_stat_item {
NR_ZSPAGES, /* allocated in zsmalloc */
#endif
NR_FREE_CMA_PAGES,
+#ifdef CONFIG_UKSM
+ NR_UKSM_ZERO_PAGES,
+#endif
NR_VM_ZONE_STAT_ITEMS };
enum node_stat_item {
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 8fcdfa52eb4b..cc511ae57a21 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -1115,12 +1115,25 @@ extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
extern void untrack_pfn_moved(struct vm_area_struct *vma);
#endif
+#ifdef CONFIG_UKSM
+static inline int is_uksm_zero_pfn(unsigned long pfn)
+{
+ extern unsigned long uksm_zero_pfn;
+ return pfn == uksm_zero_pfn;
+}
+#else
+static inline int is_uksm_zero_pfn(unsigned long pfn)
+{
+ return 0;
+}
+#endif
+
#ifdef __HAVE_COLOR_ZERO_PAGE
static inline int is_zero_pfn(unsigned long pfn)
{
extern unsigned long zero_pfn;
unsigned long offset_from_zero_pfn = pfn - zero_pfn;
- return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
+ return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT) || is_uksm_zero_pfn(pfn);
}
#define my_zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr))
@@ -1129,7 +1142,7 @@ static inline int is_zero_pfn(unsigned long pfn)
static inline int is_zero_pfn(unsigned long pfn)
{
extern unsigned long zero_pfn;
- return pfn == zero_pfn;
+ return (pfn == zero_pfn) || (is_uksm_zero_pfn(pfn));
}
static inline unsigned long my_zero_pfn(unsigned long addr)
diff --git a/include/linux/sradix-tree.h b/include/linux/sradix-tree.h
new file mode 100644
index 000000000000..d71edba6b63f
--- /dev/null
+++ b/include/linux/sradix-tree.h
@@ -0,0 +1,77 @@
+#ifndef _LINUX_SRADIX_TREE_H
+#define _LINUX_SRADIX_TREE_H
+
+
+#define INIT_SRADIX_TREE(root, mask) \
+do { \
+ (root)->height = 0; \
+ (root)->gfp_mask = (mask); \
+ (root)->rnode = NULL; \
+} while (0)
+
+#define ULONG_BITS (sizeof(unsigned long) * 8)
+#define SRADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
+//#define SRADIX_TREE_MAP_SHIFT 6
+//#define SRADIX_TREE_MAP_SIZE (1UL << SRADIX_TREE_MAP_SHIFT)
+//#define SRADIX_TREE_MAP_MASK (SRADIX_TREE_MAP_SIZE-1)
+
+struct sradix_tree_node {
+ unsigned int height; /* Height from the bottom */
+ unsigned int count;
+ unsigned int fulls; /* Number of full sublevel trees */
+ struct sradix_tree_node *parent;
+ void *stores[0];
+};
+
+/* A simple radix tree implementation */
+struct sradix_tree_root {
+ unsigned int height;
+ struct sradix_tree_node *rnode;
+
+ /* Where found to have available empty stores in its sublevels */
+ struct sradix_tree_node *enter_node;
+ unsigned int shift;
+ unsigned int stores_size;
+ unsigned int mask;
+ unsigned long min; /* The first hole index */
+ unsigned long num;
+ //unsigned long *height_to_maxindex;
+
+ /* How the node is allocated and freed. */
+ struct sradix_tree_node *(*alloc)(void);
+ void (*free)(struct sradix_tree_node *node);
+
+ /* When a new node is added and removed */
+ void (*extend)(struct sradix_tree_node *parent, struct sradix_tree_node *child);
+ void (*assign)(struct sradix_tree_node *node, unsigned int index, void *item);
+ void (*rm)(struct sradix_tree_node *node, unsigned int offset);
+};
+
+struct sradix_tree_path {
+ struct sradix_tree_node *node;
+ int offset;
+};
+
+static inline
+void init_sradix_tree_root(struct sradix_tree_root *root, unsigned long shift)
+{
+ root->height = 0;
+ root->rnode = NULL;
+ root->shift = shift;
+ root->stores_size = 1UL << shift;
+ root->mask = root->stores_size - 1;
+}
+
+
+extern void *sradix_tree_next(struct sradix_tree_root *root,
+ struct sradix_tree_node *node, unsigned long index,
+ int (*iter)(void *, unsigned long));
+
+extern int sradix_tree_enter(struct sradix_tree_root *root, void **item, int num);
+
+extern void sradix_tree_delete_from_leaf(struct sradix_tree_root *root,
+ struct sradix_tree_node *node, unsigned long index);
+
+extern void *sradix_tree_lookup(struct sradix_tree_root *root, unsigned long index);
+
+#endif /* _LINUX_SRADIX_TREE_H */
diff --git a/include/linux/uksm.h b/include/linux/uksm.h
new file mode 100644
index 000000000000..bb8651f534f2
--- /dev/null
+++ b/include/linux/uksm.h
@@ -0,0 +1,149 @@
+#ifndef __LINUX_UKSM_H
+#define __LINUX_UKSM_H
+/*
+ * Memory merging support.
+ *
+ * This code enables dynamic sharing of identical pages found in different
+ * memory areas, even if they are not shared by fork().
+ */
+
+/* if !CONFIG_UKSM this file should not be compiled at all. */
+#ifdef CONFIG_UKSM
+
+#include <linux/bitops.h>
+#include <linux/mm.h>
+#include <linux/pagemap.h>
+#include <linux/rmap.h>
+#include <linux/sched.h>
+
+extern unsigned long zero_pfn __read_mostly;
+extern unsigned long uksm_zero_pfn __read_mostly;
+extern struct page *empty_uksm_zero_page;
+
+/* must be done before linked to mm */
+extern void uksm_vma_add_new(struct vm_area_struct *vma);
+extern void uksm_remove_vma(struct vm_area_struct *vma);
+
+#define UKSM_SLOT_NEED_SORT (1 << 0)
+#define UKSM_SLOT_NEED_RERAND (1 << 1)
+#define UKSM_SLOT_SCANNED (1 << 2) /* It's scanned in this round */
+#define UKSM_SLOT_FUL_SCANNED (1 << 3)
+#define UKSM_SLOT_IN_UKSM (1 << 4)
+
+struct vma_slot {
+ struct sradix_tree_node *snode;
+ unsigned long sindex;
+
+ struct list_head slot_list;
+ unsigned long fully_scanned_round;
+ unsigned long dedup_num;
+ unsigned long pages_scanned;
+ unsigned long this_sampled;
+ unsigned long last_scanned;
+ unsigned long pages_to_scan;
+ struct scan_rung *rung;
+ struct page **rmap_list_pool;
+ unsigned int *pool_counts;
+ unsigned long pool_size;
+ struct vm_area_struct *vma;
+ struct mm_struct *mm;
+ unsigned long ctime_j;
+ unsigned long pages;
+ unsigned long flags;
+ unsigned long pages_cowed; /* pages cowed this round */
+ unsigned long pages_merged; /* pages merged this round */
+ unsigned long pages_bemerged;
+
+ /* when it has page merged in this eval round */
+ struct list_head dedup_list;
+};
+
+static inline void uksm_unmap_zero_page(pte_t pte)
+{
+ if (pte_pfn(pte) == uksm_zero_pfn)
+ __dec_zone_page_state(empty_uksm_zero_page, NR_UKSM_ZERO_PAGES);
+}
+
+static inline void uksm_map_zero_page(pte_t pte)
+{
+ if (pte_pfn(pte) == uksm_zero_pfn)
+ __inc_zone_page_state(empty_uksm_zero_page, NR_UKSM_ZERO_PAGES);
+}
+
+static inline void uksm_cow_page(struct vm_area_struct *vma, struct page *page)
+{
+ if (vma->uksm_vma_slot && PageKsm(page))
+ vma->uksm_vma_slot->pages_cowed++;
+}
+
+static inline void uksm_cow_pte(struct vm_area_struct *vma, pte_t pte)
+{
+ if (vma->uksm_vma_slot && pte_pfn(pte) == uksm_zero_pfn)
+ vma->uksm_vma_slot->pages_cowed++;
+}
+
+static inline int uksm_flags_can_scan(unsigned long vm_flags)
+{
+#ifdef VM_SAO
+ if (vm_flags & VM_SAO)
+ return 0;
+#endif
+
+ return !(vm_flags & (VM_PFNMAP | VM_IO | VM_DONTEXPAND |
+ VM_HUGETLB | VM_MIXEDMAP | VM_SHARED
+ | VM_MAYSHARE | VM_GROWSUP | VM_GROWSDOWN));
+}
+
+static inline void uksm_vm_flags_mod(unsigned long *vm_flags_p)
+{
+ if (uksm_flags_can_scan(*vm_flags_p))
+ *vm_flags_p |= VM_MERGEABLE;
+}
+
+/*
+ * Just a wrapper for BUG_ON for where ksm_zeropage must not be. TODO: it will
+ * be removed when uksm zero page patch is stable enough.
+ */
+static inline void uksm_bugon_zeropage(pte_t pte)
+{
+ BUG_ON(pte_pfn(pte) == uksm_zero_pfn);
+}
+#else
+static inline void uksm_vma_add_new(struct vm_area_struct *vma)
+{
+}
+
+static inline void uksm_remove_vma(struct vm_area_struct *vma)
+{
+}
+
+static inline void uksm_unmap_zero_page(pte_t pte)
+{
+}
+
+static inline void uksm_map_zero_page(pte_t pte)
+{
+}
+
+static inline void uksm_cow_page(struct vm_area_struct *vma, struct page *page)
+{
+}
+
+static inline void uksm_cow_pte(struct vm_area_struct *vma, pte_t pte)
+{
+}
+
+static inline int uksm_flags_can_scan(unsigned long vm_flags)
+{
+ return 0;
+}
+
+static inline void uksm_vm_flags_mod(unsigned long *vm_flags_p)
+{
+}
+
+static inline void uksm_bugon_zeropage(pte_t pte)
+{
+}
+#endif /* !CONFIG_UKSM */
+#endif /* __LINUX_UKSM_H */
diff --git a/kernel/fork.c b/kernel/fork.c
index d66cd1014211..5e00818866d9 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -588,7 +588,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
__vma_link_rb(mm, tmp, rb_link, rb_parent);
rb_link = &tmp->vm_rb.rb_right;
rb_parent = &tmp->vm_rb;
-
+ uksm_vma_add_new(tmp);
mm->map_count++;
if (!(tmp->vm_flags & VM_WIPEONFORK))
retval = copy_page_range(tmp, mpnt);
diff --git a/lib/Makefile b/lib/Makefile
index afeff05fa8c5..691f13e0fdf3 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -31,7 +31,7 @@ endif
KCSAN_SANITIZE_random32.o := n
lib-y := ctype.o string.o vsprintf.o cmdline.o \
- rbtree.o radix-tree.o timerqueue.o xarray.o \
+ rbtree.o radix-tree.o sradix-tree.o timerqueue.o xarray.o \
idr.o extable.o sha1.o irq_regs.o argv_split.o \
flex_proportions.o ratelimit.o show_mem.o \
is_single_threaded.o plist.o decompress.o kobject_uevent.o \
diff --git a/lib/sradix-tree.c b/lib/sradix-tree.c
new file mode 100644
index 000000000000..ab21e6309b93
--- /dev/null
+++ b/lib/sradix-tree.c
@@ -0,0 +1,476 @@
+#include <linux/errno.h>
+#include <linux/mm.h>
+#include <linux/mman.h>
+#include <linux/spinlock.h>
+#include <linux/slab.h>
+#include <linux/gcd.h>
+#include <linux/sradix-tree.h>
+
+static inline int sradix_node_full(struct sradix_tree_root *root, struct sradix_tree_node *node)
+{
+ return node->fulls == root->stores_size ||
+ (node->height == 1 && node->count == root->stores_size);
+}
+
+/*
+ * Extend a sradix tree so it can store key @index.
+ */
+static int sradix_tree_extend(struct sradix_tree_root *root, unsigned long index)
+{
+ struct sradix_tree_node *node;
+ unsigned int height;
+
+ if (unlikely(root->rnode == NULL)) {
+ if (!(node = root->alloc()))
+ return -ENOMEM;
+
+ node->height = 1;
+ root->rnode = node;
+ root->height = 1;
+ }
+
+ /* Figure out what the height should be. */
+ height = root->height;
+ index >>= root->shift * height;
+
+ while (index) {
+ index >>= root->shift;
+ height++;
+ }
+
+ while (height > root->height) {
+ unsigned int newheight;
+
+ if (!(node = root->alloc()))
+ return -ENOMEM;
+
+ /* Increase the height. */
+ node->stores[0] = root->rnode;
+ root->rnode->parent = node;
+ if (root->extend)
+ root->extend(node, root->rnode);
+
+ newheight = root->height + 1;
+ node->height = newheight;
+ node->count = 1;
+ if (sradix_node_full(root, root->rnode))
+ node->fulls = 1;
+
+ root->rnode = node;
+ root->height = newheight;
+ }
+
+ return 0;
+}
+
+/*
+ * Search the next item from the current node, that is not NULL
+ * and can satify root->iter().
+ */
+void *sradix_tree_next(struct sradix_tree_root *root,
+ struct sradix_tree_node *node, unsigned long index,
+ int (*iter)(void *item, unsigned long height))
+{
+ unsigned long offset;
+ void *item;
+
+ if (unlikely(node == NULL)) {
+ node = root->rnode;
+ for (offset = 0; offset < root->stores_size; offset++) {
+ item = node->stores[offset];
+ if (item && (!iter || iter(item, node->height)))
+ break;
+ }
+
+ if (unlikely(offset >= root->stores_size))
+ return NULL;
+
+ if (node->height == 1)
+ return item;
+ else
+ goto go_down;
+ }
+
+ while (node) {
+ offset = (index & root->mask) + 1;
+ for (; offset < root->stores_size; offset++) {
+ item = node->stores[offset];
+ if (item && (!iter || iter(item, node->height)))
+ break;
+ }
+
+ if (offset < root->stores_size)
+ break;
+
+ node = node->parent;
+ index >>= root->shift;
+ }
+
+ if (!node)
+ return NULL;
+
+ while (node->height > 1) {
+go_down:
+ node = item;
+ for (offset = 0; offset < root->stores_size; offset++) {
+ item = node->stores[offset];
+ if (item && (!iter || iter(item, node->height)))
+ break;
+ }
+
+ if (unlikely(offset >= root->stores_size))
+ return NULL;
+ }
+
+ BUG_ON(offset > root->stores_size);
+
+ return item;
+}
+
+/*
+ * Blindly insert the item to the tree. Typically, we reuse the
+ * first empty store item.
+ */
+int sradix_tree_enter(struct sradix_tree_root *root, void **item, int num)
+{
+ unsigned long index;
+ unsigned int height;
+ struct sradix_tree_node *node, *tmp = NULL;
+ int offset, offset_saved;
+ void **store = NULL;
+ int error, i, j, shift;
+
+go_on:
+ index = root->min;
+
+ if (root->enter_node && !sradix_node_full(root, root->enter_node)) {
+ node = root->enter_node;
+ BUG_ON((index >> (root->shift * root->height)));
+ } else {
+ node = root->rnode;
+ if (node == NULL || (index >> (root->shift * root->height))
+ || sradix_node_full(root, node)) {
+ error = sradix_tree_extend(root, index);
+ if (error)
+ return error;
+
+ node = root->rnode;
+ }
+ }
+
+
+ height = node->height;
+ shift = (height - 1) * root->shift;
+ offset = (index >> shift) & root->mask;
+ while (shift > 0) {
+ offset_saved = offset;
+ for (; offset < root->stores_size; offset++) {
+ store = &node->stores[offset];
+ tmp = *store;
+
+ if (!tmp || !sradix_node_full(root, tmp))
+ break;
+ }
+ BUG_ON(offset >= root->stores_size);
+
+ if (offset != offset_saved) {
+ index += (offset - offset_saved) << shift;
+ index &= ~((1UL << shift) - 1);
+ }
+
+ if (!tmp) {
+ if (!(tmp = root->alloc()))
+ return -ENOMEM;
+
+ tmp->height = shift / root->shift;
+ *store = tmp;
+ tmp->parent = node;
+ node->count++;
+// if (root->extend)
+// root->extend(node, tmp);
+ }
+
+ node = tmp;
+ shift -= root->shift;
+ offset = (index >> shift) & root->mask;
+ }
+
+ BUG_ON(node->height != 1);
+
+
+ store = &node->stores[offset];
+ for (i = 0, j = 0;
+ j < root->stores_size - node->count &&
+ i < root->stores_size - offset && j < num; i++) {
+ if (!store[i]) {
+ store[i] = item[j];
+ if (root->assign)
+ root->assign(node, index + i, item[j]);
+ j++;
+ }
+ }
+
+ node->count += j;
+ root->num += j;
+ num -= j;
+
+ while (sradix_node_full(root, node)) {
+ node = node->parent;
+ if (!node)
+ break;
+
+ node->fulls++;
+ }
+
+ if (unlikely(!node)) {
+ /* All nodes are full */
+ root->min = 1 << (root->height * root->shift);
+ root->enter_node = NULL;
+ } else {
+ root->min = index + i - 1;
+ root->min |= (1UL << (node->height - 1)) - 1;
+ root->min++;
+ root->enter_node = node;
+ }
+
+ if (num) {
+ item += j;
+ goto go_on;
+ }
+
+ return 0;
+}
+
+
+/**
+ * sradix_tree_shrink - shrink height of a sradix tree to minimal
+ * @root sradix tree root
+ *
+ */
+static inline void sradix_tree_shrink(struct sradix_tree_root *root)
+{
+ /* try to shrink tree height */
+ while (root->height > 1) {
+ struct sradix_tree_node *to_free = root->rnode;
+
+ /*
+ * The candidate node has more than one child, or its child
+ * is not at the leftmost store, we cannot shrink.
+ */
+ if (to_free->count != 1 || !to_free->stores[0])
+ break;
+
+ root->rnode = to_free->stores[0];
+ root->rnode->parent = NULL;
+ root->height--;
+ if (unlikely(root->enter_node == to_free))
+ root->enter_node = NULL;
+ root->free(to_free);
+ }
+}
+
+/*
+ * Del the item on the known leaf node and index
+ */
+void sradix_tree_delete_from_leaf(struct sradix_tree_root *root,
+ struct sradix_tree_node *node, unsigned long index)
+{
+ unsigned int offset;
+ struct sradix_tree_node *start, *end;
+
+ BUG_ON(node->height != 1);
+
+ start = node;
+ while (node && !(--node->count))
+ node = node->parent;
+
+ end = node;
+ if (!node) {
+ root->rnode = NULL;
+ root->height = 0;
+ root->min = 0;
+ root->num = 0;
+ root->enter_node = NULL;
+ } else {
+ offset = (index >> (root->shift * (node->height - 1))) & root->mask;
+ if (root->rm)
+ root->rm(node, offset);
+ node->stores[offset] = NULL;
+ root->num--;
+ if (root->min > index) {
+ root->min = index;
+ root->enter_node = node;
+ }
+ }
+
+ if (start != end) {
+ do {
+ node = start;
+ start = start->parent;
+ if (unlikely(root->enter_node == node))
+ root->enter_node = end;
+ root->free(node);
+ } while (start != end);
+
+ /*
+ * Note that shrink may free "end", so enter_node still need to
+ * be checked inside.
+ */
+ sradix_tree_shrink(root);
+ } else if (node->count == root->stores_size - 1) {
+ /* It WAS a full leaf node. Update the ancestors */
+ node = node->parent;
+ while (node) {
+ node->fulls--;
+ if (node->fulls != root->stores_size - 1)
+ break;
+
+ node = node->parent;
+ }
+ }
+}
+
+void *sradix_tree_lookup(struct sradix_tree_root *root, unsigned long index)
+{
+ unsigned int height, offset;
+ struct sradix_tree_node *node;
+ int shift;
+
+ node = root->rnode;
+ if (node == NULL || (index >> (root->shift * root->height)))
+ return NULL;
+
+ height = root->height;
+ shift = (height - 1) * root->shift;
+
+ do {
+ offset = (index >> shift) & root->mask;
+ node = node->stores[offset];
+ if (!node)
+ return NULL;
+
+ shift -= root->shift;
+ } while (shift >= 0);
+
+ return node;
+}
+
+/*
+ * Return the item if it exists, otherwise create it in place
+ * and return the created item.
+ */
+void *sradix_tree_lookup_create(struct sradix_tree_root *root,
+ unsigned long index, void *(*item_alloc)(void))
+{
+ unsigned int height, offset;
+ struct sradix_tree_node *node, *tmp;
+ void *item;
+ int shift, error;
+
+ if (root->rnode == NULL || (index >> (root->shift * root->height))) {
+ if (item_alloc) {
+ error = sradix_tree_extend(root, index);
+ if (error)
+ return NULL;
+ } else {
+ return NULL;
+ }
+ }
+
+ node = root->rnode;
+ height = root->height;
+ shift = (height - 1) * root->shift;
+
+ do {
+ offset = (index >> shift) & root->mask;
+ if (!node->stores[offset]) {
+ if (!(tmp = root->alloc()))
+ return NULL;
+
+ tmp->height = shift / root->shift;
+ node->stores[offset] = tmp;
+ tmp->parent = node;
+ node->count++;
+ node = tmp;
+ } else {
+ node = node->stores[offset];
+ }
+
+ shift -= root->shift;
+ } while (shift > 0);
+
+ BUG_ON(node->height != 1);
+ offset = index & root->mask;
+ if (node->stores[offset]) {
+ return node->stores[offset];
+ } else if (item_alloc) {
+ if (!(item = item_alloc()))
+ return NULL;
+
+ node->stores[offset] = item;
+
+ /*
+ * NOTE: we do NOT call root->assign here, since this item is
+ * newly created by us having no meaning. Caller can call this
+ * if it's necessary to do so.
+ */
+
+ node->count++;
+ root->num++;
+
+ while (sradix_node_full(root, node)) {
+ node = node->parent;
+ if (!node)
+ break;
+
+ node->fulls++;
+ }
+
+ if (unlikely(!node)) {
+ /* All nodes are full */
+ root->min = 1 << (root->height * root->shift);
+ } else {
+ if (root->min == index) {
+ root->min |= (1UL << (node->height - 1)) - 1;
+ root->min++;
+ root->enter_node = node;
+ }
+ }
+
+ return item;
+ } else {
+ return NULL;
+ }
+
+}
+
+int sradix_tree_delete(struct sradix_tree_root *root, unsigned long index)
+{
+ unsigned int height, offset;
+ struct sradix_tree_node *node;
+ int shift;
+
+ node = root->rnode;
+ if (node == NULL || (index >> (root->shift * root->height)))
+ return -ENOENT;
+
+ height = root->height;
+ shift = (height - 1) * root->shift;
+
+ do {
+ offset = (index >> shift) & root->mask;
+ node = node->stores[offset];
+ if (!node)
+ return -ENOENT;
+