* [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c
@ 2025-03-24 12:47 Malaya Kumar Rout
2025-03-24 12:51 ` Peter Zijlstra
0 siblings, 1 reply; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-03-24 12:47 UTC (permalink / raw)
To: malayarout91
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Shuah Khan, linux-kernel, linux-kselftest
Static Analyis for bench_htab_mem.c with cppcheck:error
tools/testing/selftests/x86/lam.c:585:3:
error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:593:3:
error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:600:3:
error: Memory leak: fi [memleak]
tools/testing/selftests/x86/lam.c:1066:2:
error: Resource leak: fd [resourceLeak]
fix the issue by closing the file descriptors and
releasing the allocated memory.
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/x86/lam.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 4d4a76532dc9..0b43b83ad142 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -581,24 +581,28 @@ int do_uring(unsigned long lam)
if (file_fd < 0)
return 1;
- if (fstat(file_fd, &st) < 0)
+ if (fstat(file_fd, &st) < 0) {
+ close(file_fd);
return 1;
-
+ }
off_t file_sz = st.st_size;
int blocks = (int)(file_sz + URING_BLOCK_SZ - 1) / URING_BLOCK_SZ;
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
- if (!fi)
+ if (!fi) {
+ close(file_fd);
return 1;
-
+ }
fi->file_sz = file_sz;
fi->file_fd = file_fd;
ring = malloc(sizeof(*ring));
- if (!ring)
+ if (!ring) {
+ close(file_fd);
+ free(fi);
return 1;
-
+ }
memset(ring, 0, sizeof(struct io_ring));
if (setup_io_uring(ring))
@@ -1060,8 +1064,10 @@ void *allocate_dsa_pasid(void)
wq = mmap(NULL, 0x1000, PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
- if (wq == MAP_FAILED)
+ if (wq == MAP_FAILED) {
+ close(fd);
perror("mmap");
+ }
return wq;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c
2025-03-24 12:47 [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c Malaya Kumar Rout
@ 2025-03-24 12:51 ` Peter Zijlstra
2025-03-25 9:29 ` Ingo Molnar
0 siblings, 1 reply; 13+ messages in thread
From: Peter Zijlstra @ 2025-03-24 12:51 UTC (permalink / raw)
To: Malaya Kumar Rout
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Shuah Khan, linux-kernel, linux-kselftest
On Mon, Mar 24, 2025 at 06:17:50PM +0530, Malaya Kumar Rout wrote:
> Static Analyis for bench_htab_mem.c with cppcheck:error
> tools/testing/selftests/x86/lam.c:585:3:
> error: Resource leak: file_fd [resourceLeak]
> tools/testing/selftests/x86/lam.c:593:3:
> error: Resource leak: file_fd [resourceLeak]
> tools/testing/selftests/x86/lam.c:600:3:
> error: Memory leak: fi [memleak]
> tools/testing/selftests/x86/lam.c:1066:2:
> error: Resource leak: fd [resourceLeak]
>
> fix the issue by closing the file descriptors and
> releasing the allocated memory.
>
But but but, doesn't the program just exit on any of those 'errors'
anyway?
That is, iirc this is a single shot program.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c
2025-03-24 12:51 ` Peter Zijlstra
@ 2025-03-25 9:29 ` Ingo Molnar
2025-03-25 9:56 ` malaya kumar rout
0 siblings, 1 reply; 13+ messages in thread
From: Ingo Molnar @ 2025-03-25 9:29 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Malaya Kumar Rout, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
* Peter Zijlstra <peterz@infradead.org> wrote:
> On Mon, Mar 24, 2025 at 06:17:50PM +0530, Malaya Kumar Rout wrote:
> > Static Analyis for bench_htab_mem.c with cppcheck:error
> > tools/testing/selftests/x86/lam.c:585:3:
> > error: Resource leak: file_fd [resourceLeak]
> > tools/testing/selftests/x86/lam.c:593:3:
> > error: Resource leak: file_fd [resourceLeak]
> > tools/testing/selftests/x86/lam.c:600:3:
> > error: Memory leak: fi [memleak]
> > tools/testing/selftests/x86/lam.c:1066:2:
> > error: Resource leak: fd [resourceLeak]
> >
> > fix the issue by closing the file descriptors and
> > releasing the allocated memory.
> >
>
> But but but, doesn't the program just exit on any of those 'errors'
> anyway?
>
> That is, iirc this is a single shot program.
While that's true, still proper cleanup of resources is a good practice
- and in more complicated tools it's useful to fix even these
semi-false-positives, to make sure other warnings don't get missed.
Having said that, the error/cleanup control flow here doesn't look
overly clean here to begin with, so I'd suggest fixing that (with goto
labels or such) - which would fix the file_fd 'leak' as a happy side
effect.
Thanks,
Ingo
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c
2025-03-25 9:29 ` Ingo Molnar
@ 2025-03-25 9:56 ` malaya kumar rout
2025-03-25 13:25 ` Malaya Kumar Rout
2025-04-07 10:44 ` [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid() Malaya Kumar Rout
0 siblings, 2 replies; 13+ messages in thread
From: malaya kumar rout @ 2025-03-25 9:56 UTC (permalink / raw)
To: Ingo Molnar
Cc: Peter Zijlstra, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
I appreciate all the feedback and recommendations provided. We will
incorporate the same.
Thanks & Regards,
Malaya Kumar Rout
On Tue, Mar 25, 2025 at 2:59 PM Ingo Molnar <mingo@kernel.org> wrote:
>
>
> * Peter Zijlstra <peterz@infradead.org> wrote:
>
> > On Mon, Mar 24, 2025 at 06:17:50PM +0530, Malaya Kumar Rout wrote:
> > > Static Analyis for bench_htab_mem.c with cppcheck:error
> > > tools/testing/selftests/x86/lam.c:585:3:
> > > error: Resource leak: file_fd [resourceLeak]
> > > tools/testing/selftests/x86/lam.c:593:3:
> > > error: Resource leak: file_fd [resourceLeak]
> > > tools/testing/selftests/x86/lam.c:600:3:
> > > error: Memory leak: fi [memleak]
> > > tools/testing/selftests/x86/lam.c:1066:2:
> > > error: Resource leak: fd [resourceLeak]
> > >
> > > fix the issue by closing the file descriptors and
> > > releasing the allocated memory.
> > >
> >
> > But but but, doesn't the program just exit on any of those 'errors'
> > anyway?
> >
> > That is, iirc this is a single shot program.
>
> While that's true, still proper cleanup of resources is a good practice
> - and in more complicated tools it's useful to fix even these
> semi-false-positives, to make sure other warnings don't get missed.
>
> Having said that, the error/cleanup control flow here doesn't look
> overly clean here to begin with, so I'd suggest fixing that (with goto
> labels or such) - which would fix the file_fd 'leak' as a happy side
> effect.
>
> Thanks,
>
> Ingo
^ permalink raw reply [flat|nested] 13+ messages in thread
* (no subject)
2025-03-25 9:56 ` malaya kumar rout
@ 2025-03-25 13:25 ` Malaya Kumar Rout
2025-04-07 10:44 ` [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid() Malaya Kumar Rout
1 sibling, 0 replies; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-03-25 13:25 UTC (permalink / raw)
To: peterz
Cc: Malaya Kumar Rout, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
From 92c5ac2ffa37f6e4fe0cfa49a20857432bc67718 Mon Sep 17 00:00:00 2001
From: Malaya Kumar Rout <malayarout91@gmail.com>
Date: Tue, 25 Mar 2025 18:42:33 +0530
Subject: [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
Exception branch returns without closing
the file descriptors 'file_fd' and 'fd'
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/x86/lam.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 18d736640ece..eaba0a921322 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
return 1;
if (fstat(file_fd, &st) < 0)
- return 1;
+ goto cleanup;
off_t file_sz = st.st_size;
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
if (!fi)
- return 1;
+ goto cleanup;
fi->file_sz = file_sz;
fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
ring = malloc(sizeof(*ring));
if (!ring) {
free(fi);
- return 1;
+ goto cleanup;
}
memset(ring, 0, sizeof(struct io_ring));
@@ -730,6 +730,9 @@ int do_uring(unsigned long lam)
free(fi);
+cleanup:
+ close(file_fd);
+
return ret;
}
@@ -1189,8 +1192,10 @@ void *allocate_dsa_pasid(void)
wq = mmap(NULL, 0x1000, PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
- if (wq == MAP_FAILED)
+ if (wq == MAP_FAILED) {
+ close(fd);
perror("mmap");
+ }
return wq;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-03-25 9:56 ` malaya kumar rout
2025-03-25 13:25 ` Malaya Kumar Rout
@ 2025-04-07 10:44 ` Malaya Kumar Rout
2025-04-07 18:20 ` Ingo Molnar
1 sibling, 1 reply; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-04-07 10:44 UTC (permalink / raw)
To: mingo
Cc: Malaya Kumar Rout, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
Exception branch returns without closing
the file descriptors 'file_fd' and 'fd'
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
:wq
---
tools/testing/selftests/x86/lam.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 18d736640ece..1d3631ce4b69 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
return 1;
if (fstat(file_fd, &st) < 0)
- return 1;
+ goto cleanup;
off_t file_sz = st.st_size;
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
if (!fi)
- return 1;
+ goto cleanup;
fi->file_sz = file_sz;
fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
ring = malloc(sizeof(*ring));
if (!ring) {
free(fi);
- return 1;
+ goto cleanup;
}
memset(ring, 0, sizeof(struct io_ring));
@@ -729,6 +729,8 @@ int do_uring(unsigned long lam)
}
free(fi);
+cleanup:
+ close(file_fd);
return ret;
}
@@ -1189,9 +1191,10 @@ void *allocate_dsa_pasid(void)
wq = mmap(NULL, 0x1000, PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
- if (wq == MAP_FAILED)
+ if (wq == MAP_FAILED){
+ close(fd);
perror("mmap");
-
+ }
return wq;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-04-07 10:44 ` [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid() Malaya Kumar Rout
@ 2025-04-07 18:20 ` Ingo Molnar
2025-04-07 19:34 ` [PATCH v3] " Malaya Kumar Rout
0 siblings, 1 reply; 13+ messages in thread
From: Ingo Molnar @ 2025-04-07 18:20 UTC (permalink / raw)
To: Malaya Kumar Rout
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Shuah Khan, linux-kernel, linux-kselftest
* Malaya Kumar Rout <malayarout91@gmail.com> wrote:
> @@ -1189,9 +1191,10 @@ void *allocate_dsa_pasid(void)
>
> wq = mmap(NULL, 0x1000, PROT_WRITE,
> MAP_SHARED | MAP_POPULATE, fd, 0);
> - if (wq == MAP_FAILED)
> + if (wq == MAP_FAILED){
> + close(fd);
> perror("mmap");
We should unconditionally close 'fd' after the mmap() call, not just in
the perror() branch.
Thanks,
Ingo
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v3] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-04-07 18:20 ` Ingo Molnar
@ 2025-04-07 19:34 ` Malaya Kumar Rout
2025-04-08 8:09 ` Ingo Molnar
0 siblings, 1 reply; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-04-07 19:34 UTC (permalink / raw)
To: mingo
Cc: Malaya Kumar Rout, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
Exception branch returns without closing
the file descriptors 'file_fd' and 'fd'
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/x86/lam.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 18d736640ece..88482d8112de 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
return 1;
if (fstat(file_fd, &st) < 0)
- return 1;
+ goto cleanup;
off_t file_sz = st.st_size;
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
if (!fi)
- return 1;
+ goto cleanup;
fi->file_sz = file_sz;
fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
ring = malloc(sizeof(*ring));
if (!ring) {
free(fi);
- return 1;
+ goto cleanup;
}
memset(ring, 0, sizeof(struct io_ring));
@@ -729,6 +729,8 @@ int do_uring(unsigned long lam)
}
free(fi);
+cleanup:
+ close(file_fd);
return ret;
}
@@ -1192,6 +1194,7 @@ void *allocate_dsa_pasid(void)
if (wq == MAP_FAILED)
perror("mmap");
+ close(fd);
return wq;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v3] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-04-07 19:34 ` [PATCH v3] " Malaya Kumar Rout
@ 2025-04-08 8:09 ` Ingo Molnar
2025-04-08 18:52 ` RE:[PATCH RESEND x86-next v4] " Malaya Kumar Rout
0 siblings, 1 reply; 13+ messages in thread
From: Ingo Molnar @ 2025-04-08 8:09 UTC (permalink / raw)
To: Malaya Kumar Rout
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Shuah Khan, linux-kernel, linux-kselftest
* Malaya Kumar Rout <malayarout91@gmail.com> wrote:
> Exception branch returns without closing
> the file descriptors 'file_fd' and 'fd'
>
> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> ---
> tools/testing/selftests/x86/lam.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
> index 18d736640ece..88482d8112de 100644
> --- a/tools/testing/selftests/x86/lam.c
> +++ b/tools/testing/selftests/x86/lam.c
> @@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
> return 1;
>
> if (fstat(file_fd, &st) < 0)
> - return 1;
> + goto cleanup;
>
> off_t file_sz = st.st_size;
>
> @@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
>
> fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
> if (!fi)
> - return 1;
> + goto cleanup;
>
> fi->file_sz = file_sz;
> fi->file_fd = file_fd;
> @@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
> ring = malloc(sizeof(*ring));
> if (!ring) {
> free(fi);
> - return 1;
> + goto cleanup;
> }
>
> memset(ring, 0, sizeof(struct io_ring));
> @@ -729,6 +729,8 @@ int do_uring(unsigned long lam)
> }
>
> free(fi);
> +cleanup:
> + close(file_fd);
>
> return ret;
> }
> @@ -1192,6 +1194,7 @@ void *allocate_dsa_pasid(void)
> if (wq == MAP_FAILED)
> perror("mmap");
>
> + close(fd);
> return wq;
So in your previous patch you closed the file before the perror(),
presumably so that file-leak detection in Valgrind or whatever tool you
are using doesn't trigger.
But here it's done after the perror() call, why? It's perfectly fine to
close the mapping fd straight after an mmap() call.
Finally, it would be nice to quote the before/after output of the leak
detection tool you are using.
Thanks,
Ingo
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE:[PATCH RESEND x86-next v4] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-04-08 8:09 ` Ingo Molnar
@ 2025-04-08 18:52 ` Malaya Kumar Rout
2025-04-08 20:12 ` [PATCH " Ingo Molnar
0 siblings, 1 reply; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-04-08 18:52 UTC (permalink / raw)
To: mingo
Cc: Malaya Kumar Rout, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
Exception branch returns without closing
the file descriptors 'file_fd' and 'fd'
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/x86/lam.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 18d736640ece..0873b0e5f48b 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
return 1;
if (fstat(file_fd, &st) < 0)
- return 1;
+ goto cleanup;
off_t file_sz = st.st_size;
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
if (!fi)
- return 1;
+ goto cleanup;
fi->file_sz = file_sz;
fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
ring = malloc(sizeof(*ring));
if (!ring) {
free(fi);
- return 1;
+ goto cleanup;
}
memset(ring, 0, sizeof(struct io_ring));
@@ -729,6 +729,8 @@ int do_uring(unsigned long lam)
}
free(fi);
+cleanup:
+ close(file_fd);
return ret;
}
@@ -1189,6 +1191,7 @@ void *allocate_dsa_pasid(void)
wq = mmap(NULL, 0x1000, PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
+ close(fd);
if (wq == MAP_FAILED)
perror("mmap");
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH RESEND x86-next v4] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-04-08 18:52 ` RE:[PATCH RESEND x86-next v4] " Malaya Kumar Rout
@ 2025-04-08 20:12 ` Ingo Molnar
2025-04-09 13:53 ` [PATCH " Malaya Kumar Rout
0 siblings, 1 reply; 13+ messages in thread
From: Ingo Molnar @ 2025-04-08 20:12 UTC (permalink / raw)
To: Malaya Kumar Rout
Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Shuah Khan, linux-kernel, linux-kselftest
* Malaya Kumar Rout <malayarout91@gmail.com> wrote:
> Exception branch returns without closing
> the file descriptors 'file_fd' and 'fd'
>
> Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
> ---
> tools/testing/selftests/x86/lam.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
Why have you ignored this request I made:
>> Finally, it would be nice to quote the before/after output of the
>> leak detection tool you are using.
?
Thanks,
Ingo
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v4] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()
2025-04-08 20:12 ` [PATCH " Ingo Molnar
@ 2025-04-09 13:53 ` Malaya Kumar Rout
2025-04-09 19:41 ` [tip: x86/mm] selftests/x86/lam: Fix clean up fds " tip-bot2 for Malaya Kumar Rout
0 siblings, 1 reply; 13+ messages in thread
From: Malaya Kumar Rout @ 2025-04-09 13:53 UTC (permalink / raw)
To: mingo
Cc: Malaya Kumar Rout, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86, H. Peter Anvin, Shuah Khan, linux-kernel,
linux-kselftest
This patch resolves resource leaks reported by cppcheck in lam.c.
Specifically, the 'file_fd' and 'fd' were not closed in do_uring()
and allocate_dsa_pasid() functions, respectively.
cppcheck output before this patch:
tools/testing/selftests/x86/lam.c:685:3: error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:693:3: error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:1195:2: error: Resource leak: fd [resourceLeak]
cppcheck output after this patch:
No resource leaks found
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/testing/selftests/x86/lam.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 18d736640ece..0873b0e5f48b 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
return 1;
if (fstat(file_fd, &st) < 0)
- return 1;
+ goto cleanup;
off_t file_sz = st.st_size;
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
if (!fi)
- return 1;
+ goto cleanup;
fi->file_sz = file_sz;
fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
ring = malloc(sizeof(*ring));
if (!ring) {
free(fi);
- return 1;
+ goto cleanup;
}
memset(ring, 0, sizeof(struct io_ring));
@@ -729,6 +729,8 @@ int do_uring(unsigned long lam)
}
free(fi);
+cleanup:
+ close(file_fd);
return ret;
}
@@ -1189,6 +1191,7 @@ void *allocate_dsa_pasid(void)
wq = mmap(NULL, 0x1000, PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
+ close(fd);
if (wq == MAP_FAILED)
perror("mmap");
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [tip: x86/mm] selftests/x86/lam: Fix clean up fds in do_uring() and allocate_dsa_pasid()
2025-04-09 13:53 ` [PATCH " Malaya Kumar Rout
@ 2025-04-09 19:41 ` tip-bot2 for Malaya Kumar Rout
0 siblings, 0 replies; 13+ messages in thread
From: tip-bot2 for Malaya Kumar Rout @ 2025-04-09 19:41 UTC (permalink / raw)
To: linux-tip-commits; +Cc: Malaya Kumar Rout, Ingo Molnar, x86, linux-kernel
The following commit has been merged into the x86/mm branch of tip:
Commit-ID: 60567e93c05d7064c93830cf4bf0d2c58f11b2f2
Gitweb: https://git.kernel.org/tip/60567e93c05d7064c93830cf4bf0d2c58f11b2f2
Author: Malaya Kumar Rout <malayarout91@gmail.com>
AuthorDate: Wed, 09 Apr 2025 19:23:37 +05:30
Committer: Ingo Molnar <mingo@kernel.org>
CommitterDate: Wed, 09 Apr 2025 21:30:37 +02:00
selftests/x86/lam: Fix clean up fds in do_uring() and allocate_dsa_pasid()
Resolve minor fd leaks reported by cppcheck in lam.c.
Specifically, the 'file_fd' and 'fd' were not closed in do_uring()
and allocate_dsa_pasid() functions, respectively.
cppcheck output before this patch:
tools/testing/selftests/x86/lam.c:685:3: error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:693:3: error: Resource leak: file_fd [resourceLeak]
tools/testing/selftests/x86/lam.c:1195:2: error: Resource leak: fd [resourceLeak]
cppcheck output after this patch:
No resource leaks found
While this is a standalone test tool that doesn't really leak anything
in practice, as exit() cleans it up all, clean up resources nevertheless.
[ mingo: Updated the changelog. ]
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20250409135341.28987-1-malayarout91@gmail.com
---
tools/testing/selftests/x86/lam.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 18d7366..0873b0e 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
return 1;
if (fstat(file_fd, &st) < 0)
- return 1;
+ goto cleanup;
off_t file_sz = st.st_size;
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
if (!fi)
- return 1;
+ goto cleanup;
fi->file_sz = file_sz;
fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
ring = malloc(sizeof(*ring));
if (!ring) {
free(fi);
- return 1;
+ goto cleanup;
}
memset(ring, 0, sizeof(struct io_ring));
@@ -729,6 +729,8 @@ out:
}
free(fi);
+cleanup:
+ close(file_fd);
return ret;
}
@@ -1189,6 +1191,7 @@ void *allocate_dsa_pasid(void)
wq = mmap(NULL, 0x1000, PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
+ close(fd);
if (wq == MAP_FAILED)
perror("mmap");
^ permalink raw reply related [flat|nested] 13+ messages in thread
end of thread, other threads:[~2025-04-09 19:41 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 12:47 [PATCH] selftests/x86/lam: fix memory leak and resource leak in lam.c Malaya Kumar Rout
2025-03-24 12:51 ` Peter Zijlstra
2025-03-25 9:29 ` Ingo Molnar
2025-03-25 9:56 ` malaya kumar rout
2025-03-25 13:25 ` Malaya Kumar Rout
2025-04-07 10:44 ` [PATCH v2] selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid() Malaya Kumar Rout
2025-04-07 18:20 ` Ingo Molnar
2025-04-07 19:34 ` [PATCH v3] " Malaya Kumar Rout
2025-04-08 8:09 ` Ingo Molnar
2025-04-08 18:52 ` RE:[PATCH RESEND x86-next v4] " Malaya Kumar Rout
2025-04-08 20:12 ` [PATCH " Ingo Molnar
2025-04-09 13:53 ` [PATCH " Malaya Kumar Rout
2025-04-09 19:41 ` [tip: x86/mm] selftests/x86/lam: Fix clean up fds " tip-bot2 for Malaya Kumar Rout
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox