From mboxrd@z Thu Jan 1 00:00:00 1970 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 94DC1C7EE2A for ; Tue, 24 Jun 2025 08:33:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F5276B00B0; Tue, 24 Jun 2025 04:33:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A5A56B00B3; Tue, 24 Jun 2025 04:33:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2944D6B00B5; Tue, 24 Jun 2025 04:33:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1070D6B00B0 for ; Tue, 24 Jun 2025 04:33:04 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8053114012F for ; Tue, 24 Jun 2025 08:33:03 +0000 (UTC) X-FDA: 83589629046.04.B44CE58 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by imf29.hostedemail.com (Postfix) with ESMTP id 90B7912000E for ; Tue, 24 Jun 2025 08:33:01 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=XCpoLt5d; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of david.laight.linux@gmail.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=david.laight.linux@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750753981; a=rsa-sha256; cv=none; b=fuN4qlDUw6fMg4NG/aAbCMgLJWEWjiiOuR+kMtmbdc+MyLfewRg4GTxS1j6MM7Yi9cKFDW 39Z4ZHgu8DJ24XjKaUhPN50v79DIWcyGHlTIPp/yHCA2D1b8DGK22ENrmOdvDiw5kubRn9 CVDNQy2SBjM1f4Z42D59oh20U4Ulshk= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=XCpoLt5d; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of david.laight.linux@gmail.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=david.laight.linux@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750753981; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HAcUzdSSJoRNC6czCmlN0ZjREWVQ1d/qdtR05/VTcrw=; b=Cv6sXcBNeQ7qSaGWMe3OCStg7tJtz+IwGcQa0uhYVtPcyN1F5lOp0hj+OtvLskr+fopb0H PUIeBNRyzOd90YRuaO1gn2oePXHrpINgVYvreCt9D79YVBTCzm64ETnQnD5aQeJ1EtdykK 7vGq8sZshoKNSq+tH4CJS+QkK41iEz0= Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-3a5257748e1so3298428f8f.2 for ; Tue, 24 Jun 2025 01:33:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750753980; x=1751358780; darn=kvack.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=HAcUzdSSJoRNC6czCmlN0ZjREWVQ1d/qdtR05/VTcrw=; b=XCpoLt5dbP1fNfNefizpUhf8LzJlUZr4M9B1bIzccAkRGKy0XJIKbSqlIkLe4nt14y ee1uGB2N4dv0bHKkkWZ7mn7ZfijUOs/qbPabBG9SZjJgj1zutbpbnkt7XkrTfdf74sHz LhZPn0/yfHKMUOgSXtX1VtEf/q+rVKHwIsDHo1bMZoJMVPoZGFjWlqSoszpnP/ByjPgD IkIhV2fVYOQ5D0J2iCdoa6bXqMohD/MiPO6btyNvf5U6PQa8kQci/+DGSgmjxjQdZs0B bVH4AMoyAL+bigXTMqib/gJfI0fB7CMmq4JLFJNH5D3lwoAGEsPMRU4nxXTNQ6vCw6oO UEtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750753980; x=1751358780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HAcUzdSSJoRNC6czCmlN0ZjREWVQ1d/qdtR05/VTcrw=; b=hFg/4SLvrxNR3YmqMIHsqWk8zXPcJxH1w41cod1baZ+M5u444DkzXMilHmxjk/SvSG Vpoan8FFQVV4c/p0gkWi4KbS1WGMgAYrfcg4uvcNbQ1/OQoj+f81SJ/dnaRnQPTVooxK q8U5XiwvDmXeFvtBqqojcO9DkGmF4vTJiIvCZ9rkkC9kvAA7vOKY3kIzq4RhJW9l2OSD wBB3C9p+vofmiTRT/aewniANPu5vn65EjSPfvYpUJiY7JOBu5c7KgfmkzAE9DdEQr8VQ 2l+z6KR+X55swJ0+QGFo2BsthU/Ry/4GDx6lPMH3PnHixG8MFOljEnqTNpufo7CFJF/o lvSA== X-Forwarded-Encrypted: i=1; AJvYcCVxUfrZg5F5uR5EuGv7B/8W8h4vCu18dC6tiv+ilNlOzAJW5NX9+aUiHcbsC8dgRO+g6ZaknljdTg==@kvack.org X-Gm-Message-State: AOJu0Yz2yY5YI48i53l0/0nYdpFzJbq8rtl2lTZ3uLcxhcEsPifr/Jot Pnv4LE1i8C5KizIaaUYIhc+Y0DsvN0gElL9uj9qgHf7kvlHoznooTdtR X-Gm-Gg: ASbGnctckkAVmIvurXLGU5dSe76RELk7SMya6cOgQpCA/FDjBOIjel/zdmDYJK9syCw CK04me95RLH+2ZVTcWu8lTRMRXIx0mtlPVR3xoIZ2xgVoUFMftbX2u9s6bGMm6XZnDzwgPjOHMp JmNR3/hJghkWAJtwDRn+wHyFnJQA8SoFJ9XVcGkf21nlt9nJ5vNPq6uuCkk82S7aSugMWDFL8XS 3HajDHg6wRcmqAslC+op6Eo0QLoNuhoDwfcCdk9j5/hbWR+i+j4T0ATFVVfJqSD72hj63bav8Tf LHtBAA6z5lEbeCMNv8+3KIlqcjbIO7CmBAz0qw3dPBkjgqNa/nYWJkfODnddL6AKEXY/O/9/RqM 2jLmvJ3rXYhyL899qkIokL5yZ X-Google-Smtp-Source: AGHT+IFa8wKlAMcVrotXu5zgRzJw7hFatpGh+UkLiPFv+ni6YiXds6pDi5irzSKPmq0uy7jsxhYgzw== X-Received: by 2002:a05:6000:645:b0:3a4:fefb:c8d3 with SMTP id ffacd0b85a97d-3a6d130e802mr13287583f8f.40.1750753979983; Tue, 24 Jun 2025 01:32:59 -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-3a6e810caefsm1285937f8f.87.2025.06.24.01.32.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jun 2025 01:32:59 -0700 (PDT) Date: Tue, 24 Jun 2025 09:32:58 +0100 From: David Laight To: Christophe Leroy Cc: Michael Ellerman , Nicholas Piggin , Naveen N Rao , Madhavan Srinivasan , Alexander Viro , Christian Brauner , Jan Kara , Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Darren Hart , Davidlohr Bueso , Andre Almeida , Andrew Morton , Dave Hansen , Linus Torvalds , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH 0/5] powerpc: Implement masked user access Message-ID: <20250624093258.4906c0e0@pumpkin> In-Reply-To: References: <20250622172043.3fb0e54c@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: 549ccenc9czz8dgi9trp9t4xmkuh1qjs X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 90B7912000E X-HE-Tag: 1750753981-100058 X-HE-Meta: U2FsdGVkX1+k0QfWtR1qWeLEjC1G7o6sC4aWzU3TNz4WcB+JKT/qvRSEEy6K3LCgCyfwYmzwbsnAFMdbUWFVxF/RnQBX+b0q4QLJC7qUhXsIiqLHzzECkaoyUxrn1kAsuphlSOkaorDiCdvE2icSdACJjjuGfIv8Cu3B6V0i6cIlBnj3cwvfX51ddsceAu0FKQcDvpW9kMdgRIlgI8hLVEaWXdK6lImisA/o3RWGORr3HeeSpebiMFN21X5enB7ksS+M4OhatLl/8e1jQW7tlf80sPPfmuY6DEswyiHR7N46en/Ob/huH6Y0PXgNG25EBFMss5oRjmVAPLXuYdTyHwSmGNWnsXPH/shrgdL0SBH6IVeMlLfrdo5yaBvKF+PmISCvoIAcuLF8vzqjjVZgKDy02PpRYP8T9NDnRdIqW6W0Hq056rR3022u/Pf3fPOXBfqwreL08UzzFaG7jmNY7jGULUpztfZyerroH+id1vpl1ta9SPJDQ3m5ZDsE/l5vB+SBdjIK3KmVbKHZ5stpzV+CSL5vYoEN0lZ+bjv9Vl7elQsePgHssFISK5nL4n4IrkPpwuIjt/i2PV6ns3AbRRAgT/EnA51JIOurHfX5HUWhZVhV6M4p8j8FJDSJhKtVdpHkNZfS58HKnM3vmpqBGxUd23PamIzrreMVFN2IYFP5pC7/QU9CocGH7wrGAA8JowlykRSMGi2L2E8m1ZiZKfcTM0/C7I3mc8SQHSV7YaaD2isU+MPEoIJeYkPpYzRHDxPDOJQUoFaN1vIav5dKto04YNQYwIPYxB4QoACq3yoTDX4l1Dbw1/I/4Q9kideEgplzVbz64piD9lSC2hH0ub9q12YEc8ykvjh8OUtUkwgkvirZMXRNTPutjvq7/BqabZfHMnG8auiJpYBur+5QiSwJ64kW/KETnGCfDzdz2PfQ52Gjk9qnLYnl6KFyN4PqR613mb8iawSF1b9xUIL Oh54kZkw 32UBi8SYdderWL97H+bxEmboyD4PTYwpxvkx1u8gDHxutGk3DS2nkOIoGHRHTUCUh0hTMk++QzRQw41fY0Ddc98+gFpFG9381dA4kAgi0sQqa++TmIPXhApuj7CK2AOWUA43PX1PByjRNzaQYmQHpr56wiE1V3zcmedttAZZsr2qpIM105gWlL3q5ivIy3V+fQc+ySZYFADQYeNdEqxFlmYgNnFfPpFiGQHoRIMRihYGNlJpglyNPtNdSH+WxM4ACdAFRbdKXnzbrUmol3+vxTzv7GBC+QltP7JiYPnXr+NO7IwyJXY6x0+K/weSFvSnGhc2VGfBXFAzOclqiDCgNjxUCcJFFL7koZUjhaiEDyDkLueo2qh+P1OCE1U6JbHNAWmWBB6BOj2lpISWNVCWlotI+ym47/OVV8gdvoDRdTwRMPj/VjhMr3ONq89cxLTMmeHdU 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: On Tue, 24 Jun 2025 07:27:47 +0200 Christophe Leroy wrote: > Le 22/06/2025 =C3=A0 18:20, David Laight a =C3=A9crit=C2=A0: > > On Sun, 22 Jun 2025 11:52:38 +0200 > > Christophe Leroy wrote: > > =20 > >> Masked user access avoids the address/size verification by access_ok(). > >> Allthough its main purpose is to skip the speculation in the > >> verification of user address and size hence avoid the need of spec > >> mitigation, it also has the advantage to reduce the amount of > >> instructions needed so it also benefits to platforms that don't > >> need speculation mitigation, especially when the size of the copy is > >> not know at build time. =20 > >=20 > > It also removes a conditional branch that is quite likely to be > > statically predicted 'the wrong way'. =20 >=20 > But include/asm-generic/access_ok.h defines access_ok() as: >=20 > #define access_ok(addr, size) likely(__access_ok(addr, size)) >=20 > So GCC uses the 'unlikely' variant of the branch instruction to force=20 > the correct prediction, doesn't it ? Nope... Most architectures don't have likely/unlikely variants of branches. So all gcc can do is decide which path is the fall-through and whether the branch is forwards or backwards. Additionally unless there is code in both the 'if' and 'else' clauses the [un]likely seems to have no effect. So on simple cpu that predict 'backwards branches taken' you can get the desired effect - but it may need an 'asm comment' to force the compiler to generate the required branches (eg forwards branch directly to a backwards unconditional jump). On x86 it is all more complicated. I think the pre-fetch code is likely to assume 'not taken' (but might use stale info on the cache line). The predictor itself never does 'static prediction' - it is always based on the referenced branch prediction data structure. So, unless you are in a loop (eg running a benchmark!) there is pretty much a 50% chance of a branch mispredict. I've been trying to benchmark different versions of the u64 * u64 / u64 function - and I think mispredicted branches make a big difference. I need to sit down and sequence the test cases so that I can see the effect of each branch! >=20 > > =20 > >> Unlike x86_64 which masks the address to 'all bits set' when the > >> user address is invalid, here the address is set to an address in > >> the gap. It avoids relying on the zero page to catch offseted > >> accesses. On book3s/32 it makes sure the opening remains on user > >> segment. The overcost is a single instruction in the masking. =20 > >=20 > > That isn't true (any more). > > Linus changed the check to (approx): > > if (uaddr > TASK_SIZE) > > uaddr =3D TASK_SIZE; > > (Implemented with a conditional move) =20 >=20 > Ah ok, I overlooked that, I didn't know the cmove instruction, seem=20 > similar to the isel instruction on powerpc e500. It got added for the 386 - I learnt 8086 :-) I suspect x86 got there first... Although called 'conditional move' I very much suspect the write is actually unconditional. So the hardware implementation is much the same as 'add carry' except the ALU operation is a simple multiplex. Which means it is unlikely to be speculative. David >=20 > Christophe >=20