Message ID | 20240123163623.1342917-1-dave.hansen@linux.intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-35654-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp547527dyi; Tue, 23 Jan 2024 11:05:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IEomPAx2DGV10o1n8lnhT7ecY4UFiQ8CSo6b0ZaLh4Y2u3HzgGDp123qmskRBRT1HF+1Nzp X-Received: by 2002:ad4:5cae:0:b0:681:76d9:52b5 with SMTP id q14-20020ad45cae000000b0068176d952b5mr1633675qvh.130.1706036726911; Tue, 23 Jan 2024 11:05:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706036726; cv=pass; d=google.com; s=arc-20160816; b=cozccQ4mF9jEd0hEw03oKGYaYoU4IocSthNjDLRD4JoV4p+LLSd6m7DFSNIR6/3Io6 kW4QtDdU+QADvtzjZTPvN3dU0P3tj/aBug/xTYDSLdM0Xwm/lb0zHAy58lsSBg7oW6me p5BFcVWD+teIAh+KbrB7Gx1LqNwWjw+gpDdCPcmQIkQK2u0Eh5auzXQByOCBVK2RA7Yw E1wCSmHAJ9vIOVv2/Vh6QNYmDasWSiP7JCQFD7Za5wMeujLi2X0ryUhFIWbeo29AA4IS 82/snaW7QraUnN483d9yUMtG+ePSwCe0Ct6l+iOQwOIoOOk2IeSOEdaLHbkOgX7dfRs/ vP9A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=V8j9jxSVJsEeo2O2rWOFgwt5b8BlnYrT0KqWO9QodG8=; fh=d9x3Oq05dwJlWCB7iq8x31ypSVkLtMHfbWkAR2jdv34=; b=WCAqkzqhWk4fxWr4wF1X6mudKxwhQ6rpjiDJXlpvrLM3azoAFLqXR69+wrg8iyPmpC +Fqwv3ukK88EuNtnufQmoF1reLXHojtekoerejHmYnIiBJqvJpI+MfOyluTzT0cnWsSc xwonTXXSWgx/Oi0EGGwAkb6Jy8V9OK7tveaFNA3Pz3+X5Scm2h4q2P8r9YLM6Yn7faug SLr1uFGD26UJdXhNp2Ml/I52Q/DdWnheS9hsZpTaBEw2Y3IyUeEdC71UFUHHpdmI7uT7 4cK8UNKHdJm1+QfIEN8DiJ77TFNs8UBxN8Bx1n5GdGyI5G3r2HBwTAqlOJLWrPnL++LZ zH1A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OVKP4mxg; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-35654-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35654-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p2-20020a0ccb82000000b0067f44854155si8474629qvk.92.2024.01.23.11.05.26 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 11:05:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35654-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OVKP4mxg; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-35654-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35654-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 7168F1C2309A for <ouuuleilei@gmail.com>; Tue, 23 Jan 2024 16:37:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A6D287F7E7; Tue, 23 Jan 2024 16:36:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="OVKP4mxg" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 480F07F7D4 for <linux-kernel@vger.kernel.org>; Tue, 23 Jan 2024 16:36:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=134.134.136.20 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706027788; cv=none; b=Xw+z0diSRsnMqo3am/w9Y6tuw1cp5PhW8gf/bdbMUMuYrSL0PwgmRaca3RxlODhnA2PWmFEBv5Da8XdO1qbSKGCnE/YFEVR7XCKNXRZTOtMC2sds8JG25aw9/DSOTYDXSxcJFJUJhL/WGgxepyi3UCjO6urjGA0rjrlut/F28qY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706027788; c=relaxed/simple; bh=I5Pr9VrhFDT1EYyfq7w0S3sXfpiBp5IxVXRO7Ss02Ss=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FtXFyaqlwTx1L+vRTI58whGm2vCbi4Kc4I0dZeHJB/pF22voHZECkNiVsEgnJ1yctPsx5jl0Z2XuPhq68J09XQGcgfJ05jRno3AJgMDGhSaTeekkmeG8lx5X8+Rm/eLX9gMHE7AP3GU+20Hk42SH8BEeQUno5GE6BO1jV59s9VU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=OVKP4mxg; arc=none smtp.client-ip=134.134.136.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706027786; x=1737563786; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=I5Pr9VrhFDT1EYyfq7w0S3sXfpiBp5IxVXRO7Ss02Ss=; b=OVKP4mxgVjAx97Kybw/C2Gy01Y3W3bwa5accLDQsmUigJU6zait4Exk8 hBIUyzoX7DVaw9oY3asDEkMA85uwc38LqXEISEQZgrT2GD9l4DjhP87Ig SXkD6Jh16gzsXCTO7bjnxwOWIzdBHu11AANQoUesVw0N4l+SUBE+UFI71 at8nC6YVL71L/nWWFbjCMXwYDZeAS6tPL4rbJRGorJ3daxTf6uhhvyYsV kn3FDMpHOXBhdkytL47aVDfRAx9GKQ4JJExat+mgtAKi4YN9/HSGkvR6K f/C7sHchJDwP5fxNXkevnSxqEOHqluwKG346DFIfwVWLoDKne3LunLOYX w==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="391996001" X-IronPort-AV: E=Sophos;i="6.05,214,1701158400"; d="scan'208";a="391996001" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 08:36:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="959186714" X-IronPort-AV: E=Sophos;i="6.05,214,1701158400"; d="scan'208";a="959186714" Received: from viggo.jf.intel.com (HELO ray2.amr.corp.intel.com) ([10.54.77.144]) by orsmga005.jf.intel.com with ESMTP; 23 Jan 2024 08:36:25 -0800 From: Dave Hansen <dave.hansen@linux.intel.com> To: linux-kernel@vger.kernel.org Cc: Dave Hansen <dave.hansen@linux.intel.com>, David Binderman <dcb314@hotmail.com>, Andy Lutomirski <luto@kernel.org>, Peter Zijlstra <peterz@infradead.org>, Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>, x86@kernel.org Subject: [PATCH] x86/mm: Simplify redundant overlap calculation Date: Tue, 23 Jan 2024 08:36:23 -0800 Message-Id: <20240123163623.1342917-1-dave.hansen@linux.intel.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788901354121618628 X-GMAIL-MSGID: 1788909166886348079 |
Series |
x86/mm: Simplify redundant overlap calculation
|
|
Commit Message
Dave Hansen
Jan. 23, 2024, 4:36 p.m. UTC
There have been a couple of reports that the two sides of the overlaps() calculation are redundant. I spent way too much time looking at this, but I became convinced that they are redundant when a little test program of mine produced identical disassembly for both versions of the check. Remove the second condition. It is exactly the same as the first. Fixes: 91ee8f5c1f50 ("x86/mm/cpa: Allow range check for static protections") Reported-by: David Binderman <dcb314@hotmail.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: x86@kernel.org --- arch/x86/mm/pat/set_memory.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Comments
On 1/23/2024 9:00 AM, Dave Hansen wrote: > On 1/23/24 08:54, David Binderman wrote: >>> Remove the second condition. It is exactly the same as the first. >> I don't think the first condition is sufficient. I suspect something like >> >> return (r2_start <= r1_start && r1_start <= r2_end) || >> (r2_start <= r1_end && r1_end <= r2_end); >> This check seems accurate however Dave's single line check below also looks accurate to me. See the analysis below. >> Given the range [r2_start .. r2_end], then if r1_start or r1_end >> are in that range, you have overlap. >> >> Unless you know different. > > First of all, I've gotten these bounds checks wrong in code more times > than I can count. I have zero trust that I'll get them right. :) > > But the compiler seems to know different at least: > > int overlaps1(unsigned long r1_start, unsigned long r1_end, > unsigned long r2_start, unsigned long r2_end) > { > return (r1_start <= r2_end && r1_end >= r2_start) || > (r2_start <= r1_end && r2_end >= r1_start); > } Dave, I think if you change the order of the && in the 2nd check it makes it easier to visually realize that both of these checks are indeed the same: (r1_start <= r2_end ) && (r1_end >= r2_start) || (r2_end >= r1_start) && (r2_start <= r1_end ) The first operation in () on both lines is exactly the same. Same is true for the second operation after the &&. > > int overlaps2(unsigned long r1_start, unsigned long r1_end, > unsigned long r2_start, unsigned long r2_end) > { > return (r1_start <= r2_end && r1_end >= r2_start); > } > I completely agree that overlap1() and overlap2() are expected to generate the same output for any input. However, the next question is whether overlap2() is enough to detect there is indeed an overlap between the ranges. I find that would be true based on the assumption that the end is always greater than or equal to the start in both ranges. I have now spent way too much time on this. But if you rearrange the check in overlaps2() as below then I find it easier to put it in words: (r1_start <= r2_end && r2_start <= r1_end) "Both of the ranges have to start before either of ranges end for there to be an overlap". Sohil
On 1/23/2024 8:36 AM, Dave Hansen wrote: > There have been a couple of reports that the two sides of the > overlaps() calculation are redundant. I spent way too much time > looking at this, but I became convinced that they are redundant > when a little test program of mine produced identical disassembly > for both versions of the check. > > Remove the second condition. It is exactly the same as the first. > > Fixes: 91ee8f5c1f50 ("x86/mm/cpa: Allow range check for static protections") > Reported-by: David Binderman <dcb314@hotmail.com> > Cc: Andy Lutomirski <luto@kernel.org> > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Thomas Gleixner <tglx@linutronix.de> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Borislav Petkov <bp@alien8.de> > Cc: x86@kernel.org > --- > arch/x86/mm/pat/set_memory.c | 3 +-- > 1 file changed, 1 insertion(+), 2 deletions(-) > Reviewed-by: Sohil Mehta <sohil.mehta@intel.com> > diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c > index e9b448d1b1b70..fdc00516c0b54 100644 > --- a/arch/x86/mm/pat/set_memory.c > +++ b/arch/x86/mm/pat/set_memory.c > @@ -435,8 +435,7 @@ static void cpa_flush(struct cpa_data *data, int cache) > static bool overlaps(unsigned long r1_start, unsigned long r1_end, > unsigned long r2_start, unsigned long r2_end) > { > - return (r1_start <= r2_end && r1_end >= r2_start) || > - (r2_start <= r1_end && r2_end >= r1_start); > + return (r1_start <= r2_end && r1_end >= r2_start); > } > > #ifdef CONFIG_PCI_BIOS
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index e9b448d1b1b70..fdc00516c0b54 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -435,8 +435,7 @@ static void cpa_flush(struct cpa_data *data, int cache) static bool overlaps(unsigned long r1_start, unsigned long r1_end, unsigned long r2_start, unsigned long r2_end) { - return (r1_start <= r2_end && r1_end >= r2_start) || - (r2_start <= r1_end && r2_end >= r1_start); + return (r1_start <= r2_end && r1_end >= r2_start); } #ifdef CONFIG_PCI_BIOS