* [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
@ 2025-12-19 9:42 Petr Vorel
2025-12-19 10:48 ` Cyril Hrubis
2025-12-19 13:06 ` Li Wang via ltp
0 siblings, 2 replies; 13+ messages in thread
From: Petr Vorel @ 2025-12-19 9:42 UTC (permalink / raw)
To: ltp; +Cc: Michal Hocko
Previously tst_max_swapfiles() had fine tuning for a specific kernel
version which was fragile due various backports in enterprise kernels.
Let's try to create and use as many swap files until swapon() fails.
Then check for expected EPERM.
It was required to increase cmd_buffer size to avoid directive output
may be truncated warning.
Suggested-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Changes v3->v4:
* Do *not* skip testing when expected minimum swap files was already
used in SUT (Cyril)
* Rename variables (Cyril)
* Run test only once (remove -i2 from runtest file)
Link to v3:
https://lore.kernel.org/ltp/20251118143607.45308-3-pvorel@suse.cz/
https://patchwork.ozlabs.org/project/ltp/patch/20251118143607.45308-3-pvorel@suse.cz/
testcases/kernel/syscalls/swapon/swapon03.c | 49 ++++++++++++---------
1 file changed, 27 insertions(+), 22 deletions(-)
diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index c014a48912..e8dca1e283 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -6,9 +6,12 @@
*/
/*\
- * This test case checks whether swapon(2) system call returns:
+ * Test checks whether :man2:`swapon` system call returns EPERM when the maximum
+ * number of swap files are already in use.
*
- * - EPERM when there are more than MAX_SWAPFILES already in use.
+ * NOTE: test does not try to calculate MAX_SWAPFILES from the internal
+ * kernel implementation, instead make sure at least 15 swaps were created
+ * before the maximum of swaps was reached.
*/
#include <stdio.h>
@@ -20,6 +23,13 @@
#include "lapi/syscalls.h"
#include "libswap.h"
+/*
+ * MAX_SWAPFILES from the internal kernel implementation is currently <23, 29>,
+ * depending on kernel configuration (see man swapon(2). Chose small enough
+ * value for future changes.
+ */
+#define MIN_SWAP_FILES 15
+
#define MNTPOINT "mntpoint"
#define TEST_FILE MNTPOINT"/testswap"
@@ -27,31 +37,28 @@ static int swapfiles;
static int setup_swap(void)
{
- int j, max_swapfiles, used_swapfiles;
+ int used_swapfiles, min_swapfiles;
char filename[FILENAME_MAX];
- /* Determine how many more files are to be created */
- max_swapfiles = tst_max_swapfiles();
used_swapfiles = tst_count_swaps();
- swapfiles = max_swapfiles - used_swapfiles;
- if (swapfiles > max_swapfiles)
- swapfiles = max_swapfiles;
-
- /*create and turn on remaining swapfiles */
- for (j = 0; j < swapfiles; j++) {
+ min_swapfiles = MIN_SWAP_FILES - used_swapfiles;
+ while (true) {
/* Create the swapfile */
- snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
- SAFE_MAKE_SMALL_SWAPFILE(filename);
+ snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, swapfiles);
+ MAKE_SMALL_SWAPFILE(filename);
+
+ /* Quit on a first swap file over max, check for EPERM */
+ if (swapon(filename, 0) == -1) {
+ if (errno == EPERM && swapfiles > min_swapfiles)
+ break;
- /* turn on the swap file */
- TST_EXP_PASS_SILENT(swapon(filename, 0));
- if (!TST_PASS)
- tst_brk(TFAIL, "Failed to setup swap files");
+ tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
+ }
+ swapfiles++;
}
tst_res(TINFO, "Successfully created %d swap files", swapfiles);
- MAKE_SMALL_SWAPFILE(TEST_FILE);
return 0;
}
@@ -61,7 +68,7 @@ static int setup_swap(void)
*/
static int check_and_swapoff(const char *filename)
{
- char cmd_buffer[256];
+ char cmd_buffer[FILENAME_MAX+28];
int rc = -1;
snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
@@ -83,11 +90,9 @@ static void clean_swap(void)
char filename[FILENAME_MAX];
for (j = 0; j < swapfiles; j++) {
- snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
+ snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j);
check_and_swapoff(filename);
}
-
- check_and_swapoff(TEST_FILE);
}
static void verify_swapon(void)
--
2.51.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 9:42 [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails Petr Vorel
@ 2025-12-19 10:48 ` Cyril Hrubis
2025-12-19 12:41 ` Li Wang via ltp
2025-12-19 14:02 ` Petr Vorel
2025-12-19 13:06 ` Li Wang via ltp
1 sibling, 2 replies; 13+ messages in thread
From: Cyril Hrubis @ 2025-12-19 10:48 UTC (permalink / raw)
To: Petr Vorel; +Cc: Michal Hocko, ltp
Hi!
> tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> - MAKE_SMALL_SWAPFILE(TEST_FILE);
This should stay here, right? I suppose that the test works even when we
pass non-existing file in the verify_swapon() but we shouldn't bet on
that.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 10:48 ` Cyril Hrubis
@ 2025-12-19 12:41 ` Li Wang via ltp
2025-12-19 14:03 ` Petr Vorel
2025-12-19 14:04 ` Cyril Hrubis
2025-12-19 14:02 ` Petr Vorel
1 sibling, 2 replies; 13+ messages in thread
From: Li Wang via ltp @ 2025-12-19 12:41 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: Michal Hocko, ltp
On Fri, Dec 19, 2025 at 6:47 PM Cyril Hrubis <chrubis@suse.cz> wrote:
> Hi!
> > tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > - MAKE_SMALL_SWAPFILE(TEST_FILE);
>
> This should stay here, right? I suppose that the test works even when we
> pass non-existing file in the verify_swapon() but we shouldn't bet on
> that.
>
Not exactly, I guess here deleted it because is_swap_supported(TEST_FILE)
had already created the file but didn't clean it up in setup, so it still
exists.
Or, we need to do is improve is_swap_supported(), remove the any test file,
and then add it back separately.
--
Regards,
Li Wang
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 12:41 ` Li Wang via ltp
@ 2025-12-19 14:03 ` Petr Vorel
2025-12-19 14:04 ` Cyril Hrubis
1 sibling, 0 replies; 13+ messages in thread
From: Petr Vorel @ 2025-12-19 14:03 UTC (permalink / raw)
To: Li Wang; +Cc: ltp
> On Fri, Dec 19, 2025 at 6:47 PM Cyril Hrubis <chrubis@suse.cz> wrote:
> > Hi!
> > > tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > > - MAKE_SMALL_SWAPFILE(TEST_FILE);
> > This should stay here, right? I suppose that the test works even when we
> > pass non-existing file in the verify_swapon() but we shouldn't bet on
> > that.
> Not exactly, I guess here deleted it because is_swap_supported(TEST_FILE)
> had already created the file but didn't clean it up in setup, so it still
> exists.
Yes.
> Or, we need to do is improve is_swap_supported(), remove the any test file,
> and then add it back separately.
My previous reply:
https://lore.kernel.org/ltp/20251219140216.GA247368@pevik/
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 12:41 ` Li Wang via ltp
2025-12-19 14:03 ` Petr Vorel
@ 2025-12-19 14:04 ` Cyril Hrubis
1 sibling, 0 replies; 13+ messages in thread
From: Cyril Hrubis @ 2025-12-19 14:04 UTC (permalink / raw)
To: Li Wang; +Cc: Michal Hocko, ltp
Hi!
> > > tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > > - MAKE_SMALL_SWAPFILE(TEST_FILE);
> >
> > This should stay here, right? I suppose that the test works even when we
> > pass non-existing file in the verify_swapon() but we shouldn't bet on
> > that.
> >
>
> Not exactly, I guess here deleted it because is_swap_supported(TEST_FILE)
> had already created the file but didn't clean it up in setup, so it still
> exists.
>
> Or, we need to do is improve is_swap_supported(), remove the any test file,
> and then add it back separately.
Ah, now I remmeber, I proposed not to create the swapfile in the
is_swap_supported() and instead a pass a path to a swapfile to the
function last time I got confused by the code.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 10:48 ` Cyril Hrubis
2025-12-19 12:41 ` Li Wang via ltp
@ 2025-12-19 14:02 ` Petr Vorel
2025-12-19 14:41 ` Petr Vorel
1 sibling, 1 reply; 13+ messages in thread
From: Petr Vorel @ 2025-12-19 14:02 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
[ Removing Michal to not bother him with LTP internals ]
> Hi!
> > tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > - MAKE_SMALL_SWAPFILE(TEST_FILE);
> This should stay here, right? I suppose that the test works even when we
> pass non-existing file in the verify_swapon() but we shouldn't bet on
> that.
FYI swap file is created by is_swap_supported(TEST_FILE). But sure, I can
also keep MAKE_SMALL_SWAPFILE(TEST_FILE) to make sure file is also created.
We talked about related cleanup, which I wanted to postpone, but should I
refactor is_swap_supported() to not include swapon() call. Because when there
are too many swap files already mounted, tests is skipped with:
libswap.c:224: TCONF: Permission denied for swapon()
due EPERM, which is actually subject of testing for swapon03.c
I'm not sure about naming. Maybe have is_swap_supported() without swapon() call
(for swapon03.c) and create new function is_swapon_supported(), which would call
is_swap_supported() + swapon() and swapoff() (for all other tests).
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 14:02 ` Petr Vorel
@ 2025-12-19 14:41 ` Petr Vorel
2025-12-20 4:27 ` Li Wang via ltp
0 siblings, 1 reply; 13+ messages in thread
From: Petr Vorel @ 2025-12-19 14:41 UTC (permalink / raw)
To: Cyril Hrubis, ltp, Li Wang
> [ Removing Michal to not bother him with LTP internals ]
> > Hi!
> > > tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > > - MAKE_SMALL_SWAPFILE(TEST_FILE);
> > This should stay here, right? I suppose that the test works even when we
> > pass non-existing file in the verify_swapon() but we shouldn't bet on
> > that.
> FYI swap file is created by is_swap_supported(TEST_FILE). But sure, I can
> also keep MAKE_SMALL_SWAPFILE(TEST_FILE) to make sure file is also created.
> We talked about related cleanup, which I wanted to postpone, but should I
> refactor is_swap_supported() to not include swapon() call. Because when there
> are too many swap files already mounted, tests is skipped with:
> libswap.c:224: TCONF: Permission denied for swapon()
> due EPERM, which is actually subject of testing for swapon03.c
And, as I posted in v3 [1], we can avoid this work, if we require at least
single swap created by the test. Or, allow a corner case all swaps are used,
but just verify that by counting swap in /proc/swaps.
Kind regards,
Petr
[1] https://lore.kernel.org/ltp/20251219142512.GC247368@pevik/
> I'm not sure about naming. Maybe have is_swap_supported() without swapon() call
> (for swapon03.c) and create new function is_swapon_supported(), which would call
> is_swap_supported() + swapon() and swapoff() (for all other tests).
> Kind regards,
> Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 14:41 ` Petr Vorel
@ 2025-12-20 4:27 ` Li Wang via ltp
2025-12-20 4:32 ` Li Wang via ltp
0 siblings, 1 reply; 13+ messages in thread
From: Li Wang via ltp @ 2025-12-20 4:27 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
On Fri, Dec 19, 2025 at 10:48 PM Petr Vorel <pvorel@suse.cz> wrote:
> > [ Removing Michal to not bother him with LTP internals ]
>
> > > Hi!
> > > > tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> > > > - MAKE_SMALL_SWAPFILE(TEST_FILE);
>
> > > This should stay here, right? I suppose that the test works even when
> we
> > > pass non-existing file in the verify_swapon() but we shouldn't bet on
> > > that.
>
> > FYI swap file is created by is_swap_supported(TEST_FILE). But sure, I can
> > also keep MAKE_SMALL_SWAPFILE(TEST_FILE) to make sure file is also
> created.
> > We talked about related cleanup, which I wanted to postpone, but should I
> > refactor is_swap_supported() to not include swapon() call. Because when
> there
> > are too many swap files already mounted, tests is skipped with:
>
> > libswap.c:224: TCONF: Permission denied for swapon()
> > due EPERM, which is actually subject of testing for swapon03.c
>
> And, as I posted in v3 [1], we can avoid this work, if we require at least
> single swap created by the test. Or, allow a corner case all swaps are
> used,
> but just verify that by counting swap in /proc/swaps.
>
> Kind regards,
> Petr
>
> [1] https://lore.kernel.org/ltp/20251219142512.GC247368@pevik/
>
> > I'm not sure about naming. Maybe have is_swap_supported() without
> swapon() call
> > (for swapon03.c) and create new function is_swapon_supported(), which
> would call
> > is_swap_supported() + swapon() and swapoff() (for all other tests).
>
I think your idea is correct, current is_swap_supported() appears to contain
two different questions:
1. Does the kernel/filesystem support swap files at all / can we create a
swapfile?
2. Can we successfully swapon() a swapfile right now?
For most tests, (2) is a reasonable prerequisite; for swapon03 it is not,
because
the test is specifically about swapon() failing with EPERM when you hit the
swapfile limit.
To summarize the discussion, the approach might be:
is_swap_supported():
- swap syscalls present (/proc/swaps exists)
- filesystem allows creating files (has enough space)
- can mkswap (or whatever MAKE_SMALL_SWAPFILE does) successfully
- possibly sanity-check that swapfiles are supported on this FS
is_swapon_supported():
- call is_swap_supported()
- create a temporary swapfile
- swapon() / swapoff() it
- clean up
Of course, the refactor work can be achieved in separate patch set.
--
Regards,
Li Wang
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-20 4:27 ` Li Wang via ltp
@ 2025-12-20 4:32 ` Li Wang via ltp
2025-12-22 7:59 ` Petr Vorel
0 siblings, 1 reply; 13+ messages in thread
From: Li Wang via ltp @ 2025-12-20 4:32 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
> is_swap_supported():
>
Or, naming it as is_swapfile_supported().
--
Regards,
Li Wang
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-20 4:32 ` Li Wang via ltp
@ 2025-12-22 7:59 ` Petr Vorel
2025-12-22 8:55 ` Li Wang via ltp
0 siblings, 1 reply; 13+ messages in thread
From: Petr Vorel @ 2025-12-22 7:59 UTC (permalink / raw)
To: Li Wang; +Cc: ltp
Hi Li,
> > is_swap_supported():
> Or, naming it as is_swapfile_supported().
Thanks a lot for a great summary. Could we merge the above with just tst_brk()?
Or you'd prefer tst_res() and break?
And I'll do cleanup in beginning of January?
Of course I can send the change as a v5 patch.
Kind regards,
Petr
+++ testcases/kernel/syscalls/swapon/swapon03.c
@@ -53,7 +53,7 @@ static int setup_swap(void)
if (errno == EPERM && swapfiles > min_swapfiles)
break;
- tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
+ tst_brk(TFAIL | TERRNO, "swapon(%s, 0)", filename);
}
swapfiles++;
}
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 9:42 [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-12-19 10:48 ` Cyril Hrubis
@ 2025-12-19 13:06 ` Li Wang via ltp
2025-12-19 14:18 ` Petr Vorel
1 sibling, 1 reply; 13+ messages in thread
From: Li Wang via ltp @ 2025-12-19 13:06 UTC (permalink / raw)
To: Petr Vorel; +Cc: Michal Hocko, ltp
On Fri, Dec 19, 2025 at 5:42 PM Petr Vorel <pvorel@suse.cz> wrote:
> Previously tst_max_swapfiles() had fine tuning for a specific kernel
> version which was fragile due various backports in enterprise kernels.
>
> Let's try to create and use as many swap files until swapon() fails.
> Then check for expected EPERM.
>
> It was required to increase cmd_buffer size to avoid directive output
> may be truncated warning.
>
> Suggested-by: Michal Hocko <mhocko@suse.com>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
> Changes v3->v4:
> * Do *not* skip testing when expected minimum swap files was already
> used in SUT (Cyril)
> * Rename variables (Cyril)
> * Run test only once (remove -i2 from runtest file)
>
> Link to v3:
> https://lore.kernel.org/ltp/20251118143607.45308-3-pvorel@suse.cz/
>
> https://patchwork.ozlabs.org/project/ltp/patch/20251118143607.45308-3-pvorel@suse.cz/
> testcases/kernel/syscalls/swapon/swapon03.c | 49 ++++++++++++---------
> 1 file changed, 27 insertions(+), 22 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/swapon/swapon03.c
> b/testcases/kernel/syscalls/swapon/swapon03.c
> index c014a48912..e8dca1e283 100644
> --- a/testcases/kernel/syscalls/swapon/swapon03.c
> +++ b/testcases/kernel/syscalls/swapon/swapon03.c
> @@ -6,9 +6,12 @@
> */
>
> /*\
> - * This test case checks whether swapon(2) system call returns:
> + * Test checks whether :man2:`swapon` system call returns EPERM when the
> maximum
> + * number of swap files are already in use.
> *
> - * - EPERM when there are more than MAX_SWAPFILES already in use.
> + * NOTE: test does not try to calculate MAX_SWAPFILES from the internal
> + * kernel implementation, instead make sure at least 15 swaps were created
> + * before the maximum of swaps was reached.
> */
>
> #include <stdio.h>
> @@ -20,6 +23,13 @@
> #include "lapi/syscalls.h"
> #include "libswap.h"
>
> +/*
> + * MAX_SWAPFILES from the internal kernel implementation is currently
> <23, 29>,
> + * depending on kernel configuration (see man swapon(2). Chose small
> enough
> + * value for future changes.
> + */
> +#define MIN_SWAP_FILES 15
> +
> #define MNTPOINT "mntpoint"
> #define TEST_FILE MNTPOINT"/testswap"
>
> @@ -27,31 +37,28 @@ static int swapfiles;
>
> static int setup_swap(void)
> {
> - int j, max_swapfiles, used_swapfiles;
> + int used_swapfiles, min_swapfiles;
> char filename[FILENAME_MAX];
>
> - /* Determine how many more files are to be created */
> - max_swapfiles = tst_max_swapfiles();
> used_swapfiles = tst_count_swaps();
> - swapfiles = max_swapfiles - used_swapfiles;
> - if (swapfiles > max_swapfiles)
> - swapfiles = max_swapfiles;
> -
> - /*create and turn on remaining swapfiles */
> - for (j = 0; j < swapfiles; j++) {
> + min_swapfiles = MIN_SWAP_FILES - used_swapfiles;
>
I can assume a potential issue here is: if a test system already
contains swapfiles more than MIN_SWAP_FILES, here min_swapfile
will be a negative value.
It sounds weird to mount a negative number of file for test.
What about:
min_swapfiles = MIN_SWAP_FILES > used_swapfiles ? \
(MIN_SWAP_FILES - used_swapfiles) : 0;
>
> + while (true) {
>
There is another issue in the infinite loop, if a kernel bug makes more
swapfile does not return EPERM but any others, here not report failure
and only keep looping forever.
Maybe we should set a uplimit (e.g MAX_TRIES) to avoid that happening.
/* Create the swapfile */
> - snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE,
> j + 2);
> - SAFE_MAKE_SMALL_SWAPFILE(filename);
> + snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE,
> swapfiles);
> + MAKE_SMALL_SWAPFILE(filename);
> +
> + /* Quit on a first swap file over max, check for EPERM */
> + if (swapon(filename, 0) == -1) {
> + if (errno == EPERM && swapfiles > min_swapfiles)
> + break;
>
> - /* turn on the swap file */
> - TST_EXP_PASS_SILENT(swapon(filename, 0));
> - if (!TST_PASS)
> - tst_brk(TFAIL, "Failed to setup swap files");
> + tst_res(TFAIL | TERRNO, "swapon(%s, 0)", filename);
> + }
> + swapfiles++;
> }
>
> tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> - MAKE_SMALL_SWAPFILE(TEST_FILE);
>
> return 0;
> }
> @@ -61,7 +68,7 @@ static int setup_swap(void)
> */
> static int check_and_swapoff(const char *filename)
> {
> - char cmd_buffer[256];
> + char cmd_buffer[FILENAME_MAX+28];
> int rc = -1;
>
Here we'd better initialize 'rc = 0' though the return value is not used
anywhere.
>
> snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file'
> /proc/swaps", filename);
> @@ -83,11 +90,9 @@ static void clean_swap(void)
> char filename[FILENAME_MAX];
>
> for (j = 0; j < swapfiles; j++) {
> - snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE,
> j + 2);
> + snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE,
> j);
> check_and_swapoff(filename);
> }
> -
> - check_and_swapoff(TEST_FILE);
> }
>
> static void verify_swapon(void)
> --
> 2.51.0
>
>
--
Regards,
Li Wang
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails
2025-12-19 13:06 ` Li Wang via ltp
@ 2025-12-19 14:18 ` Petr Vorel
0 siblings, 0 replies; 13+ messages in thread
From: Petr Vorel @ 2025-12-19 14:18 UTC (permalink / raw)
To: Li Wang; +Cc: ltp
...
> > - /*create and turn on remaining swapfiles */
> > - for (j = 0; j < swapfiles; j++) {
> > + min_swapfiles = MIN_SWAP_FILES - used_swapfiles;
> I can assume a potential issue here is: if a test system already
> contains swapfiles more than MIN_SWAP_FILES, here min_swapfile
> will be a negative value.
> It sounds weird to mount a negative number of file for test.
> What about:
> min_swapfiles = MIN_SWAP_FILES > used_swapfiles ? \
> (MIN_SWAP_FILES - used_swapfiles) : 0;
min_swapfiles is used only in comparison
if (errno == EPERM && swapfiles > min_swapfiles)
Therefore it's ok:
Due current limitation of requiring at least single swap, this later call will
always contain 1:
tst_res(TINFO, "Successfully created %d swap files", swapfiles);
I'll try to remove this limitation, therefore I'll print this only if
meaningful:
if (swapfiles > 0)
tst_res(TINFO, "Successfully created %d swap files", swapfiles);
else
tst_res(TINFO, "No swap file created");
> > + while (true) {
> There is another issue in the infinite loop, if a kernel bug makes more
> swapfile does not return EPERM but any others, here not report failure
> and only keep looping forever.
Or it can endup like:
swapon03.c:58: TFAIL: swapon(mntpoint/testswap116, 0): EPERM (1)
swapon03.c:51: TINFO: create a swapfile size of 1 megabytes (MB)
swapon03.c:51: TCONF: Insufficient disk space to create swap file
swapon03.c:79: TWARN: Failed to swapoff mntpoint/testswap01
swapon03.c:79: TWARN: Failed to swapoff mntpoint/testswap02
swapon03.c:79: TWARN: Failed to swapoff mntpoint/testswap03
swapon03.c:79: TWARN: Failed to swapoff mntpoint/testswap04
swapon03.c:79: TWARN: Failed to swapoff mntpoint/testswap05
(again bugs in cleanup).
Good point. I was thinking that Cyril's suggestion does not have break and
intend to add tst_brk(), but in the end I forget on it.
> Maybe we should set a uplimit (e.g MAX_TRIES) to avoid that happening.
tst_brk() is simpler than MAX_TRIES therefore I'd prefer it. But it skips
testing for following filesystems.
> /* Create the swapfile */
> > - snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE,
> > j + 2);
> > - SAFE_MAKE_SMALL_SWAPFILE(filename);
> > + snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE,
> > swapfiles);
> > + MAKE_SMALL_SWAPFILE(filename);
> > +
> > + /* Quit on a first swap file over max, check for EPERM */
> > + if (swapon(filename, 0) == -1) {
> > + if (errno == EPERM && swapfiles > min_swapfiles)
> > + break;
...
> > static int check_and_swapoff(const char *filename)
> > {
> > - char cmd_buffer[256];
> > + char cmd_buffer[FILENAME_MAX+28];
> > int rc = -1;
> Here we'd better initialize 'rc = 0' though the return value is not used
> anywhere.
I'd prefer to postpone cleanup like this to later. Otherwise we did not manage
to get this to LTP release :(.
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2025-12-22 8:55 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-19 9:42 [LTP] [PATCH v4 1/1] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-12-19 10:48 ` Cyril Hrubis
2025-12-19 12:41 ` Li Wang via ltp
2025-12-19 14:03 ` Petr Vorel
2025-12-19 14:04 ` Cyril Hrubis
2025-12-19 14:02 ` Petr Vorel
2025-12-19 14:41 ` Petr Vorel
2025-12-20 4:27 ` Li Wang via ltp
2025-12-20 4:32 ` Li Wang via ltp
2025-12-22 7:59 ` Petr Vorel
2025-12-22 8:55 ` Li Wang via ltp
2025-12-19 13:06 ` Li Wang via ltp
2025-12-19 14:18 ` Petr Vorel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox