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 X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2D46C433DB for ; Mon, 22 Mar 2021 11:29:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8AE0461991 for ; Mon, 22 Mar 2021 11:29:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8AE0461991 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 689116B00B6; Mon, 22 Mar 2021 07:10:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 660416B00B8; Mon, 22 Mar 2021 07:10:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 502BE6B00B9; Mon, 22 Mar 2021 07:10:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0203.hostedemail.com [216.40.44.203]) by kanga.kvack.org (Postfix) with ESMTP id 3308C6B00B6 for ; Mon, 22 Mar 2021 07:10:56 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 94D3C1804367E for ; Mon, 22 Mar 2021 11:29:46 +0000 (UTC) X-FDA: 77947290372.26.43723A8 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf13.hostedemail.com (Postfix) with ESMTP id 2B087E0011F1 for ; Mon, 22 Mar 2021 11:29:46 +0000 (UTC) Received: by mail-lf1-f44.google.com with SMTP id o10so20674044lfb.9 for ; Mon, 22 Mar 2021 04:29:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=kH80zx8fgBcaAMtcI+DPy23ounK72NEc7TVGapB9YJA=; b=D1EdJvVcRscbq/GBf+ctMYaojj/rUvk/UoG1MbcT1Veo9iHGl3/4IUWRf9UZ50ESk4 WgwalH0dIBKxCHx7Tun2HU6R7nIGTQM301lhUdEfv0RZNr+Qxf+yeqWQhaVjW1L3UxtF uYcEZnfr/oyiYVnwK8BF5chUByqmveLjEoe5Q5bhEaPR4/sp0UKaVnuOdk26zrcNYQQ0 Oq3wktgVevjGROiZlJKopjTIus6ycwDfgni7oSXjDrifaxutyyBPMB5yv3npEZewyHVu 7V87JQXMbvSEsDAq3DorNQJCQtPA0Dne1WAUnBuxUirrHZppYWr0oLauShiO8CE6Nrrz PkWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=kH80zx8fgBcaAMtcI+DPy23ounK72NEc7TVGapB9YJA=; b=KJtf5WgSNOIQ0Px33OBFaCItU0gJ/USPTff9mIksPpRqHzUdyySN6A8kZGuMlbRqvX yS+GQ35EjyytxF/+JqCRS3owlNBab3VpGHXKhxY2z74cXU2hKigaOb8YfiJdIkaVjEnl HeoZUATyfvM/QG5EZqgZCIMT8lUuYIc4DRf8T/FH1STo9QteGPXXjebW/gndgvvoXKS5 imY5to+nV02rJ5B/u51uzcvy3bEsSg5K36rOjkpxlMljQwHWN7ROTAd3dzP4zLsF0pTH CPPRp7PQs4ki6ly3O6dJpAoDTTIS/ac8dAV09AxmRtwLPNun2yvWy7RzedCNoyuSMmqz iOoA== X-Gm-Message-State: AOAM533RUNJla+i7bklY4cD91tRDujnKvQxI6oHJfEMZAzPUGAl1zQNJ +QtoQnnwlgAX8p/yhzuaS5P3PA== X-Google-Smtp-Source: ABdhPJwf/h7V1Zqs4EwvkaaGjE8LX8cCxjQF09dWEQ51VG/y8ij9n6cohALMgfxgCr6ZmBxfpCVtjQ== X-Received: by 2002:a19:4101:: with SMTP id o1mr8797848lfa.16.1616412584897; Mon, 22 Mar 2021 04:29:44 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id p22sm1545817lfh.113.2021.03.22.04.29.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 04:29:44 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id A8A02101DEB; Mon, 22 Mar 2021 14:29:51 +0300 (+03) Date: Mon, 22 Mar 2021 14:29:51 +0300 From: "Kirill A. Shutemov" To: Yu-cheng Yu 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 , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang Subject: Re: [PATCH v23 09/28] x86/mm: Introduce _PAGE_COW Message-ID: <20210322112951.6mqjgxmkafmiavpb@box> References: <20210316151054.5405-1-yu-cheng.yu@intel.com> <20210316151054.5405-10-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210316151054.5405-10-yu-cheng.yu@intel.com> X-Stat-Signature: 4ytjuzikrw53tcotishss6ypyo1mjeub X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2B087E0011F1 Received-SPF: none (shutemov.name>: No applicable sender policy available) receiver=imf13; identity=mailfrom; envelope-from=""; helo=mail-lf1-f44.google.com; client-ip=209.85.167.44 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616412586-918009 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 Tue, Mar 16, 2021 at 08:10:35AM -0700, Yu-cheng Yu wrote: > 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 by hardware > and cannot normally be set by hardware on a Write=0 PTE. Software must > normally be involved to create one of these PTEs, so software can simply > opt to not create them. > > 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) A modified, copy-on-write (COW) page: (Write=0, Cow=1) > (b) A R/O page that has been COW'ed: (Write=0, Cow=1) > 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) A shadow stack PTE: (Write=0, Dirty=1) > (d) A shared shadow stack PTE: (Write=0, Cow=1) > 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) A page where the processor observed a Write=1 PTE, started a write, set > Dirty=1, but then observed a Write=0 PTE. That's possible today, but > will not happen on processors that support shadow stack. > > Define _PAGE_COW and update pte_*() helpers and apply the same changes to > pmd and pud. > > After this, there are six free bits left in the 64-bit PTE, and no more > free bits in the 32-bit PTE (except for PAE) and Shadow Stack is not > implemented for the 32-bit kernel. > > Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov -- Kirill A. Shutemov