From patchwork Sat Mar 30 17:35:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13611621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEBCFCD128A for ; Sat, 30 Mar 2024 17:37:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1500C6B0083; Sat, 30 Mar 2024 13:37:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D9196B0085; Sat, 30 Mar 2024 13:37:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE2356B0087; Sat, 30 Mar 2024 13:37:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CF8176B0083 for ; Sat, 30 Mar 2024 13:37:42 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 57D2440183 for ; Sat, 30 Mar 2024 17:37:42 +0000 (UTC) X-FDA: 81954412764.21.DD55311 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf28.hostedemail.com (Postfix) with ESMTP id B79B4C000A for ; Sat, 30 Mar 2024 17:37:40 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf28.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711820260; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N8PfA3NK2cQ9ZgT9YZsgdlkp1xZyrqW9uXnR78rMD9M=; b=gMDcOMq5JreKJGXLgtSaj2ZXfLmpHQv+yyw+tMMX7ZJrITv+jgWxCxLff3oBLskP6k7zM7 w6Rgvfp9KXTP3jz5c4HpUeVP128gHt+bzSYhqvqvpi1yWrFEVaPU/3GZbnyhNlLPJ7c7A+ ZcDqgxQzBYIhVJD6coNb7JpKwZf6aBk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf28.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711820260; a=rsa-sha256; cv=none; b=yoa5yeSju1dKXpIeqIYQo2bcr64tzYZG1i7dMk4PzhNbwtxFprdyQkySqkXMMppIag2c3G jZGAnglrOHg6XG89usiitQHFwOA8WChSmKOHp/oWY3QvNA298fR5m2C9skfYCmz9ud8qM1 d0CHRP0KgIOrFxs5YoXyR/efvYrvSpA= 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> MIME-Version: 1.0 X-Rspamd-Queue-Id: B79B4C000A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: xdz4befe4bug4gb48i7jommzzds71yk4 X-HE-Tag: 1711820260-52617 X-HE-Meta: U2FsdGVkX1+kB77plUd5Y73BrSW9hyFRn6p5COmARnrs1loA1Ub6iZbQxCRcaTWsaMwAjiVdqayZtOCEiyUVQ2gr7t99hl5F1R8VSuPw+k8ilZxkRi0uZcPA05ibzDpk2p3wOvolB3BH9m8VwlxrcbPlLANzMXUU4h7dbsplmG4EMQ/PMrVBQutjag/e5e9Dcdt0nwryIsL/RPwwIENp6qITKIVYVcRRMgbjs8O9appWaVcemhQc22eBE/woA1va1w1kQle4wQEPmCExKrP2dhug9gFxsdC/X/s5jJskiGjpEb36U2Siv+FqvWSkcbYUJmG0KjD2Vo4kjlDaJ6v+UXshxFTvg1X6G3p5w8LIb0UXybE868ZtkMnfv+JxU+L6ZXRAeJTqhg0idEw5hwDmywChIUWf72GpPFs6DQQQ3X7P8USpDn7RhkdirGQiuspc4fiYSGUUUc7/jYTWICY4iW3zjgN9feWl5Qp91SYLutoFDXXC4kTM0D4EIvpjU9Vy8xPWxJ1mG7M2cY2ZqD0Wv9hw81AAh5w3ItQj4bxcp6pZUfkm3n4cvcKstZUKRFQS9zF1i9aWm5/fgfUeIpCQPi9LoSwf17MpdH8OA7NAVmQgNhsCfw/qW0wYcwKb/dNafF6c1FiGj/nrsLe2RtWpbqpxvLx41JSmqzLswAB2fRK79lrW5q72DOvq78R+YY/mNCdj0yURocBuwhKLQ5hr0fnCmapf6RNrCrNUh0vK+9f3RrHy94fSmcMzs32uxYKRlE8mbM2JMZ0JeTgZLA2TfNdP6nnR7axLuBCgjTFOzsvP80cRGTgNAE0ILg5u4MD9njBRAlqWP3kmO1A3zQQW3cbccX0dVEu/+c8J5PGamrfbIDR2LRSSDE1OGhQCMcVZP8mjdE4S31AEhSkI7yjPRDsxXYTVCVyjij/b1YFAJ0mTrzGDnQS35mgoRZ4HYPK+rM0ypZErlGUX7lqnWMk owKWyRr6 IaSUXHLjkvnaz5gv4sg6iban/XRqoJsVRkizCMJ5tk+KTYkSz6Y3x4OC+qNa+RdDtTDJwf2r+DJ/viFBbgmt72uQEIvSPqvzmEqb9PqEilgRSfjyTeQ6YkxEq3QLc5zRMMkCNGDFY3QuIgPDWZmKP6EcMSMHil3UvWzimIWI3j0xuo3V4DSyTMfaNVex3az3cAYjPidgPRh+0eP0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Allocate a pre-filled random buffer using the seed. Replace iterative copying 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/selftests/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 */ #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) @@ -296,7 +297,7 @@ static void mremap_expand_merge_offset(FILE *maps_fp, unsigned 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 = "mremap mremap move within range"; void *src, *dest; @@ -316,10 +317,7 @@ static void mremap_move_within_range(char pattern_seed) src = (void *)((unsigned long)src & ~(SIZE_MB(2) - 1)); /* Set byte pattern for source block. */ - srand(pattern_seed); - for (i = 0; i < SIZE_MB(2); i++) { - ((char *)src)[i] = (char) rand(); - } + memcpy(src, rand_addr, SIZE_MB(2)); dest = src - SIZE_MB(2); @@ -357,7 +355,7 @@ static void mremap_move_within_range(char pattern_seed) /* 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, } /* Set byte pattern for source block. */ - srand(pattern_seed); - for (t = 0; t < threshold; t++) - memset((char *) src_addr + t, (char) rand(), 1); + memcpy(src_addr, rand_addr, threshold); /* Mask to zero out lower bits of address for alignment */ align_mask = ~(c.dest_alignment - 1); @@ -420,9 +416,7 @@ static long long remap_region(struct config c, unsigned int threshold_mb, } /* Set byte pattern for the dest preamble block. */ - srand(pattern_seed); - for (d = 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); } 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 = "mremap move 1mb from start at 1MB+256KB aligned src"; void *src = NULL, *dest = NULL; @@ -520,10 +515,7 @@ static void mremap_move_1mb_from_start(char pattern_seed) } /* Set byte pattern for source block. */ - srand(pattern_seed); - for (i = 0; i < SIZE_MB(2); i++) { - ((char *)src)[i] = (char) rand(); - } + memcpy(src, rand_addr, SIZE_MB(2)); /* * Unmap the beginning of dest so that the aligned address @@ -568,10 +560,10 @@ static void mremap_move_1mb_from_start(char pattern_seed) 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 = remap_region(test_case.config, threshold_mb, - pattern_seed); + pattern_seed, rand_addr); if (remap_time < 0) { if (test_case.expect_failure) @@ -642,7 +634,15 @@ int main(int argc, char **argv) int failures = 0; int i, run_perf_tests; unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD; + + /* hard-coded test configs */ + size_t max_test_variable_region_size = _2GB; + size_t max_test_constant_region_size = _2MB; + size_t dest_preamble_size = 10 * _4MB; + unsigned int pattern_seed; + char *rand_addr; + size_t rand_size; int num_expand_tests = 2; int num_misc_tests = 2; struct test test_cases[MAX_TEST] = {}; @@ -659,6 +659,31 @@ int main(int argc, char **argv) ksft_print_msg("Test configs:\n\tthreshold_mb=%u\n\tpattern_seed=%u\n\n", threshold_mb, pattern_seed); + /* + * set preallocated random array according to test configs; see the + * functions for the logic of setting the size + */ + if (!threshold_mb) + rand_size = MAX(max_test_variable_region_size, + max_test_constant_region_size); + else + rand_size = MAX(MIN(threshold_mb * _1MB, + max_test_variable_region_size), + max_test_constant_region_size); + rand_size = MAX(dest_preamble_size, rand_size); + + rand_addr = (char *)mmap(NULL, rand_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (rand_addr == 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 = 0; i < rand_size; ++i) + rand_addr[i] = (char) rand(); + page_size = sysconf(_SC_PAGESIZE); /* Expected mremap failures */ @@ -730,13 +755,13 @@ int main(int argc, char **argv) for (i = 0; i < ARRAY_SIZE(test_cases); i++) run_mremap_test_case(test_cases[i], &failures, threshold_mb, - pattern_seed); + pattern_seed, rand_addr); maps_fp = fopen("/proc/self/maps", "r"); if (maps_fp == 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(errno)); } mremap_expand_merge(maps_fp, page_size); @@ -744,17 +769,20 @@ int main(int argc, char **argv) fclose(maps_fp); - 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); if (run_perf_tests) { ksft_print_msg("\n%s\n", "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:"); for (i = 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); } + munmap(rand_addr, rand_size); + if (failures > 0) ksft_exit_fail(); else From patchwork Sat Mar 30 17:35:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13611622 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B290CD1288 for ; Sat, 30 Mar 2024 17:37:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A02C96B0085; Sat, 30 Mar 2024 13:37:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B2116B0087; Sat, 30 Mar 2024 13:37:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82C506B0088; Sat, 30 Mar 2024 13:37:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 68D9A6B0085 for ; Sat, 30 Mar 2024 13:37:46 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2415BA0110 for ; Sat, 30 Mar 2024 17:37:46 +0000 (UTC) X-FDA: 81954412932.27.73A0E67 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf30.hostedemail.com (Postfix) with ESMTP id 8169080006 for ; Sat, 30 Mar 2024 17:37:44 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711820264; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Twry901NhtClORuAc/UO3kKFYO8ct03W9JVVqVI8P1M=; b=tdGHBZ7bdboPgeFmTV7l/mgo69VSEstFs6/Yj6i6pgvYzEqWlJ+t0Kt7CN3D/0AWG/7ADS NHhW6HT793b6bH3fXUBKxKxCqj3hdR2zBVyMaPSUy5RvlRZ8fhvvJNIti8F4bmHsl+Y4tf XisTrxYcvJTbi4Cn8eYN8sCizie5dqM= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711820264; a=rsa-sha256; cv=none; b=B4oE5XMSxtDRt1vOrhbnqclCTBq808PhupdH7GeujhXSuk452V7ClN2bmXsEQUhN8iFPHF AruMdWAvmW+slBXNy1tXY/USk0+I8Nnmz4MzLExgt4msk7adoVH3jVLFRKERYcq7nWEGEx V+mTR+zbSHmkQoydhS1bpUr5DWUw6u4= 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> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8169080006 X-Rspam-User: X-Stat-Signature: 5yb5sxkt51qpyqqmoj3dniijkxfbe8cc X-Rspamd-Server: rspam01 X-HE-Tag: 1711820264-717639 X-HE-Meta: U2FsdGVkX19c7A0vIk00tHeix+w1bVxrmh/AgKVu8TV3UD1arDTjLQP3IP+e5wA5ssAHpzchYjISMpp1wAH9KgJWp6CbVj0qwHe5U4pkqRr3J8VA2hPF2ELK/OcAVJ5UZSK+oTjMw1s4Qd/aYy4YDwt2qqWnT9L8sRcKgHPQPLc3NmFv7hZzQGvyLA8E+uVVa7ZOhD7MkBNL9liPIY+BlO33iuCpnNb84noYx4wKgFJTbLyfnyTicBVmZmMB6F8x9/sUYIpbX2IP7cK5xUqz3IF3DvJu8xk5EYonbnMm3q9Sxoo8VKE32eWcl9PetzPZpCxnCeUXuCB4XHcc0ez0UYWZvmW8nC63j7BwDCwflB48LdDl6zbQM72xS/Gg6TAX7PxTqi9gb77vC7e9XaaR7b46hU+K1u8aL3CO3ezPx4fW6FsF8NccUx/qrC0IwgIS6ypIUYgO80r/5iK8omS7m1V70EzHAxQTj22eb9gK/SYX0YQj2FQp3XyVlX9fcvUIZ7LL3LRd0PqiAAmtTwLQ/apVUV4TJqTqwsP24H/zR8k+c52ieC6zX+Z/+hKL0Y9RcSq7o36EwRLKwI9/Ee+5fNVJsWez99B6NZaqeZ40iA1nMB6Lku3UrsQvqz0tALcdA4UYYP7SvOhWLkLoK1EYam+1IgF/2ZRjlgjMPj8VzjLhRqKYsZGuPYobDjZO46IHOIlAzIrjUB12smTUZV9RBrVPYPMHMdAhkxuCem5LXnyIjXxjjtF0flZTq833mBItkkuzZQH2z6tswFyA3GO3ibdcvtzgmNfwl5uT/hKN22XxcvfWixDNJ2ZDHHcEPbHYSHYRtnje7DPl0p10s4AWmRGQ14Dg6G5n15lkpwNedQsUw4tgLSxVov84Mh1wHc4kWUzDIdnNP24j+pgNZCTGYbkzpobyRuB5VxcTlMTvUNXF2dsWp4ik2kr6izDj64MDcYI68LcaO4owTdLoiNG UCmKMRU/ qrgp80oxkd+2ZMiWjuJKGDM9O+ysZxP/A970bfbECBIvmZyRqBHafpv4vAmhUr0f3WP/dbO5924x715ZKnoTGF3Y+nJWwslnGX9+H/GVWo2mmJd/3hA7Xu+ZycWGqFAw+KL7rgH+KQDi1SW+f8sxKe4o9kmdh9Ly2F+AwC8qdUatspUmMualW+7hG8YaxGBnNowbksl3GVG+Fp0M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Mismatch index is currently being checked by a brute force iteration over the 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 small. 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/selftests/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 = should_fail \ } +/* compute square root using binary search */ +static unsigned long get_sqrt(unsigned long val) +{ + unsigned long low = 1; + + /* assuming rand_size is less than 1TB */ + unsigned long high = (1UL << 20); + + while (low <= high) { + unsigned long mid = low + (high - low) / 2; + unsigned long temp = mid * mid; + + if (temp == val) + return mid; + if (temp < val) + low = mid + 1; + high = 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 pattern_seed, char *rand_addr) /* 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 = {0, 0}, t_end = {0, 0}; long long start_ns, end_ns, align_mask, ret, offset; unsigned long long threshold; + unsigned long num_chunks; if (threshold_mb == VALIDATION_NO_THRESHOLD) threshold = c.region_size; @@ -430,15 +451,42 @@ static long long remap_region(struct config c, unsigned int threshold_mb, goto clean_up_dest_preamble; } - /* Verify byte pattern after remapping */ - srand(pattern_seed); - for (t = 0; t < threshold; t++) { - char c = (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 = get_sqrt(threshold); + for (unsigned long i = 0; i < num_chunks; ++i) { + size_t chunk_size = threshold / num_chunks; + unsigned long shift = i * chunk_size; + + if (!memcmp(dest_addr + shift, rand_addr + shift, chunk_size)) + continue; + + /* brute force iteration only over mismatch segment */ + for (t = shift; t < shift + chunk_size; ++t) { + if (((char *) dest_addr)[t] != 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 = -1; + goto clean_up_dest; + } + } + } - if (((char *) dest_addr)[t] != c) { + /* + * if threshold is not divisible by num_chunks, then check the + * last chunk + */ + for (t = num_chunks * (threshold / num_chunks); t < threshold; ++t) { + if (((char *) dest_addr)[t] != 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 = -1; goto clean_up_dest; @@ -446,22 +494,44 @@ static long long remap_region(struct config c, unsigned int threshold_mb, } /* Verify the dest preamble byte pattern after remapping */ - if (c.dest_preamble_size) { - srand(pattern_seed); - for (d = 0; d < c.dest_preamble_size; d++) { - char c = (char) rand(); - - if (((char *) dest_preamble_addr)[d] != 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 = get_sqrt(c.dest_preamble_size); + + for (unsigned long i = 0; i < num_chunks; ++i) { + size_t chunk_size = c.dest_preamble_size / num_chunks; + unsigned long shift = i * chunk_size; + + if (!memcmp(dest_preamble_addr + shift, rand_addr + shift, + chunk_size)) + continue; + + /* brute force iteration only over mismatched segment */ + for (d = shift; d < shift + chunk_size; ++d) { + if (((char *) dest_preamble_addr)[d] != 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 = -1; goto clean_up_dest; } } } + for (d = num_chunks * (c.dest_preamble_size / num_chunks); d < c.dest_preamble_size; ++d) { + if (((char *) dest_preamble_addr)[d] != 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 = -1; + goto clean_up_dest; + } + } + +no_preamble: start_ns = t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec; end_ns = t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec; ret = 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 = remap_region(test_case.config, threshold_mb, - pattern_seed, rand_addr); + rand_addr); if (remap_time < 0) { if (test_case.expect_failure) From patchwork Sat Mar 30 17:35:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13611623 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B75D0CD1288 for ; Sat, 30 Mar 2024 17:37:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34AD06B0087; Sat, 30 Mar 2024 13:37:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FB726B0088; Sat, 30 Mar 2024 13:37:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C3256B0089; Sat, 30 Mar 2024 13:37:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F1D806B0087 for ; Sat, 30 Mar 2024 13:37:50 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B5A0840189 for ; Sat, 30 Mar 2024 17:37:50 +0000 (UTC) X-FDA: 81954413100.13.D194BDF Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 451142000E for ; Sat, 30 Mar 2024 17:37:49 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711820269; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8c0uUObWm0hvYQbz6NLtxGEuA7R6BgUnr5sLFNNiplE=; b=DUnYRE4a9pW/Gkon9nPgHweMp8U7a2PQNRt9L42f5Sis8+iAnjs7+yeXpjHuX1QWi/0MD4 vNBUIp+uFfi0LTRYCQ6K51Tb00puxYCiHQcPmN27jFe+0yilrOioN4Zx2oFVBVDbuucKfP +2O8AHCPsr7lACqCoT0N6cUm+i6MLic= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711820269; a=rsa-sha256; cv=none; b=MbkWJ0nQvCLjhSsooA4CJdQrshNkFTX52H9NFGXKJfxBIDyGBhIpaajdj1ubseLy3hrWsS BTTbPC49UMLih2YYfuH53iCg9zQLWER5N3Ggqn47CPDHwEyvZlTpLnJS5ARwZpCYfNdfR0 KfgrhmRzgr8eNVyMNAmOqEN7IWPosiU= 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> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 451142000E X-Stat-Signature: yrfoyrk5ru54c47zoxskn9rjsgzbdct5 X-Rspam-User: X-HE-Tag: 1711820269-617688 X-HE-Meta: U2FsdGVkX1/RKMQGGf0OLqGdBKTyOtaKCyqMnBOtEgGBk60t6EmF7taAVtEoJ/WUpf8Xvfp5BXfn2f20QmmfRNqL7uCtkyEUxXROguesywDZjeHFqUeJTTQyId1oHoKHsLR+porBC9Ip0d5JVl8PlJCG//oU9oRpbcHOMkTCgBcCow7b8BlhGoI/K+bL/0W7sy5ET1yg0tn32bPpvNr0E63v8diGZFbOECcWJKAOqkd/KIr525dvrGGmQefHeiUqOzRHwHfvuZOGbAHV6pTJUpjP6lz6Ds/kv9xvT3JBVyLKMi9kEBLo9V3THj2YOYHFn9ZYnzVn+F/JlfHfcikmkt24RISh4lVqZuDoVbGpvj0XWdhOsA9lDT/ZYJY6P7t+N5LEK3bmEFVIlGPKH9/wTOA+pm4oRmF9Rp54GRAfNUyd9EDbi7eZfjfMFRMM0FIrz1UjMxaAdooKbe/oDvyxECLeIkVQ69CmK8jx+xqiMXZzxoF0CSjwyHwa5gznwNuyYLdi9bzxYeEJPop2hGgHaQqeYG2VDCCFvwwmhMyASohB/6y4lbqeqmFHoOJkswlHRD39W5wAx9FQz5Whb0fjJqab+WZvrw6DHcMHF+FdVzfqagfvF1KsyYLJqj4qxPcGKSW6emEhG0+qTixKzQ8rJZQhn3KvcVPYOfy7NjlkfyA2YYdIW+XA/srefbWnCucb0gPD6FDk7TRALighD38CzDlpMBmjASTaf9XL74YfhESqQP7c7BqPwbg6mdx321KSmRCacI+YJeDI1/qWoe+ELk/xH5YP5rv19h8WXlZwVpjsUxFs2uNWAWqXKrtksmLTxA4/bD8HpDLDJyE0HAH14+6G6TleWgIfbGph1EBp0DjuNTKuz1JzSF+SpOm/NTiBWDFEJZhKecA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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/selftests/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 contained * 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 = NULL; size_t len = 0; bool success = false; + unsigned long first_val, second_val; rewind(maps_fp); while (getline(&line, &len, maps_fp) != -1) { - char *first = strtok(line, "- "); - void *first_val = (void *)strtol(first, NULL, 16); - char *second = strtok(NULL, "- "); - void *second_val = (void *) strtol(second, NULL, 16); + if (sscanf(line, "%lx-%lx", &first_val, &second_val) != 2) { + ksft_exit_fail_msg("cannot parse /proc/self/maps\n"); + break; + } if (first_val <= start && second_val >= end) { success = true; @@ -255,7 +257,8 @@ static void mremap_expand_merge(FILE *maps_fp, unsigned long page_size) goto out; } - success = is_range_mapped(maps_fp, start, start + 3 * page_size); + success = is_range_mapped(maps_fp, (unsigned long)start, + (unsigned long)(start + 3 * page_size)); munmap(start, 3 * page_size); out: @@ -294,7 +297,8 @@ static void mremap_expand_merge_offset(FILE *maps_fp, unsigned long page_size) goto out; } - success = is_range_mapped(maps_fp, start, start + 3 * page_size); + success = is_range_mapped(maps_fp, (unsigned long)start, + (unsigned long)(start + 3 * page_size)); munmap(start, 3 * page_size); out: