From nobody Mon Feb 9 05:53:10 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 70BFE3D0C6; Sat, 30 Mar 2024 17:37:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711820268; cv=none; b=ikL6KX/bCwvc9rOobvIGDVLGZ/TgdfaTlls9Z2lPPYXOyiOew0TNok9Us9ihzLbfYHp2Nf0Q+RMC+lQIe0aKVY/IqDVNyqb521edGKy8zdxqrboJ3QJP4NJTB4SDYI9SLej4pg/6Hm8zJ2hNDDx5z/qTNIqIyr158VdfkocUFOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711820268; c=relaxed/simple; bh=86LynxX/Wa5mVbZP8ibHc/96NXH3pPQppzNPOikiI/U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FWbtpzHBETvvBhxhBWQnrE2gEheYkvLCum9F6oLtZIYolr4S4nngi//ZANlMNY6zP9nmTVPWksUCtDj8EnNPrT0B3kfTV/ePrrXgP3shgrVk8Dz6G+q0bCPUjRxXHnsuFlhRNmhCITjX+VEYeIjqVSXl4WdX2fcgo79I4pG2OiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 824DE11FB; Sat, 30 Mar 2024 10:38:12 -0700 (PDT) Received: from e116581.. (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2C7803F7B4; Sat, 30 Mar 2024 10:37:35 -0700 (PDT) From: Dev Jain To: akpm@linux-foundation.org Cc: shuah@kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, kaleshsingh@google.com, jhubbard@nvidia.com, Anshuman.Khandual@arm.com, Dev Jain Subject: [PATCH 1/3] selftests/mm: mremap_test: Optimize using pre-filled random array and memcpy Date: Sat, 30 Mar 2024 23:05:55 +0530 Message-Id: <20240330173557.2697684-2-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240330173557.2697684-1-dev.jain@arm.com> References: <20240330173557.2697684-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allocate a pre-filled random buffer using the seed. Replace iterative copyi= ng of the random sequence to buffers using the highly optimized library function memcpy(). Signed-off-by: Dev Jain --- tools/testing/selftests/mm/mremap_test.c | 78 ++++++++++++++++-------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 2f8b991f78cb..7fed9cc3911e 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -23,6 +23,7 @@ #define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */ =20 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) #define SIZE_MB(m) ((size_t)m * (1024 * 1024)) #define SIZE_KB(k) ((size_t)k * 1024) =20 @@ -296,7 +297,7 @@ static void mremap_expand_merge_offset(FILE *maps_fp, u= nsigned long page_size) * * |DDDDddddSSSSssss| */ -static void mremap_move_within_range(char pattern_seed) +static void mremap_move_within_range(unsigned int pattern_seed, char *rand= _addr) { char *test_name =3D "mremap mremap move within range"; void *src, *dest; @@ -316,10 +317,7 @@ static void mremap_move_within_range(char pattern_seed) src =3D (void *)((unsigned long)src & ~(SIZE_MB(2) - 1)); =20 /* Set byte pattern for source block. */ - srand(pattern_seed); - for (i =3D 0; i < SIZE_MB(2); i++) { - ((char *)src)[i] =3D (char) rand(); - } + memcpy(src, rand_addr, SIZE_MB(2)); =20 dest =3D src - SIZE_MB(2); =20 @@ -357,7 +355,7 @@ static void mremap_move_within_range(char pattern_seed) =20 /* Returns the time taken for the remap on success else returns -1. */ static long long remap_region(struct config c, unsigned int threshold_mb, - char pattern_seed) + unsigned int pattern_seed, char *rand_addr) { void *addr, *src_addr, *dest_addr, *dest_preamble_addr; int d; @@ -378,9 +376,7 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, } =20 /* Set byte pattern for source block. */ - srand(pattern_seed); - for (t =3D 0; t < threshold; t++) - memset((char *) src_addr + t, (char) rand(), 1); + memcpy(src_addr, rand_addr, threshold); =20 /* Mask to zero out lower bits of address for alignment */ align_mask =3D ~(c.dest_alignment - 1); @@ -420,9 +416,7 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, } =20 /* Set byte pattern for the dest preamble block. */ - srand(pattern_seed); - for (d =3D 0; d < c.dest_preamble_size; d++) - memset((char *) dest_preamble_addr + d, (char) rand(), 1); + memcpy(dest_preamble_addr, rand_addr, c.dest_preamble_size); } =20 clock_gettime(CLOCK_MONOTONIC, &t_start); @@ -494,7 +488,8 @@ static long long remap_region(struct config c, unsigned= int threshold_mb, * the beginning of the mapping just because the aligned * down address landed on a mapping that maybe does not exist. */ -static void mremap_move_1mb_from_start(char pattern_seed) +static void mremap_move_1mb_from_start(unsigned int pattern_seed, + char *rand_addr) { char *test_name =3D "mremap move 1mb from start at 1MB+256KB aligned src"; void *src =3D NULL, *dest =3D NULL; @@ -520,10 +515,7 @@ static void mremap_move_1mb_from_start(char pattern_se= ed) } =20 /* Set byte pattern for source block. */ - srand(pattern_seed); - for (i =3D 0; i < SIZE_MB(2); i++) { - ((char *)src)[i] =3D (char) rand(); - } + memcpy(src, rand_addr, SIZE_MB(2)); =20 /* * Unmap the beginning of dest so that the aligned address @@ -568,10 +560,10 @@ static void mremap_move_1mb_from_start(char pattern_s= eed) =20 static void run_mremap_test_case(struct test test_case, int *failures, unsigned int threshold_mb, - unsigned int pattern_seed) + unsigned int pattern_seed, char *rand_addr) { long long remap_time =3D remap_region(test_case.config, threshold_mb, - pattern_seed); + pattern_seed, rand_addr); =20 if (remap_time < 0) { if (test_case.expect_failure) @@ -642,7 +634,15 @@ int main(int argc, char **argv) int failures =3D 0; int i, run_perf_tests; unsigned int threshold_mb =3D VALIDATION_DEFAULT_THRESHOLD; + + /* hard-coded test configs */ + size_t max_test_variable_region_size =3D _2GB; + size_t max_test_constant_region_size =3D _2MB; + size_t dest_preamble_size =3D 10 * _4MB; + unsigned int pattern_seed; + char *rand_addr; + size_t rand_size; int num_expand_tests =3D 2; int num_misc_tests =3D 2; struct test test_cases[MAX_TEST] =3D {}; @@ -659,6 +659,31 @@ int main(int argc, char **argv) ksft_print_msg("Test configs:\n\tthreshold_mb=3D%u\n\tpattern_seed=3D%u\n= \n", threshold_mb, pattern_seed); =20 + /* + * set preallocated random array according to test configs; see the + * functions for the logic of setting the size + */ + if (!threshold_mb) + rand_size =3D MAX(max_test_variable_region_size, + max_test_constant_region_size); + else + rand_size =3D MAX(MIN(threshold_mb * _1MB, + max_test_variable_region_size), + max_test_constant_region_size); + rand_size =3D MAX(dest_preamble_size, rand_size); + + rand_addr =3D (char *)mmap(NULL, rand_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (rand_addr =3D=3D MAP_FAILED) { + perror("mmap"); + ksft_exit_fail_msg("cannot mmap rand_addr\n"); + } + + /* fill stream of random bytes */ + srand(pattern_seed); + for (unsigned long i =3D 0; i < rand_size; ++i) + rand_addr[i] =3D (char) rand(); + page_size =3D sysconf(_SC_PAGESIZE); =20 /* Expected mremap failures */ @@ -730,13 +755,13 @@ int main(int argc, char **argv) =20 for (i =3D 0; i < ARRAY_SIZE(test_cases); i++) run_mremap_test_case(test_cases[i], &failures, threshold_mb, - pattern_seed); + pattern_seed, rand_addr); =20 maps_fp =3D fopen("/proc/self/maps", "r"); =20 if (maps_fp =3D=3D NULL) { - ksft_print_msg("Failed to read /proc/self/maps: %s\n", strerror(errno)); - exit(KSFT_FAIL); + munmap(rand_addr, rand_size); + ksft_exit_fail_msg("Failed to read /proc/self/maps: %s\n", strerror(errn= o)); } =20 mremap_expand_merge(maps_fp, page_size); @@ -744,17 +769,20 @@ int main(int argc, char **argv) =20 fclose(maps_fp); =20 - mremap_move_within_range(pattern_seed); - mremap_move_1mb_from_start(pattern_seed); + mremap_move_within_range(pattern_seed, rand_addr); + mremap_move_1mb_from_start(pattern_seed, rand_addr); =20 if (run_perf_tests) { ksft_print_msg("\n%s\n", "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:"= ); for (i =3D 0; i < ARRAY_SIZE(perf_test_cases); i++) run_mremap_test_case(perf_test_cases[i], &failures, - threshold_mb, pattern_seed); + threshold_mb, pattern_seed, + rand_addr); } =20 + munmap(rand_addr, rand_size); + if (failures > 0) ksft_exit_fail(); else --=20 2.34.1 From nobody Mon Feb 9 05:53:10 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 70BC03D0C4; Sat, 30 Mar 2024 17:37:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711820267; cv=none; b=LnHaY27V7xUeBpGKJnkbHRLP9E74OerbmKLgtVHPFv+7LzK8oPJMkLmZj97adARA7wk/zo+/3afzBiAmdo7QBNd/GybljTunj5lFKqMf0XZ+Ctg2BDYcltjPj2wMDuZZJqNgJf22870Ahve/y9o+N+WFm0zJKOCUtchhDS7qV7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711820267; c=relaxed/simple; bh=Af8eg09+2gdRgXFSi9yiCRsX/YUl+szxGem0bcZ8LeE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eEir7LEsB9tGf61irIAfGx/4FxAaTGGVNK75a637ncMI3AbCcriWUSPv3sdCDu8VmTfA6uqTeyOL/EWcX28Sf20Rj/UBirkypCWrA0WDRYZsp0iu4UiFJYh0kpPAx+lx4OrrI4TLsrgmv7643pftFFHvKcqKN49EGFbLHeRjUJk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B32B212FC; Sat, 30 Mar 2024 10:38:16 -0700 (PDT) Received: from e116581.. (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 6E5853F7B4; Sat, 30 Mar 2024 10:37:40 -0700 (PDT) From: Dev Jain To: akpm@linux-foundation.org Cc: shuah@kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, kaleshsingh@google.com, jhubbard@nvidia.com, Anshuman.Khandual@arm.com, Dev Jain Subject: [PATCH 2/3] selftests/mm: mremap_test: Optimize execution time from minutes to seconds using chunkwise memcmp Date: Sat, 30 Mar 2024 23:05:56 +0530 Message-Id: <20240330173557.2697684-3-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240330173557.2697684-1-dev.jain@arm.com> References: <20240330173557.2697684-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mismatch index is currently being checked by a brute force iteration over t= he buffer. Instead, break the comparison into O(sqrt(n)) number of chunks, with the chunk size of this order only, where n is the size of the buffer. Do a brute-force iteration to print to stdout only when the highly optimized memcmp() library function returns a mismatch in the chunk. The time complexity of this algorithm is O(sqrt(n)) * t, where t is the time taken by memcmp(); for our test conditions, it is safe to assume t to be sm= all. NOTE: This patch depends on the previous one. Signed-off-by: Dev Jain --- tools/testing/selftests/mm/mremap_test.c | 112 ++++++++++++++++++----- 1 file changed, 91 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 7fed9cc3911e..678c79d5b8ef 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -70,6 +70,27 @@ enum { .expect_failure =3D should_fail \ } =20 +/* compute square root using binary search */ +static unsigned long get_sqrt(unsigned long val) +{ + unsigned long low =3D 1; + + /* assuming rand_size is less than 1TB */ + unsigned long high =3D (1UL << 20); + + while (low <=3D high) { + unsigned long mid =3D low + (high - low) / 2; + unsigned long temp =3D mid * mid; + + if (temp =3D=3D val) + return mid; + if (temp < val) + low =3D mid + 1; + high =3D mid - 1; + } + return low; +} + /* * Returns false if the requested remap region overlaps with an * existing mapping (e.g text, stack) else returns true. @@ -355,14 +376,14 @@ static void mremap_move_within_range(unsigned int pat= tern_seed, char *rand_addr) =20 /* Returns the time taken for the remap on success else returns -1. */ static long long remap_region(struct config c, unsigned int threshold_mb, - unsigned int pattern_seed, char *rand_addr) + char *rand_addr) { void *addr, *src_addr, *dest_addr, *dest_preamble_addr; - int d; - unsigned long long t; + unsigned long long t, d; struct timespec t_start =3D {0, 0}, t_end =3D {0, 0}; long long start_ns, end_ns, align_mask, ret, offset; unsigned long long threshold; + unsigned long num_chunks; =20 if (threshold_mb =3D=3D VALIDATION_NO_THRESHOLD) threshold =3D c.region_size; @@ -430,15 +451,42 @@ static long long remap_region(struct config c, unsign= ed int threshold_mb, goto clean_up_dest_preamble; } =20 - /* Verify byte pattern after remapping */ - srand(pattern_seed); - for (t =3D 0; t < threshold; t++) { - char c =3D (char) rand(); + /* + * Verify byte pattern after remapping. Employ an algorithm with a + * square root time complexity in threshold: divide the range into + * chunks, if memcmp() returns non-zero, only then perform an + * iteration in that chunk to find the mismatch index. + */ + num_chunks =3D get_sqrt(threshold); + for (unsigned long i =3D 0; i < num_chunks; ++i) { + size_t chunk_size =3D threshold / num_chunks; + unsigned long shift =3D i * chunk_size; + + if (!memcmp(dest_addr + shift, rand_addr + shift, chunk_size)) + continue; + + /* brute force iteration only over mismatch segment */ + for (t =3D shift; t < shift + chunk_size; ++t) { + if (((char *) dest_addr)[t] !=3D rand_addr[t]) { + ksft_print_msg("Data after remap doesn't match at offset %llu\n", + t); + ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[t] & 0xff, + ((char *) dest_addr)[t] & 0xff); + ret =3D -1; + goto clean_up_dest; + } + } + } =20 - if (((char *) dest_addr)[t] !=3D c) { + /* + * if threshold is not divisible by num_chunks, then check the + * last chunk + */ + for (t =3D num_chunks * (threshold / num_chunks); t < threshold; ++t) { + if (((char *) dest_addr)[t] !=3D rand_addr[t]) { ksft_print_msg("Data after remap doesn't match at offset %llu\n", - t); - ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, + t); + ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[t] & 0xff, ((char *) dest_addr)[t] & 0xff); ret =3D -1; goto clean_up_dest; @@ -446,22 +494,44 @@ static long long remap_region(struct config c, unsign= ed int threshold_mb, } =20 /* Verify the dest preamble byte pattern after remapping */ - if (c.dest_preamble_size) { - srand(pattern_seed); - for (d =3D 0; d < c.dest_preamble_size; d++) { - char c =3D (char) rand(); - - if (((char *) dest_preamble_addr)[d] !=3D c) { - ksft_print_msg("Preamble data after remap doesn't match at offset %d\n= ", - d); - ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, - ((char *) dest_preamble_addr)[d] & 0xff); + if (!c.dest_preamble_size) + goto no_preamble; + + num_chunks =3D get_sqrt(c.dest_preamble_size); + + for (unsigned long i =3D 0; i < num_chunks; ++i) { + size_t chunk_size =3D c.dest_preamble_size / num_chunks; + unsigned long shift =3D i * chunk_size; + + if (!memcmp(dest_preamble_addr + shift, rand_addr + shift, + chunk_size)) + continue; + + /* brute force iteration only over mismatched segment */ + for (d =3D shift; d < shift + chunk_size; ++d) { + if (((char *) dest_preamble_addr)[d] !=3D rand_addr[d]) { + ksft_print_msg("Preamble data after remap doesn't match at offset %llu= \n", + d); + ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[d] & 0xff, + ((char *) dest_preamble_addr)[d] & 0xff); ret =3D -1; goto clean_up_dest; } } } =20 + for (d =3D num_chunks * (c.dest_preamble_size / num_chunks); d < c.dest_p= reamble_size; ++d) { + if (((char *) dest_preamble_addr)[d] !=3D rand_addr[d]) { + ksft_print_msg("Preamble data after remap doesn't match at offset %llu\= n", + d); + ksft_print_msg("Expected: %#x\t Got: %#x\n", rand_addr[d] & 0xff, + ((char *) dest_preamble_addr)[d] & 0xff); + ret =3D -1; + goto clean_up_dest; + } + } + +no_preamble: start_ns =3D t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec; end_ns =3D t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec; ret =3D end_ns - start_ns; @@ -563,7 +633,7 @@ static void run_mremap_test_case(struct test test_case,= int *failures, unsigned int pattern_seed, char *rand_addr) { long long remap_time =3D remap_region(test_case.config, threshold_mb, - pattern_seed, rand_addr); + rand_addr); =20 if (remap_time < 0) { if (test_case.expect_failure) --=20 2.34.1 From nobody Mon Feb 9 05:53:10 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 220353BBDB; Sat, 30 Mar 2024 17:37:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711820270; cv=none; b=jvSKN97r/IYnzf0ttlpExgSNtQbj/P+T9TkjiKJlnG3nfR4I0tLnvtAbddRUg5hlDsH+Fy1l+LHyzjgTvegxARGNxgsWzMAT+C4qOmGNfAXLwa2MslLiUw0DhS2xO1o3orrY3Da5ARwGaN9JYmNqRD5GuIeWjYt9rGBni7Yp/4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711820270; c=relaxed/simple; bh=RzPPWq8e3raIWxE/t6eIibmPKorBGYq3+yl5j/39Ods=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gh2A1xidTG80tsBvdFk21C32OM5vjZAf54NziMWUQGdg6UrjFsE0xSxJ0xFbI9OWGfqDGTQFnOiI+yNdACPzeTtLflAVpS+4hx+hUR89ADHQ7G7jqN26ulyQDd75rCKF9269dD6CCshVo29+pmnIYORYuFc0ZDqW2GKNkbekjhE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 68863106F; Sat, 30 Mar 2024 10:38:21 -0700 (PDT) Received: from e116581.. (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9E2183F7B4; Sat, 30 Mar 2024 10:37:44 -0700 (PDT) From: Dev Jain To: akpm@linux-foundation.org Cc: shuah@kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, kaleshsingh@google.com, jhubbard@nvidia.com, Anshuman.Khandual@arm.com, Dev Jain Subject: [PATCH 3/3] selftests/mm: mremap_test: Use sscanf to parse /proc/self/maps Date: Sat, 30 Mar 2024 23:05:57 +0530 Message-Id: <20240330173557.2697684-4-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240330173557.2697684-1-dev.jain@arm.com> References: <20240330173557.2697684-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enforce consistency across files by avoiding two separate functions to parse /proc/self/maps, replacing them with a simple sscanf(). Signed-off-by: Dev Jain --- tools/testing/selftests/mm/mremap_test.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 678c79d5b8ef..1b03bcfaefdf 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -148,19 +148,21 @@ static unsigned long long get_mmap_min_addr(void) * Using /proc/self/maps, assert that the specified address range is conta= ined * within a single mapping. */ -static bool is_range_mapped(FILE *maps_fp, void *start, void *end) +static bool is_range_mapped(FILE *maps_fp, unsigned long start, + unsigned long end) { char *line =3D NULL; size_t len =3D 0; bool success =3D false; + unsigned long first_val, second_val; =20 rewind(maps_fp); =20 while (getline(&line, &len, maps_fp) !=3D -1) { - char *first =3D strtok(line, "- "); - void *first_val =3D (void *)strtol(first, NULL, 16); - char *second =3D strtok(NULL, "- "); - void *second_val =3D (void *) strtol(second, NULL, 16); + if (sscanf(line, "%lx-%lx", &first_val, &second_val) !=3D 2) { + ksft_exit_fail_msg("cannot parse /proc/self/maps\n"); + break; + } =20 if (first_val <=3D start && second_val >=3D end) { success =3D true; @@ -255,7 +257,8 @@ static void mremap_expand_merge(FILE *maps_fp, unsigned= long page_size) goto out; } =20 - success =3D is_range_mapped(maps_fp, start, start + 3 * page_size); + success =3D is_range_mapped(maps_fp, (unsigned long)start, + (unsigned long)(start + 3 * page_size)); munmap(start, 3 * page_size); =20 out: @@ -294,7 +297,8 @@ static void mremap_expand_merge_offset(FILE *maps_fp, u= nsigned long page_size) goto out; } =20 - success =3D is_range_mapped(maps_fp, start, start + 3 * page_size); + success =3D is_range_mapped(maps_fp, (unsigned long)start, + (unsigned long)(start + 3 * page_size)); munmap(start, 3 * page_size); =20 out: --=20 2.34.1