-
Notifications
You must be signed in to change notification settings - Fork 8
/
base.c
3661 lines (3303 loc) · 167 KB
/
base.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
#include "base.h"
static struct kprobe kp_kallsyms_lookup_name = {
.symbol_name = "kallsyms_lookup_name",
};
static void kallsyms_lookup_name_handler_post(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
}
struct hot_cold_file_global hot_cold_file_global_info;
//置1会把内存回收信息详细打印出来
int shrink_page_printk_open1 = 0;
//不怎么关键的调试信息
int shrink_page_printk_open = 0;
unsigned long async_memory_reclaim_status = 0;
static void iterate_supers_async(void);
/*************以下代码不同内核版本有差异******************************************************************************************/
/*******以下是红帽8.3 4.18.0-240内核针对内核原生内存回收函数在本ko驱动的适配********************************************/
#if LINUX_VERSION_CODE <= KERNEL_VERSION(4,18,0)
static int (*__isolate_lru_page_async)(struct page *page, isolate_mode_t mode);
static int (*page_evictable_async)(struct page *page);
static int (*__remove_mapping_async)(struct address_space *mapping, struct page *page,bool reclaimed);
static void (*mem_cgroup_update_lru_size_async)(struct lruvec *lruvec, enum lru_list lru,int zid, int nr_pages);
static struct lruvec *(*mem_cgroup_page_lruvec_async)(struct page *page, struct pglist_data *pgdat);
static void (*__mod_lruvec_state_async)(struct lruvec *lruvec, enum node_stat_item idx,int val);
void (*free_unref_page_list_async)(struct list_head *list);
void (*mem_cgroup_uncharge_list_async)(struct list_head *page_list);
static void (*__count_memcg_events_async)(struct mem_cgroup *memcg, enum vm_event_item idx,unsigned long count);
static unsigned long (*kallsyms_lookup_name_async)(const char *name);
static void (*try_to_unmap_flush_async)(void);
static struct mem_cgroup *root_mem_cgroup_async;
static void (*putback_lru_page_async)(struct page *page);
static void (*mem_cgroup_uncharge_async)(struct page *page);
compound_page_dtor * (*compound_page_dtors_async)[NR_COMPOUND_DTORS];
static spinlock_t *sb_lock_async;
static struct list_head *super_blocks_async;
static void (*security_sb_free_async)(struct super_block *sb);
static void (*destroy_super_rcu_async)(struct rcu_head *head);
static bool (*try_to_unmap_async)(struct page *page, enum ttu_flags flags);
int (*page_referenced_async)(struct page *page,int is_locked,struct mem_cgroup *memcg,unsigned long *vm_flags);
#ifdef USE_KERNEL_SHRINK_INACTIVE_LIST
static unsigned long (*shrink_page_list_async)(struct list_head *page_list,struct pglist_data *pgdat,struct scan_control_async *sc,enum ttu_flags ttu_flags,struct reclaim_stat *stat,bool force_reclaim);
static void (*putback_inactive_pages_async)(struct lruvec *lruvec, struct list_head *page_list);
#endif
static inline compound_page_dtor *get_compound_page_dtor_async(struct page *page)
{
VM_BUG_ON_PAGE(page[1].compound_dtor >= NR_COMPOUND_DTORS, page);
//compound_page_dtors_async[page[1].compound_dtor] 就是编译通不过,为啥,现在这样才通过
return (*compound_page_dtors_async)[page[1].compound_dtor];
}
//源码跟内核count_memcg_events()一样,只是改了名字
//static inline void count_memcg_events(struct mem_cgroup *memcg,
static inline void count_memcg_events_async(struct mem_cgroup *memcg,
enum vm_event_item idx,
unsigned long count)
{
unsigned long flags;
local_irq_save(flags);
//源码跟内核__count_memcg_events()一样,只是改了名字
__count_memcg_events_async(memcg, idx, count);
local_irq_restore(flags);
}
//源码跟内核count_memcg_page_event()一样,只是改了名字
static inline void count_memcg_page_event_async(struct page *page,
enum vm_event_item idx)
{
if (page->mem_cgroup)
count_memcg_events_async(page->mem_cgroup, idx, 1);
}
static __always_inline void __update_lru_size_async(struct lruvec *lruvec,
enum lru_list lru, enum zone_type zid,
int nr_pages)
{
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
__mod_lruvec_state_async(lruvec, NR_LRU_BASE + lru, nr_pages);
__mod_zone_page_state(&pgdat->node_zones[zid],
NR_ZONE_LRU_BASE + lru, nr_pages);
}
static __always_inline void update_lru_size_async(struct lruvec *lruvec,
enum lru_list lru, enum zone_type zid,
int nr_pages)
{
__update_lru_size_async(lruvec, lru, zid, nr_pages);
#ifdef CONFIG_MEMCG
mem_cgroup_update_lru_size_async(lruvec, lru, zid, nr_pages);
#endif
}
static __always_inline void del_page_from_lru_list_async(struct page *page,
struct lruvec *lruvec, enum lru_list lru)
{
list_del(&page->lru);
update_lru_size_async(lruvec, lru, page_zonenum(page), -hpage_nr_pages(page));
}
static __always_inline void add_page_to_lru_list_async(struct page *page,
struct lruvec *lruvec, enum lru_list lru)
{
update_lru_size_async(lruvec, lru, page_zonenum(page), hpage_nr_pages(page));
list_add(&page->lru, &lruvec->lists[lru]);
}
static __always_inline void add_page_to_lru_list_tail_async(struct page *page,
struct lruvec *lruvec, enum lru_list lru)
{
update_lru_size_async(lruvec, lru, page_zonenum(page), hpage_nr_pages(page));
list_add_tail(&page->lru, &lruvec->lists[lru]);
}
#ifndef USE_KERNEL_SHRINK_INACTIVE_LIST
//源码来自内核shrink_page_list(),但是针对pagecache内存回收简化很多,执行该函数回收内存的page大部分都是长时间未访问的clean pagecache
unsigned int async_shrink_free_page(struct pglist_data *pgdat,struct lruvec *lruvec,struct list_head *page_list,
struct scan_control_async *sc,struct reclaim_stat *stat)
{
LIST_HEAD(ret_pages);
LIST_HEAD(free_pages);
int pgactivate = 0;
unsigned nr_unqueued_dirty = 0;
unsigned nr_dirty = 0;
unsigned nr_congested = 0;
unsigned nr_reclaimed = 0;
unsigned nr_writeback = 0;
unsigned nr_immediate = 0;
unsigned nr_ref_keep = 0;
unsigned nr_unmap_fail = 0;
unsigned int lock_fail_count = 0;
unsigned int writeback_count = 0;
unsigned int dirty_count = 0;
unsigned int page_has_private_count = 0;
unsigned int mapping_count = 0;
unsigned int free_pages_fail_count = 0;
while (!list_empty(page_list)) {
struct address_space *mapping;
struct page *page;
int may_enter_fs;
cond_resched();
page = lru_to_page(page_list);
list_del(&page->lru);
if (!trylock_page(page)){
lock_fail_count ++;
goto keep;
}
mapping = page_mapping(page);
may_enter_fs = (sc->gfp_mask & __GFP_FS);
/****page是witeback页*********************/
if (PageWriteback(page)) {
writeback_count ++;
if(!PageReclaim(page)){
SetPageReclaim(page);
nr_writeback += 1;
}else if (PageReclaim(page) &&test_bit(PGDAT_WRITEBACK, &pgdat->flags)){
nr_immediate += 1;
}
goto activate_locked;
}
/****page是mmap页*********************/
if (page_mapped(page)){
enum ttu_flags flags = TTU_BATCH_FLUSH;
if (!try_to_unmap_async(page, flags)) {
nr_unmap_fail++;
goto activate_locked;
}
}
//为了保证内存回收绝对准确,一定得做一些可能发生的异常限制
if (PageTransHuge(page) || PageAnon(page) || PageSwapBacked(page))
panic("%s page:0x%llx page->flags:0x%lx",__func__,(u64)page,page->flags);
/****page是脏页*********************/
if (PageDirty(page)) {
dirty_count ++;
nr_dirty++;
goto activate_locked;
//这里goto keep 分支,忘了unlock_page()了,导致其他进程访问到该page时因为page lock就休眠了
//goto keep;
}
/*******释放page的bh******************/
if (page_has_private(page)) {
page_has_private_count ++;
if (!try_to_release_page(page,sc->gfp_mask)){
goto activate_locked;
}
if (!mapping && page_count(page) == 1) {
unlock_page(page);
if (put_page_testzero(page)){
goto free_it;
}
else {
nr_reclaimed++;
continue;
}
}
}
/********把page从radix tree剔除************************/
if (!mapping || !__remove_mapping_async(mapping, page, true)){
mapping_count ++;
goto keep_locked;
}
unlock_page(page);
free_it:
nr_reclaimed++;
//如果要释放的page引用计数不是0,那就有问题了,主动触发crash
if(atomic_read(&page->_refcount) != 0){
panic("page:0x%llx refcount:%d error!!!!!\n",(u64)page,atomic_read(&page->_refcount));
}
list_add(&page->lru, &free_pages);
continue;
activate_locked:
if (!PageMlocked(page)) {
SetPageActive(page);
pgactivate++;
/*page要添加到active lru链表,这里增加对应的memory cgroup中在active lru链表的page统计数-------------*/
//count_memcg_page_event(page, PGACTIVATE);
count_memcg_page_event_async(page, PGACTIVATE);
}
keep_locked:
unlock_page(page);
keep:
list_add(&page->lru, &ret_pages);
free_pages_fail_count ++;
}
mem_cgroup_uncharge_list_async(&free_pages);
try_to_unmap_flush_async();
free_unref_page_list_async(&free_pages);
/*共有pgactivate个page要添加到active lru链表,这里增加全局的在active lru链表的page统计数---------------*/
list_splice(&ret_pages, page_list);
count_vm_events(PGACTIVATE, pgactivate);
if (stat) {
stat->nr_dirty = nr_dirty;
stat->nr_congested = nr_congested;
stat->nr_unqueued_dirty = nr_unqueued_dirty;
stat->nr_writeback = nr_writeback;
stat->nr_immediate = nr_immediate;
stat->nr_activate = pgactivate;
stat->nr_ref_keep = nr_ref_keep;
stat->nr_unmap_fail = nr_unmap_fail;
}
hot_cold_file_global_info.hot_cold_file_shrink_counter.lock_fail_count += lock_fail_count;
hot_cold_file_global_info.hot_cold_file_shrink_counter.nr_unmap_fail += nr_unmap_fail;
hot_cold_file_global_info.hot_cold_file_shrink_counter.writeback_count += writeback_count;
hot_cold_file_global_info.hot_cold_file_shrink_counter.dirty_count += dirty_count;
hot_cold_file_global_info.hot_cold_file_shrink_counter.page_has_private_count += page_has_private_count;
hot_cold_file_global_info.hot_cold_file_shrink_counter.mapping_count += mapping_count;
hot_cold_file_global_info.hot_cold_file_shrink_counter.free_pages_count += nr_reclaimed;
hot_cold_file_global_info.hot_cold_file_shrink_counter.free_pages_fail_count += free_pages_fail_count;
return nr_reclaimed;
}
int __hot_cold_file_isolate_lru_pages(pg_data_t *pgdat,struct page * page,struct list_head *dst,isolate_mode_t mode)
{
struct lruvec *lruvec;
int lru;
lruvec = mem_cgroup_lruvec(page->mem_cgroup, pgdat);
lru = page_lru_base_type(page);
/*__isolate_lru_page里清除page的PageLRU属性,因为要把page从lru链表剔除了,并且令page的引用计数加1*/
//switch (__isolate_lru_page(page, mode)) {
switch (__isolate_lru_page_async(page, mode)) {
case 0:
//把page从lru链表剔除,并减少page所属lru链表的page数
//del_page_from_lru_list(page, lruvec, lru + PageActive(page));
del_page_from_lru_list_async(page, lruvec, lru + PageActive(page));
//如果page在active lru链表上则要清理掉Active属性,因为内存回收的page一定是处于inactive lru链表,否则内存回收最后会因为page有PageActive属性而触发crash
if(PageActive(page))
ClearPageActive(page);
//再把page添加到dst临时链表
list_add(&page->lru,dst);
return 0;
case -EBUSY:
if(shrink_page_printk_open1)
printk("2:%s %s %d page:0x%llx page->flags:0x%lx EBUSY\n",__func__,current->comm,current->pid,(u64)page,page->flags);
break;
default:
//实际测试发现,这个会成立,这个正常,因为该page可能被内核原生内存回收隔离成功,就没有了lru属性。但是这里不再触发bug,仅仅一个告警打印
if(shrink_page_printk_open1)
printk("3:%s %s %d page:0x%llx PageUnevictable:%d PageLRU:%d !!!!!!!!!!!!!\n",__func__,current->comm,current->pid,(u64)page,PageUnevictable(page),PageLRU(page));
#if 0
BUG();
#endif
}
return -1;
}
//static void putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list)
unsigned int hot_cold_file_putback_inactive_pages(struct pglist_data *pgdat, struct list_head *page_list)
{
//struct pglist_data *pgdat = lruvec_pgdat(lruvec);
unsigned int move = 0;
LIST_HEAD(pages_to_free);
struct lruvec *lruvec;
spin_lock(&pgdat->lru_lock);
/*
* Put back any unfreeable pages.
*/
while (!list_empty(page_list)) {
struct page *page = lru_to_page(page_list);
int lru;
if(shrink_page_printk_open1)
printk("1:%s %s %d page:0x%llx page->flags:0x%lx\n",__func__,current->comm,current->pid,(u64)page,page->flags);
VM_BUG_ON_PAGE(PageLRU(page), page);
list_del(&page->lru);
if (unlikely(!page_evictable_async(page))) {
spin_unlock(&pgdat->lru_lock);
putback_lru_page_async(page);
spin_lock(&pgdat->lru_lock);
continue;
}
/*怎么保证这些内存释放失败的page添加会原有的lru链表呢?page->mem_cgroup 是page锁绑定的memcg,再有memcg找到它的lruvec,完美*/
lruvec = mem_cgroup_page_lruvec_async(page, pgdat);
SetPageLRU(page);
lru = page_lru(page);
//add_page_to_lru_list(page, lruvec, lru);
add_page_to_lru_list_async(page, lruvec, lru);
move ++;
/*if (is_active_lru(lru)) { 这段代码不需要,不用统计
int file = is_file_lru(lru);
int numpages = hpage_nr_pages(page);
reclaim_stat->recent_rotated[file] += numpages;
}*/
if (put_page_testzero(page)) {
if(shrink_page_printk_open1)
printk("2:%s %s %d put_page_testzero page:0x%llx page->flags:0x%lx PageCompound:%d\n",__func__,current->comm,current->pid,(u64)page,page->flags,PageCompound(page));
__ClearPageLRU(page);
__ClearPageActive(page);
//里边调用了__mod_lruvec_state、mem_cgroup_update_lru_size函数,导致“undefined!”
//del_page_from_lru_list(page, lruvec, lru);
del_page_from_lru_list_async(page, lruvec, lru);
if (unlikely(PageCompound(page))) {
spin_unlock(&pgdat->lru_lock);
mem_cgroup_uncharge_async(page);
(*get_compound_page_dtor_async(page))(page);
spin_lock(&pgdat->lru_lock);
} else
list_add(&page->lru, &pages_to_free);
}
}
spin_unlock(&pgdat->lru_lock);
/*
* To save our caller's stack, now use input list for pages to free.
*/
list_splice(&pages_to_free, page_list);
return move;
}
#else
/*以下代码是使用内核原生的内存回收源码,不再使用我自己写的,稳定为主*/
#if LINUX_VERSION_CODE <= KERNEL_VERSION(4,18,0)
/*以下代码直接从内核复制过来*/
#ifdef ARCH_HAS_PREFETCHW
#define prefetchw_prev_lru_page(_page, _base, _field) \
do { \
if ((_page)->lru.prev != _base) { \
struct page *prev; \
\
prev = lru_to_page(&(_page->lru)); \
prefetchw(&prev->_field); \
} \
} while (0)
#else
#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
#endif
static __always_inline void update_lru_sizes_async(struct lruvec *lruvec,
enum lru_list lru, unsigned long *nr_zone_taken)
{
int zid;
for (zid = 0; zid < MAX_NR_ZONES; zid++) {
if (!nr_zone_taken[zid])
continue;
__update_lru_size_async(lruvec, lru, zid, -nr_zone_taken[zid]);
#ifdef CONFIG_MEMCG
mem_cgroup_update_lru_size_async(lruvec, lru, zid, -nr_zone_taken[zid]);
#endif
}
}
static unsigned long isolate_lru_pages_async(unsigned long nr_to_scan,
struct lruvec *lruvec, struct list_head *dst,
unsigned long *nr_scanned, struct scan_control_async *sc,
isolate_mode_t mode, enum lru_list lru)
{
struct list_head *src = &lruvec->lists[lru];
unsigned long nr_taken = 0;
unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
unsigned long skipped = 0;
unsigned long scan, total_scan, nr_pages;
LIST_HEAD(pages_skipped);
scan = 0;
for (total_scan = 0;
scan < nr_to_scan && nr_taken < nr_to_scan && !list_empty(src);
total_scan++) {
struct page *page;
page = lru_to_page(src);
prefetchw_prev_lru_page(page, src, flags);
VM_BUG_ON_PAGE(!PageLRU(page), page);
if (page_zonenum(page) > sc->reclaim_idx) {
list_move(&page->lru, &pages_skipped);
nr_skipped[page_zonenum(page)]++;
continue;
}
/*
* Do not count skipped pages because that makes the function
* return with no isolated pages if the LRU mostly contains
* ineligible pages. This causes the VM to not reclaim any
* pages, triggering a premature OOM.
*/
scan++;
switch (__isolate_lru_page_async(page, mode)) {
case 0:
nr_pages = hpage_nr_pages(page);
nr_taken += nr_pages;
nr_zone_taken[page_zonenum(page)] += nr_pages;
list_move(&page->lru, dst);
break;
case -EBUSY:
/* else it is being freed elsewhere */
list_move(&page->lru, src);
continue;
default:
BUG();
}
}
/*
* Splice any skipped pages to the start of the LRU list. Note that
* this disrupts the LRU order when reclaiming for lower zones but
* we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
* scanning would soon rescan the same pages to skip and put the
* system at risk of premature OOM.
*/
if (!list_empty(&pages_skipped)) {
int zid;
list_splice(&pages_skipped, src);
for (zid = 0; zid < MAX_NR_ZONES; zid++) {
if (!nr_skipped[zid])
continue;
__count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
skipped += nr_skipped[zid];
}
}
*nr_scanned = total_scan;
#if 0
trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
total_scan, skipped, nr_taken, mode, lru);
#endif
update_lru_sizes_async(lruvec, lru, nr_zone_taken);
return nr_taken;
}
/*现在对内存回收代码做了很大改进,就是使用内核原生内存回收函数isolate_lru_pages、shrink_page_list、putback_inactive_pages,不再使用自己编写的。
*主要是出于稳定性考虑,毕竟使用内核原生的更稳定。这里主要有几点说下
1:现在file_area内存回收以memory cgroup为主,参与内存回收的page都保证是一个lruvec的,因此可以直接使用内核内存回收原生的
isolate_lru_pages、shrink_page_list、putback_inactive_pages函数了,不用再使用我编写的代码了
2:可以直接kprobe内核shrink_inactive_list函数进行内存回收,这样就不用使用复制使用内核原生代码了。但是,shrink_inactive_list里的
too_many_isolated()和lru_add_drain()函数就会执行到,影响到内存回收。还有很多冗余的统计代码。都要考虑
3:最后,还有一个隐藏很深的知识点。首先cold_file_isolate_lru_pages_and_shrink函数里,把同一个lruvec的32个page移动到inactive lru链表尾,然后
里边执行shrink_inactive_list_async函数隔离并释放掉刚才移动到lruvec的inactive lru链表尾的32个page。这里就有个问题,如果隔离这32个page前,
其他进程把page移动到lruvec的inactive lru链表尾怎么办?完全有可能,这样的话,这里释放掉的32个page就不是同一个文件的文件页了,有了干扰page。
但是情况没那么严重,因为 新的page添加到lru链表时,是添加到inactive lru链表头。active lru链表的page也是移动到inactive lru链表尾。只有
有racliam标记的page在writeback完成后,才会移动到inactive lru链表尾。这种page本来就应该释放掉,只不过会被我的异步内存回收线程释放掉而已。
*/
static noinline_for_stack unsigned long
shrink_inactive_list_async(unsigned long nr_to_scan, struct lruvec *lruvec,
struct scan_control_async *sc, enum lru_list lru)
{
LIST_HEAD(page_list);
unsigned long nr_scanned;
unsigned long nr_reclaimed = 0;
unsigned long nr_taken;
struct reclaim_stat stat = {};
isolate_mode_t isolate_mode = 0;
int file = is_file_lru(lru);
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
#if 0
struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
bool stalled = false;
while (unlikely(too_many_isolated(pgdat, file, sc))) {
if (stalled)
return 0;
/* wait a bit for the reclaimer. */
msleep(100);
stalled = true;
/* We are about to die and free our memory. Return now. */
if (fatal_signal_pending(current))
return SWAP_CLUSTER_MAX;
}
lru_add_drain();
#endif
if (!sc->may_unmap)
isolate_mode |= ISOLATE_UNMAPPED;
spin_lock_irq(&pgdat->lru_lock);
nr_taken = isolate_lru_pages_async(nr_to_scan, lruvec, &page_list,
&nr_scanned, sc, isolate_mode, lru);
#if 0
__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
reclaim_stat->recent_scanned[file] += nr_taken;
if (current_is_kswapd()) {
if (global_reclaim(sc))
__count_vm_events(PGSCAN_KSWAPD, nr_scanned);
count_memcg_events(lruvec_memcg(lruvec), PGSCAN_KSWAPD,
nr_scanned);
} else {
if (global_reclaim(sc))
__count_vm_events(PGSCAN_DIRECT, nr_scanned);
count_memcg_events(lruvec_memcg(lruvec), PGSCAN_DIRECT,
nr_scanned);
}
#endif
spin_unlock_irq(&pgdat->lru_lock);
if (nr_taken == 0)
return 0;
nr_reclaimed = shrink_page_list_async(&page_list, pgdat, sc, 0,
&stat, true);
spin_lock_irq(&pgdat->lru_lock);
#if 0
if (current_is_kswapd()) {
if (global_reclaim(sc))
__count_vm_events(PGSTEAL_KSWAPD, nr_reclaimed);
count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_KSWAPD,
nr_reclaimed);
} else {
if (global_reclaim(sc))
__count_vm_events(PGSTEAL_DIRECT, nr_reclaimed);
count_memcg_events(lruvec_memcg(lruvec), PGSTEAL_DIRECT,
nr_reclaimed);
}
#endif
putback_inactive_pages_async(lruvec, &page_list);
#if 0
__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
#endif
spin_unlock_irq(&pgdat->lru_lock);
mem_cgroup_uncharge_list_async(&page_list);
free_unref_page_list_async(&page_list);
#if 0
/*
* If dirty pages are scanned that are not queued for IO, it
* implies that flushers are not doing their job. This can
* happen when memory pressure pushes dirty pages to the end of
* the LRU before the dirty limits are breached and the dirty
* data has expired. It can also happen when the proportion of
* dirty pages grows not through writes but through memory
* pressure reclaiming all the clean cache. And in some cases,
* the flushers simply cannot keep up with the allocation
* rate. Nudge the flusher threads in case they are asleep.
*/
if (stat.nr_unqueued_dirty == nr_taken)
wakeup_flusher_threads(WB_REASON_VMSCAN);
#endif
sc->nr.dirty += stat.nr_dirty;
sc->nr.congested += stat.nr_congested;
sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
sc->nr.writeback += stat.nr_writeback;
sc->nr.immediate += stat.nr_immediate;
sc->nr.taken += nr_taken;
if (file)
sc->nr.file_taken += nr_taken;
#if 0
trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
nr_scanned, nr_reclaimed, &stat, sc->priority, file);
#endif
return nr_reclaimed;
}
#else
#endif
#endif
/*******以下是红帽9.2 5.14.0-284.11.1内核针对内核原生内存回收函数在本ko驱动的适配********************************************/
#elif LINUX_VERSION_CODE <= KERNEL_VERSION(5,14,0)
#ifdef USE_KERNEL_SHRINK_INACTIVE_LIST
static unsigned long (*isolate_lru_pages_async)(unsigned long nr_to_scan,struct lruvec *lruvec, struct list_head *dst,unsigned long *nr_scanned, struct scan_control_async *sc,enum lru_list lru);
static unsigned int (*shrink_page_list_async)(struct list_head *page_list,struct pglist_data *pgdat,struct scan_control_async *sc,struct reclaim_stat *stat,bool ignore_references);
static unsigned int (*move_pages_to_lru_async)(struct lruvec *lruvec,struct list_head *list);
#endif
static int(* __remove_mapping_async)(struct address_space *mapping, struct folio *folio,bool reclaimed, struct mem_cgroup *target_memcg);
static void (*mem_cgroup_update_lru_size_async)(struct lruvec *lruvec, enum lru_list lru,int zid, int nr_pages);
void (*free_unref_page_list_async)(struct list_head *list);
void mem_cgroup_uncharge_list_async(struct list_head *page_list);
static void (*__mem_cgroup_uncharge_list_async)(struct list_head *page_list);
static void (*__count_memcg_events_async)(struct mem_cgroup *memcg, enum vm_event_item idx,unsigned long count);
static unsigned long (*kallsyms_lookup_name_async)(const char *name);
static void (*putback_lru_page_async)(struct page *page);
static struct mem_cgroup *root_mem_cgroup_async;
static void (*try_to_unmap_flush_async)(void);
static void (*__mod_memcg_lruvec_state_async)(struct lruvec *lruvec, enum node_stat_item idx,int val);
static bool (*mem_cgroup_disabled_async)(void);
extern void __mod_lruvec_page_state(struct page *page, enum node_stat_item idx,int val);
compound_page_dtor * const (*compound_page_dtors_async)[NR_COMPOUND_DTORS];
static spinlock_t *sb_lock_async;
static struct list_head *super_blocks_async;
static void (*security_sb_free_async)(struct super_block *sb);
static void (*destroy_super_rcu_async)(struct rcu_head *head);
void (*cache_random_seq_destroy_async)(struct kmem_cache *cachep);
static void (*try_to_unmap_async)(struct folio *folio, enum ttu_flags flags);
int (*page_referenced_async)(struct folio *folio, int is_locked,struct mem_cgroup *memcg, unsigned long *vm_flags);
/* Check if a page is dirty or under writeback */
inline static void folio_check_dirty_writeback_async(struct folio *folio,
bool *dirty, bool *writeback)
{
struct address_space *mapping;
/*
* Anonymous pages are not handled by flushers and must be written
* from reclaim context. Do not stall reclaim based on them
*/
if (!folio_is_file_lru(folio) ||
(folio_test_anon(folio) && !folio_test_swapbacked(folio))) {
*dirty = false;
*writeback = false;
return;
}
/* By default assume that the folio flags are accurate */
*dirty = folio_test_dirty(folio);
*writeback = folio_test_writeback(folio);
/* Verify dirty/writeback state if the filesystem supports it */
if (!folio_test_private(folio))
return;
mapping = folio_mapping(folio);
if (mapping && mapping->a_ops->is_dirty_writeback)
mapping->a_ops->is_dirty_writeback(&folio->page, dirty, writeback);
}
static inline void destroy_compound_page_async(struct page *page)
{
VM_BUG_ON_PAGE(page[1].compound_dtor >= NR_COMPOUND_DTORS, page);
//编译通过了,但是这种通过函数指针数组引用函数指针的格式合法吗????????????????
(*compound_page_dtors_async[page[1].compound_dtor])(page);
}
void mem_cgroup_uncharge_list_async(struct list_head *page_list)
{
/*绝了,mem_cgroup_disabled()源码里是inline类型,但cat /proc/kallsyms 却可以看到mem_cgroup_disabled()。并且可以直接在ko里用
* 看着mem_cgroup_disabled()函数就是export的非inline类型函数,服了,有这结果估计是编译搞的?????*/
if (mem_cgroup_disabled_async())
return;
__mem_cgroup_uncharge_list_async(page_list);
}
static inline void count_memcg_events_async(struct mem_cgroup *memcg,
enum vm_event_item idx,
unsigned long count)
{
unsigned long flags;
local_irq_save(flags);
__count_memcg_events_async(memcg, idx, count);
local_irq_restore(flags);
}
static inline void count_memcg_page_event_async(struct page *page,
enum vm_event_item idx)
{
struct mem_cgroup *memcg = page_memcg(page);
if (memcg)
count_memcg_events_async(memcg, idx, 1);
}
static void __mod_lruvec_state_async(struct lruvec *lruvec, enum node_stat_item idx,
int val)
{
/* Update node */
__mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
if (!mem_cgroup_disabled_async())
__mod_memcg_lruvec_state_async(lruvec, idx, val);
}
static __always_inline void update_lru_size_async(struct lruvec *lruvec,
enum lru_list lru, enum zone_type zid,
long nr_pages)
{
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
__mod_lruvec_state_async(lruvec, NR_LRU_BASE + lru, nr_pages);
__mod_zone_page_state(&pgdat->node_zones[zid],
NR_ZONE_LRU_BASE + lru, nr_pages);
#ifdef CONFIG_MEMCG
mem_cgroup_update_lru_size_async(lruvec, lru, zid, nr_pages);
#endif
}
static __always_inline void lruvec_add_folio_async(struct lruvec *lruvec, struct folio *folio)
{
enum lru_list lru = folio_lru_list(folio);
update_lru_size_async(lruvec, lru, folio_zonenum(folio),
folio_nr_pages(folio));
if (lru != LRU_UNEVICTABLE)
list_add(&folio->lru, &lruvec->lists[lru]);
}
static __always_inline void add_page_to_lru_list_async(struct page *page,
struct lruvec *lruvec)
{
lruvec_add_folio_async(lruvec, page_folio(page));
}
static __always_inline void lruvec_del_folio_async(struct lruvec *lruvec, struct folio *folio)
{
enum lru_list lru = folio_lru_list(folio);
if (lru != LRU_UNEVICTABLE)
list_del(&folio->lru);
update_lru_size_async(lruvec, lru, folio_zonenum(folio),
-folio_nr_pages(folio));
}
static __always_inline void del_page_from_lru_list_async(struct page *page,
struct lruvec *lruvec)
{
lruvec_del_folio_async(lruvec, page_folio(page));
}
static __always_inline
void lruvec_add_folio_tail_async(struct lruvec *lruvec, struct folio *folio)
{
enum lru_list lru = folio_lru_list(folio);
update_lru_size_async(lruvec, lru, folio_zonenum(folio),folio_nr_pages(folio));
/* This is not expected to be used on LRU_UNEVICTABLE */
list_add_tail(&folio->lru, &lruvec->lists[lru]);
}
static __always_inline void add_page_to_lru_list_tail_async(struct page *page,
struct lruvec *lruvec)
{
lruvec_add_folio_tail_async(lruvec, page_folio(page));
}
struct lruvec *mem_cgroup_lruvec_async(struct mem_cgroup *memcg,
struct pglist_data *pgdat)
{
struct mem_cgroup_per_node *mz;
struct lruvec *lruvec;
if (mem_cgroup_disabled_async()) {
lruvec = &pgdat->__lruvec;
goto out;
}
if (!memcg)
memcg = root_mem_cgroup_async;
mz = memcg->nodeinfo[pgdat->node_id];
lruvec = &mz->lruvec;
out:
/*
* Since a node can be onlined after the mem_cgroup was created,
* we have to be prepared to initialize lruvec->pgdat here;
* and if offlined then reonlined, we need to reinitialize it.
*/
if (unlikely(lruvec->pgdat != pgdat))
lruvec->pgdat = pgdat;
return lruvec;
}
#ifndef USE_KERNEL_SHRINK_INACTIVE_LIST
static inline bool page_evictable_async(struct page *page)
{
bool ret;
rcu_read_lock();
ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
rcu_read_unlock();
return ret;
}
unsigned int async_shrink_free_page(struct pglist_data *pgdat,struct lruvec *lruvec,struct list_head *page_list,
struct scan_control_async *sc,struct reclaim_stat *stat)
{
LIST_HEAD(ret_pages);
LIST_HEAD(free_pages);
#if 0
LIST_HEAD(demote_pages);
bool do_demote_pass;
#endif
unsigned int nr_reclaimed = 0;
unsigned int pgactivate = 0;
unsigned int lock_fail_count = 0;
unsigned int writeback_count = 0;
unsigned int dirty_count = 0;
unsigned int page_has_private_count = 0;
unsigned int mapping_count = 0;
unsigned int free_pages_fail_count = 0;
unsigned int page_unevictable_count = 0;
unsigned int nr_unmap_fail = 0;
memset(stat, 0, sizeof(*stat));
cond_resched();
#if 0
//该场景根本用不到,注释掉得了。这里的sc是从vmscan.c直接复制过来的scan_control_async,必须要与内核的scan_control结构定义一模一样
do_demote_pass = can_demote_async(pgdat->node_id, sc);
retry:
#endif
while (!list_empty(page_list)) {
struct address_space *mapping;
struct page *page;
struct folio *folio;
//enum page_references references = PAGEREF_RECLAIM;
bool dirty, writeback, may_enter_fs;
unsigned int nr_pages;
cond_resched();
//注意,这里从page_list链表首先取出的是folio
folio = lru_to_folio(page_list);
list_del(&folio->lru);
//通过folio得到page
page = &folio->page;
if (!trylock_page(page)){
lock_fail_count ++;
goto keep;
}
//这个判断要注释掉,异步内存回收的page可能处于active lru链表
//VM_BUG_ON_PAGE(PageActive(page), page);
nr_pages = compound_nr(page);
/* Account the number of base pages even though THP */
sc->nr_scanned += nr_pages;
if (unlikely(!page_evictable_async(page))){
page_unevictable_count ++;
goto activate_locked;
}
#if 0
//强制不回收mmap的page
if (/*!sc->may_unmap &&*/ page_mapped(page))
goto keep_locked;
#endif
may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
/*
* The number of dirty pages determines if a node is marked
* reclaim_congested. kswapd will stall and start writing
* pages if the tail of the LRU is all dirty unqueued pages.
*/
folio_check_dirty_writeback_async(folio, &dirty, &writeback);
if (dirty || writeback)
stat->nr_dirty += nr_pages;
if (dirty && !writeback)
stat->nr_unqueued_dirty += nr_pages;
/*
* Treat this page as congested if the underlying BDI is or if
* pages are cycling through the LRU so quickly that the
* pages marked for immediate reclaim are making it to the
* end of the LRU a second time.
*/
mapping = page_mapping(page);
if (writeback && PageReclaim(page))
stat->nr_congested += nr_pages;
//遇到writebak页,不做任何处理,等脏页回写进程把它落盘。然后等几分钟后变成冷page,就会被异步回收掉。我的回收策略是只回收长时间不被冷page,这种page刚被访问过
if (PageWriteback(page)) {
writeback_count ++;
if(PageReclaim(page)){
SetPageReclaim(page);
stat->nr_writeback += nr_pages;
}else if (PageReclaim(page) &&test_bit(PGDAT_WRITEBACK, &pgdat->flags)){
stat->nr_immediate += nr_pages;
}
goto activate_locked;
}
/*
* Before reclaiming the page, try to relocate
* its contents to another node.
*/
#if 0
if (do_demote_pass &&
(thp_migration_supported() || !PageTransHuge(page))) {
list_add(&page->lru, &demote_pages);
unlock_page(page);
continue;
}
#endif
/*****这段代码是新内核加的?????????????*******************************/
/*
* THP may get split above, need minus tail pages and update
* nr_pages to avoid accounting tail pages twice.
*
* The tail pages that are added into swap cache successfully
* reach here.
*/
if ((nr_pages > 1) && !PageTransHuge(page)) {
sc->nr_scanned -= (nr_pages - 1);
nr_pages = 1;
}
/****page是mmap页*********************/
if (page_mapped(page)){
enum ttu_flags flags = TTU_BATCH_FLUSH;
try_to_unmap_async(folio, flags);
if (page_mapped(page)) {
nr_unmap_fail++;
goto activate_locked;
}
}
//为了保证内存回收绝对准确,一定得做一些可能发生的异常限制
if (PageTransHuge(page) || PageAnon(page) || PageSwapBacked(page))
panic("%s page:0x%llx page->flags:0x%lx",__func__,(u64)page,page->flags);
//遇到脏页,不做任何处理,等脏页回写进程把它落盘。然后等几分钟后变成冷page,就会被异步回收掉。我的回收策略是只回收长时间不被冷page,这种page刚被访问过
if (PageDirty(page)) {
dirty_count ++;
goto activate_locked;
//这里goto keep 分支,忘了unlock_page()了,导致其他进程访问到该page时因为page lock就休眠了!!!!!!!!!!!!!!!!
//goto keep;
}
if (page_has_private(page)) {
page_has_private_count ++;
if (!try_to_release_page(page, sc->gfp_mask)){
goto activate_locked;
}
if (!mapping && page_count(page) == 1) {
unlock_page(page);
if (put_page_testzero(page))
goto free_it;
else {
/*
* rare race with speculative reference.
* the speculative reference will free
* this page shortly, so we may
* increment nr_reclaimed here (and
* leave it off the LRU).
*/
nr_reclaimed++;
continue;
}
}
}
if (!mapping || !__remove_mapping_async(mapping, folio, true,
folio_memcg(folio)))
{
mapping_count ++;
goto keep_locked;
}
unlock_page(page);
free_it:
//如果要释放的page引用计数不是0,那就有问题了,主动触发crash
if(atomic_read(&page->_refcount) != 0){
panic("page:0x%llx refcount:%d error!!!!!\n",(u64)page,atomic_read(&page->_refcount));
}
/*
* THP may get swapped out in a whole, need account
* all base pages.
*/