Skip to contentSkip to conversation

mm: move folio LRU helpers out of swap

PATCH In review Jianyue Wu 7 participants 15 messages Apr 7 – Apr 8

Subsystems (3)
lore
Share & tools
lore

Jianyue Wu

author
#0 lore raw

PATCH 1/1 mm: move folio LRU helpers out of swap

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Here move folio lru helpers out of mm/swap.c and put them into file for their own.

This is cleanup, no functional change involved.

---
 MAINTAINERS          |    2 +
 include/linux/mm.h   |    2 +
 include/linux/swap.h |   38 --
 mm/Makefile          |    2 +-
 mm/folio_lru.c       | 1074 ++++++++++++++++++++++++++++++++++++++++++
 mm/folio_lru.h       |   53 +++
 mm/swap.c            | 1064 +----------------------------------------
 7 files changed, 1133 insertions(+), 1102 deletions(-)
 create mode 100644 mm/folio_lru.c
 create mode 100644 mm/folio_lru.h
MAINTAINERS +2 -0
index 77fdfcb55f06..7dc04858de66 100644
@@ -16867,6 +16867,8 @@ R:	Shakeel Butt <[email protected]>
 R:	Lorenzo Stoakes <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	mm/folio_lru.c
+F:	mm/folio_lru.h
 F:	mm/vmscan.c
 F:	mm/workingset.c
 
include/linux/mm.h +2 -0
index 5be3d8a8f806..8cb54a5f47c1 100644
@@ -39,6 +39,8 @@
 #include <linux/bitops.h>
 #include <linux/iommu-debug-pagealloc.h>
 
+#include "../../mm/folio_lru.h"
+
 struct mempolicy;
 struct anon_vma;
 struct anon_vma_chain;
include/linux/swap.h +0 -38
index 62fc7499b408..4de5e441576c 100644
@@ -327,44 +327,6 @@ extern unsigned long totalreserve_pages;
 
 
 /* linux/mm/swap.c */
-void lru_note_cost_unlock_irq(struct lruvec *lruvec, bool file,
-		unsigned int nr_io, unsigned int nr_rotated)
-		__releases(lruvec->lru_lock);
-void lru_note_cost_refault(struct folio *);
-void folio_add_lru(struct folio *);
-void folio_add_lru_vma(struct folio *, struct vm_area_struct *);
-void mark_page_accessed(struct page *);
-void folio_mark_accessed(struct folio *);
-
-static inline bool folio_may_be_lru_cached(struct folio *folio)
-{
-	/*
-	 * Holding PMD-sized folios in per-CPU LRU cache unbalances accounting.
-	 * Holding small numbers of low-order mTHP folios in per-CPU LRU cache
-	 * will be sensible, but nobody has implemented and tested that yet.
-	 */
-	return !folio_test_large(folio);
-}
-
-extern atomic_t lru_disable_count;
-
-static inline bool lru_cache_disabled(void)
-{
-	return atomic_read(&lru_disable_count);
-}
-
-static inline void lru_cache_enable(void)
-{
-	atomic_dec(&lru_disable_count);
-}
-
-extern void lru_cache_disable(void);
-extern void lru_add_drain(void);
-extern void lru_add_drain_cpu(int cpu);
-extern void lru_add_drain_cpu_zone(struct zone *zone);
-extern void lru_add_drain_all(void);
-void folio_deactivate(struct folio *folio);
-void folio_mark_lazyfree(struct folio *folio);
 extern void swap_setup(void);
 
 /* linux/mm/vmscan.c */
mm/Makefile +1 -1
index 8ad2ab08244e..f2e31f2dd3ff 100644
@@ -50,7 +50,7 @@ endif
 
 obj-y			:= filemap.o mempool.o oom_kill.o fadvise.o \
 			   maccess.o page-writeback.o folio-compat.o \
-			   readahead.o swap.o truncate.o vmscan.o shrinker.o \
+			   readahead.o folio_lru.o swap.o truncate.o vmscan.o shrinker.o \
 			   shmem.o util.o mmzone.o vmstat.o backing-dev.o \
 			   mm_init.o percpu.o slab_common.o \
 			   compaction.o show_mem.o \
mm/folio_lru.c new file +1074 -0

1,074 lines not on this page · raw message

mm/folio_lru.h new file +53 -0

53 lines not on this page · raw message

mm/swap.c +1 -1063

