[PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper

Kairui Song posted 6 patches 7 months, 3 weeks ago
There is a newer version of this series
[PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
Posted by Kairui Song 7 months, 3 weeks ago
From: Kairui Song <kasong@tencent.com>

There are no remaining users of folio_index() outside the mm subsystem.
Move it to mm/swap.h to co-locate it with swap_cache_index(), eliminating
a forward declaration, and a function call overhead.

Also remove the helper that was used to fix circular header dependency
issue.

Signed-off-by: Kairui Song <kasong@tencent.com>
---
 include/linux/pagemap.h | 20 --------------------
 mm/memfd.c              |  1 +
 mm/migrate.c            |  1 +
 mm/swap.h               | 18 ++++++++++++++++++
 mm/swapfile.c           |  6 ------
 5 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index a0bed4568c66..f61fdb8e165d 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -884,26 +884,6 @@ static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
 			mapping_gfp_mask(mapping));
 }
 
-extern pgoff_t __folio_swap_cache_index(struct folio *folio);
-
-/**
- * folio_index - File index of a folio.
- * @folio: The folio.
- *
- * For a folio which is either in the page cache or the swap cache,
- * return its index within the address_space it belongs to.  If you know
- * the page is definitely in the page cache, you can look at the folio's
- * index directly.
- *
- * Return: The index (offset in units of pages) of a folio in its file.
- */
-static inline pgoff_t folio_index(struct folio *folio)
-{
-	if (unlikely(folio_test_swapcache(folio)))
-		return __folio_swap_cache_index(folio);
-	return folio->index;
-}
-
 /**
  * folio_next_index - Get the index of the next folio.
  * @folio: The current folio.
diff --git a/mm/memfd.c b/mm/memfd.c
index c64df1343059..ab367e61553d 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -20,6 +20,7 @@
 #include <linux/memfd.h>
 #include <linux/pid_namespace.h>
 #include <uapi/linux/memfd.h>
+#include "swap.h"
 
 /*
  * We need a tag: a new tag would expand every xa_node by 8 bytes,
diff --git a/mm/migrate.c b/mm/migrate.c
index f3ee6d8d5e2e..662e5dc44b33 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -50,6 +50,7 @@
 #include <trace/events/migrate.h>
 
 #include "internal.h"
+#include "swap.h"
 
 bool isolate_movable_page(struct page *page, isolate_mode_t mode)
 {
diff --git a/mm/swap.h b/mm/swap.h
index 6f4a3f927edb..2f18d21c17fb 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -50,6 +50,24 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)
 	return swp_offset(entry) & SWAP_ADDRESS_SPACE_MASK;
 }
 
+/**
+ * folio_index - File index of a folio.
+ * @folio: The folio.
+ *
+ * For a folio which is either in the page cache or the swap cache,
+ * return its index within the address_space it belongs to.  If you know
+ * the page is definitely in the page cache, you can look at the folio's
+ * index directly.
+ *
+ * Return: The index (offset in units of pages) of a folio in its file.
+ */
+static inline pgoff_t folio_index(struct folio *folio)
+{
+	if (unlikely(folio_test_swapcache(folio)))
+		return swap_cache_index(folio->swap);
+	return folio->index;
+}
+
 void show_swap_cache_info(void);
 void *get_shadow_from_swap_cache(swp_entry_t entry);
 int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
diff --git a/mm/swapfile.c b/mm/swapfile.c
index e727021b8e2c..bf6c98009909 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3662,12 +3662,6 @@ struct address_space *swapcache_mapping(struct folio *folio)
 }
 EXPORT_SYMBOL_GPL(swapcache_mapping);
 
-pgoff_t __folio_swap_cache_index(struct folio *folio)
-{
-	return swap_cache_index(folio->swap);
-}
-EXPORT_SYMBOL_GPL(__folio_swap_cache_index);
-
 /*
  * add_swap_count_continuation - called when a swap count is duplicated
  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
-- 
2.49.0
Re: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
Posted by Matthew Wilcox 7 months, 3 weeks ago
On Mon, Apr 28, 2025 at 02:59:07AM +0800, Kairui Song wrote:
> +++ b/mm/swap.h
> @@ -50,6 +50,24 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)
>  	return swp_offset(entry) & SWAP_ADDRESS_SPACE_MASK;
>  }
>  
> +/**
> + * folio_index - File index of a folio.
> + * @folio: The folio.
> + *
> + * For a folio which is either in the page cache or the swap cache,
> + * return its index within the address_space it belongs to.  If you know
> + * the page is definitely in the page cache, you can look at the folio's
> + * index directly.
> + *
> + * Return: The index (offset in units of pages) of a folio in its file.
> + */
> +static inline pgoff_t folio_index(struct folio *folio)
> +{
> +	if (unlikely(folio_test_swapcache(folio)))
> +		return swap_cache_index(folio->swap);
> +	return folio->index;
> +}

I think the build warning can be cured by moving this outside
CONFIG_SWAP
Re: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
Posted by Kairui Song 7 months, 3 weeks ago
On Mon, Apr 28, 2025 at 7:58 AM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Mon, Apr 28, 2025 at 02:59:07AM +0800, Kairui Song wrote:
> > +++ b/mm/swap.h
> > @@ -50,6 +50,24 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)
> >       return swp_offset(entry) & SWAP_ADDRESS_SPACE_MASK;
> >  }
> >
> > +/**
> > + * folio_index - File index of a folio.
> > + * @folio: The folio.
> > + *
> > + * For a folio which is either in the page cache or the swap cache,
> > + * return its index within the address_space it belongs to.  If you know
> > + * the page is definitely in the page cache, you can look at the folio's
> > + * index directly.
> > + *
> > + * Return: The index (offset in units of pages) of a folio in its file.
> > + */
> > +static inline pgoff_t folio_index(struct folio *folio)
> > +{
> > +     if (unlikely(folio_test_swapcache(folio)))
> > +             return swap_cache_index(folio->swap);
> > +     return folio->index;
> > +}
>
> I think the build warning can be cured by moving this outside
> CONFIG_SWAP
>

Thanks, I will fix it in V2.
Re: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
Posted by kernel test robot 7 months, 3 weeks ago
Hi Kairui,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Kairui-Song/fuse-drop-usage-of-folio_index/20250428-030234
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250427185908.90450-6-ryncsn%40gmail.com
patch subject: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20250428/202504280527.sowjkuQU-lkp@intel.com/config)
compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project f819f46284f2a79790038e1f6649172789734ae8)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250428/202504280527.sowjkuQU-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202504280527.sowjkuQU-lkp@intel.com/

All errors (new ones prefixed by >>):

>> mm/page-writeback.c:2767:36: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    2767 |                 __xa_set_mark(&mapping->i_pages, folio_index(folio),
         |                                                  ^
   mm/page-writeback.c:2767:36: note: did you mean 'folio_inode'?
   include/linux/pagemap.h:567:29: note: 'folio_inode' declared here
     567 | static inline struct inode *folio_inode(struct folio *folio)
         |                             ^
   mm/page-writeback.c:3047:38: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3047 |                 __xa_clear_mark(&mapping->i_pages, folio_index(folio),
         |                                                    ^
   mm/page-writeback.c:3084:36: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3084 |                 XA_STATE(xas, &mapping->i_pages, folio_index(folio));
         |                                                  ^
   3 errors generated.
--
>> mm/gup.c:3652:21: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    3652 |                                     next_idx != folio_index(fbatch.folios[i]))
         |                                                 ^
   mm/gup.c:3652:21: note: did you mean 'folio_inode'?
   include/linux/pagemap.h:567:29: note: 'folio_inode' declared here
     567 | static inline struct inode *folio_inode(struct folio *folio)
         |                             ^
   1 error generated.


vim +/folio_index +2767 mm/page-writeback.c

b9ea25152e5636 Konstantin Khlebnikov   2015-04-14  2742  
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2743) /*
dc6e0ae5b1700c Kemeng Shi              2024-04-25  2744   * Mark the folio dirty, and set it dirty in the page cache.
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2745)  *
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2746)  * If warn is true, then emit a warning if the folio is not uptodate and has
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2747)  * not been truncated.
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2748)  *
a8cd9d4ce35eae Shakeel Butt            2024-10-24  2749   * It is the caller's responsibility to prevent the folio from being truncated
a8cd9d4ce35eae Shakeel Butt            2024-10-24  2750   * while this function is in progress, although it may have been truncated
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2751)  * before this function is called.  Most callers have the folio locked.
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2752)  * A few have the folio blocked from truncation through other means (e.g.
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2753)  * zap_vma_pages() has it mapped and is holding the page table lock).
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2754)  * When called from mark_buffer_dirty(), the filesystem should hold a
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2755)  * reference to the buffer_head that is being marked dirty, which causes
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2756)  * try_to_free_buffers() to fail.
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2757)  */
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2758) void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2759) 			     int warn)
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2760) {
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2761) 	unsigned long flags;
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2762) 
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2763) 	xa_lock_irqsave(&mapping->i_pages, flags);
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2764) 	if (folio->mapping) {	/* Race with truncate? */
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2765) 		WARN_ON_ONCE(warn && !folio_test_uptodate(folio));
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2766) 		folio_account_dirtied(folio, mapping);
203a3151661611 Matthew Wilcox (Oracle  2021-05-04 @2767) 		__xa_set_mark(&mapping->i_pages, folio_index(folio),
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2768) 				PAGECACHE_TAG_DIRTY);
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2769) 	}
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2770) 	xa_unlock_irqrestore(&mapping->i_pages, flags);
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2771) }
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2772) 

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
Posted by kernel test robot 7 months, 3 weeks ago
Hi Kairui,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Kairui-Song/fuse-drop-usage-of-folio_index/20250428-030234
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250427185908.90450-6-ryncsn%40gmail.com
patch subject: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20250428/202504280515.4WwtyfOc-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250428/202504280515.4WwtyfOc-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202504280515.4WwtyfOc-lkp@intel.com/

