Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization
@ 2025-02-06  9:49 Jan Stancek
  2025-02-06 12:44 ` Cyril Hrubis
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Stancek @ 2025-02-06  9:49 UTC (permalink / raw)
  To: ltp

gcc 15 is a bit more clever and noticed that 'buf' isn't used
for anything so it optimized it out, including call to malloc.
So, there's also no mmap() call behind it and test fails,
because nothing was allocated.

Introduce LTP_VAR_USED macro, that makes compiler aware of the
variable and doesn't optimize it out.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 include/tst_common.h                               | 2 ++
 testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/tst_common.h b/include/tst_common.h
index b14bbae04077..3de826acd0ec 100644
--- a/include/tst_common.h
+++ b/include/tst_common.h
@@ -13,6 +13,8 @@
 #define LTP_ATTRIBUTE_UNUSED		__attribute__((unused))
 #define LTP_ATTRIBUTE_UNUSED_RESULT	__attribute__((warn_unused_result))
 
+#define LTP_VAR_USED(p) asm volatile("" :: "m"(p)); p
+
 #ifndef ARRAY_SIZE
 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #endif
diff --git a/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c b/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
index 90cf4fcb3b89..51783fc22b80 100644
--- a/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
+++ b/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
@@ -26,7 +26,7 @@ void test_mallinfo2(void)
 	char *buf;
 	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
 
-	buf = malloc(size);
+	LTP_VAR_USED(buf) = malloc(size);
 
 	if (!buf)
 		tst_brk(TCONF, "Current system can not malloc 2G space, skip it");
-- 
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization
  2025-02-06  9:49 [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization Jan Stancek
@ 2025-02-06 12:44 ` Cyril Hrubis
  2025-02-06 12:58   ` Jan Stancek
  0 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2025-02-06 12:44 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp

Hi!
> gcc 15 is a bit more clever and noticed that 'buf' isn't used
> for anything so it optimized it out, including call to malloc.
> So, there's also no mmap() call behind it and test fails,
> because nothing was allocated.

Huh, that sounds like the optimizations are getting more and more evil
over the time.

> Introduce LTP_VAR_USED macro, that makes compiler aware of the
> variable and doesn't optimize it out.
> 
> Signed-off-by: Jan Stancek <jstancek@redhat.com>
> ---
>  include/tst_common.h                               | 2 ++
>  testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c | 2 +-
>  2 files changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/include/tst_common.h b/include/tst_common.h
> index b14bbae04077..3de826acd0ec 100644
> --- a/include/tst_common.h
> +++ b/include/tst_common.h
> @@ -13,6 +13,8 @@
>  #define LTP_ATTRIBUTE_UNUSED		__attribute__((unused))
>  #define LTP_ATTRIBUTE_UNUSED_RESULT	__attribute__((warn_unused_result))
>  
> +#define LTP_VAR_USED(p) asm volatile("" :: "m"(p)); p

Shouldn't __attribute__((used)) suffice?

>  #ifndef ARRAY_SIZE
>  # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
>  #endif
> diff --git a/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c b/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
> index 90cf4fcb3b89..51783fc22b80 100644
> --- a/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
> +++ b/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
> @@ -26,7 +26,7 @@ void test_mallinfo2(void)
>  	char *buf;
>  	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
>  
> -	buf = malloc(size);
> +	LTP_VAR_USED(buf) = malloc(size);
>  
>  	if (!buf)
>  		tst_brk(TCONF, "Current system can not malloc 2G space, skip it");
> -- 
> 2.43.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization
  2025-02-06 12:44 ` Cyril Hrubis
@ 2025-02-06 12:58   ` Jan Stancek
  2025-02-06 13:22     ` Cyril Hrubis
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Stancek @ 2025-02-06 12:58 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On Thu, Feb 6, 2025 at 1:45 PM Cyril Hrubis <chrubis@suse.cz> wrote:
>
> Hi!
> > gcc 15 is a bit more clever and noticed that 'buf' isn't used
> > for anything so it optimized it out, including call to malloc.
> > So, there's also no mmap() call behind it and test fails,
> > because nothing was allocated.
>
> Huh, that sounds like the optimizations are getting more and more evil
> over the time.

Also see the next patch for bpf.

>
> > Introduce LTP_VAR_USED macro, that makes compiler aware of the
> > variable and doesn't optimize it out.
> >
> > Signed-off-by: Jan Stancek <jstancek@redhat.com>
> > ---
> >  include/tst_common.h                               | 2 ++
> >  testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c | 2 +-
> >  2 files changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/tst_common.h b/include/tst_common.h
> > index b14bbae04077..3de826acd0ec 100644
> > --- a/include/tst_common.h
> > +++ b/include/tst_common.h
> > @@ -13,6 +13,8 @@
> >  #define LTP_ATTRIBUTE_UNUSED         __attribute__((unused))
> >  #define LTP_ATTRIBUTE_UNUSED_RESULT  __attribute__((warn_unused_result))
> >
> > +#define LTP_VAR_USED(p) asm volatile("" :: "m"(p)); p
>
> Shouldn't __attribute__((used)) suffice?

