linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/util: reduce stack usage of folio_mapcount
@ 2022-08-01 17:31 Kairui Song
  2022-08-11 23:07 ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Kairui Song @ 2022-08-01 17:31 UTC (permalink / raw)
  To: linux-mm; +Cc: linux-kernel, Matthew Wilcox, Andrew Morton, Kairui Song

From: Kairui Song <kasong@tencent.com>

folio_entire_mapcount will call PageHeadHuge which is a function call,
and blocks the compiler from recognizing this redundant load.

After rearranging the code, stack usage is dropped from 32 to 24, and
the function size is smaller (tested on GCC 12):

Before:
Stack usage:
mm/util.c:845:5:folio_mapcount  32      static
Size:
0000000000000ea0 00000000000000c7 T folio_mapcount

After:
Stack usage:
mm/util.c:845:5:folio_mapcount  24      static
Size:
0000000000000ea0 00000000000000b0 T folio_mapcount

Signed-off-by: Kairui Song <kasong@tencent.com>
---
 mm/util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/util.c b/mm/util.c
index 0837570c9225..98a589bb89c9 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -850,10 +850,10 @@ int folio_mapcount(struct folio *folio)
 		return atomic_read(&folio->_mapcount) + 1;
 
 	compound = folio_entire_mapcount(folio);
-	nr = folio_nr_pages(folio);
 	if (folio_test_hugetlb(folio))
 		return compound;
 	ret = compound;
+	nr = folio_nr_pages(folio);
 	for (i = 0; i < nr; i++)
 		ret += atomic_read(&folio_page(folio, i)->_mapcount) + 1;
 	/* File pages has compound_mapcount included in _mapcount */
-- 
2.35.2



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/util: reduce stack usage of folio_mapcount
  2022-08-01 17:31 [PATCH] mm/util: reduce stack usage of folio_mapcount Kairui Song
@ 2022-08-11 23:07 ` Andrew Morton
  2022-08-12  4:49   ` Kairui Song
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2022-08-11 23:07 UTC (permalink / raw)
  To: Kairui Song; +Cc: Kairui Song, linux-mm, linux-kernel, Matthew Wilcox

On Tue,  2 Aug 2022 01:31:55 +0800 Kairui Song <ryncsn@gmail.com> wrote:

> From: Kairui Song <kasong@tencent.com>
> 
> folio_entire_mapcount will call PageHeadHuge which is a function call,
> and blocks the compiler from recognizing this redundant load.

Did you mean folio_test_hugetlb() rather than folio_entire_mapcount()?


> After rearranging the code, stack usage is dropped from 32 to 24, and
> the function size is smaller (tested on GCC 12):
> 
> Before:
> Stack usage:
> mm/util.c:845:5:folio_mapcount  32      static
> Size:
> 0000000000000ea0 00000000000000c7 T folio_mapcount
> 
> After:
> Stack usage:
> mm/util.c:845:5:folio_mapcount  24      static
> Size:
> 0000000000000ea0 00000000000000b0 T folio_mapcount
> 
> ...
>
> @@ -850,10 +850,10 @@ int folio_mapcount(struct folio *folio)
>  		return atomic_read(&folio->_mapcount) + 1;
>  
>  	compound = folio_entire_mapcount(folio);
> -	nr = folio_nr_pages(folio);
>  	if (folio_test_hugetlb(folio))
>  		return compound;
>  	ret = compound;
> +	nr = folio_nr_pages(folio);
>  	for (i = 0; i < nr; i++)
>  		ret += atomic_read(&folio_page(folio, i)->_mapcount) + 1;
>  	/* File pages has compound_mapcount included in _mapcount */
> -- 
> 2.35.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/util: reduce stack usage of folio_mapcount
  2022-08-11 23:07 ` Andrew Morton
@ 2022-08-12  4:49   ` Kairui Song
  0 siblings, 0 replies; 3+ messages in thread
From: Kairui Song @ 2022-08-12  4:49 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-mm, linux-kernel, Matthew Wilcox

Andrew Morton <akpm@linux-foundation.org> 于2022年8月12日周五 07:07写道:
>
> On Tue,  2 Aug 2022 01:31:55 +0800 Kairui Song <ryncsn@gmail.com> wrote:
>
> > From: Kairui Song <kasong@tencent.com>
> >
> > folio_entire_mapcount will call PageHeadHuge which is a function call,
> > and blocks the compiler from recognizing this redundant load.
>
> Did you mean folio_test_hugetlb() rather than folio_entire_mapcount()?

Thanks for checking out this patch, and Yes, it's folio_test_hugetlb,
my mistake...

>
>
> > After rearranging the code, stack usage is dropped from 32 to 24, and
> > the function size is smaller (tested on GCC 12):
> >
> > Before:
> > Stack usage:
> > mm/util.c:845:5:folio_mapcount  32      static
> > Size:
> > 0000000000000ea0 00000000000000c7 T folio_mapcount
> >
> > After:
> > Stack usage:
> > mm/util.c:845:5:folio_mapcount  24      static
> > Size:
> > 0000000000000ea0 00000000000000b0 T folio_mapcount
> >
> > ...
> >
> > @@ -850,10 +850,10 @@ int folio_mapcount(struct folio *folio)
> >               return atomic_read(&folio->_mapcount) + 1;
> >
> >       compound = folio_entire_mapcount(folio);
> > -     nr = folio_nr_pages(folio);
> >       if (folio_test_hugetlb(folio))
> >               return compound;
> >       ret = compound;
> > +     nr = folio_nr_pages(folio);
> >       for (i = 0; i < nr; i++)
> >               ret += atomic_read(&folio_page(folio, i)->_mapcount) + 1;
> >       /* File pages has compound_mapcount included in _mapcount */
> > --
> > 2.35.2

Is the rest of the patch a valid fix? Should I send V2?


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-12  4:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-01 17:31 [PATCH] mm/util: reduce stack usage of folio_mapcount Kairui Song
2022-08-11 23:07 ` Andrew Morton
2022-08-12  4:49   ` Kairui Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).