[PATCH net-next v2 6/8] selftests: net: lib: Move checks from forwarding/lib.sh here

2024-10-24 Thread Petr Machata
For logging to be useful, something has to set RET and retmsg by calling
ret_set_ksft_status(). There is a suite of functions to that end in
forwarding/lib: check_err, check_fail et.al. Move them to net/lib.sh so
that every net test can use them.

Existing lib.sh users might be using these same names for their functions.
However lib.sh is always sourced near the top of the file (checked), and
whatever new definitions will simply override the ones provided by lib.sh.

Signed-off-by: Petr Machata 
Reviewed-by: Amit Cohen 
Acked-by: Shuah Khan 
---

Notes:
CC: Shuah Khan 
CC: Benjamin Poirier 
CC: Hangbin Liu 
CC: linux-kselftest@vger.kernel.org
CC: Jiri Pirko 
---

 tools/testing/selftests/net/forwarding/lib.sh | 73 ---
 tools/testing/selftests/net/lib.sh| 73 +++
 2 files changed, 73 insertions(+), 73 deletions(-)

diff --git a/tools/testing/selftests/net/forwarding/lib.sh 
b/tools/testing/selftests/net/forwarding/lib.sh
index d28dbf27c1f0..8625e3c99f55 100644
--- a/tools/testing/selftests/net/forwarding/lib.sh
+++ b/tools/testing/selftests/net/forwarding/lib.sh
@@ -445,79 +445,6 @@ done
 ##
 # Helpers
 
-# Whether FAILs should be interpreted as XFAILs. Internal.
-FAIL_TO_XFAIL=
-
-check_err()
-{
-   local err=$1
-   local msg=$2
-
-   if ((err)); then
-   if [[ $FAIL_TO_XFAIL = yes ]]; then
-   ret_set_ksft_status $ksft_xfail "$msg"
-   else
-   ret_set_ksft_status $ksft_fail "$msg"
-   fi
-   fi
-}
-
-check_fail()
-{
-   local err=$1
-   local msg=$2
-
-   check_err $((!err)) "$msg"
-}
-
-check_err_fail()
-{
-   local should_fail=$1; shift
-   local err=$1; shift
-   local what=$1; shift
-
-   if ((should_fail)); then
-   check_fail $err "$what succeeded, but should have failed"
-   else
-   check_err $err "$what failed"
-   fi
-}
-
-xfail()
-{
-   FAIL_TO_XFAIL=yes "$@"
-}
-
-xfail_on_slow()
-{
-   if [[ $KSFT_MACHINE_SLOW = yes ]]; then
-   FAIL_TO_XFAIL=yes "$@"
-   else
-   "$@"
-   fi
-}
-
-omit_on_slow()
-{
-   if [[ $KSFT_MACHINE_SLOW != yes ]]; then
-   "$@"
-   fi
-}
-
-xfail_on_veth()
-{
-   local dev=$1; shift
-   local kind
-
-   kind=$(ip -j -d link show dev $dev |
-   jq -r '.[].linkinfo.info_kind')
-   if [[ $kind = veth ]]; then
-   FAIL_TO_XFAIL=yes "$@"
-   else
-   "$@"
-   fi
-}
-
 not()
 {
"$@"
diff --git a/tools/testing/selftests/net/lib.sh 
b/tools/testing/selftests/net/lib.sh
index 4f52b8e48a3a..6bcf5d13879d 100644
--- a/tools/testing/selftests/net/lib.sh
+++ b/tools/testing/selftests/net/lib.sh
@@ -361,3 +361,76 @@ tests_run()
$current_test
done
 }
+
+# Whether FAILs should be interpreted as XFAILs. Internal.
+FAIL_TO_XFAIL=
+
+check_err()
+{
+   local err=$1
+   local msg=$2
+
+   if ((err)); then
+   if [[ $FAIL_TO_XFAIL = yes ]]; then
+   ret_set_ksft_status $ksft_xfail "$msg"
+   else
+   ret_set_ksft_status $ksft_fail "$msg"
+   fi
+   fi
+}
+
+check_fail()
+{
+   local err=$1
+   local msg=$2
+
+   check_err $((!err)) "$msg"
+}
+
+check_err_fail()
+{
+   local should_fail=$1; shift
+   local err=$1; shift
+   local what=$1; shift
+
+   if ((should_fail)); then
+   check_fail $err "$what succeeded, but should have failed"
+   else
+   check_err $err "$what failed"
+   fi
+}
+
+xfail()
+{
+   FAIL_TO_XFAIL=yes "$@"
+}
+
+xfail_on_slow()
+{
+   if [[ $KSFT_MACHINE_SLOW = yes ]]; then
+   FAIL_TO_XFAIL=yes "$@"
+   else
+   "$@"
+   fi
+}
+
+omit_on_slow()
+{
+   if [[ $KSFT_MACHINE_SLOW != yes ]]; then
+   "$@"
+   fi
+}
+
+xfail_on_veth()
+{
+   local dev=$1; shift
+   local kind
+
+   kind=$(ip -j -d link show dev $dev |
+   jq -r '.[].linkinfo.info_kind')
+   if [[ $kind = veth ]]; then
+   FAIL_TO_XFAIL=yes "$@"
+   else
+   "$@"
+   fi
+}
-- 
2.45.0




[PATCH v2] lib/math: Add int_sqrt test suite

2024-10-24 Thread Luis Felipe Hernandez
Adds test suite for integer based square root function.

The test suite is designed to verify the correctness of the int_sqrt
math library function.

Signed-off-by: Luis Felipe Hernandez 
---
 lib/Kconfig.debug   | 16 +++
 lib/math/Makefile   |  1 +
 lib/math/tests/Makefile |  1 +
 lib/math/tests/int_sqrt_kunit.c | 51 +
 4 files changed, 69 insertions(+)
 create mode 100644 lib/math/tests/int_sqrt_kunit.c

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 7312ae7c3cc5..772c681dff3e 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2993,6 +2993,22 @@ config TEST_OBJPOOL
 
  If unsure, say N.
 
+config INT_SQRT_KUNIT_TEST
+   tristate "Integer square root test test" if !KUNIT_ALL_TESTS
+   depends on KUNIT
+   default KUNIT_ALL_TESTS
+   help
+ This option enables the KUnit test suite for the int_sqrt function,
+ which performs square root calculation. The test suite checks
+ various scenarios, including edge cases, to ensure correctness.
+
+ Enabling this option will include tests that check various scenarios
+ and edge cases to ensure the accuracy and reliability of the square 
root
+ function.
+
+ If unsure, say N
+
+
 endif # RUNTIME_TESTING_MENU
 
 config ARCH_USE_MEMTEST
