diff mbox series

[v5,1/7] libltpswap: Add tst_max_swapfiles API

Message ID 20240226135336.19733-1-xuyang2018.jy@fujitsu.com
State Accepted
Headers show
Series [v5,1/7] libltpswap: Add tst_max_swapfiles API | expand

Commit Message

Yang Xu Feb. 26, 2024, 1:53 p.m. UTC
Currently, the kernel constant MAX_SWAPFILES value is calculated as below
------------------------------------
 #ifdef CONFIG_DEVICE_PRIVATE
 #define SWP_DEVICE_NUM 4
 #else
 #define SWP_DEVICE_NUM 0
 #endif

 #ifdef CONFIG_MIGRATION
 #define SWP_MIGRATION_NUM 3
 #else
 #define SWP_MIGRATION_NUM 0

 #ifdef CONFIG_MEMORY_FAILURE
 #define SWP_HWPOISON_NUM 1
 #else
 #define SWP_HWPOISON_NUM 0
 #endif

 #define SWP_PTE_MARKER_NUM 1
 #define MAX_SWAPFILES_SHIFT   5

 #define MAX_SWAPFILES \
	((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
	SWP_MIGRATION_NUM - SWP_HWPOISON_NUM - \
	SWP_PTE_MARKER_NUM)
------------------------------------
Also, man-pages missed something after 5.14 kernel. I have sent two patches[1][2] to
add it. The kernel patches modify this kernel constant in[3][4][5][6]. Also, after kernel 6.2.0
[7],kernel always compile in pte markers.

[1]https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=26f3ec74e
[2]https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=6bf3937fc
[3]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/swap.h?id=5042db43cc
[4]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/swap.h?id=b756a3b5e
[5]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/swap.h?id=679d10331
[6]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/swap.h?id=6c287605f
[7]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/swap.h?id=ca92ea3dc5

Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 include/libswap.h         |  7 +++++++
 libs/libltpswap/libswap.c | 44 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

Comments

Li Wang Feb. 27, 2024, 9:13 a.m. UTC | #1
Hi Xu, Petr,

The patch set generally looks good to me.

Reviewed-by: Li Wang <liwang@redhat.com>
Petr Vorel Feb. 28, 2024, 7:19 a.m. UTC | #2
Hi Yang Xu,

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

Kind regards,
Petr
Yang Xu Feb. 28, 2024, 10:32 a.m. UTC | #3
Hi Li, Petr

> Hi Xu, Petr,
> 
> The patch set generally looks good to me.
> 
> Reviewed-by: Li Wang <liwang@redhat.com <mailto:liwang@redhat.com>>

Thanks for your review, merged!

Best Regars
Yang Xu
> 
> 
> -- 
> Regards,
> Li Wang
diff mbox series

Patch

diff --git a/include/libswap.h b/include/libswap.h
index bdc5aacc6..cc68d5e6b 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -21,4 +21,11 @@  int make_swapfile(const char *swapfile, int blocks, int safe);
  * we are testing on.
  */
 bool is_swap_supported(const char *filename);
+
+/*
+ * Get kernel constant MAX_SWAPFILES value.
+ *
+ */
+int tst_max_swapfiles(void);
+
 #endif /* __LIBSWAP_H__ */
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index c79de19d7..3528a3fb9 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -12,10 +12,13 @@ 
 #include <stdbool.h>
 
 #define TST_NO_DEFAULT_MAIN
+#define DEFAULT_MAX_SWAPFILE 32
 
 #include "tst_test.h"
 #include "libswap.h"
 #include "lapi/syscalls.h"
+#include "tst_kconfig.h"
+#include "tst_safe_stdio.h"
 
 static const char *const swap_supported_fs[] = {
 	"btrfs",
@@ -217,3 +220,44 @@  bool is_swap_supported(const char *filename)
 
 	return true;
 }
+
+/*
+ * Get kernel constant MAX_SWAPFILES value.
+ */
+int tst_max_swapfiles(void)
+{
+	unsigned int swp_migration_num = 0, swp_hwpoison_num = 0, swp_device_num = 0, swp_pte_marker_num = 0;
+	struct tst_kconfig_var migration = TST_KCONFIG_INIT("CONFIG_MIGRATION");
+	struct tst_kconfig_var memory = TST_KCONFIG_INIT("CONFIG_MEMORY_FAILURE");
+	struct tst_kconfig_var device = TST_KCONFIG_INIT("CONFIG_DEVICE_PRIVATE");
+	struct tst_kconfig_var marker = TST_KCONFIG_INIT("CONFIG_PTE_MARKER");
+
+	tst_kconfig_read(&migration, 1);
+	tst_kconfig_read(&memory, 1);
+	tst_kconfig_read(&device, 1);
+	tst_kconfig_read(&marker, 1);
+
+	if (migration.choice == 'y') {
+		if (tst_kvercmp(5, 19, 0) < 0)
+			swp_migration_num = 2;
+		else
+			swp_migration_num = 3;
+	}
+
+	if (memory.choice == 'y')
+		swp_hwpoison_num = 1;
+
+	if (device.choice == 'y') {
+		if (tst_kvercmp(4, 14, 0) >= 0)
+			swp_device_num = 2;
+		if (tst_kvercmp(5, 14, 0) >= 0)
+			swp_device_num = 4;
+	}
+
+	if ((marker.choice == 'y' && tst_kvercmp(5, 19, 0) >= 0) ||
+		tst_kvercmp(6, 2, 0) >= 0) {
+		swp_pte_marker_num = 1;
+	}
+
+	return DEFAULT_MAX_SWAPFILE - swp_migration_num - swp_hwpoison_num - swp_device_num - swp_pte_marker_num;
+}