All of lore.kernel.org
 help / color / mirror / Atom feed
From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Cc: Michal Hocko <mhocko@suse.com>
Subject: Re: [LTP] [PATCH 2/3] swapon03: Try to swapon() as many files until it fails
Date: Thu, 06 Nov 2025 11:11:14 +0100	[thread overview]
Message-ID: <2396783.ElGaqSPkdT@thinkpad> (raw)
In-Reply-To: <20251105154716.995786-3-pvorel@suse.cz>

Hi Petr,
some comments inline

On Wednesday, November 5, 2025 4:47:15 PM CET Petr Vorel 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.
> 
> Suggested-by: Michal Hocko <mhocko@suse.com>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
>  testcases/kernel/syscalls/swapon/swapon03.c | 63 +++++++++++++--------
>  1 file changed, 40 insertions(+), 23 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
> index b89d853188..0027f874be 100644
> --- a/testcases/kernel/syscalls/swapon/swapon03.c
> +++ b/testcases/kernel/syscalls/swapon/swapon03.c
> @@ -1,14 +1,18 @@
>  // SPDX-License-Identifier: GPL-2.0-or-later
>  /*
> + * Copyright (c) Linux Test Project, 2009-2025
>   * Copyright (c) International Business Machines Corp., 2007
>   * Created by <rsalveti@linux.vnet.ibm.com>
> - *
>   */
>  
>  /*\
> - * 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 (which is currently <23, 29> depending on kernel
> + * configuration). Instead test exptect that at least 15 swap files minus
> + * currently used swap can be created.
>   */
>  
>  #include <stdio.h>
> @@ -20,49 +24,54 @@
>  #include "lapi/syscalls.h"
>  #include "libswap.h"
>  
> +#define NUM_SWAP_FILES 15
>  #define MNTPOINT	"mntpoint"
>  #define TEST_FILE	MNTPOINT"/testswap"
>  
> -static int swapfiles;
> +static int *swapfiles;
>  
>  static void setup_swap(void)
>  {
>  	pid_t pid;
> -	int status;
> -	int j, max_swapfiles, used_swapfiles;
> +	int status, used_swapfiles, expected_swapfiles;
>  	char filename[FILENAME_MAX];
>  
>  	SAFE_SETEUID(0);
>  
> -	/* 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;
> +	expected_swapfiles = NUM_SWAP_FILES - used_swapfiles;
> +
> +	if (expected_swapfiles < 0) {
do we want to proceed when expected_swapfiles is 0 here?
> +		tst_brk(TCONF, "Warning: too many used swap files (%d)",
> +			expected_swapfiles);
I think this should be used_swapfiles ?
> +	}
>  
>  	pid = SAFE_FORK();
>  	if (pid == 0) {
> -		/*create and turn on remaining swapfiles */
> -		for (j = 0; j < swapfiles; j++) {
> -
> +		while (true) {
>  			/* Create the swapfile */
> -			snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
> +			snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, *swapfiles);
>  			MAKE_SMALL_SWAPFILE(filename);
>  
> -			/* turn on the swap file */
> -			TST_EXP_PASS_SILENT(swapon(filename, 0));
> +			/* Quit on a first swap file over max */
> +			if (swapon(filename, 0) == -1)
so now first swapon() fail is here and then we check for errno==EPERM by
making another swapon() call in verify_swapon function.
Is it possible to combine setup_swap() and verify_swapon() and check for
EPERM on first swapon() fail itself?

> +				break;
> +			(*swapfiles)++;
>  		}
>  		exit(0);
>  	} else {
>  		waitpid(pid, &status, 0);
>  	}
>  
> -	if (WEXITSTATUS(status))
> +	if (WEXITSTATUS(status) || *swapfiles == 0)
>  		tst_brk(TBROK, "Failed to setup swap files");
>  
> -	tst_res(TINFO, "Successfully created %d swap files", swapfiles);
> -	MAKE_SMALL_SWAPFILE(TEST_FILE);
> +	if (*swapfiles < expected_swapfiles) {
> +		tst_res(TWARN, "Successfully created only %d swap files (>= %d expected)",
> +			*swapfiles, expected_swapfiles);
> +	} else {
> +		tst_res(TINFO, "Successfully created %d swap files", *swapfiles);
> +	}
>  }
>  
>  /*
> @@ -86,8 +95,8 @@ static void clean_swap(void)
>  	int j;
>  	char filename[FILENAME_MAX];
>  
> -	for (j = 0; j < swapfiles; j++) {
> -		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
> +	for (j = 0; j < *swapfiles; j++) {
> +		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j);
>  		check_and_swapoff(filename);
>  	}
>  
> @@ -105,12 +114,20 @@ static void setup(void)
>  		tst_brk(TCONF, "swap not supported by kernel");
>  
>  	is_swap_supported(TEST_FILE);
> +
> +	swapfiles = SAFE_MMAP(NULL, sizeof(*swapfiles), PROT_READ | PROT_WRITE,
> +			MAP_SHARED | MAP_ANONYMOUS, -1, 0);
> +	*swapfiles = 0;
> +
>  	setup_swap();
>  }
>  
>  static void cleanup(void)
>  {
> -	clean_swap();
> +	if (swapfiles) {
> +		clean_swap();
> +		SAFE_MUNMAP(swapfiles, sizeof(*swapfiles));
> +	}
>  }
>  
>  static struct tst_test test = {
> 

Regards,
Avinesh



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

  parent reply	other threads:[~2025-11-06 10:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-05 15:47 [LTP] [PATCH 0/3] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-11-05 15:47 ` [LTP] [PATCH 1/3] swapon03: Cleanup Petr Vorel
2025-11-05 15:47 ` [LTP] [PATCH 2/3] swapon03: Try to swapon() as many files until it fails Petr Vorel
2025-11-06  3:46   ` Li Wang via ltp
2025-11-06  9:07     ` Petr Vorel
2025-11-06 10:11   ` Avinesh Kumar [this message]
2025-11-06 12:03     ` Petr Vorel
2025-11-06 13:31       ` Avinesh Kumar
2025-11-05 15:47 ` [LTP] [PATCH 3/3] libswap: Remove now unused tst_max_swapfiles() Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2396783.ElGaqSPkdT@thinkpad \
    --to=akumar@suse.de \
    --cc=ltp@lists.linux.it \
    --cc=mhocko@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.