diff --git a/lib/math/Makefile b/lib/math/Makefile
index 3ef11305f8d2..25bcb968b369 100644
--- a/lib/math/Makefile
+++ b/lib/math/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_INT_POW_TEST)  += tests/int_pow_kunit.o
 obj-$(CONFIG_TEST_DIV64)   += test_div64.o
 obj-$(CONFIG_TEST_MULDIV64)+= test_mul_u64_u64_div_u64.o
 obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o
+obj-y  += tests/
diff --git a/lib/math/tests/Makefile b/lib/math/tests/Makefile
index 6a169123320a..e1a79f093b2d 100644
--- a/lib/math/tests/Makefile
+++ b/lib/math/tests/Makefile
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o
+obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
diff --git a/lib/math/tests/int_sqrt_kunit.c b/lib/math/tests/int_sqrt_kunit.c
new file mode 100644
index ..a93aba31cd05
--- /dev/null
+++ b/lib/math/tests/int_sqrt_kunit.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct test_case_params {
+   unsigned long x;
+   unsigned long expected_result;
+   const char *name;
+};
+
+static const struct test_case_params params[] = {
+   { 0, 0, "edge-case: square root of 0" },
+   { 4, 2, "perfect square: square root of 4" },
+   { 81, 9, "perfect square: square root of 9" },
+   { 2, 1, "non-perfect square: square root of 2" },
+   { 5, 2, "non-perfect square: square root of 5"},
+   { ULONG_MAX, 4294967295, "large input"},
+};
+
+static void get_desc(const struct test_case_params *tc, char *desc)
+{
+   strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(int_sqrt, params, get_desc);
+
+static void int_sqrt_test(struct kunit *test)
+{
+   const struct test_case_params *tc = (const struct test_case_params 
*)test->param_value;
+
+   KUNIT_EXPECT_EQ(test, tc->expected_result, int_sqrt(tc->x));
+}
+
+static struct kunit_case math_int_sqrt_test_cases[] = {
+   KUNIT_CASE_PARAM(int_sqrt_test, int_sqrt_gen_params),
+   {}
+};
+
+static struct kunit_suite int_sqrt_test_suite = {
+   .name = "math-int_sqrt",
+   .test_cases = math_int_sqrt_test_cases,
+};
+
+kunit_test_suites(&int_sqrt_test_suite);
+
+MODULE_DESCRIPTION("math.int_sqrt KUnit test suite");
+MODULE_LICENSE("GPL");
-- 
2.47.0




Re: [PATCH v2 3/5] arm64: signal: Improve POR_EL0 handling to avoid uaccess failures

2024-10-24 Thread Kevin Brodsky
On 24/10/2024 12:59, Catalin Marinas wrote:
> On Wed, Oct 23, 2024 at 04:05:09PM +0100, Kevin Brodsky wrote:
>> +/*
>> + * Save the unpriv access state into ua_state and reset it to disable any
>> + * restrictions.
>> + */
>> +static void save_reset_user_access_state(struct user_access_state *ua_state)
>> +{
>> +if (system_supports_poe()) {
>> +/*
>> + * Enable all permissions in all 8 keys
>> + * (inspired by REPEAT_BYTE())
>> + */
>> +u64 por_enable_all = (~0u / POE_MASK) * POE_RXW;
> I think this should be ~0ul.

It is ~0u on purpose, because unlike in REPEAT_BYTE(), I only wanted the
lower 32 bits to be filled with POE_RXW (we only have 8 keys, the top 32
bits are RES0). That said, given that D128 has 4-bit pkeys, we could
anticipate and fill the top 32 bits too (should make no difference on D64).

>> @@ -907,6 +964,7 @@ SYSCALL_DEFINE0(rt_sigreturn)
>>  {
>>  struct pt_regs *regs = current_pt_regs();
>>  struct rt_sigframe __user *frame;
>> +struct user_access_state ua_state;
>>  
>>  /* Always make any pending restarted system calls return -EINTR */
>>  current->restart_block.fn = do_no_restart_syscall;
>> @@ -923,12 +981,14 @@ SYSCALL_DEFINE0(rt_sigreturn)
>>  if (!access_ok(frame, sizeof (*frame)))
>>  goto badframe;
>>  
>> -if (restore_sigframe(regs, frame))
>> +if (restore_sigframe(regs, frame, &ua_state))
>>  goto badframe;
>>  
>>  if (restore_altstack(&frame->uc.uc_stack))
>>  goto badframe;
>>  
>> +restore_user_access_state(&ua_state);
>> +
>>  return regs->regs[0];
>>  
>>  badframe:
> The saving part I'm fine with. For restoring, I was wondering whether we
> can get a more privileged POR_EL0 if reading the frame somehow failed.
> This is largely theoretical, there are other ways to attack like
> writing POR_EL0 directly than unmapping/remapping the signal stack.
>
> What I'd change here is always restore_user_access_state() to
> POR_EL0_INIT. Maybe just initialise ua_state above and add the function
> call after the badframe label.

I'm not sure I understand. When we enter this function, POR_EL0 is set
to whatever the signal handler set it to (POR_EL0_INIT by default).
There are then two cases:
1) Everything succeeds, including reading the saved POR_EL0 from the
frame. We then call restore_user_access_state(), setting POR_EL0 to the
value we've read, and return to userspace.
2) Any uaccess fails (for instance reading POR_EL0). In that case we
leave POR_EL0 unchanged and deliver SIGSEGV.

In case 2 POR_EL0 is most likely already set to POR_EL0_INIT, or
whatever the signal handler set it to. It's not clear to me that forcing
it to POR_EL0_INIT helps much. Either way it's doubtful that the SIGSEGV
handler will be able to recover, since the new signal frame we will
create for it may be a mix of interrupted state and signal handler state
(depending on exactly where we fail).

Kevin



[PATCH] selftests: Add kselftest framework to the testfile

2024-10-24 Thread Shivam Chaudhary
This patch updates the existing test that checks
for `open(O_TMPFILE)` and `linkat()` behaviors in
mount namespaces to use the kselftest framework.

This includes the following changes:

- Replaced direct error handling with
 `ksft_test_result_*` macros for better reporting
  of test outcomes.

- Added `ksft_print_header()` and `ksft_set_plan()`
 to structure test outputs more effectively.

- Introduced the helper function `is_unshare()` to
  handle unshare-related checks.

- Improved the test flow by adding more detailed pass/fail
  reporting for unshare, mounting, file opening, and linking
  operations.

- Skips the test if it's not run as root, providing an
  appropriate Warning.

Test logs:

Before change:

- Withou root
 error: unshare, errno 1

- With root
 No, output

After change:

- Without root
 TAP version 13
 1..1
 ok 1 # SKIP This test needs root to ru

- With root
 TAP version 13
 1..1
 ok 1 unshare(): we have a new mount namespace.
 1..2
 ok 2 mount(): Root filesystem private mount: Success
 1..3
 ok 3 mount(): Mounting tmpfs on /tmp: Success
 1..4
 ok 4 openat(): Open first temporary file: Success
 1..5
 ok 5 linkat(): Linking the temporary file: Success
 1..6
 ok 6 openat(): Opening the second temporary file: Success
 # Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0

Signed-off-by: Shivam Chaudhary 
---
 .../selftests/tmpfs/bug-link-o-tmpfile.c  | 72 +++
 1 file changed, 58 insertions(+), 14 deletions(-)

diff --git a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c 
b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
index b5c3ddb90942..26dea19c1614 100644
--- a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
+++ b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
@@ -23,45 +23,89 @@
 #include 
 #include 
 
-int main(void)
-{
-   int fd;
+#include "../kselftest.h"
 
-   if (unshare(CLONE_NEWNS) == -1) {
+static int is_unshare(int flag)
+{
+   if (unshare(flag) == -1) {
if (errno == ENOSYS || errno == EPERM) {
-   fprintf(stderr, "error: unshare, errno %d\n", errno);
-   return 4;
+   ksft_test_result_fail("error: unshare, errno %d\n", 
errno);
+   return -1; // Return -1 for failure
}
fprintf(stderr, "error: unshare, errno %d\n", errno);
+   return -1;
+   }
+
+   return 0; // Return 0 for success
+}
+
+int main(void)
+{
+   int fd;
+
+   // Setting up kselftest framework
+   ksft_print_header();
+   ksft_set_plan(1);
+
+   // Check if test is run as root
+   if (geteuid()) {
+   ksft_test_result_skip("This test needs root to run!\n");
return 1;
}
-   if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
-   fprintf(stderr, "error: mount '/', errno %d\n", errno);
+
+   if (is_unshare(CLONE_NEWNS) == 0) {
+   ksft_test_result_pass("unshare(): we have a new mount 
namespace.\n");
+   } else {
+   ksft_test_result_fail("unshare(): failed\n");
return 1;
}
 
+   ksft_set_plan(2);
+
+   if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1) {
+   ksft_test_result_fail("mount(): Root filesystem private mount: 
Fail %d\n", errno);
+   return 1;
+   } else {
+   ksft_test_result_pass("mount(): Root filesystem private mount: 
Success\n");
+   }
+
+   ksft_set_plan(3);
/* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
-   fprintf(stderr, "error: mount tmpfs, errno %d\n", errno);
+   ksft_test_result_fail("mount(): Mounting tmpfs on /tmp: Fail 
%d\n", errno);
return 1;
+   } else {
+   ksft_test_result_pass("mount(): Mounting tmpfs on /tmp: 
Success\n");
}
 
-   fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
+   ksft_set_plan(4);
+   fd = openat(AT_FDCWD, "/tmp", O_WRONLY | O_TMPFILE, 0600);
if (fd == -1) {
-   fprintf(stderr, "error: open 1, errno %d\n", errno);
+   ksft_test_result_fail("openat(): Open first temporary file: 
Fail %d\n", errno);
return 1;
+   } else {
+   ksft_test_result_pass("openat(): Open first temporary file: 
Success\n");
}
+
+   ksft_set_plan(5);
if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
-   fprintf(stderr, "error: linkat, errno %d\n", errno);
+   ksft_test_result_fail("linkat(): Linking the temporary file: 
Fail %d\n", errno);
+   close(fd); // Ensure fd is closed on failure
return 1;
+   } else {
+   ksft_test_result_pass("linkat(): Linking the temporary file: 
Success\n");
}
close(fd);
 
-   fd = openat(AT_FDCWD, "/tmp

[PATCH net-next v2 8/8] selftests: net: fdb_notify: Add a test for FDB notifications

2024-10-24 Thread Petr Machata
Check that only one notification is produced for various FDB edit
operations.

Regarding the ip_link_add() and ip_link_master() helpers. This pattern of
action plus corresponding defer is bound to come up often, and a dedicated
vocabulary to capture it will be handy. tunnel_create() and vlan_create()
from forwarding/lib.sh are somewhat opaque and perhaps too kitchen-sinky,
so I tried to go in the opposite direction with these ones, and wrapped
only the bare minimum to schedule a corresponding cleanup.

Signed-off-by: Petr Machata 
Reviewed-by: Amit Cohen 
Acked-by: Shuah Khan 
---

Notes:
CC: Shuah Khan 
CC: Benjamin Poirier 
CC: Hangbin Liu 
CC: linux-kselftest@vger.kernel.org
CC: Jiri Pirko 
---

 tools/testing/selftests/net/Makefile  |  2 +-
 tools/testing/selftests/net/fdb_notify.sh | 95 +++
 tools/testing/selftests/net/lib.sh| 17 
 3 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/net/fdb_notify.sh

diff --git a/tools/testing/selftests/net/Makefile 
b/tools/testing/selftests/net/Makefile
index 26a4883a65c9..ab0e8f30bfe7 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -92,7 +92,7 @@ TEST_PROGS += test_vxlan_mdb.sh
 TEST_PROGS += test_bridge_neigh_suppress.sh
 TEST_PROGS += test_vxlan_nolocalbypass.sh
 TEST_PROGS += test_bridge_backup_port.sh
-TEST_PROGS += fdb_flush.sh
+TEST_PROGS += fdb_flush.sh fdb_notify.sh
 TEST_PROGS += fq_band_pktlimit.sh
 TEST_PROGS += vlan_hw_filter.sh
 TEST_PROGS += bpf_offload.py
diff --git a/tools/testing/selftests/net/fdb_notify.sh 
b/tools/testing/selftests/net/fdb_notify.sh
new file mode 100755
index ..a98047361988
--- /dev/null
+++ b/tools/testing/selftests/net/fdb_notify.sh
@@ -0,0 +1,95 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+source lib.sh
+
+ALL_TESTS="
+   test_dup_bridge
+   test_dup_vxlan_self
+   test_dup_vxlan_master
+   test_dup_macvlan_self
+   test_dup_macvlan_master
+"
+
+do_test_dup()
+{
+   local op=$1; shift
+   local what=$1; shift
+   local tmpf
+
+   RET=0
+
+   tmpf=$(mktemp)
+   defer rm "$tmpf"
+
+   defer_scope_push
+   bridge monitor fdb &> "$tmpf" &
+   defer kill_process $!
+
+   bridge fdb "$op" 00:11:22:33:44:55 vlan 1 "$@"
+   sleep 0.2
+   defer_scope_pop
+
+   local count=$(grep -c -e 00:11:22:33:44:55 $tmpf)
+   ((count == 1))
+   check_err $? "Got $count notifications, expected 1"
+
+   log_test "$what $op: Duplicate notifications"
+}
+
+test_dup_bridge()
+{
+   ip_link_add br up type bridge vlan_filtering 1
+   do_test_dup add "bridge" dev br self
+   do_test_dup del "bridge" dev br self
+}
+
+test_dup_vxlan_self()
+{
+   ip_link_add br up type bridge vlan_filtering 1
+   ip_link_add vx up type vxlan id 2000 dstport 4789
+   ip_link_master vx br
+
+   do_test_dup add "vxlan" dev vx self dst 192.0.2.1
+   do_test_dup del "vxlan" dev vx self dst 192.0.2.1
+}
+
+test_dup_vxlan_master()
+{
+   ip_link_add br up type bridge vlan_filtering 1
+   ip_link_add vx up type vxlan id 2000 dstport 4789
+   ip_link_master vx br
+
+   do_test_dup add "vxlan master" dev vx master
+   do_test_dup del "vxlan master" dev vx master
+}
+
+test_dup_macvlan_self()
+{
+   ip_link_add dd up type dummy
+   ip_link_add mv up link dd type macvlan mode passthru
+
+   do_test_dup add "macvlan self" dev mv self
+   do_test_dup del "macvlan self" dev mv self
+}
+
+test_dup_macvlan_master()
+{
+   ip_link_add br up type bridge vlan_filtering 1
+   ip_link_add dd up type dummy
+   ip_link_add mv up link dd type macvlan mode passthru
+   ip_link_master mv br
+
+   do_test_dup add "macvlan master" dev mv self
+   do_test_dup del "macvlan master" dev mv self
+}
+
+cleanup()
+{
+   defer_scopes_cleanup
+}
+
+trap cleanup EXIT
+tests_run
+
+exit $EXIT_STATUS
diff --git a/tools/testing/selftests/net/lib.sh 
b/tools/testing/selftests/net/lib.sh
index 24f63e45735d..8994fec1c38f 100644
--- a/tools/testing/selftests/net/lib.sh
+++ b/tools/testing/selftests/net/lib.sh
@@ -442,3 +442,20 @@ kill_process()
# Suppress noise from killing the process.
{ kill $pid && wait $pid; } 2>/dev/null
 }
+
+ip_link_add()
+{
+   local name=$1; shift
+
+   ip link add name "$name" "$@"
+   defer ip link del dev "$name"
+}
+
+ip_link_master()
+{
+   local member=$1; shift
+   local master=$1; shift
+
+   ip link set dev "$member" master "$master"
+   defer ip link set dev "$member" nomaster
+}
-- 
2.45.0




[PATCH net-next v2 4/8] selftests: net: lib: Move logging from forwarding/lib.sh here

2024-10-24 Thread Petr Machata
Many net selftests invent their own logging helpers. These really should be
in a library sourced by these tests. Currently forwarding/lib.sh has a
suite of perfectly fine logging helpers, but sourcing a forwarding/ library
from a higher-level directory smells of layering violation. In this patch,
move the logging helpers to net/lib.sh so that every net test can use them.

Together with the logging helpers, it's also necessary to move
pause_on_fail(), and EXIT_STATUS and RET.

Existing lib.sh users might be using these same names for their functions
or variables. However lib.sh is always sourced near the top of the
file (checked), and whatever new definitions will simply override the ones
provided by lib.sh.

Signed-off-by: Petr Machata 
Reviewed-by: Amit Cohen 
Acked-by: Shuah Khan 
---

Notes:
CC: Shuah Khan 
CC: Benjamin Poirier 
CC: Hangbin Liu 
CC: linux-kselftest@vger.kernel.org
CC: Jiri Pirko 
---

 tools/testing/selftests/net/forwarding/lib.sh | 113 -
 tools/testing/selftests/net/lib.sh| 115 ++
 2 files changed, 115 insertions(+), 113 deletions(-)

diff --git a/tools/testing/selftests/net/forwarding/lib.sh 
b/tools/testing/selftests/net/forwarding/lib.sh
index 89c25f72b10c..41dd14c42c48 100644
--- a/tools/testing/selftests/net/forwarding/lib.sh
+++ b/tools/testing/selftests/net/forwarding/lib.sh
@@ -48,7 +48,6 @@ declare -A NETIFS=(
 : "${WAIT_TIME:=5}"
 
 # Whether to pause on, respectively, after a failure and before cleanup.
-: "${PAUSE_ON_FAIL:=no}"
 : "${PAUSE_ON_CLEANUP:=no}"
 
 # Whether to create virtual interfaces, and what netdevice type they should be.
@@ -446,22 +445,6 @@ done
 ##
 # Helpers
 
-# Exit status to return at the end. Set in case one of the tests fails.
-EXIT_STATUS=0
-# Per-test return value. Clear at the beginning of each test.
-RET=0
-
-ret_set_ksft_status()
-{
-   local ksft_status=$1; shift
-   local msg=$1; shift
-
-   RET=$(ksft_status_merge $RET $ksft_status)
-   if (( $? )); then
-   retmsg=$msg
-   fi
-}
-
 # Whether FAILs should be interpreted as XFAILs. Internal.
 FAIL_TO_XFAIL=
 
@@ -535,102 +518,6 @@ xfail_on_veth()
fi
 }
 
-log_test_result()
-{
-   local test_name=$1; shift
-   local opt_str=$1; shift
-   local result=$1; shift
-   local retmsg=$1; shift
-
-   printf "TEST: %-60s  [%s]\n" "$test_name $opt_str" "$result"
-   if [[ $retmsg ]]; then
-   printf "\t%s\n" "$retmsg"
-   fi
-}
-
-pause_on_fail()
-{
-   if [[ $PAUSE_ON_FAIL == yes ]]; then
-   echo "Hit enter to continue, 'q' to quit"
-   read a
-   [[ $a == q ]] && exit 1
-   fi
-}
-
-handle_test_result_pass()
-{
-   local test_name=$1; shift
-   local opt_str=$1; shift
-
-   log_test_result "$test_name" "$opt_str" " OK "
-}
-
-handle_test_result_fail()
-{
-   local test_name=$1; shift
-   local opt_str=$1; shift
-
-   log_test_result "$test_name" "$opt_str" FAIL "$retmsg"
-   pause_on_fail
-}
-
-handle_test_result_xfail()
-{
-   local test_name=$1; shift
-   local opt_str=$1; shift
-
-   log_test_result "$test_name" "$opt_str" XFAIL "$retmsg"
-   pause_on_fail
-}
-
-handle_test_result_skip()
-{
-   local test_name=$1; shift
-   local opt_str=$1; shift
-
-   log_test_result "$test_name" "$opt_str" SKIP "$retmsg"
-}
-
-log_test()
-{
-   local test_name=$1
-   local opt_str=$2
-
-   if [[ $# -eq 2 ]]; then
-   opt_str="($opt_str)"
-   fi
-
-   if ((RET == ksft_pass)); then
-   handle_test_result_pass "$test_name" "$opt_str"
-   elif ((RET == ksft_xfail)); then
-   handle_test_result_xfail "$test_name" "$opt_str"
-   elif ((RET == ksft_skip)); then
-   handle_test_result_skip "$test_name" "$opt_str"
-   else
-   handle_test_result_fail "$test_name" "$opt_str"
-   fi
-
-   EXIT_STATUS=$(ksft_exit_status_merge $EXIT_STATUS $RET)
-   return $RET
-}
-
-log_test_skip()
-{
-   RET=$ksft_skip retmsg= log_test "$@"
-}
-
-log_test_xfail()
-{
-   RET=$ksft_xfail retmsg= log_test "$@"
-}
-
-log_info()
-{
-   local msg=$1
-
-   echo "INFO: $msg"
-}
-
 not()
 {
"$@"
diff --git a/tools/testing/selftests/net/lib.sh 
b/tools/testing/selftests/net/lib.sh
index c8991cc6bf28..691318b1ec55 100644
--- a/tools/testing/selftests/net/lib.sh
+++ b/tools/testing/selftests/net/lib.sh
@@ -9,6 +9,9 @@ source "$net_dir/lib/sh/defer.sh"
 
 : "${WAIT_TIMEOUT:=20}"
 
+# Whether to pause on after a failure.
+: "${PAUSE_ON_FAIL:=no}"
+
 BUSYWAIT_TIMEOUT=$((WAIT_TIMEOUT * 1000)) # ms
 
 # Kselftest framework constants.
@@ -20,6 +23,11 @@ ksft_skip=4
 # namespace list created by setup_ns
 NS_LIST=()
 
+# Exit status to return at the end. Set in case one of the tests fails.
+EXIT_STATUS=0
+# Per-test return value. Clear at

Re: [PATCH v2 3/5] arm64: signal: Improve POR_EL0 handling to avoid uaccess failures

2024-10-24 Thread Dave Martin
On Thu, Oct 24, 2024 at 04:42:10PM +0100, Catalin Marinas wrote:
> On Thu, Oct 24, 2024 at 04:55:48PM +0200, Kevin Brodsky wrote:
> > On 24/10/2024 12:59, Catalin Marinas wrote:
> > > On Wed, Oct 23, 2024 at 04:05:09PM +0100, Kevin Brodsky wrote:
> > >> +/*
> > >> + * Save the unpriv access state into ua_state and reset it to disable 
> > >> any
> > >> + * restrictions.
> > >> + */
> > >> +static void save_reset_user_access_state(struct user_access_state 
> > >> *ua_state)
> > >> +{
> > >> +if (system_supports_poe()) {
> > >> +/*
> > >> + * Enable all permissions in all 8 keys
> > >> + * (inspired by REPEAT_BYTE())
> > >> + */
> > >> +u64 por_enable_all = (~0u / POE_MASK) * POE_RXW;
> > > I think this should be ~0ul.
> > 
> > It is ~0u on purpose, because unlike in REPEAT_BYTE(), I only wanted the
> > lower 32 bits to be filled with POE_RXW (we only have 8 keys, the top 32
> > bits are RES0). That said, given that D128 has 4-bit pkeys, we could
> > anticipate and fill the top 32 bits too (should make no difference on D64).
> 
> I guess we could leave it as 32-bit for now and remember to update it
> when we enable more keys with D128. Setting the top RES0 bits doesn't
> hurt either since they are already documented in the Arm ARM. Up to you,
> it's fine like above as well.

Can we maybe just have a brute-force loop that constructs the value
using the appropriate #define macros?

The compiler will const-fold it; I'd be prepared to bet that the
generated code would be identical...


> > >> @@ -907,6 +964,7 @@ SYSCALL_DEFINE0(rt_sigreturn)
> > >>  {
> > >>  struct pt_regs *regs = current_pt_regs();
> > >>  struct rt_sigframe __user *frame;
> > >> +struct user_access_state ua_state;
> > >>  
> > >>  /* Always make any pending restarted system calls return -EINTR 
> > >> */
> > >>  current->restart_block.fn = do_no_restart_syscall;
> > >> @@ -923,12 +981,14 @@ SYSCALL_DEFINE0(rt_sigreturn)
> > >>  if (!access_ok(frame, sizeof (*frame)))
> > >>  goto badframe;
> > >>  
> > >> -if (restore_sigframe(regs, frame))
> > >> +if (restore_sigframe(regs, frame, &ua_state))
> > >>  goto badframe;
> > >>  
> > >>  if (restore_altstack(&frame->uc.uc_stack))
> > >>  goto badframe;
> > >>  
> > >> +restore_user_access_state(&ua_state);
> > >> +
> > >>  return regs->regs[0];
> > >>  
> > >>  badframe:
> > > The saving part I'm fine with. For restoring, I was wondering whether we
> > > can get a more privileged POR_EL0 if reading the frame somehow failed.
> > > This is largely theoretical, there are other ways to attack like
> > > writing POR_EL0 directly than unmapping/remapping the signal stack.
> > >
> > > What I'd change here is always restore_user_access_state() to
> > > POR_EL0_INIT. Maybe just initialise ua_state above and add the function
> > > call after the badframe label.
> > 
> > I'm not sure I understand. When we enter this function, POR_EL0 is set
> > to whatever the signal handler set it to (POR_EL0_INIT by default).
> > There are then two cases:
> > 1) Everything succeeds, including reading the saved POR_EL0 from the
> > frame. We then call restore_user_access_state(), setting POR_EL0 to the
> > value we've read, and return to userspace.
> > 2) Any uaccess fails (for instance reading POR_EL0). In that case we
> > leave POR_EL0 unchanged and deliver SIGSEGV.
> > 
> > In case 2 POR_EL0 is most likely already set to POR_EL0_INIT, or
> > whatever the signal handler set it to. It's not clear to me that forcing
> > it to POR_EL0_INIT helps much. Either way it's doubtful that the SIGSEGV
> > handler will be able to recover, since the new signal frame we will
> > create for it may be a mix of interrupted state and signal handler state
> > (depending on exactly where we fail).
> 
> If the SIGSEGV delivery succeeds, returning would restore the POR_EL0
> set up by the previous signal handler, potentially more privileged. Does
> it matter? Can it return all the way to the original context?

That seems a valid concern.

It looks a bit like we don't back out the temporary change to POR_EL0
if writing the sigframe fails, so the temporary "allow all" perms might
get saved out into the SIGSEGV sigframe on the alternate signal
stack, and will then be restored as the user thread's POR_EL0 when the
SIGSEGV returns.

(This is all assuming that the force_sig(SIGSEGV) logic works properly
at all...  I'm still trying to puzzle it out!)

Cheers
---Dave



Re: [PATCH] lib/math: Add int_sqrt test suite

2024-10-24 Thread Andy Shevchenko
On Thu, Oct 24, 2024 at 03:17:28PM -0400, Luis Felipe Hernandez wrote:
> Adds test suite for integer based square root function.
> 
> The test suite is designed to verify the correctness of the int_sqrt
> math library function.

...

>  obj-$(CONFIG_TEST_DIV64) += test_div64.o
>  obj-$(CONFIG_TEST_MULDIV64)  += test_mul_u64_u64_div_u64.o
>  obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o
> +obj-y  += tests/
> \ No newline at end of file

^^^ Please, fix this.

...

> +#include 

+ limits.h

> +#include 

+ module.h
+ string.h

-- 
With Best Regards,
Andy Shevchenko





Re: [PATCH] selftests: Add kselftest framework to the testfile

2024-10-24 Thread Shuah Khan

On 10/24/24 10:55, Shivam Chaudhary wrote:

This patch updates the existing test that checks
for `open(O_TMPFILE)` and `linkat()` behaviors in
mount namespaces to use the kselftest framework.

This includes the following changes:

- Replaced direct error handling with
  `ksft_test_result_*` macros for better reporting
   of test outcomes.


Replace



- Added `ksft_print_header()` and `ksft_set_plan()`
  to structure test outputs more effectively.

- Introduced the helper function `is_unshare()` to
   handle unshare-related checks.



Introduce


- Improved the test flow by adding more detailed pass/fail
   reporting for unshare, mounting, file opening, and linking
   operations.


Improve



- Skips the test if it's not run as root, providing an
   appropriate Warning.


Check submitting patches document for details how to write
short log and change log that hep reviewers.

Also - you haven't cc'ed the right mailing lists. Send v2
cc'ing kselftest mailing list and correcting the short
log and change log problems.




Test logs:

Before change:

- Withou root
  error: unshare, errno 1

- With root
  No, output

After change:

- Without root
  TAP version 13
  1..1
  ok 1 # SKIP This test needs root to ru

- With root
  TAP version 13
  1..1
  ok 1 unshare(): we have a new mount namespace.
  1..2
  ok 2 mount(): Root filesystem private mount: Success
  1..3
  ok 3 mount(): Mounting tmpfs on /tmp: Success
  1..4
  ok 4 openat(): Open first temporary file: Success
  1..5
  ok 5 linkat(): Linking the temporary file: Success
  1..6
  ok 6 openat(): Opening the second temporary file: Success
  # Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0

Signed-off-by: Shivam Chaudhary 
---
  .../selftests/tmpfs/bug-link-o-tmpfile.c  | 72 +++
  1 file changed, 58 insertions(+), 14 deletions(-)

diff --git a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c 
b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
index b5c3ddb90942..26dea19c1614 100644
--- a/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
+++ b/tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c
@@ -23,45 +23,89 @@
  #include 
  #include 
  
-int main(void)

-{
-   int fd;
+#include "../kselftest.h"
  
-	if (unshare(CLONE_NEWNS) == -1) {

+static int is_unshare(int flag)
+{
+   if (unshare(flag) == -1) {
if (errno == ENOSYS || errno == EPERM) {
-   fprintf(stderr, "error: unshare, errno %d\n", errno);
-   return 4;
+   ksft_test_result_fail("error: unshare, errno %d\n", 
errno);
+   return -1; // Return -1 for failure
}
fprintf(stderr, "error: unshare, errno %d\n", errno);
+   return -1;
+   }
+
+   return 0; // Return 0 for success
+}
+
+int main(void)
+{
+   int fd;
+
+   // Setting up kselftest framework
+   ksft_print_header();
+   ksft_set_plan(1);
+
+   // Check if test is run as root
+   if (geteuid()) {
+   ksft_test_result_skip("This test needs root to run!\n");
return 1;
}
-   if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
-   fprintf(stderr, "error: mount '/', errno %d\n", errno);
+
+   if (is_unshare(CLONE_NEWNS) == 0) {
+   ksft_test_result_pass("unshare(): we have a new mount 
namespace.\n");
+   } else {
+   ksft_test_result_fail("unshare(): failed\n");
return 1;
}
  
+	ksft_set_plan(2);

+
+   if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1) {
+   ksft_test_result_fail("mount(): Root filesystem private mount: Fail 
%d\n", errno);
+   return 1;
+   } else {
+   ksft_test_result_pass("mount(): Root filesystem private mount: 
Success\n");
+   }
+
+   ksft_set_plan(3);
/* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
-   fprintf(stderr, "error: mount tmpfs, errno %d\n", errno);
+   ksft_test_result_fail("mount(): Mounting tmpfs on /tmp: Fail 
%d\n", errno);
return 1;
+   } else {
+   ksft_test_result_pass("mount(): Mounting tmpfs on /tmp: 
Success\n");
}
  
-	fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);

+   ksft_set_plan(4);
+   fd = openat(AT_FDCWD, "/tmp", O_WRONLY | O_TMPFILE, 0600);
if (fd == -1) {
-   fprintf(stderr, "error: open 1, errno %d\n", errno);
+   ksft_test_result_fail("openat(): Open first temporary file: Fail 
%d\n", errno);
return 1;
+   } else {
+   ksft_test_result_pass("openat(): Open first temporary file: 
Success\n");
}
+
+   ksft_set_plan(5);
if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
-   fprintf(stderr, "error: linkat, errno %d\n", errno);
+   

Re: [PATCH net-next 4/8] selftests: net: lib: Move logging from forwarding/lib.sh here

2024-10-24 Thread Petr Machata


Shuah Khan  writes:

> On 10/22/24 08:50, Petr Machata wrote:
>> Many net selftests invent their own logging helpers. These really should be
>> in a library sourced by these tests. Currently forwarding/lib.sh has a
>> suite of perfectly fine logging helpers, but sourcing a forwarding/ library
>> from a higher-level directory smells of layering violation. In this patch,
>> move the logging helpers to net/lib.sh so that every net test can use them.
>> Together with the logging helpers, it's also necessary to move
>> pause_on_fail(), and EXIT_STATUS and RET.
>> Existing lib.sh users might be using these same names for their functions
>> or variables. However lib.sh is always sourced near the top of the
>> file (checked), and whatever new definitions will simply override the ones
>> provided by lib.sh.
>
> I like the direction to leverage and make logging functions
> common. It make sense to use prefix to clearly indicate
> that they are common and to namespace conflicts in the future.

Yeah, for newly added logical blocks we should probably do something
like the defer patchset did, where the module is introduced as a
separate small library with clear namespacing and marking of what is
internal and what is public. But there are years of baggage in bash
selftests at this point, so the changes can't quite be very dramatic,
because any changes risk invalidating a test.



Re: [PATCH v2 3/5] arm64: signal: Improve POR_EL0 handling to avoid uaccess failures

2024-10-24 Thread Catalin Marinas
On Wed, Oct 23, 2024 at 04:05:09PM +0100, Kevin Brodsky wrote:
> diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c
> index f5fb48dabebe..d2e4e50977ae 100644
> --- a/arch/arm64/kernel/signal.c
> +++ b/arch/arm64/kernel/signal.c
> @@ -66,9 +66,63 @@ struct rt_sigframe_user_layout {
>   unsigned long end_offset;
>  };
>  
> +/*
> + * Holds any EL0-controlled state that influences unprivileged memory 
> accesses.
> + * This includes both accesses done in userspace and uaccess done in the 
> kernel.
> + *
> + * This state needs to be carefully managed to ensure that it doesn't cause
> + * uaccess to fail when setting up the signal frame, and the signal handler
> + * itself also expects a well-defined state when entered.
> + */
> +struct user_access_state {
> + u64 por_el0;
> +};
> +
>  #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
>  #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
>  
> +/*
> + * Save the unpriv access state into ua_state and reset it to disable any
> + * restrictions.
> + */
> +static void save_reset_user_access_state(struct user_access_state *ua_state)
> +{
> + if (system_supports_poe()) {
> + /*
> +  * Enable all permissions in all 8 keys
> +  * (inspired by REPEAT_BYTE())
> +  */
> + u64 por_enable_all = (~0u / POE_MASK) * POE_RXW;

I think this should be ~0ul.

> @@ -907,6 +964,7 @@ SYSCALL_DEFINE0(rt_sigreturn)
>  {
>   struct pt_regs *regs = current_pt_regs();
>   struct rt_sigframe __user *frame;
> + struct user_access_state ua_state;
>  
>   /* Always make any pending restarted system calls return -EINTR */
>   current->restart_block.fn = do_no_restart_syscall;
> @@ -923,12 +981,14 @@ SYSCALL_DEFINE0(rt_sigreturn)
>   if (!access_ok(frame, sizeof (*frame)))
>   goto badframe;
>  
> - if (restore_sigframe(regs, frame))
> + if (restore_sigframe(regs, frame, &ua_state))
>   goto badframe;
>  
>   if (restore_altstack(&frame->uc.uc_stack))
>   goto badframe;
>  
> + restore_user_access_state(&ua_state);
> +
>   return regs->regs[0];
>  
>  badframe:

The saving part I'm fine with. For restoring, I was wondering whether we
can get a more privileged POR_EL0 if reading the frame somehow failed.
This is largely theoretical, there are other ways to attack like
writing POR_EL0 directly than unmapping/remapping the signal stack.

What I'd change here is always restore_user_access_state() to
POR_EL0_INIT. Maybe just initialise ua_state above and add the function
call after the badframe label.

Either way:

Reviewed-by: Catalin Marinas 



Re: [PATCH V6 9/9] iommufd: map file selftest

2024-10-24 Thread Steven Sistare

cc linux-selftest for this one patch of a series.
No framework changes, only added new cases to existing iommufd selftests.
The full series is here:
  
https://lore.kernel.org/linux-iommu/1729783554-56916-1-git-send-email-steven.sist...@oracle.com/

- Steve

On 10/24/2024 11:25 AM, Steve Sistare wrote:

Add test cases to exercise IOMMU_IOAS_MAP_FILE.

Signed-off-by: Steve Sistare 
Reviewed-by: Nicolin Chen 
---
  tools/testing/selftests/iommu/iommufd.c  | 127 ---
  tools/testing/selftests/iommu/iommufd_fail_nth.c |  39 +++
  tools/testing/selftests/iommu/iommufd_utils.h|  57 ++
  3 files changed, 208 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/iommu/iommufd.c 
b/tools/testing/selftests/iommu/iommufd.c
index 4927b9a..e379adf 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -1,5 +1,6 @@
  // SPDX-License-Identifier: GPL-2.0-only
  /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */
+#include 
  #include 
  #include 
  #include 
@@ -49,6 +50,9 @@ static __attribute__((constructor)) void setup_sizes(void)
vrc = mmap(buffer, BUFFER_SIZE, PROT_READ | PROT_WRITE,
   MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
assert(vrc == buffer);
+
+   mfd_buffer = memfd_mmap(BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
+   &mfd);
  }
  
  FIXTURE(iommufd)

@@ -128,6 +132,7 @@ static __attribute__((constructor)) void setup_sizes(void)
TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP, length);
TEST_LENGTH(iommu_option, IOMMU_OPTION, val64);
TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved);
+   TEST_LENGTH(iommu_ioas_map_file, IOMMU_IOAS_MAP_FILE, iova);
  #undef TEST_LENGTH
  }
  
@@ -1372,6 +1377,7 @@ static void check_access_rw(struct __test_metadata *_metadata, int fd,

  {
unsigned int mock_domains;
bool hugepages;
+   bool file;
  };
  
  FIXTURE_SETUP(iommufd_mock_domain)

@@ -1410,26 +1416,45 @@ static void check_access_rw(struct __test_metadata 
*_metadata, int fd,
  {
.mock_domains = 1,
.hugepages = false,
+   .file = false,
  };
  
  FIXTURE_VARIANT_ADD(iommufd_mock_domain, two_domains)

  {
.mock_domains = 2,
.hugepages = false,
+   .file = false,
  };
  
  FIXTURE_VARIANT_ADD(iommufd_mock_domain, one_domain_hugepage)

  {
.mock_domains = 1,
.hugepages = true,
+   .file = false,
  };
  
  FIXTURE_VARIANT_ADD(iommufd_mock_domain, two_domains_hugepage)

  {
.mock_domains = 2,
.hugepages = true,
+   .file = false,
  };
  
+FIXTURE_VARIANT_ADD(iommufd_mock_domain, one_domain_file)

+{
+   .mock_domains = 1,
+   .hugepages = false,
+   .file = true,
+};
+
+FIXTURE_VARIANT_ADD(iommufd_mock_domain, one_domain_file_hugepage)
+{
+   .mock_domains = 1,
+   .hugepages = true,
+   .file = true,
+};
+
+
  /* Have the kernel check that the user pages made it to the iommu_domain */
  #define check_mock_iova(_ptr, _iova, _length)\
({   \
@@ -1455,7 +1480,10 @@ static void check_access_rw(struct __test_metadata 
*_metadata, int fd,
}\
})
  
-TEST_F(iommufd_mock_domain, basic)

+static void
+test_basic_mmap(struct __test_metadata *_metadata,
+   struct _test_data_iommufd_mock_domain *self,
+   const struct _fixture_variant_iommufd_mock_domain *variant)
  {
size_t buf_size = self->mmap_buf_size;
uint8_t *buf;
@@ -1478,6 +1506,43 @@ static void check_access_rw(struct __test_metadata 
*_metadata, int fd,
test_err_ioctl_ioas_map(EFAULT, buf, buf_size, &iova);
  }
  
+static void

+test_basic_file(struct __test_metadata *_metadata,
+   struct _test_data_iommufd_mock_domain *self,
+   const struct _fixture_variant_iommufd_mock_domain *variant)
+{
+   size_t buf_size = self->mmap_buf_size;
+   uint8_t *buf;
+   __u64 iova;
+   int mfd_tmp;
+   int prot = PROT_READ | PROT_WRITE;
+
+   /* Simple one page map */
+   test_ioctl_ioas_map_file(mfd, 0, PAGE_SIZE, &iova);
+   check_mock_iova(mfd_buffer, iova, PAGE_SIZE);
+
+   buf = memfd_mmap(buf_size, prot, MAP_SHARED, &mfd_tmp);
+   ASSERT_NE(MAP_FAILED, buf);
+
+   /* EFAULT half way through mapping */
+   ASSERT_EQ(0, munmap(buf + buf_size / 2, buf_size / 2));
+   test_err_ioctl_ioas_map_file(EFAULT, 0, buf_size, &iova);
+
+   /* EFAULT on first page */
+   ASSERT_EQ(0, munmap(buf, buf_size / 2));
+   test_err_ioctl_ioas_map_file(EFAULT, 0, buf_size, &iova);
+
+   close(mfd_tmp);
+}
+
+TEST_F(iommufd_mock_domain, basic)
+{
+   if (variant->file)
+   test_basic_file(_metadata, self, variant);
+   

[PATCH] lib/math: Add int_sqrt test suite

2024-10-24 Thread Luis Felipe Hernandez
Adds test suite for integer based square root function.

The test suite is designed to verify the correctness of the int_sqrt
math library function.

Signed-off-by: Luis Felipe Hernandez 
---
 lib/Kconfig.debug   | 17 
 lib/math/Makefile   |  1 +
 lib/math/tests/Makefile |  1 +
 lib/math/tests/int_sqrt_kunit.c | 48 +
 4 files changed, 67 insertions(+)
 create mode 100644 lib/math/tests/int_sqrt_kunit.c

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 7312ae7c3cc5..a7e63dae9281 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2993,6 +2993,23 @@ config TEST_OBJPOOL
 
  If unsure, say N.
 
+config INT_SQRT_KUNIT_TEST
+   tristate "Integer square root test test" if !KUNIT_ALL_TESTS
+   depends on KUNIT
+   default KUNIT_ALL_TESTS
+   help
+ This option enables the KUnit test suite for the int_sqrt function,
+ which performs square root calculation. The test suite is designed to
+ verify that the implementation of int_sqrt correctly computes the
+ square root of a number.
+
+ Enabling this option will include tests that check various scenarios
+ and edge cases to ensure the accuracy and reliability of the square 
root
+ function.
+
+ If unsure, say N
+
+
 endif # RUNTIME_TESTING_MENU
 
 config ARCH_USE_MEMTEST
diff --git a/lib/math/Makefile b/lib/math/Makefile
index 3ef11305f8d2..c7b043ffb249 100644
--- a/lib/math/Makefile
+++ b/lib/math/Makefile
@@ -9,3 +9,4 @@ obj-$(CONFIG_INT_POW_TEST)  += tests/int_pow_kunit.o
 obj-$(CONFIG_TEST_DIV64)   += test_div64.o
 obj-$(CONFIG_TEST_MULDIV64)+= test_mul_u64_u64_div_u64.o
 obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o
+obj-y  += tests/
\ No newline at end of file
diff --git a/lib/math/tests/Makefile b/lib/math/tests/Makefile
index 6a169123320a..e1a79f093b2d 100644
--- a/lib/math/tests/Makefile
+++ b/lib/math/tests/Makefile
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o
+obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
diff --git a/lib/math/tests/int_sqrt_kunit.c b/lib/math/tests/int_sqrt_kunit.c
new file mode 100644
index ..9935f52e808f
--- /dev/null
+++ b/lib/math/tests/int_sqrt_kunit.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include 
+#include 
+
+struct test_case_params {
+   unsigned long x;
+   unsigned long expected_result;
+   const char *name;
+};
+
+static const struct test_case_params params[] = {
+   { 0, 0, "edge-case: square root of 0" },
+   { 4, 2, "perfect square: square root of 4" },
+   { 81, 9, "perfect square: square root of 9" },
+   { 2, 1, "non-perfect square: square root of 2" },
+   { 5, 2, "non-perfect square: square root of 5"},
+   { ULONG_MAX, 4294967295, "large input"},
+};
+
+static void get_desc(const struct test_case_params *tc, char *desc)
+{
+   strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(int_sqrt, params, get_desc);
+
+static void int_sqrt_test(struct kunit *test)
+{
+   const struct test_case_params *tc = (const struct test_case_params 
*)test->param_value;
+
+   KUNIT_EXPECT_EQ(test, tc->expected_result, int_sqrt(tc->x));
+}
+
+static struct kunit_case math_int_sqrt_test_cases[] = {
+   KUNIT_CASE_PARAM(int_sqrt_test, int_sqrt_gen_params),
+   {}
+};
+
+static struct kunit_suite int_sqrt_test_suite = {
+   .name = "math-int_sqrt",
+   .test_cases = math_int_sqrt_test_cases,
+};
+
+kunit_test_suites(&int_sqrt_test_suite);
+
+MODULE_DESCRIPTION("math.int_sqrt KUnit test suite");
+MODULE_LICENSE("GPL");
-- 
2.47.0




[PATCH bpf-next] selftests/bpf: Use make/remove netns helpers in mptcp

2024-10-24 Thread Geliang Tang
From: Geliang Tang 

New netns selftest helpers make_netns() and remove_netns() has been added
in network_helpers.c, let's use them in mptcp selftests too.

Signed-off-by: Geliang Tang 
Reviewed-by: Matthieu Baerts (NGI0) 
---
 tools/testing/selftests/bpf/prog_tests/mptcp.c | 18 --
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c 
b/tools/testing/selftests/bpf/prog_tests/mptcp.c
index d2ca32fa3b21..8276398f7d6a 100644
--- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
+++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
@@ -66,12 +66,18 @@ struct mptcp_storage {
 
 static struct nstoken *create_netns(void)
 {
-   SYS(fail, "ip netns add %s", NS_TEST);
-   SYS(fail, "ip -net %s link set dev lo up", NS_TEST);
+   struct nstoken *nstoken;
 
-   return open_netns(NS_TEST);
-fail:
-   return NULL;
+   if (make_netns(NS_TEST) < 0)
+   return NULL;
+
+   nstoken = open_netns(NS_TEST);
+   if (!nstoken) {
+   log_err("open netns %s failed", NS_TEST);
+   remove_netns(NS_TEST);
+   }
+
+   return nstoken;
 }
 
 static void cleanup_netns(struct nstoken *nstoken)
@@ -79,7 +85,7 @@ static void cleanup_netns(struct nstoken *nstoken)
if (nstoken)
close_netns(nstoken);
 
-   SYS_NOFAIL("ip netns del %s", NS_TEST);
+   remove_netns(NS_TEST);
 }
 
 static int start_mptcp_server(int family, const char *addr_str, __u16 port,
-- 
2.45.2