1,076 lines not on this page · raw message

--
2.43.0

reviewer
#1 lore raw
Quoted 11 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Here move folio lru helpers out of mm/swap.c and put them into file for their own.

This is cleanup, no functional change involved.

Quoted 8 lines
---
 MAINTAINERS          |    2 +
 include/linux/mm.h   |    2 +
 include/linux/swap.h |   38 --
 mm/Makefile          |    2 +-
 mm/folio_lru.c       | 1074 ++++++++++++++++++++++++++++++++++++++++++
 mm/folio_lru.h       |   53 +++
 mm/swap.c            | 1064 +----------------------------------------

All that big churn is really worth it? Are there any other reasons than "not so appropriate"?

Really if this is not a part of a much bigger plan then NAK.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#2 lore raw
Quoted 5 lines · Michal Hocko

All that big churn is really worth it? Are there any other reasons than "not so appropriate"?

Really if this is not a part of a much bigger plan then NAK.

Hi Michal,

The intent here is to keep generic LRU/reclaim helpers separate from swap-specific code paths.

The LRU helpers are shared by both file and anonymous memory, whereas swap.c is meant to host logic tied to swap devices and swap entries. Placing LRU code there would blur the boundary between general reclaim and swap, and make the code harder to follow.

Separately, I’ve been looking at routing zram’s swap-slot handling through swap-owned hooks (e.g., swap_ops / swapon probing), which would involve swapfile.c and swap.h. That’s likely orthogonal to this LRU move, but it’s driven by the same goal of clarifying the swap boundary.

Best regards,
Jianyue Wu

reviewer
#3 lore raw
Quoted 11 lines · Jianyue Wu
Quoted 5 lines · Michal Hocko

All that big churn is really worth it? Are there any other reasons than "not so appropriate"?

Really if this is not a part of a much bigger plan then NAK.

Hi Michal,

The intent here is to keep generic LRU/reclaim helpers separate from swap-specific code paths.

OK, but why do we want this and cause a lot of code churn to achiev that?

Quoted 4 lines

The LRU helpers are shared by both file and anonymous memory, whereas swap.c is meant to host logic tied to swap devices and swap entries. Placing LRU code there would blur the boundary between general reclaim and swap, and make the code harder to follow.

The code is harder to follow than it could be, no questions about that. But that alone is a rather weak argument to justify a lot of code move that cause a lot of conflicts.

Quoted 4 lines

Separately, I’ve been looking at routing zram’s swap-slot handling through swap-owned hooks (e.g., swap_ops / swapon probing), which would involve swapfile.c and swap.h. That’s likely orthogonal to this LRU move, but it’s driven by the same goal of clarifying the swap boundary.

Does that work benefits huge by this work?

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
In reply to Jianyue Wu
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

A note that shmem also uses swap.

Signature · 3 lines
Cheers,

David
author
#5 lore raw
Quoted 3 lines · David Hildenbrand (Arm)

A note that shmem also uses swap.

Hi David,

Thanks for the hint. I agree shmem participates in the same anonymous/swap-side paths. I'll fix the commit message in the next revision to say that clearly.

Best regards,
Jianyue Wu

Quoted 5 lines · Michal Hocko

All that big churn is really worth it? Are there any other reasons than "not so appropriate"?

Really if this is not a part of a much bigger plan then NAK.

Hi Michal,

The intent here is to keep generic LRU/reclaim helpers separate from swap-specific code paths.

The LRU helpers are shared by both file and anonymous memory, whereas swap.c is meant to host logic tied to swap devices and swap entries. Placing LRU code there would blur the boundary between general reclaim and swap, and make the code harder to follow.

Separately, I’ve been looking at routing zram’s swap-slot handling through swap-owned hooks (e.g., swap_ops / swapon probing), which would involve swapfile.c and swap.h. That’s likely orthogonal to this LRU move, but it’s driven by the same goal of clarifying the swap boundary.

Best regards,
Jianyue Wu

#6 lore raw
In reply to Jianyue Wu

include/linux/mm.h line 42

Quoted 6 lines · Jianyue Wu

+++ b/include/linux/mm.h

include/linux/mm.h +1 -0
@@ -39,6 +39,8 @@
 #include <linux/bitops.h>
 #include <linux/iommu-debug-pagealloc.h>
 
+#include "../../mm/folio_lru.h"

This is enough to reject this patch.

I don't think the patch is a good idea either. Yes, the current situation is messy, but you're not improving things by moving it to its own file.

reviewer
#7 lore raw
Quoted 10 lines · Matthew Wilcox

include/linux/mm.h line 42

Quoted 6 lines · Jianyue Wu

+++ b/include/linux/mm.h

include/linux/mm.h +1 -0
@@ -39,6 +39,8 @@
 #include <linux/bitops.h>
 #include <linux/iommu-debug-pagealloc.h>
 
+#include "../../mm/folio_lru.h"

This is enough to reject this patch.

It’s really odd for a top-level include to pull in a low-level header file.

it seems we do have some code outside mm who needs lru APIs:

   1   1036  fs/fuse/dev.c <<fuse_try_move_folio>>
             folio_add_lru(newfolio);

1 868 arch/x86/kvm/svm/sev.c <<sev_launch_update_data>>

             mark_page_accessed(inpages[i]);
   2   1440  arch/x86/kvm/svm/sev.c <<sev_launch_secret>>
             mark_page_accessed(pages[i]);
   3    123  drivers/gpu/drm/i915/gem/i915_gem_phys.c
<<i915_gem_object_put_pages_phys>>
             mark_page_accessed(page);
   4    203  drivers/gpu/drm/i915/gem/i915_gem_userptr.c
<<i915_gem_userptr_put_pages>>
             mark_page_accessed(page);
   5    119  drivers/gpu/drm/i915/gt/shmem_utils.c <<__shmem_rw>>
             mark_page_accessed(page);
   6    150  drivers/gpu/drm/i915/gt/shmem_utils.c <<shmem_read_to_iosys_map>>
             mark_page_accessed(page);
   7    406  drivers/gpu/drm/radeon/radeon_ttm.c <<radeon_ttm_tt_unpin_userptr>>
             mark_page_accessed(page);
   8    353  drivers/gpu/drm/ttm/ttm_tt.c <<ttm_tt_swapout>>
             mark_page_accessed(to_page);
   9    571  fs/hfs/bnode.c <<hfs_bnode_put>>
             mark_page_accessed(node->page[i]);
  10    682  fs/hfsplus/bnode.c <<hfs_bnode_put>>
             mark_page_accessed(node->page[i]);
  11   2801  virt/kvm/kvm_main.c <<kvm_set_page_accessed>>
             mark_page_accessed(page);

but for the belows, it seems they are safe to be not in the top-level include - I mean include/linux/swap.h:

workingset_age_nonresident();
workingset_test_recent();
workingset_refault();
workingset_eviction();
workingset_activation();
lru_note_cost_unlock_irq();
void folio_add_lru_vma();
void lru_note_cost_refault();
void folio_add_lru_vma();
...

Many of them are essentially MM-specific.

Quoted 4 lines

I don't think the patch is a good idea either. Yes, the current situation is messy, but you're not improving things by moving it to its own file.

Thanks
Barry

#8 lore raw
In reply to Jianyue Wu
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

author
#9 lore raw
Quoted 6 lines · Johannes Weiner

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

Thanks everyone for the reviews and the clear feedback.

I’m withdrawing this patch. I understand the concerns about the relative include path, the limited benefit versus churn/conflicts. I learned a lot from the discussion, many details in mm parts.

Thanks again for your time.

Best regards,
Jianyue Wu

Quoted 13 lines · Johannes Weiner
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

reviewer
#10 lore raw
Quoted 9 lines · Jianyue Wu
Quoted 6 lines · Johannes Weiner

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

Quoted 5 lines

Thanks everyone for the reviews and the clear feedback.

I’m withdrawing this patch. I understand the concerns about the relative include path, the limited benefit versus churn/conflicts. I learned a lot from the discussion, many details in mm parts.

Please don’t abandon this direction — it just needs a better approach. While this patch isn’t perfect, cleaning up include/linux/swap.h and mm/swap.c seems like a worthwhile path. include/linux/swap.h is basically a tangled mess.

Thanks
Barry

#11 lore raw
In reply to Johannes Weiner
Quoted 6 lines · Johannes Weiner
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Quoted 6 lines

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

Can't agree more. We should move page_cluster & sysctl and swap_setup() to mm/swap_state.c, and rename mm/swap.c to mm/folio_lru.c or something like that.

