diff mbox series

[v2] libswap.c: Improve calculate swap dev number

Message ID 20240305141057.8754-1-wegao@suse.com
State Accepted
Headers show
Series [v2] libswap.c: Improve calculate swap dev number | expand

Commit Message

Wei Gao March 5, 2024, 2:10 p.m. UTC
I encounter a dead loop on following code in our test on ppc64 machine:
while ((c = fgetc(fp)) != EOF)

Use fgets instead of fgetc can avoid above issue.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 libs/libltpswap/libswap.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

Petr Vorel March 5, 2024, 2:45 p.m. UTC | #1
> I encounter a dead loop on following code in our test on ppc64 machine:
> while ((c = fgetc(fp)) != EOF)

FYI it fails also on aarch64 and s390x, thus I'll add it to the commit message
before merge. I also try to have look why it's broken.

> Use fgets instead of fgetc can avoid above issue.

LGTM. I probably merge it tonight.

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
>  libs/libltpswap/libswap.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)

> diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> index c10db91c0..a26ea25e4 100644
> --- a/libs/libltpswap/libswap.c
> +++ b/libs/libltpswap/libswap.c
> @@ -13,6 +13,7 @@

>  #define TST_NO_DEFAULT_MAIN
>  #define DEFAULT_MAX_SWAPFILE 32
> +#define BUFSIZE 200

>  #include "tst_test.h"
>  #include "libswap.h"
> @@ -279,16 +280,14 @@ int tst_count_swaps(void)
>  {
>  	FILE *fp;
>  	int used = -1;
> -	char c;
> +	char buf[BUFSIZE];

>  	fp = SAFE_FOPEN("/proc/swaps", "r");
>  	if (fp == NULL)
>  		return -1;

> -	while ((c = fgetc(fp)) != EOF) {
> -		if (c == '\n')
> -			used++;
> -	}
> +	while (fgets(buf, BUFSIZE, fp) != NULL)
> +		used++;

>  	SAFE_FCLOSE(fp);
>  	if (used < 0)
Petr Vorel March 5, 2024, 9:04 p.m. UTC | #2
Hi Wei, all,

thanks, merged!

FYI swapon03 with this patch still fails on SLES 12-SP5 (4.12 based kernel):

tst_kconfig.c:87: TINFO: Parsing kernel config '/proc/config.gz'
tst_kconfig.c:87: TINFO: Parsing kernel config '/proc/config.gz'
swapon03.c:55: TFAIL: swapon(filename, 0) failed: EPERM (1)
swapon03.c:55: TFAIL: swapon(filename, 0) failed: EPERM (1)

But that's likely fails even without the patch (going to verify older SLES
releases).

Kind regards,
Petr
Yang Xu March 6, 2024, 1:32 a.m. UTC | #3
Hi Petr, all,


> Hi Wei, all,
> 
> thanks, merged!
> 
> FYI swapon03 with this patch still fails on SLES 12-SP5 (4.12 based kernel):
> 
> tst_kconfig.c:87: TINFO: Parsing kernel config '/proc/config.gz'
> tst_kconfig.c:87: TINFO: Parsing kernel config '/proc/config.gz'
> swapon03.c:55: TFAIL: swapon(filename, 0) failed: EPERM (1)
> swapon03.c:55: TFAIL: swapon(filename, 0) failed: EPERM (1)
> 
> But that's likely fails even without the patch (going to verify older SLES
> releases).
> 
> Kind regards,
> Petr

I guess SLES 12-SP5 has backported some patch that modified kernel 
constant MAX_SWAPFILES value[1]. But ltp doesn't add fallback for SLES 
like I did for RHEL9.

I think you can paste your kernel code(include/linux/swap.h), then you 
or we can figure out what cause this failure.

[1]https://github.com/linux-test-project/ltp/commit/c1b8c011e447088b08787462e0c2ed50cd8c43f3

Best Regards
Yang Xu
Petr Vorel March 6, 2024, 9:59 a.m. UTC | #4
Hi Xu,

> Hi Petr, all,


> > Hi Wei, all,

> > thanks, merged!

> > FYI swapon03 with this patch still fails on SLES 12-SP5 (4.12 based kernel):

> > tst_kconfig.c:87: TINFO: Parsing kernel config '/proc/config.gz'
> > tst_kconfig.c:87: TINFO: Parsing kernel config '/proc/config.gz'
> > swapon03.c:55: TFAIL: swapon(filename, 0) failed: EPERM (1)
> > swapon03.c:55: TFAIL: swapon(filename, 0) failed: EPERM (1)

> > But that's likely fails even without the patch (going to verify older SLES
> > releases).

> > Kind regards,
> > Petr

> I guess SLES 12-SP5 has backported some patch that modified kernel 
> constant MAX_SWAPFILES value[1]. But ltp doesn't add fallback for SLES 
> like I did for RHEL9.

Very good point, there are some patches.

> I think you can paste your kernel code(include/linux/swap.h), then you 
> or we can figure out what cause this failure.

I'll try to figure out what is missing and post the config (either with patches
or just config). Thanks!

> [1]https://github.com/linux-test-project/ltp/commit/c1b8c011e447088b08787462e0c2ed50cd8c43f3

> Best Regards
> Yang Xu
diff mbox series

Patch

diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index c10db91c0..a26ea25e4 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -13,6 +13,7 @@ 
 
 #define TST_NO_DEFAULT_MAIN
 #define DEFAULT_MAX_SWAPFILE 32
+#define BUFSIZE 200
 
 #include "tst_test.h"
 #include "libswap.h"
@@ -279,16 +280,14 @@  int tst_count_swaps(void)
 {
 	FILE *fp;
 	int used = -1;
-	char c;
+	char buf[BUFSIZE];
 
 	fp = SAFE_FOPEN("/proc/swaps", "r");
 	if (fp == NULL)
 		return -1;
 
-	while ((c = fgetc(fp)) != EOF) {
-		if (c == '\n')
-			used++;
-	}
+	while (fgets(buf, BUFSIZE, fp) != NULL)
+		used++;
 
 	SAFE_FCLOSE(fp);
 	if (used < 0)