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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
|
/*
* zbud.c - Compression buddies allocator
*
* Copyright (c) 2010-2012, Dan Magenheimer, Oracle Corp.
*
* Compression buddies ("zbud") provides for efficiently packing two
* (or, possibly in the future, more) compressed pages ("zpages") into
* a single "raw" pageframe and for tracking both zpages and pageframes
* so that whole pageframes can be easily reclaimed in LRU-like order.
* It is designed to be used in conjunction with transcendent memory
* ("tmem"); for example separate LRU lists are maintained for persistent
* vs. ephemeral pages.
*
* A zbudpage is an overlay for a struct page and thus each zbudpage
* refers to a physical pageframe of RAM. When the caller passes a
* struct page from the kernel's page allocator, zbud "transforms" it
* to a zbudpage which sets/uses a different set of fields than the
* struct-page and thus must "untransform" it back by reinitializing
* certain fields before the struct-page can be freed. The fields
* of a zbudpage include a page lock for controlling access to the
* corresponding pageframe, and there is a size field for each zpage.
* Each zbudpage also lives on two linked lists: a "budlist" which is
* used to support efficient buddying of zpages; and an "lru" which
* is used for reclaiming pageframes in approximately least-recently-used
* order.
*
* A zbudpageframe is a pageframe divided up into aligned 64-byte "chunks"
* which contain the compressed data for zero, one, or two zbuds. Contained
* with the compressed data is a tmem_handle which is a key to allow
* the same data to be found via the tmem interface so the zpage can
* be invalidated (for ephemeral pages) or repatriated to the swap cache
* (for persistent pages). The contents of a zbudpageframe must never
* be accessed without holding the page lock for the corresponding
* zbudpage and, to accomodate highmem machines, the contents may
* only be examined or changes when kmapped. Thus, when in use, a
* kmapped zbudpageframe is referred to in the zbud code as "void *zbpg".
*
* Note that the term "zbud" refers to the combination of a zpage and
* a tmem_handle that is stored as one of possibly two "buddied" zpages;
* it also generically refers to this allocator... sorry for any confusion.
*
* A zbudref is a pointer to a struct zbudpage (which can be cast to a
* struct page), with the LSB either cleared or set to indicate, respectively,
* the first or second zpage in the zbudpageframe. Since a zbudref can be
* cast to a pointer, it is used as the tmem "pampd" pointer and uniquely
* references a stored tmem page and so is the only zbud data structure
* externally visible to zbud.c/zbud.h.
*
* Since we wish to reclaim entire pageframes but zpages may be randomly
* added and deleted to any given pageframe, we approximate LRU by
* promoting a pageframe to MRU when a zpage is added to it, but
* leaving it at the current place in the list when a zpage is deleted
* from it. As a side effect, zpages that are difficult to buddy (e.g.
* very large paages) will be reclaimed faster than average, which seems
* reasonable.
*
* In the current implementation, no more than two zpages may be stored in
* any pageframe and no zpage ever crosses a pageframe boundary. While
* other zpage allocation mechanisms may allow greater density, this two
* zpage-per-pageframe limit both ensures simple reclaim of pageframes
* (including garbage collection of references to the contents of those
* pageframes from tmem data structures) AND avoids the need for compaction.
* With additional complexity, zbud could be modified to support storing
* up to three zpages per pageframe or, to handle larger average zpages,
* up to three zpages per pair of pageframes, but it is not clear if the
* additional complexity would be worth it. So consider it an exercise
* for future developers.
*
* Note also that zbud does no page allocation or freeing. This is so
* that the caller has complete control over and, for accounting, visibility
* into if/when pages are allocated and freed.
*
* Finally, note that zbud limits the size of zpages it can store; the
* caller must check the zpage size with zbud_max_buddy_size before
* storing it, else BUGs will result. User beware.
*/
#include <linux/module.h>
#include <linux/highmem.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/pagemap.h>
#include <linux/atomic.h>
#include <linux/bug.h>
#include "tmem.h"
#include "zcache.h"
#include "zbud.h"
/*
* We need to ensure that a struct zbudpage is never larger than a
* struct page. This is checked with a BUG_ON in zbud_init.
*
* The unevictable field indicates that a zbud is being added to the
* zbudpage. Since this is a two-phase process (due to tmem locking),
* this field locks the zbudpage against eviction when a zbud match
* or creation is in process. Since this addition process may occur
* in parallel for two zbuds in one zbudpage, the field is a counter
* that must not exceed two.
*/
struct zbudpage {
union {
struct page page;
struct {
unsigned long space_for_flags;
struct {
unsigned zbud0_size:PAGE_SHIFT;
unsigned zbud1_size:PAGE_SHIFT;
unsigned unevictable:2;
};
struct list_head budlist;
struct list_head lru;
};
};
};
#if (PAGE_SHIFT * 2) + 2 > BITS_PER_LONG
#error "zbud won't work for this arch, PAGE_SIZE is too large"
#endif
struct zbudref {
union {
struct zbudpage *zbudpage;
unsigned long zbudref;
};
};
#define CHUNK_SHIFT 6
#define CHUNK_SIZE (1 << CHUNK_SHIFT)
#define CHUNK_MASK (~(CHUNK_SIZE-1))
#define NCHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
#define MAX_CHUNK (NCHUNKS-1)
/*
* The following functions deal with the difference between struct
* page and struct zbudpage. Note the hack of using the pageflags
* from struct page; this is to avoid duplicating all the complex
* pageflag macros.
*/
static inline void zbudpage_spin_lock(struct zbudpage *zbudpage)
{
struct page *page = (struct page *)zbudpage;
while (unlikely(test_and_set_bit_lock(PG_locked, &page->flags))) {
do {
cpu_relax();
} while (test_bit(PG_locked, &page->flags));
}
}
static inline void zbudpage_spin_unlock(struct zbudpage *zbudpage)
{
struct page *page = (struct page *)zbudpage;
clear_bit(PG_locked, &page->flags);
}
static inline int zbudpage_spin_trylock(struct zbudpage *zbudpage)
{
return trylock_page((struct page *)zbudpage);
}
static inline int zbudpage_is_locked(struct zbudpage *zbudpage)
{
return PageLocked((struct page *)zbudpage);
}
static inline void *kmap_zbudpage_atomic(struct zbudpage *zbudpage)
{
return kmap_atomic((struct page *)zbudpage);
}
/*
* A dying zbudpage is an ephemeral page in the process of being evicted.
* Any data contained in the zbudpage is invalid and we are just waiting for
* the tmem pampds to be invalidated before freeing the page
*/
static inline int zbudpage_is_dying(struct zbudpage *zbudpage)
{
struct page *page = (struct page *)zbudpage;
return test_bit(PG_reclaim, &page->flags);
}
static inline void zbudpage_set_dying(struct zbudpage *zbudpage)
{
struct page *page = (struct page *)zbudpage;
set_bit(PG_reclaim, &page->flags);
}
static inline void zbudpage_clear_dying(struct zbudpage *zbudpage)
{
struct page *page
|