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 332F9C433FE for ; Fri, 14 Oct 2022 09:41:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 621E96B0072; Fri, 14 Oct 2022 05:41:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D1BC6B0075; Fri, 14 Oct 2022 05:41:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 472496B0078; Fri, 14 Oct 2022 05:41:31 -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 2E2816B0072 for ; Fri, 14 Oct 2022 05:41:31 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id ECDC3160BA7 for ; Fri, 14 Oct 2022 09:41:30 +0000 (UTC) X-FDA: 80019062340.24.6B4F699 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf25.hostedemail.com (Postfix) with ESMTP id 5F88BA002D for ; Fri, 14 Oct 2022 09:41:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=cDuxjcCIkfQXBHxEziGaNIflAudAGusXs+3PecRNCV4=; b=bD5QITAaj83TFVLwAT1dsGPwBB ftlzocySEN2acPKAHoaII5bSggpkHTza1oW3UH03GXzXSxH2i75IhDKVBAJbTDG1V1XZiPMMV5zOw DAXRFx6Xq9mzOeJ17jaL/cnqSNRBWWMvLeKeQVhpAmOnckAbt+ljtVwN0u9/csW807gEgMZNO3fe6 QxyBw8vJ9juiJcXcGSJzbXl+umz4/59a37wesfZziNb/HzqBAVKYVVs/+LLFpXLSJws9i84xJkYps 2m3/ADjpGOk4GFVzdYMyHPwa0smEXnLjJHN0VV7dYveJY4oFIRfSmIcCJd2TdatQlNRhdmGmrjhU1 6e3OucOw==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ojHBp-007W5e-7x; Fri, 14 Oct 2022 09:41:13 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 8C02630015F; Fri, 14 Oct 2022 11:41:06 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6FA042BDDD449; Fri, 14 Oct 2022 11:41:06 +0200 (CEST) Date: Fri, 14 Oct 2022 11:41:06 +0200 From: Peter Zijlstra To: Rick Edgecombe Cc: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, Yu-cheng Yu Subject: Re: [PATCH v2 10/39] x86/mm: Introduce _PAGE_COW Message-ID: References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-11-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220929222936.14584-11-rick.p.edgecombe@intel.com> ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=bD5QITAa; spf=none (imf25.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=peterz@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665740490; a=rsa-sha256; cv=none; b=a2dletzW5kx2Da4L5A3XryBFoXUGLQSn9DlQSS5xiR4riKghctw9CPH2Z6zJ13MqM6EoGB 7/TWHff10YlVEpB12b8Qz6B+Vn915ebVEVGJX7HwcZnqek79wr0RBYejzNSnn8o9chDyZY dd9cFK5BMKDmZ6kFWSLwRHcnFfe5zI4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665740490; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cDuxjcCIkfQXBHxEziGaNIflAudAGusXs+3PecRNCV4=; b=p4luMZlSp/cF1wiHQEGPdRJDurdtW0OUqq3WA5i+OXpK9FYs24VX2+zbKtLDGtVGbX1e7a z2wJDoVVttRrrc1UxLi3BHdmFjGuBHDKG0x4M09ibCvvT3xtX1ik2q2KzX3g3iNbtphL22 DVt0jxeOWjbUcAoubC7LW9kgRiGXr/8= X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=bD5QITAa; spf=none (imf25.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=peterz@infradead.org; dmarc=none X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 5F88BA002D X-Stat-Signature: engrathyntj7je4pbtmqknztr8see183 X-HE-Tag: 1665740489-889092 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: On Thu, Sep 29, 2022 at 03:29:07PM -0700, Rick Edgecombe wrote: > From: Yu-cheng Yu > > There is essentially no room left in the x86 hardware PTEs on some OSes > (not Linux). That left the hardware architects looking for a way to > represent a new memory type (shadow stack) within the existing bits. > They chose to repurpose a lightly-used state: Write=0,Dirty=1. > > The reason it's lightly used is that Dirty=1 is normally set _before_ a > write. A write with a Write=0 PTE would typically only generate a fault, > not set Dirty=1. Hardware can (rarely) both set Write=1 *and* generate the s/Write/Dirty/ > fault, resulting in a Dirty=0,Write=1 PTE. Hardware which supports shadow s/Dirty=0,Write=1/Write=0,Dirty=1/ > stacks will no longer exhibit this oddity. > > The kernel should avoid inadvertently creating shadow stack memory because > it is security sensitive. So given the above, all it needs to do is avoid > manually crating Write=0,Dirty=1 PTEs in software. Whichever way around you choose, please be consistent. > In places where Linux normally creates Write=0,Dirty=1, it can use the > software-defined _PAGE_COW in place of the hardware _PAGE_DIRTY. In other > words, whenever Linux needs to create Write=0,Dirty=1, it instead creates > Write=0,Cow=1 except for shadow stack, which is Write=0,Dirty=1. This > clearly separates shadow stack from other data, and results in the > following: > > (a) (Write=0,Cow=1,Dirty=0) A modified, copy-on-write (COW) page. > Previously when a typical anonymous writable mapping was made COW via > fork(), the kernel would mark it Write=0,Dirty=1. Now it will instead > use the Cow bit. > (b) (Write=0,Cow=1,Dirty=0) A R/O page that has been COW'ed. The user page > is in a R/O VMA, and get_user_pages() needs a writable copy. The page > fault handler creates a copy of the page and sets the new copy's PTE > as Write=0 and Cow=1. > (c) (Write=0,Cow=0,Dirty=1) A shadow stack PTE. > (d) (Write=0,Cow=1,Dirty=0) A shared shadow stack PTE. When a shadow stack > page is being shared among processes (this happens at fork()), its PTE > is made Dirty=0, so the next shadow stack access causes a fault, and > the page is duplicated and Dirty=1 is set again. This is the COW > equivalent for shadow stack pages, even though it's copy-on-access > rather than copy-on-write. > (e) (Write=0,Cow=0,Dirty=1) A Cow PTE created when a processor without > shadow stack support set Dirty=1. Please restureture this (and the comment) something like: (Write=0,Dirty=0,Cow=1): - copy_present_pte(): A modified copy-on-write page. - ... (Write=0,Dirty=1,Cow=0): - FEATURE_CET: Shadow Stack entry - !FEATURE_CET: see the above Cow=1 cases