linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/mm: use __auto_type in swap() macro
@ 2025-07-30 14:23 Pranav Tyagi
  2025-08-06 15:45 ` Pranav Tyagi
  2025-08-06 16:48 ` Joshua Hahn
  0 siblings, 2 replies; 6+ messages in thread
From: Pranav Tyagi @ 2025-07-30 14:23 UTC (permalink / raw)
  To: akpm, peterx, shuah, linux-mm, linux-kselftest, linux-kernel
  Cc: linux-kernel-mentees, Pranav Tyagi

Replace typeof() with __auto_type in the swap() macro in uffd-stress.c.
__auto_type was introduced in GCC 4.9 and reduces the compile time for
all compilers. No functional changes intended.

Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
---
 tools/testing/selftests/mm/uffd-stress.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
index 40af7f67c407..c0f64df5085c 100644
--- a/tools/testing/selftests/mm/uffd-stress.c
+++ b/tools/testing/selftests/mm/uffd-stress.c
@@ -51,7 +51,7 @@ static char *zeropage;
 pthread_attr_t attr;
 
 #define swap(a, b) \
-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+	do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
 const char *examples =
 	"# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
-- 
2.49.0


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

* Re: [PATCH] selftests/mm: use __auto_type in swap() macro
  2025-07-30 14:23 [PATCH] selftests/mm: use __auto_type in swap() macro Pranav Tyagi
@ 2025-08-06 15:45 ` Pranav Tyagi
  2025-08-06 16:46   ` Peter Xu
  2025-08-06 16:48 ` Joshua Hahn
  1 sibling, 1 reply; 6+ messages in thread
From: Pranav Tyagi @ 2025-08-06 15:45 UTC (permalink / raw)
  To: akpm, peterx, shuah, linux-mm, linux-kselftest, linux-kernel
  Cc: linux-kernel-mentees

On Wed, Jul 30, 2025 at 7:53 PM Pranav Tyagi <pranav.tyagi03@gmail.com> wrote:
>
> Replace typeof() with __auto_type in the swap() macro in uffd-stress.c.
> __auto_type was introduced in GCC 4.9 and reduces the compile time for
> all compilers. No functional changes intended.
>
> Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> ---
>  tools/testing/selftests/mm/uffd-stress.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
> index 40af7f67c407..c0f64df5085c 100644
> --- a/tools/testing/selftests/mm/uffd-stress.c
> +++ b/tools/testing/selftests/mm/uffd-stress.c
> @@ -51,7 +51,7 @@ static char *zeropage;
>  pthread_attr_t attr;
>
>  #define swap(a, b) \
> -       do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> +       do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
>
>  const char *examples =
>         "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
> --
> 2.49.0
>

Hi,

Just a gentle follow-up on this cleanup patch. From what I could find,
this is the only use of
typeof() left in the mm selftests, so this should be the only instance
needing this change.

Thanks for considering!

Regards
Pranav Tyagi

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

* Re: [PATCH] selftests/mm: use __auto_type in swap() macro
  2025-08-06 15:45 ` Pranav Tyagi
@ 2025-08-06 16:46   ` Peter Xu
  2025-08-26 16:23     ` Pranav Tyagi
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Xu @ 2025-08-06 16:46 UTC (permalink / raw)
  To: Pranav Tyagi
  Cc: akpm, shuah, linux-mm, linux-kselftest, linux-kernel,
	linux-kernel-mentees

On Wed, Aug 06, 2025 at 09:15:50PM +0530, Pranav Tyagi wrote:
> On Wed, Jul 30, 2025 at 7:53 PM Pranav Tyagi <pranav.tyagi03@gmail.com> wrote:
> >
> > Replace typeof() with __auto_type in the swap() macro in uffd-stress.c.
> > __auto_type was introduced in GCC 4.9 and reduces the compile time for
> > all compilers. No functional changes intended.
> >
> > Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> > ---
> >  tools/testing/selftests/mm/uffd-stress.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
> > index 40af7f67c407..c0f64df5085c 100644
> > --- a/tools/testing/selftests/mm/uffd-stress.c
> > +++ b/tools/testing/selftests/mm/uffd-stress.c
> > @@ -51,7 +51,7 @@ static char *zeropage;
> >  pthread_attr_t attr;
> >
> >  #define swap(a, b) \
> > -       do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> > +       do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> >
> >  const char *examples =
> >         "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
> > --
> > 2.49.0
> >
> 
> Hi,
> 
> Just a gentle follow-up on this cleanup patch. From what I could find,
> this is the only use of
> typeof() left in the mm selftests, so this should be the only instance
> needing this change.
> 
> Thanks for considering!

Hi,

Andrew should have queued this one in branch akpm/mm-nonmm-unstable (even
though I'm not familiar with the branch).

Said that, I'm also not familiar with __auto_type.  Looks like it's more
efficiently processed by the compiler in some special use cases, however
it's also new so maybe some tools (sparse?) may not recognize it.

Is it the plan that the whole Linux kernel is moving towards __auto_type?
I still see quite a few of typeof() usages (not "a few", but 2966 instances).

Thanks,

-- 
Peter Xu


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

* Re: [PATCH] selftests/mm: use __auto_type in swap() macro
  2025-07-30 14:23 [PATCH] selftests/mm: use __auto_type in swap() macro Pranav Tyagi
  2025-08-06 15:45 ` Pranav Tyagi
@ 2025-08-06 16:48 ` Joshua Hahn
  2025-08-26 16:26   ` Pranav Tyagi
  1 sibling, 1 reply; 6+ messages in thread
From: Joshua Hahn @ 2025-08-06 16:48 UTC (permalink / raw)
  To: Pranav Tyagi
  Cc: akpm, peterx, shuah, linux-mm, linux-kselftest, linux-kernel,
	linux-kernel-mentees

On Wed, 30 Jul 2025 19:53:01 +0530 Pranav Tyagi <pranav.tyagi03@gmail.com> wrote:

Hi Pranav,

Thank you for this patch! Sorry for the late response, thank you for bumping
the patch. I just have a few comments about the commit message.

> Replace typeof() with __auto_type in the swap() macro in uffd-stress.c.
> __auto_type was introduced in GCC 4.9 and reduces the compile time for
> all compilers. No functional changes intended.

From what I understand, the compiler optimizations from typeof() --> __auto_type
applies when the argument is a variably modified type. It seems like this
internal definition of swap() is only used within selftests/mm/uffd-stress.c,
and in particular is only used twice, in these two lines:

/* prepare next bounce */
swap(area_src, area_dst);

swap(area_src_alias, area_dst_alias);

Where area_src, area_dst, area_src_alias, area_dst_alias are all char * which
the compiler knows the size of at compile time. Given this, I wonder if there
are any compile time speedups.

But this is just my understanding, based on a quick look at the code. Please
feel free to correct me, if I am incorrectly understanding your changes or if
my understanding of __auto_type is incorrect : -)

With that said, I think the main benefit that we get from using __auto_type
has more to do with readability. Since __auto_type can only be used to
declare local variables (as we are doing here), maybe we can rephrase the
commit to be about improving readability, and not compile time?

Again, please let me know if I am overlooking something.

One other thought -- while this internal definition of swap() is confined to
selftests/mm/uffd-stress.c, there is another identical definition in
include/linux/minmax.h that may be used more widely across not only mm
stresstests, but across subsystems as well. Without having taken a deeper
look into this, perhaps this version of swap is indeed called on some data
structures with variable type, and we might be able to see some tangible
compile time improvements?

In any case, this change looks good to me, but maybe a new commit message
that can more closely describe the effects would be better : -) Once you add
those changes, please feel free to add my review tag for the mm selftest change:

Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>

> Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> ---
>  tools/testing/selftests/mm/uffd-stress.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
> index 40af7f67c407..c0f64df5085c 100644
> --- a/tools/testing/selftests/mm/uffd-stress.c
> +++ b/tools/testing/selftests/mm/uffd-stress.c
> @@ -51,7 +51,7 @@ static char *zeropage;
>  pthread_attr_t attr;
>  
>  #define swap(a, b) \
> -	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> +	do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
>  
>  const char *examples =
>  	"# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
> -- 
> 2.49.0

Sent using hkml (https://github.com/sjp38/hackermail)

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

* Re: [PATCH] selftests/mm: use __auto_type in swap() macro
  2025-08-06 16:46   ` Peter Xu
@ 2025-08-26 16:23     ` Pranav Tyagi
  0 siblings, 0 replies; 6+ messages in thread
From: Pranav Tyagi @ 2025-08-26 16:23 UTC (permalink / raw)
  To: Peter Xu
  Cc: akpm, shuah, linux-mm, linux-kselftest, linux-kernel,
	linux-kernel-mentees

On Wed, Aug 6, 2025 at 10:16 PM Peter Xu <peterx@redhat.com> wrote:
>
> On Wed, Aug 06, 2025 at 09:15:50PM +0530, Pranav Tyagi wrote:
> > On Wed, Jul 30, 2025 at 7:53 PM Pranav Tyagi <pranav.tyagi03@gmail.com> wrote:
> > >
> > > Replace typeof() with __auto_type in the swap() macro in uffd-stress.c.
> > > __auto_type was introduced in GCC 4.9 and reduces the compile time for
> > > all compilers. No functional changes intended.
> > >
> > > Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> > > ---
> > >  tools/testing/selftests/mm/uffd-stress.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
> > > index 40af7f67c407..c0f64df5085c 100644
> > > --- a/tools/testing/selftests/mm/uffd-stress.c
> > > +++ b/tools/testing/selftests/mm/uffd-stress.c
> > > @@ -51,7 +51,7 @@ static char *zeropage;
> > >  pthread_attr_t attr;
> > >
> > >  #define swap(a, b) \
> > > -       do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> > > +       do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> > >
> > >  const char *examples =
> > >         "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
> > > --
> > > 2.49.0
> > >
> >
> > Hi,
> >
> > Just a gentle follow-up on this cleanup patch. From what I could find,
> > this is the only use of
> > typeof() left in the mm selftests, so this should be the only instance
> > needing this change.
> >
> > Thanks for considering!
>
> Hi,
>
> Andrew should have queued this one in branch akpm/mm-nonmm-unstable (even
> though I'm not familiar with the branch).
>
> Said that, I'm also not familiar with __auto_type.  Looks like it's more
> efficiently processed by the compiler in some special use cases, however
> it's also new so maybe some tools (sparse?) may not recognize it.
>
> Is it the plan that the whole Linux kernel is moving towards __auto_type?
> I still see quite a few of typeof() usages (not "a few", but 2966 instances).
>
> Thanks,
>
> --
> Peter Xu
>

Hi Peter,

Thank you for the feedback and I sincerely apologize for the delayed
response.

You’re right — this patch was initially queued under akpm/mm-nonmm-unstable
and it now lives in the mm-unstable branch.

The motivation behind switching to __auto_type is twofold: it reduces compile
time across compilers and improves readability. Support for __auto_type has
been available in GCC since 4.9 (quite some time back) and in sparse since
v0.6.2 (2020). To the best of my knowledge, it has since been widely adopted
by toolchains.

That said, I acknowledge there are still many uses of typeof() in the kernel,
as you pointed out. My intent with this patch (and a few related ones I’ve
submitted) is to encourage more consistent use of __auto_type, starting with
selftests where it is already in use in several places.

Thanks again for your review and for pointing out the current branch status.

Regards
Pranav Tyagi

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

* Re: [PATCH] selftests/mm: use __auto_type in swap() macro
  2025-08-06 16:48 ` Joshua Hahn
@ 2025-08-26 16:26   ` Pranav Tyagi
  0 siblings, 0 replies; 6+ messages in thread
From: Pranav Tyagi @ 2025-08-26 16:26 UTC (permalink / raw)
  To: Joshua Hahn
  Cc: akpm, peterx, shuah, linux-mm, linux-kselftest, linux-kernel,
	linux-kernel-mentees

On Wed, Aug 6, 2025 at 10:18 PM Joshua Hahn <joshua.hahnjy@gmail.com> wrote:
>
> On Wed, 30 Jul 2025 19:53:01 +0530 Pranav Tyagi <pranav.tyagi03@gmail.com> wrote:
>
> Hi Pranav,
>
> Thank you for this patch! Sorry for the late response, thank you for bumping
> the patch. I just have a few comments about the commit message.
>
> > Replace typeof() with __auto_type in the swap() macro in uffd-stress.c.
> > __auto_type was introduced in GCC 4.9 and reduces the compile time for
> > all compilers. No functional changes intended.
>
> From what I understand, the compiler optimizations from typeof() --> __auto_type
> applies when the argument is a variably modified type. It seems like this
> internal definition of swap() is only used within selftests/mm/uffd-stress.c,
> and in particular is only used twice, in these two lines:
>
> /* prepare next bounce */
> swap(area_src, area_dst);
>
> swap(area_src_alias, area_dst_alias);
>
> Where area_src, area_dst, area_src_alias, area_dst_alias are all char * which
> the compiler knows the size of at compile time. Given this, I wonder if there
> are any compile time speedups.
>
> But this is just my understanding, based on a quick look at the code. Please
> feel free to correct me, if I am incorrectly understanding your changes or if
> my understanding of __auto_type is incorrect : -)
>
> With that said, I think the main benefit that we get from using __auto_type
> has more to do with readability. Since __auto_type can only be used to
> declare local variables (as we are doing here), maybe we can rephrase the
> commit to be about improving readability, and not compile time?
>
> Again, please let me know if I am overlooking something.
>
> One other thought -- while this internal definition of swap() is confined to
> selftests/mm/uffd-stress.c, there is another identical definition in
> include/linux/minmax.h that may be used more widely across not only mm
> stresstests, but across subsystems as well. Without having taken a deeper
> look into this, perhaps this version of swap is indeed called on some data
> structures with variable type, and we might be able to see some tangible
> compile time improvements?
>
> In any case, this change looks good to me, but maybe a new commit message
> that can more closely describe the effects would be better : -) Once you add
> those changes, please feel free to add my review tag for the mm selftest change:
>
> Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
>
> > Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
> > ---
> >  tools/testing/selftests/mm/uffd-stress.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
> > index 40af7f67c407..c0f64df5085c 100644
> > --- a/tools/testing/selftests/mm/uffd-stress.c
> > +++ b/tools/testing/selftests/mm/uffd-stress.c
> > @@ -51,7 +51,7 @@ static char *zeropage;
> >  pthread_attr_t attr;
> >
> >  #define swap(a, b) \
> > -     do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> > +     do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> >
> >  const char *examples =
> >       "# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
> > --
> > 2.49.0
>
> Sent using hkml (https://github.com/sjp38/hackermail)

Hi Joshua,

Thanks for the detailed review and I sincerely apologize for the delayed
response.

You’re correct — __auto_type mainly provides compiler optimizations when the
argument is a variably modified type and in this case the compiler already
knows the size of the arguments at compile time. The motivation here is more
about readability and consistency, as __auto_type is already used in several
places within selftests. This patch has since been picked up into the
mm-unstable branch.

I also looked at the swap() definition in include/linux/minmax.h, which is
indeed more widely used across subsystems. That version may offer tangible
compile-time improvements in cases involving variable argument types and I
plan to follow up with a patch there.

Thanks again for the thorough review.

Regards
Pranav Tyagi

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

end of thread, other threads:[~2025-08-26 16:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-30 14:23 [PATCH] selftests/mm: use __auto_type in swap() macro Pranav Tyagi
2025-08-06 15:45 ` Pranav Tyagi
2025-08-06 16:46   ` Peter Xu
2025-08-26 16:23     ` Pranav Tyagi
2025-08-06 16:48 ` Joshua Hahn
2025-08-26 16:26   ` Pranav Tyagi

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).