All errors (new ones prefixed by >>):

   mm/page-writeback.c: In function '__folio_mark_dirty':
>> mm/page-writeback.c:2767:50: error: implicit declaration of function 'folio_index'; did you mean 'folio_inode'? [-Wimplicit-function-declaration]
    2767 |                 __xa_set_mark(&mapping->i_pages, folio_index(folio),
         |                                                  ^~~~~~~~~~~
         |                                                  folio_inode
--
   mm/gup.c: In function 'memfd_pin_folios':
>> mm/gup.c:3652:49: error: implicit declaration of function 'folio_index'; did you mean 'folio_inode'? [-Wimplicit-function-declaration]
    3652 |                                     next_idx != folio_index(fbatch.folios[i]))
         |                                                 ^~~~~~~~~~~
         |                                                 folio_inode


vim +2767 mm/page-writeback.c

b9ea25152e5636 Konstantin Khlebnikov   2015-04-14  2742  
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2743) /*
dc6e0ae5b1700c Kemeng Shi              2024-04-25  2744   * Mark the folio dirty, and set it dirty in the page cache.
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2745)  *
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2746)  * If warn is true, then emit a warning if the folio is not uptodate and has
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2747)  * not been truncated.
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2748)  *
a8cd9d4ce35eae Shakeel Butt            2024-10-24  2749   * It is the caller's responsibility to prevent the folio from being truncated
a8cd9d4ce35eae Shakeel Butt            2024-10-24  2750   * while this function is in progress, although it may have been truncated
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2751)  * before this function is called.  Most callers have the folio locked.
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2752)  * A few have the folio blocked from truncation through other means (e.g.
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2753)  * zap_vma_pages() has it mapped and is holding the page table lock).
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2754)  * When called from mark_buffer_dirty(), the filesystem should hold a
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2755)  * reference to the buffer_head that is being marked dirty, which causes
3d84d897920c75 Matthew Wilcox (Oracle  2024-04-16  2756)  * try_to_free_buffers() to fail.
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2757)  */
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2758) void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2759) 			     int warn)
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2760) {
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2761) 	unsigned long flags;
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2762) 
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2763) 	xa_lock_irqsave(&mapping->i_pages, flags);
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2764) 	if (folio->mapping) {	/* Race with truncate? */
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2765) 		WARN_ON_ONCE(warn && !folio_test_uptodate(folio));
203a3151661611 Matthew Wilcox (Oracle  2021-05-04  2766) 		folio_account_dirtied(folio, mapping);
203a3151661611 Matthew Wilcox (Oracle  2021-05-04 @2767) 		__xa_set_mark(&mapping->i_pages, folio_index(folio),
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2768) 				PAGECACHE_TAG_DIRTY);
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2769) 	}
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2770) 	xa_unlock_irqrestore(&mapping->i_pages, flags);
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2771) }
6e1cae881a0646 Matthew Wilcox (Oracle  2021-06-28  2772) 

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki