]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/mm: ksm-functional-tests: fix partial write handling
authorVineet Agarwal <agarwal.vineet2006@gmail.com>
Mon, 4 May 2026 08:13:13 +0000 (13:43 +0530)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 2 Jun 2026 22:22:15 +0000 (15:22 -0700)
Update write() checks to properly detect and handle partial writes.

Previously, the write() calls used <= 0 to detect failure.  This condition
is never true for partial writes (ret > 0 but ret < len), so partial
writes were silently treated as success.

Fix this by verifying that write() returns the full expected length and
treating any mismatch as failure.

Link: https://lore.kernel.org/20260504081638.683223-1-agarwal.vineet2006@gmail.com
Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Cc: Liam Howlett <liam@infradead.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/ksm_functional_tests.c

index 8d874c4754f38f1a5310938466e491baf3f21f74..31c06c72203fd393c51b85ff1d79496e8b7c00a7 100644 (file)
@@ -498,6 +498,7 @@ static void test_prctl_fork(void)
 static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms)
 {
        int ksm_fd;
+       size_t len;
 
        ksm_fd = open("/sys/kernel/mm/ksm/run", O_RDWR);
        if (ksm_fd < 0)
@@ -506,11 +507,13 @@ static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_ms)
        if (write(ksm_fd, "1", 1) != 1)
                return -errno;
 
-       if (write(pages_to_scan_fd, pages_to_scan, strlen(pages_to_scan)) <= 0)
-               return -errno;
+       len = strlen(pages_to_scan);
+       if (write(pages_to_scan_fd, pages_to_scan, len) != len)
+               return -1;
 
-       if (write(sleep_millisecs_fd, sleep_ms, strlen(sleep_ms)) <= 0)
-               return -errno;
+       len = strlen(sleep_ms);
+       if (write(sleep_millisecs_fd, sleep_ms, len) != len)
+               return -1;
 
        return 0;
 }
@@ -526,11 +529,11 @@ static int stop_ksmd_and_restore_frequency(void)
        if (write(ksm_fd, "2", 1) != 1)
                return -errno;
 
-       if (write(pages_to_scan_fd, "100", 3) <= 0)
-               return -errno;
+       if (write(pages_to_scan_fd, "100", 3) != 3)
+               return -1;
 
-       if (write(sleep_millisecs_fd, "20", 2) <= 0)
-               return -errno;
+       if (write(sleep_millisecs_fd, "20", 2) != 2)
+               return -1;
 
        return 0;
 }