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)