reviewer
#12 lore raw
Quoted 11 lines · Baoquan He
Quoted 9 lines · Johannes Weiner
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

Quoted 7 lines
Quoted 3 lines

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

Can't agree more. We should move page_cluster & sysctl and swap_setup() to mm/swap_state.c, and rename mm/swap.c to mm/folio_lru.c or something like that.

Another issue is that many MM-internal items are placed in the top-level include/linux/swap.h, which is supposed to be used by code outside the MM subsystem as well.

For example, the following are actually MM-internal:

extern int vm_swappiness;
swap_setup();
workingset_*();
try_to_free_pages();
zone_reclaimable_pages();
lruvec_lru_size();
try_to_free_mem_cgroup_pages();
...

Thanks
Barry

#13 lore raw
Quoted 26 lines · Barry Song
Quoted 11 lines · Baoquan He
Quoted 9 lines · Johannes Weiner
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

Quoted 7 lines
Quoted 3 lines

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

Can't agree more. We should move page_cluster & sysctl and swap_setup() to mm/swap_state.c, and rename mm/swap.c to mm/folio_lru.c or something like that.

Another issue is that many MM-internal items are placed in the top-level include/linux/swap.h, which is supposed to be used by code outside the MM subsystem as well.

For example, the following are actually MM-internal:

Quoted 8 lines

extern int vm_swappiness;
swap_setup();
workingset_*();
try_to_free_pages();
zone_reclaimable_pages();
lruvec_lru_size();
try_to_free_mem_cgroup_pages();
...

Ah, indeed. Seems we have been putting generic code related to reclaim into swap.h|.c due to historic reason. From discussion, I think the below three things are doable.

1) move page_cluster & sysctl and swap_setup() into mm/swap_state.c because page_cluster is only used in mm/swap_state.c as Johannes suggestd. And swap_setup() may need be renamed too?

2) rename mm/swap.c to mm/folio_lru.c or something like that to reflect its real content;

3) Just Barry pointed out, move generic code related to reclaim out of include/linux/swap.h e.g move into mm/internal.h if they are only called inside mm/ folder,

Thanks
Baoquan

author
#14 lore raw
Quoted 47 lines · Baoquan He
Quoted 26 lines · Barry Song
Quoted 11 lines · Baoquan He
Quoted 9 lines · Johannes Weiner
Quoted 4 lines · Jianyue Wu

All allocated folios are added into lru lists for later reclaim whether they are file or anonymous folios. However those folio lru operation codes are put in mm/swap.c which is not so appropriate. Because swap code is only for anonymous folios.

Ironically, the only thing that doesn't fit in this file is the swap stuff. The page_cluster & sysctl should be static in swap_state.c.

Quoted 7 lines
Quoted 3 lines

The rest seems thematically relatively clean - infrastructure for managing folio lifetime. swap.c is not the best name for it, but short of finding a very clear improvement I'd just leave it alone.

Can't agree more. We should move page_cluster & sysctl and swap_setup() to mm/swap_state.c, and rename mm/swap.c to mm/folio_lru.c or something like that.

Another issue is that many MM-internal items are placed in the top-level include/linux/swap.h, which is supposed to be used by code outside the MM subsystem as well.

For example, the following are actually MM-internal:

Quoted 8 lines

extern int vm_swappiness;
swap_setup();
workingset_*();
try_to_free_pages();
zone_reclaimable_pages();
lruvec_lru_size();
try_to_free_mem_cgroup_pages();
...

Ah, indeed. Seems we have been putting generic code related to reclaim into swap.h|.c due to historic reason. From discussion, I think the below three things are doable.

1) move page_cluster & sysctl and swap_setup() into mm/swap_state.c because page_cluster is only used in mm/swap_state.c as Johannes suggestd. And swap_setup() may need be renamed too?

2) rename mm/swap.c to mm/folio_lru.c or something like that to reflect its real content;

Quoted 7 lines

3) Just Barry pointed out, move generic code related to reclaim out of include/linux/swap.h e.g move into mm/internal.h if they are only called inside mm/ folder,

Thanks
Baoquan

Thank you all for the hint and the summary.

This direction looks reasonable to me. I'll start with moving page_cluster and the related sysctl into mm/swap_state.c, and see how swap_setup() should be adjusted accordingly.

For the other parts, especially the header cleanup, I'll take a closer look and follow up with patches.

Best regards,
Jianyue Wu