It's ignored for local variables. It does work for global ones.

>
> >  #ifndef ARRAY_SIZE
> >  # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
> >  #endif
> > diff --git a/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c b/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
> > index 90cf4fcb3b89..51783fc22b80 100644
> > --- a/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
> > +++ b/testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c
> > @@ -26,7 +26,7 @@ void test_mallinfo2(void)
> >       char *buf;
> >       size_t size = 2UL * 1024UL * 1024UL * 1024UL;
> >
> > -     buf = malloc(size);
> > +     LTP_VAR_USED(buf) = malloc(size);
> >
> >       if (!buf)
> >               tst_brk(TCONF, "Current system can not malloc 2G space, skip it");
> > --
> > 2.43.0
> >
> >
> > --
> > Mailing list info: https://lists.linux.it/listinfo/ltp
>
> --
> Cyril Hrubis
> chrubis@suse.cz
>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization
  2025-02-06 12:58   ` Jan Stancek
@ 2025-02-06 13:22     ` Cyril Hrubis
  2025-02-07  9:49       ` Jan Stancek
  0 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2025-02-06 13:22 UTC (permalink / raw)
  To: Jan Stancek; +Cc: ltp

Hi!
> > > gcc 15 is a bit more clever and noticed that 'buf' isn't used
> > > for anything so it optimized it out, including call to malloc.
> > > So, there's also no mmap() call behind it and test fails,
> > > because nothing was allocated.
> >
> > Huh, that sounds like the optimizations are getting more and more evil
> > over the time.
> 
> Also see the next patch for bpf.

Sigh...

> > > Introduce LTP_VAR_USED macro, that makes compiler aware of the
> > > variable and doesn't optimize it out.
> > >
> > > Signed-off-by: Jan Stancek <jstancek@redhat.com>
> > > ---
> > >  include/tst_common.h                               | 2 ++
> > >  testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c | 2 +-
> > >  2 files changed, 3 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/include/tst_common.h b/include/tst_common.h
> > > index b14bbae04077..3de826acd0ec 100644
> > > --- a/include/tst_common.h
> > > +++ b/include/tst_common.h
> > > @@ -13,6 +13,8 @@
> > >  #define LTP_ATTRIBUTE_UNUSED         __attribute__((unused))
> > >  #define LTP_ATTRIBUTE_UNUSED_RESULT  __attribute__((warn_unused_result))
> > >
> > > +#define LTP_VAR_USED(p) asm volatile("" :: "m"(p)); p
> >
> > Shouldn't __attribute__((used)) suffice?
> 
> It's ignored for local variables. It does work for global ones.

Maybe mention that in the commit description...

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization
  2025-02-06 13:22     ` Cyril Hrubis
@ 2025-02-07  9:49       ` Jan Stancek
  0 siblings, 0 replies; 5+ messages in thread
From: Jan Stancek @ 2025-02-07  9:49 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On Thu, Feb 6, 2025 at 2:23 PM Cyril Hrubis <chrubis@suse.cz> wrote:
>
> Hi!
> > > > gcc 15 is a bit more clever and noticed that 'buf' isn't used
> > > > for anything so it optimized it out, including call to malloc.
> > > > So, there's also no mmap() call behind it and test fails,
> > > > because nothing was allocated.
> > >
> > > Huh, that sounds like the optimizations are getting more and more evil
> > > over the time.
> >
> > Also see the next patch for bpf.
>
> Sigh...
>
> > > > Introduce LTP_VAR_USED macro, that makes compiler aware of the
> > > > variable and doesn't optimize it out.
> > > >
> > > > Signed-off-by: Jan Stancek <jstancek@redhat.com>
> > > > ---
> > > >  include/tst_common.h                               | 2 ++
> > > >  testcases/kernel/syscalls/mallinfo2/mallinfo2_01.c | 2 +-
> > > >  2 files changed, 3 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/include/tst_common.h b/include/tst_common.h
> > > > index b14bbae04077..3de826acd0ec 100644
> > > > --- a/include/tst_common.h
> > > > +++ b/include/tst_common.h
> > > > @@ -13,6 +13,8 @@
> > > >  #define LTP_ATTRIBUTE_UNUSED         __attribute__((unused))
> > > >  #define LTP_ATTRIBUTE_UNUSED_RESULT  __attribute__((warn_unused_result))
> > > >
> > > > +#define LTP_VAR_USED(p) asm volatile("" :: "m"(p)); p
> > >
> > > Shouldn't __attribute__((used)) suffice?
> >
> > It's ignored for local variables. It does work for global ones.
>
> Maybe mention that in the commit description...

Pushed with updated description.

>
> Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
>
> --
> Cyril Hrubis
> chrubis@suse.cz
>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

end of thread, other threads:[~2025-02-07  9:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-06  9:49 [LTP] [PATCH] syscalls/mallinfo02: introduce LTP_VAR_USED to avoid optimization Jan Stancek
2025-02-06 12:44 ` Cyril Hrubis
2025-02-06 12:58   ` Jan Stancek
2025-02-06 13:22     ` Cyril Hrubis
2025-02-07  9:49       ` Jan Stancek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox