From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 7CC473B71C7 for ; Tue, 11 Aug 2026 11:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786447188; cv=none; b=dnxr+qJJX4T/bs4L6BfZ3nBZQeD61vwCslZh8r+uQh6qihGBBdvv1finHEnr5P56Xa7oG9P8jClD93SnA14/YPc/JTgfqaFHJ+q7QTOQMjMOBqdxviTq+DFQNOvt8cKUeCoQj9Au7tvqVehXZbs5peIv7MBavNbZYTNj940CoBs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786447188; c=relaxed/simple; bh=1SHCK5t3ErZgbkd5/bMFTQiVvMK8i5VxwUfjw14JCqE=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kRIEq6deuRX4Eqlo0Dg/OiTsh71MHJTWpmusUuKrLM1hMAhuUPL2P5sSAaEVskD3Tc+xUC6VoRX14S81zhsIvejnRrx3Z3UCgjRRQbmHTQi+vESVQuO9RTZexzdk5lipTLWK53mPBBW/+bQHLxmEJaN7sXKJse7SbovUkXBSmjA= 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=S2rxrJaR; arc=none smtp.client-ip=209.85.221.54 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="S2rxrJaR" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-47f71156e1aso1227541f8f.3 for ; Tue, 11 Aug 2026 04:19:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786447185; x=1787051985; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=WpHpX3dMx1OmUziffO2NidzvFmW3rlX7MNbm6J/WhK0=; b=S2rxrJaRRhZH/uITPBh/pasLsALBKQI5v8IfGdfU2m8kLw5IlGyZkMCp5cqNmfj1Wf V3BFIXB9Hp5zY9kSwFgVLs0OLJ5t7yykxgN0dkg9GaW/vLqNaUl+USBmAkzqJlwT2eR9 nnrBYXAjqs1tZoqiHc5Pgbe/hct8/ZvtLC9K9csz7j80nRx8kjpczpY0mvKm090QdXl1 wmjiKAUVSj/I+tO0d3/WOUUtM8DFfhz8KzIzxBan+XGcUbRqV0A26M6ghh+DSGQx7cUg bipk9Y6XuHKj9FF9Uzq1CEmtDxJ+h8f30elZkq4CINtLC6ULezdYmiwg02Wx0zrcZ5Va 6JiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786447185; x=1787051985; h=content-transfer-encoding:content-type: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 :content-type; bh=WpHpX3dMx1OmUziffO2NidzvFmW3rlX7MNbm6J/WhK0=; b=ePeOLUw0WOId/xMv2Pva03Ja1tAYYZBhmf9vWAM2f5FyigM0rptpjnJuB2XbcA1DQU zslqn6quwhHQFk7WeHVBQCMXaX1Xk4+pdZCYsyTl4iSf9V9NHfZOvDALcL/kSU2sDv6P DxS3uXMz6hqp0GmIPYe5pvpw7VtXieb01hY9wmAGLSLPYvF0kSKxg05I6OOOLS2G4Fmk TanmLCS3K0nvvftbk7FebEujqHEk2z6oeIS9m6mtlAnacsvWdUxtmIZ6F1sTwCd5BqUC tYp5VSNsvf4jxiEy0NCh1DllxweNJKn5bQkuN6QMeRbghVI/9lsCfcDKadjrMzzWfa8T +T5Q== X-Forwarded-Encrypted: i=1; AHgh+RpYZC0R0OJWR0i7iTjF9PKXLanFRxj1UA4pZqHqazK9p2/NG8E2l9O1QlNXAZCHQTS/Bsf3vP9sIrEYwmY=@vger.kernel.org X-Gm-Message-State: AOJu0YxlSJlG7ZwouWkwAurLlKE5oWhc7N9AM327HQvDlgHMNrUI0XWN ulPijUp+Z3QC9N++FMaydHrQuM7Z/iQ9/kOb9u76YDilZ4JG4+xzSFxg X-Gm-Gg: AR+sD104wno0TM/VZxAj8/6lYHnUx+uWNP41cWB+KHW0EdL9LZNt6UNwsazexQWXPiO ryAFQglHy9IucsOuCkveqqoJPqGnVEkZkIMvJvnt58LvhDVVdOFFlCvjctx8CBFGgX3h+ExXsTh 7CF11JfD13CCU4qkarsvJZMSIRWcwhI2DAPnXI81qd1kkBUAy1YE8DhTQZ0DIZYBo2t3KHr9Dk9 rioxhk4XCLtJy8anSo6t5W5IMHb01m0OaUs8PxYpm0tHycS+dY7W98WdibpPdPyh/7nTfz1KfcC S5QpL74VBHg4ZQ3OxZ0FW6WUJX35EcErA76lDuJB7Zh7p1jpoXW6kuw+iMZt+XK0nUJZYC2YyF2 sw/0q/6xOJy0HFOjqa87zv6gNGeBWsU35cHUSl0aVYsQ5+vQflRBDvCKRjQYKdg2XGNNYJFtweg iCmHFJYR5+3uNvJCrjGu0QrSVUjSmV/RBaA3Gpths+fLZsI1l/tOgZJD+KK49oFSPzw+F5qFqtS 1NSSus83R3EcfkrGnfcevbT2Q== X-Received: by 2002:a5d:5e8f:0:b0:47f:6f6a:6a7f with SMTP id ffacd0b85a97d-4814ad890fbmr4370323f8f.8.1786447184497; Tue, 11 Aug 2026 04:19:44 -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-4814a5ac75dsm3318120f8f.7.2026.08.11.04.19.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2026 04:19:44 -0700 (PDT) Date: Tue, 11 Aug 2026 12:19:41 +0100 From: David Laight To: Kevin Brodsky Cc: "David Hildenbrand (Arm)" , "Christophe Leroy (CS GROUP)" , linux-mm@kvack.org, Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Pasha Tatashin , Russell King , Catalin Marinas , Will Deacon , Ryan Roberts , linux-arm-kernel@lists.infradead.org, Huacai Chen , loongarch@lists.linux.dev, "James E.J. Bottomley" , Helge Deller , linux-parisc@vger.kernel.org, Madhavan Srinivasan , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Gerald Schaefer , linux-s390@vger.kernel.org, "David S. Miller" , Andreas Larsson , sparclinux@vger.kernel.org, Richard Weinberger , Anton Ivanov , Johannes Berg , linux-um@lists.infradead.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Ning Sun , x86@kernel.org, tboot-devel@lists.sourceforge.net, Ard Biesheuvel , Ilias Apalodimas , linux-efi@vger.kernel.org, Vishal Moola , Alistair Popple , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: Re: [PATCH 03/22] mm: introduce MMF_KERNEL flag and set it for init_mm Message-ID: <20260811121941.4ca20b88@pumpkin> In-Reply-To: <643a624d-0106-44ed-a7f5-aaf9316f94fd@arm.com> References: <20260714-remove_pgtable_cdtor-v1-0-44be8a7685d7@arm.com> <20260714-remove_pgtable_cdtor-v1-3-44be8a7685d7@arm.com> <01f07ea2-8bdd-4450-8a49-548f8b646cfd@kernel.org> <33fc9d31-2866-4315-9510-0004deb93994@kernel.org> <20260811091016.5ca67143@pumpkin> <643a624d-0106-44ed-a7f5-aaf9316f94fd@arm.com> 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=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, 11 Aug 2026 11:00:09 +0200 Kevin Brodsky wrote: > On 11/08/2026 10:10, David Laight wrote: > > On Wed, 5 Aug 2026 14:07:01 +0200 > > "David Hildenbrand (Arm)" wrote: > > =20 > >> On 8/3/26 15:59, Christophe Leroy (CS GROUP) wrote: =20 > >>> > >>> Le 14/07/2026 =C3=A0 16:03, Kevin Brodsky a =C3=A9crit=C2=A0: =20 > >>>> mm code often needs to know whether some mm represents a kernel or > >>>> user address space. This is currently done by comparing the mm > >>>> pointer with &init_mm; besides not being particularly elegant, this > >>>> ignores the fact that other mm's (e.g. efi_mm) may also represent > >>>> parts of the kernel address space. > >>>> > >>>> Introduce a new mm flag MMF_KERNEL and set it for init_mm. > >>>> Subsequent patches will use this flag to replace comparisons with > >>>> &init_mm. No functional change is introduced for now. =20 > >>> Did you consider performance impact ? This test is usually done in qu= ite > >>> critical memory handling functions. > >>> > >>> init_mm is known at link time. Before your patch 08/22 there is just a > >>> comparison of mm (r3) with a constant (loaded in r10): > >>> > >>> c0014048 : > >>> c0014048:=C2=A0=C2=A0=C2=A0 3d 40 c1 09=C2=A0=C2=A0=C2=A0=C2=A0 lis= =C2=A0=C2=A0=C2=A0=C2=A0 r10,-16119 > >>> c001404c:=C2=A0=C2=A0=C2=A0 39 4a 03 98=C2=A0=C2=A0=C2=A0=C2=A0 addi= =C2=A0=C2=A0=C2=A0 r10,r10,920 > >>> c0014050:=C2=A0=C2=A0=C2=A0 7c 03 50 00=C2=A0=C2=A0=C2=A0=C2=A0 cmpw= =C2=A0=C2=A0=C2=A0 r3,r10 > >>> c0014054:=C2=A0=C2=A0=C2=A0 4d 82 00 20=C2=A0=C2=A0=C2=A0=C2=A0 beqlr > >>> ... > >>> > >>> After patch 08/22 we have, it first checks that mm is not 0, then it = loads the > >>> word located at mm+528 then AND it with 0x1. This load might be costl= y. > >>> > >>> c0014048 : > >>> c0014048:=C2=A0=C2=A0=C2=A0 2c 03 00 00=C2=A0=C2=A0=C2=A0=C2=A0 cmpwi= =C2=A0=C2=A0 r3,0 > >>> c001404c:=C2=A0=C2=A0=C2=A0 7c 85 23 78=C2=A0=C2=A0=C2=A0=C2=A0 mr=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 r5,r4 > >>> c0014050:=C2=A0=C2=A0=C2=A0 41 82 00 10=C2=A0=C2=A0=C2=A0=C2=A0 beq= =C2=A0=C2=A0=C2=A0=C2=A0 c0014060 > >>> c0014054:=C2=A0=C2=A0=C2=A0 81 23 02 10=C2=A0=C2=A0=C2=A0=C2=A0 lwz= =C2=A0=C2=A0=C2=A0=C2=A0 r9,528(r3) > >>> c0014058:=C2=A0=C2=A0=C2=A0 71 29 00 01=C2=A0=C2=A0=C2=A0=C2=A0 andi.= =C2=A0=C2=A0 r9,r9,1 > >>> c001405c:=C2=A0=C2=A0=C2=A0 4c 82 00 20=C2=A0=C2=A0=C2=A0=C2=A0 bnelr= =20 > >> Is that a real problem, though? =20 > > The cost of the read is likely to matter most if the branch gets mispre= dicted. > > I'd also guess the mm isn't usually NULL - so that branch needs to stat= ically > > predicted correctly as well. > > I'd also not assume that the mm is in the cache (unless the surrounding= code > > has already accessed it), the L1 data caches are small. =20 >=20 > You're right, I didn't think about this the right way round - in general > it may not be easy to predict that mm points to init_mm, and an > arbitrary user mm isn't guaranteed to be cached. Repeated calls to > mm_is_kernel(mm) in the same function shouldn't cost much though. Depends. If it is real function call the code will be in the I-cache and is likely to get predicted correctly by the third call. OTOH if inlined you've all the I-cache loads and that static branch prediction might get them all wrong. Oh, and saving the result in a local might cause a register spill. David >=20 > Would some (micro)benchmark be particularly relevant to measure the > overhead, if any? >=20 > - Kevin