From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 67E142FF164 for ; Thu, 4 Jun 2026 14:57:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780585044; cv=none; b=rK6d9mHGBWZ52IPs3P7cEdJi1JxOY7rpZWGLBbZNYXphrF4ab8M5ivWmi1y459CBOp2PXiAwSfQYeeB2gwGe6XlnIlDrkszjVaI8acH8hfOybCCHzV2ZhBT2vGH5lNjsx3SavKpDUxHvIqOFaCrIu7Y6F3VlUBHnx8NtCc7Cw6o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780585044; c=relaxed/simple; bh=3iWBU1OaM7a8axtLjTGKJ9LNyGSU1IBS4T2VSUllGqI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=IbgaxMRCJdmEuwvJ0NktwjCxqYgCEsnHBDP6CxI0aha6EJ7ykONUnFCNY3vnMtDO26r5XREFOXEQ2BrQFxkr//jqYYF3xHq1TmIZyM6tIwy8SXSlw220gILy/2M7oi/54TQk7xdO5Z3jcLg8u+UA4aIcIx43EX7Bf/jn2YsnsGM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PVtSEQgz; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PVtSEQgz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36D951F00893; Thu, 4 Jun 2026 14:57:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780585043; bh=Bix+GKowZd/Tu2/8AOiC5/xieOCPGmZRVo7G52Me3BQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=PVtSEQgz7nlEqg/mbJQSHn2Z4W2hlXLOOVzXFtr0xkZuNVyOitrIznvfPJuNh0V2f G5agYFu8foKSxFtN3HaA7DvzuzpyxDocFMdOTb4Fpqi07h4GtdhHF1tOZ/SMxJTLTo pSCmzn7IYBZzgA+h+z1aN6DvfEWxlKShFPv3JGJB95xSiKW9YtDod4le3tqPIGCIrW gBqA3oOQyXm7M8Y2WI+0ErODeycyohEaDB7pvPhwu+sVd1PDraI8s8T9VM9oB+xceD a23f8QWV2baOHxHzHyQ3Imcj9FbL2r+aHocWvydvlSvAZvAx/mxHia2v6VeHqIGJs2 enqpASk3WHs4w== Date: Thu, 4 Jun 2026 17:57:14 +0300 From: Mike Rapoport To: Lance Yang Cc: david@kernel.org, akpm@linux-foundation.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, luto@kernel.org, peterz@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, xueyuan.chen21@gmail.com, ioworker0@gmail.com Subject: Re: [RFC PATCH 1/2] mm/secretmem: try to restore large page mappings in direct map Message-ID: References: <20260604031133.56010-1-lance.yang@linux.dev> 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-Disposition: inline In-Reply-To: <20260604031133.56010-1-lance.yang@linux.dev> On Thu, Jun 04, 2026 at 11:11:33AM +0800, Lance Yang wrote: > On Wed, Jun 03, 2026 at 05:48:56PM +0200, David Hildenbrand (Arm) wrote: > >On 6/3/26 15:09, Lance Yang wrote: > >> > >> > >> On 2026/6/3 20:35, Mike Rapoport wrote: > >>> On Wed, Jun 03, 2026 at 07:41:34PM +0800, Lance Yang wrote: > >>>> > >>>> Good point, I kept it separate on purpose :) > >>>> > >>>> Putting collapse into set_direct_map_default_noflush() would change the > >>>> semantics of that helper a bit, IMHO. > >>> > >>> For x86 default means present + rw + PSE, so yuu can look at it as actually > >>> better enforcing the semantics :) > >> > >> Yep. One x86 detail though, default seems to miss _PAGE_GLOBAL today. Not > >> sure if that is intentional or just historical. See patch #02. > >> > >>>> I would expect arch_try_collapse_direct_map() to also be useful for cases > >>>> where a direct-map permission change could split a large maping first, > >>>> and the user wants to try restoring the large mapping after changing it > >>>> back. One example[1] is making a direct-map range read-only for security, > >>>> which I am also working on :) > >>> > >>> I don't think users should care. The users care for particular permissions > >>> of a range in the direct map. It should be up to the architecture to select > >>> most suitable mapping size. The splits are implicit, I don't see why > >>> collapses can't be implicit as well. > >> > >> And agreed, users should not care about the final mapping size, that is > >> up to the arch. > >> > >> TBH, my concern is making the collapse cost implicit for every > >> set_direct_map_default_noflush() caller. I still lean toward keeping > >> it opt-in, but happy to hear what folks prefer :) > > > >If we could easily do that automatically, that would likely be preferable. > > > >Especially given that we are getting other users of direct-map removal soon that > >would face similar problems (e.g., guest_memfd). > > Yeah. Makes sense to me ;) > > > > >What the performance impact of trying to collapse after every directmap update? > >Imagine we have a full PMD range with directmap-removed PTEs? > > Collapse can turn 512 PTEs into one PMD, and, if the large range is > compatible, 512 PMDs into one PUD. > > Eeah try walks the page tables, takes pgd_lock, and scans entries until > it hits a non-present entry, mismatched flags, or non-contiguous PFN. The > same kind of check is done for PMD entries before a PUD collapse. > > If nothing collapses, it balis out without flush_tlb_all(). If at least > one collapse succeeds, flush_tlb_all() is called once before freeing the > old page tables, and that is probably the expensive part :) > > So failed tries are cheaper that a real collapse, but not free. > > Not sure how often the remove/restore cycles happens, whether automatic > collapse is worth it depends on that. Keeping it explicit lets callers > take that cost only when they know the collapse is really useful ... The callers don't have any clue if the collapse is useful. In secretmem case, it changes permissions of a single 4k page. How should it decide whether to collapse or not? Or any other caller of set_memory_* APIs for that matter? Moreover, secretmem won't know if there are bpf allocations in the same PUD that also hammer direct map permissions? It's either we decide that large mappings in the direct map are worth taking the cost of collapse or we live with the fragmented direct map. And even though it's hard to measure, we'd need some numbers for at least some use cases to get a feeling of what's involved. > Thanks, Lance > -- Sincerely yours, Mike.