From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33CC33CA487 for ; Tue, 14 Apr 2026 09:47:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776160037; cv=none; b=QALZLqrHJSlqOWy4PPRnXBYdHz9pves3pvvAX4IVDLO/h4q9DWgZUyzYs1d5O9LJTt2YxfkrpstX0UFDAJx6erQfuk9bt2EpdTlAh0/nBIGQNlEwbahh2JIs4zhjWTQGblawqrSOxmVAnNJvgZPAa9iZ0z3Pw0whZMdb6YEi84o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776160037; c=relaxed/simple; bh=3nLNLQY/FGetYY0hQ2Ckpl5LT5IYhZTMc+wZFqrFgZc=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KXs/b+RWQqJQ3wt8I1bh+/v87hzL5p88YjkW1LCd7RQuD0LNR4re3ezEAgRUp6hyyIGBwscI0qB9AHvuBKA0ym6VNt38kYX/i8LqdkCBOtJ01PBiBK/ZVOSQB6vuGkDoy3KD51jYTaBin22sn4qVHElBxeQa9UMuyIlCUaWDICk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XYLfwE7v; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XYLfwE7v" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-488af9fdaa7so37259485e9.1 for ; Tue, 14 Apr 2026 02:47:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776160034; x=1776764834; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=+4CBGPNUZVh6VtntpItYJ/x6BwDqFr0ckEOccx/tIqk=; b=XYLfwE7vy6Pd+cXHpJvRdGGj3K2f/wQfXNhbtucXwdvYBQNNhJL5wnoS40Pf8WsvZc UDAGZUoi75vtCypuwrBZq51lMDOth6CrPSWxL2ojlF7cia0v1DmuF+ToKnIotyrTuMjP NdE70fNmNckkKxGLI0yyc1X0HeSlCINwn0namkt8YWOXapXkJT2Jto1Jc/P/QKDDwNTq FVQtdlTpWwdqs6A0ZSNJQzZCPEJtMgMcfXEFBVx2qfnQsv46pbdzpUZcU6QNSR94VFSd 46oYxCTXc7RyxZ5brockNG8mtyJ4IWm65svPCq/dgD7JB6Y0r9VTNoDunU1/8oodLCA8 ztoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776160034; x=1776764834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+4CBGPNUZVh6VtntpItYJ/x6BwDqFr0ckEOccx/tIqk=; b=UhIwPTs/ARg/9HC9mISMlyzZxOtZ4z5W3dMAZg1I4qkZKaEWBcmpC6EknLGfz+Fkh6 07up0iSuHgEjXlOeZX+FkiA9EG8lZoQ8O6ohPMfjF4i0hhbSxv7VxCDe2r1h3BNK3j4X cpO4DDZjK54JS/H+kw3JQ9DYiqQ+eO0t/w9woLJq6y2RM8J8Ibv+0+LnTuy1PWLyvrwd rG9u/LFAnC1wEoTi8RGG+ydxO7GUpTpyZ84qPCBRgq06JvYkf70+V+ji5atYle1SUVQO qmpEZ7EU6PbUMfxCCL+WC5w44FOns+Ttvh2fLzHwg5FCmq8SI9mP4IX7dLzZViWUSjnd MGrw== X-Forwarded-Encrypted: i=1; AFNElJ9XqkUt4oktEMZdglmEcq32o5EoYcqjoJzowaX9t0hqRTVu4wB8tkxnYWCJ2DKOL5JcBrbDLcNkkJmmceQ=@vger.kernel.org X-Gm-Message-State: AOJu0Ywxu9KTbDa4x6KNtAepbZI8WC0H3aWUnDpf5FjVXfDJ418xqbod YK6sDluu/isY0yonGoNrJt3jnrb7iMUD+KEKGi+60SHGHYl+raM7r37e X-Gm-Gg: AeBDietp0pZWftEN12ZrauCTXd3U2BZ9pI5gHFJ8zDkFqOQpC1vZf75hRmSbmIof0F0 Po4HEhPMClr3b0/Ql5XahQQ9uItw13LqSYvsrhfGj9UGIIUwBEBZzk0CLJlBPmhtuI/FU/wCS9U R6Zxp+Bbyo1YdF5p5PGiusxWlRhBK2VhRkW8lBMTmOi4Fq4sz97fBEZQatxrZdqh0MkXAMm/EJu EXJEdC53xCw0AV6dzeK6MHfzKnK6cFpUnl061X/HaaKgIEb+rX2SErkLEXyD1Y6z+d0kmImTa3O pmiBBFscwT4JVySSYlQa/exGvNYwWRmWFOdQbfsgbkW5FKYpSgJrc4a4LLcg+jPk77R3cwA1W6I T68+YsPtFOGacp9lKvshpTdhrtGRgndvqjcXqlEaIMqE9gOxBQ1bdzQqqJjrM4FYjTbl0Kjzgu4 ppBR8rt3EF6wRTAeYdSvenINSNQu/pHTAaGVl9DzNwWTO0YV44U3Gf0j+mQSePokvZ X-Received: by 2002:a05:6000:268a:b0:43d:7403:4b60 with SMTP id ffacd0b85a97d-43d74034c72mr14460452f8f.3.1776160034363; Tue, 14 Apr 2026 02:47:14 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d63de2e4csm40900179f8f.2.2026.04.14.02.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 02:47:14 -0700 (PDT) Date: Tue, 14 Apr 2026 10:47:12 +0100 From: David Laight To: "David Hildenbrand (Arm)" Cc: Dev Jain , akpm@linux-foundation.org, shuah@kernel.org, ljs@kernel.org, Liam.Howlett@oracle.com, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com, Sarthak Sharma Subject: Re: [PATCH] selftests/mm: Simplify byte pattern checking in mremap_test Message-ID: <20260414104712.2b634741@pumpkin> In-Reply-To: <134c372e-5c9e-493d-b954-d9954546beaf@kernel.org> References: <20260410143031.148173-1-dev.jain@arm.com> <5297e0da-d8ec-49df-9b32-0d9f907588d6@kernel.org> <8b5544eb-5ec0-4c85-a2da-7a454fa606dc@arm.com> <134c372e-5c9e-493d-b954-d9954546beaf@kernel.org> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 14 Apr 2026 10:01:57 +0200 "David Hildenbrand (Arm)" wrote: > On 4/14/26 07:09, Dev Jain wrote: > > > > > > On 14/04/26 12:57 am, David Hildenbrand (Arm) wrote: > >> On 4/10/26 16:30, Dev Jain wrote: > >>> The original version of mremap_test (7df666253f26: "kselftests: vm: add > >>> mremap tests") validated remapped contents byte-by-byte and printed a > >>> mismatch index in case the bytes streams are not equal. That made > >>> validation expensive in both cases: for "no mismatch" (the common case when > >>> mremap is not buggy), it still walked all bytes in C; for "mismatch", it > >>> broke out of the loop after printing the mismatch index. > >>> > >>> Later, my commit 7033c6cc9620 ("selftests/mm: mremap_test: optimize > >>> execution time from minutes to seconds using chunkwise memcmp") tried to > >>> optimize both cases by using chunk-wise memcmp() and only scanning bytes > >>> within a range which has been determined by memcmp as mismatching. > >>> > >>> But get_sqrt() in that commit is buggy: `high = mid - 1` is applied > >>> unconditionally. This makes the speed of checking the mismatch index > >>> suboptimal. > >> > >> So is that the only problem with 7033c6cc9620: the speed? > > > > Yes. > > > > I'll explain the algorithm in 7033c6cc9620. > > > > The problem statement is: given two buffers of equal length n, find the > > first mismatch index. > > > > Algorithm: Divide the buffers into sqrt(n) chunks. Do a memcmp() over > > each chunk. If all of them succeed, the buffers are equal, giving the > > result in O(sqrt(n)) * t, where t = time taken by memcmp(). > > > > Otherwise, worst case is that we find the mismatch in the last chunk. > > Now brute-force iterate this chunk to find the mismatch. Since chunk > > size is sqrt(n), complexity is again > > sqrt(n) * t + sqrt(n) = O(sqrt(n)) * t. > > > > So if get_sqrt() computes a wrong square root, we lose this time > > complexity. > > Ah, thanks for clarifying. > > > > > Maybe there is an optimal value of x = #number of chunks of the buffer, > > which may not be sqrt(n). > > > > But given the information we have, a CS course on algorithms will > > say this is one of the optimal ways to do it. > > > >> > >>> > >>> The mismatch index does not provide useful debugging value here: if > >>> validation fails, we know mremap behavior is wrong, and the specific byte > >>> offset does not make root-causing easier. > >> > >> Fully agreed. > >> > >>> > >>> So instead of fixing get_sqrt(), bite the bullet, drop mismatch index > >>> scanning and just compare the two byte streams with memcmp(). > >> > >> How does this affect the execution time of the test? > > > > I just checked with ./mremap_test -t 0, the variance is very high on my > > system. > > > > In the common case of the test passing: > > > > before patch, there are multiple sub-length calls to memcmp. > > after patch, there is a single full-length call to memcmp. > > > > So the time should reduce but may not be very distinguishable. > > Okay, so doesn't matter. I agree that we should simplify all that. > > The exact index is irrelevant. Whoever wants to debug the test failure > could modify the test to find that out. It's one of the tests we don't > really expect to fail (often). > > > > >> > >>> > >>> Reported-by: Sarthak Sharma > >>> Signed-off-by: Dev Jain > >> > >> Fixes: 7033c6cc9620 ("selftests/mm: mremap_test: optimize execution time > >> from minutes to seconds using chunkwise memcmp") > >> > >> ? > > > > Not needed. 7033c6cc9620 does not create any incorrectness in the checking > > of mismatch index. > > Yes, agreed. > > > I would suggest to rewrite/simplify/clarify the patch description, not > talking about "buggy" etc, focusing on the simplification. > > " > The original version of mremap_test (7df666253f26: "kselftests: vm: add > mremap tests") validated remapped contents byte-by-byte and printed a > mismatch index in case the bytes streams didn't match. That was rather > inefficient, especially also if the test passed. > > Later, commit 7033c6cc9620 ("selftests/mm: mremap_test: optimize > execution time from minutes to seconds using chunkwise memcmp") used > memcmp() on bigger chunks, to fallback to byte-wise scanning to detect > the problematic index only if it discovered a problem. > > However, the implementation is overly complicated (e.g., get_sqrt() is > currently not optimal) and we don't really have to report the exact > index: whoever debugs the failing test can figure that out. > > Let's simplify by just comparing both byte streams with memcmp() and not > detecting the exact failed index. > " ISTM that if you get a failure it doesn't really matter how long a second scan takes. So a simple byte loop that reports the offset and data of the first error and counts the number of errors is more than enough. David