* [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
@ 2025-12-22 9:44 Petr Vorel
2025-12-22 12:58 ` Li Wang via ltp
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Petr Vorel @ 2025-12-22 9:44 UTC (permalink / raw)
To: ltp
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>
---
v4->v5:
* Use tst_brk(TFAIL | TERRNO) to avoid infinite loop (Li).
* Minor fix in doc (missing bracket).
Link to v4:
https://lore.kernel.org/ltp/20251219094219.151887-1-pvorel@suse.cz/
Note: I'll plan to do followup fixes in beginning of January (nicely
summarized by Li [1] [2]):
is_swapfile_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_swapfile_supported()
- create a temporary swapfile
- swapon() / swapoff() it
- clean up
[1] https://lore.kernel.org/ltp/CAEemH2f2nhCBzxw-5u5qGnDS9BcogD-KWOd+mrOoLvmJ0XPR9w@mail.gmail.com/
[2] https://lore.kernel.org/ltp/CAEemH2dts8FsEAM7gfKQjSv3ohkVehy9fXSf09_bqLfoDnUf1g@mail.gmail.com/
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..53c5750de4 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_brk(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] 8+ messages in thread* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2025-12-22 9:44 [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails Petr Vorel
@ 2025-12-22 12:58 ` Li Wang via ltp
2026-01-05 7:14 ` Andrea Cervesato via ltp
2026-01-06 12:03 ` Cyril Hrubis
2 siblings, 0 replies; 8+ messages in thread
From: Li Wang via ltp @ 2025-12-22 12:58 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Reviewed-by: Li Wang <liwang@redhat.com>
--
Regards,
Li Wang
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2025-12-22 9:44 [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-12-22 12:58 ` Li Wang via ltp
@ 2026-01-05 7:14 ` Andrea Cervesato via ltp
2026-01-05 7:49 ` Petr Vorel
2026-01-06 12:03 ` Cyril Hrubis
2 siblings, 1 reply; 8+ messages in thread
From: Andrea Cervesato via ltp @ 2026-01-05 7:14 UTC (permalink / raw)
To: Petr Vorel, ltp
Hi!
> - char cmd_buffer[256];
> + char cmd_buffer[FILENAME_MAX+28];
I'm not sure if +28 is needed.
Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com>
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2026-01-05 7:14 ` Andrea Cervesato via ltp
@ 2026-01-05 7:49 ` Petr Vorel
2026-01-06 11:57 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 8+ messages in thread
From: Petr Vorel @ 2026-01-05 7:49 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
> Hi!
> > - char cmd_buffer[256];
> > + char cmd_buffer[FILENAME_MAX+28];
> I'm not sure if +28 is needed.
I described the reason in the commit message:
It was required to increase cmd_buffer size to avoid directive output
may be truncated warning.
I get warning on smaller size:
swapon03.c:74:60: warning: ‘%s’ directive output may be truncated writing up to 4095 bytes into a region of size 4087 [-Wformat-truncation=]
74 | snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
| ^~
......
94 | check_and_swapoff(filename);
| ~~~~~~~~
In file included from /usr/include/stdio.h:970,
from swapon03.c:17:
In function ‘snprintf’,
inlined from ‘check_and_swapoff’ at swapon03.c:74:2,
inlined from ‘clean_swap’ at swapon03.c:94:3:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:68:10: note: ‘__builtin___snprintf_chk’ output between 29 and 4124 bytes into a destination of size 4096
68 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 | __glibc_objsize (__s), __fmt,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 | __va_arg_pack ());
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2026-01-05 7:49 ` Petr Vorel
@ 2026-01-06 11:57 ` Andrea Cervesato via ltp
2026-01-07 7:53 ` Petr Vorel
0 siblings, 1 reply; 8+ messages in thread
From: Andrea Cervesato via ltp @ 2026-01-06 11:57 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
On Mon Jan 5, 2026 at 8:49 AM CET, Petr Vorel wrote:
> > Hi!
>
> > > - char cmd_buffer[256];
> > > + char cmd_buffer[FILENAME_MAX+28];
>
> > I'm not sure if +28 is needed.
>
> I described the reason in the commit message:
>
> It was required to increase cmd_buffer size to avoid directive output
> may be truncated warning.
>
> I get warning on smaller size:
>
> swapon03.c:74:60: warning: ‘%s’ directive output may be truncated writing up to 4095 bytes into a region of size 4087 [-Wformat-truncation=]
> 74 | snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
> | ^~
> ......
> 94 | check_and_swapoff(filename);
> | ~~~~~~~~
> In file included from /usr/include/stdio.h:970,
> from swapon03.c:17:
> In function ‘snprintf’,
> inlined from ‘check_and_swapoff’ at swapon03.c:74:2,
> inlined from ‘clean_swap’ at swapon03.c:94:3:
> /usr/include/x86_64-linux-gnu/bits/stdio2.h:68:10: note: ‘__builtin___snprintf_chk’ output between 29 and 4124 bytes into a destination of size 4096
> 68 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 69 | __glibc_objsize (__s), __fmt,
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 70 | __va_arg_pack ());
>
> Kind regards,
> Petr
Sorry, I missed the git comment. Can you also comment the source code
please? So we don't guess what's going on in the buffer, based on
git message.
Besides that,
Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com>
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2026-01-06 11:57 ` Andrea Cervesato via ltp
@ 2026-01-07 7:53 ` Petr Vorel
2026-01-07 9:08 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 8+ messages in thread
From: Petr Vorel @ 2026-01-07 7:53 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi all,
> > > > - char cmd_buffer[256];
> > > > + char cmd_buffer[FILENAME_MAX+28];
> > > I'm not sure if +28 is needed.
> > I described the reason in the commit message:
> > It was required to increase cmd_buffer size to avoid directive output
> > may be truncated warning.
> > I get warning on smaller size:
> > swapon03.c:74:60: warning: ‘%s’ directive output may be truncated writing up to 4095 bytes into a region of size 4087 [-Wformat-truncation=]
> > 74 | snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
> > | ^~
> > ......
> > 94 | check_and_swapoff(filename);
> > | ~~~~~~~~
> > In file included from /usr/include/stdio.h:970,
> > from swapon03.c:17:
> > In function ‘snprintf’,
> > inlined from ‘check_and_swapoff’ at swapon03.c:74:2,
> > inlined from ‘clean_swap’ at swapon03.c:94:3:
> > /usr/include/x86_64-linux-gnu/bits/stdio2.h:68:10: note: ‘__builtin___snprintf_chk’ output between 29 and 4124 bytes into a destination of size 4096
> > 68 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
> > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 69 | __glibc_objsize (__s), __fmt,
> > | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 70 | __va_arg_pack ());
> > Kind regards,
> > Petr
> Sorry, I missed the git comment. Can you also comment the source code
> please? So we don't guess what's going on in the buffer, based on
> git message.
FYI merged with just added FILENAME_MAX+28 in the commit message to be more
obvious. With recent gcc we can have many fixes like this, I'm not sure if we
want to comment every -Wformat-truncation in the source code.
Thanks to all for a review.
I'll plan to do the rest of the test cleanup as we agreed.
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2026-01-07 7:53 ` Petr Vorel
@ 2026-01-07 9:08 ` Andrea Cervesato via ltp
0 siblings, 0 replies; 8+ messages in thread
From: Andrea Cervesato via ltp @ 2026-01-07 9:08 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
> FYI merged with just added FILENAME_MAX+28 in the commit message to be more
> obvious. With recent gcc we can have many fixes like this, I'm not sure if we
> want to comment every -Wformat-truncation in the source code.
This can be challenging yes. Let me know if you need a help.
>
> Thanks to all for a review.
> I'll plan to do the rest of the test cleanup as we agreed.
>
> Kind regards,
> Petr
Thanks,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails
2025-12-22 9:44 [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-12-22 12:58 ` Li Wang via ltp
2026-01-05 7:14 ` Andrea Cervesato via ltp
@ 2026-01-06 12:03 ` Cyril Hrubis
2 siblings, 0 replies; 8+ messages in thread
From: Cyril Hrubis @ 2026-01-06 12:03 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
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] 8+ messages in thread
end of thread, other threads:[~2026-01-07 9:09 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-22 9:44 [LTP] [PATCH v5] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-12-22 12:58 ` Li Wang via ltp
2026-01-05 7:14 ` Andrea Cervesato via ltp
2026-01-05 7:49 ` Petr Vorel
2026-01-06 11:57 ` Andrea Cervesato via ltp
2026-01-07 7:53 ` Petr Vorel
2026-01-07 9:08 ` Andrea Cervesato via ltp
2026-01-06 12:03 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox