Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3445136rwb; Mon, 3 Oct 2022 15:22:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5eTe1pmYWRfb775Rde4l9Bj3izUDOzBWD/5oabnojt6y1oL8IXkMdFP6E90eauCHj3T4cg X-Received: by 2002:a05:6402:448c:b0:457:52eb:b57e with SMTP id er12-20020a056402448c00b0045752ebb57emr20978321edb.178.1664835779794; Mon, 03 Oct 2022 15:22:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664835779; cv=none; d=google.com; s=arc-20160816; b=h92hrqZAjy9Ld8Q0Y2BW3lZ06e4n1n5Z4cpIgmwIhFZcXncQl16rp7u2v24Xq/M2AS UhRyGDVGvjzN5PlvzIOxNPPIotjFvOKjGWEIQon6sIv1VxXkEqXmlbRlUUw57G8TszJr sqiSLFXFDnNiMlEcasYlB1fNGKbT0WCdva1xJxiEHtVIoJ9ZcjGngjfuot7v3mfs+f+A pEi2HTL7VA+g2OcZdRKCdBC15neM04ywbgivOTfoz4DMOwjA7Q/4GxnkEDFajaUKURTB ZGUWF5LMSVgICgjc+iHKb9hq60ftXGaUJQ6CZJFwKkeNi1Ijsc/HL7RW50Y+XEQ9YuiD o2WA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=B9z/9SQNxbpWjvmm/wRDVvjtOvq9DNKsvNJ7C0aaLM0=; b=jSw6ooN3CHBYaVpoMCZos7lp5L6iTNX8SqFy5VtihqhymmKCWDt9XmAUM+6WwxOwdE uWzGzH/im78Deggib2I71gxPNMDmAb4eTlC0AKrSVuoQZwATbkRwQ60AdXSP7Yx/2m1C 84W1SiPZsTXQTNK9imMgVan9dlCDqk6Wqc/44NAfvPvVcj1UBjRfsVhAdEGohDbnCjje X7jxqRqMIOF5ei5khoXr5FvTYIE6cXBBWKJ2wqQrH0IIyp/Fey7tmHZ/HrCBiflxg0V2 +ZK8Sr0Yvhxju3TbRARlX398YnrsKdJfOFRmxORgwP0gaIK444esdYF8R3D5MVzJz0dq 17cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qkgELbA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v2-20020a056402174200b0043d00293d23si8498004edx.391.2022.10.03.15.22.34; Mon, 03 Oct 2022 15:22:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qkgELbA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229835AbiJCVyw (ORCPT + 99 others); Mon, 3 Oct 2022 17:54:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229847AbiJCVys (ORCPT ); Mon, 3 Oct 2022 17:54:48 -0400 Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14068A45C for ; Mon, 3 Oct 2022 14:54:44 -0700 (PDT) Received: by mail-il1-x12a.google.com with SMTP id q11so5445484ilj.10 for ; Mon, 03 Oct 2022 14:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=B9z/9SQNxbpWjvmm/wRDVvjtOvq9DNKsvNJ7C0aaLM0=; b=qkgELbA88YoJdu4NDU/eZjukcJBQUua5vbUOpqJZO+6P5Ox9LwRbghDc2G52DKQCZb XadYlJx6N9UigaTBKr2gZBGqmm/u6EBox1eysnEqKczkPt1VpfNu96zO8f5KjwBEVOTj rRjNyET1Spa98kAaOa8zY1x3h1MEWEihJZWsU3kzCz3U47r5Kvy+gtjpArdM7eVWoTJp TFXfwMYJCnsDbkwvzY4tcfZNE5vDSbw6sLgtcUTCzp95mgpwWDmNLmWXjHsNlKUmZdrK /F5XheAdLo6w+yhK0394q+fqmzw1c6qwXBl6WF66CB1cytg0ftAtThrRJLZfgiibFo3s n43A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=B9z/9SQNxbpWjvmm/wRDVvjtOvq9DNKsvNJ7C0aaLM0=; b=1STJarDhlD5mhNSK5fLqCCk6yXqZfh9wh562WGJgvPHQVi0G3qdU2aDM7ZC1FXZ2K/ iztyJNNUPLr5n8hkd9BG9Gme2z+by/aKxzjjxEKshs2o45bWYVO5U8YjxM8tQx+jvlNK M99vyIqFFlIYsDCwjvkVvb2BmSX/L5C9pbKFVtfHFrb7nwoeaQKnk2PNcNn9z8bK/2LZ XwYawBUgbODcVNS47Th0SlUdasMV7/IqCHHhwDmHix/1DAZblIOUVsaUd96fd6kXQWVE jCWrloFm32sPfHhcVyqRANeiyoMjZOFe+MQ5oLvJSx6+6NpO8ZzyhjGBK2SgSYTFUghv CXng== X-Gm-Message-State: ACrzQf2KdFiyCdYNXJzrm3DP2XdevIz+rXDv3KHCZQHi4fVOxkeN3UcO nLFdRG0QfbyV6lMmkB0nt6i3lvq8E65ieVyswn0LhA== X-Received: by 2002:a05:6e02:930:b0:2f9:9d1b:2525 with SMTP id o16-20020a056e02093000b002f99d1b2525mr4355276ilt.173.1664834083749; Mon, 03 Oct 2022 14:54:43 -0700 (PDT) MIME-Version: 1.0 References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> <20220929222936.14584-11-rick.p.edgecombe@intel.com> <20221003162613.2yvhvb6hmnae2awz@box.shutemov.name> <9e9f2ce8193ea2e86474ab999ad2a034c49d8b22.camel@intel.com> In-Reply-To: <9e9f2ce8193ea2e86474ab999ad2a034c49d8b22.camel@intel.com> From: Jann Horn Date: Mon, 3 Oct 2022 23:54:07 +0200 Message-ID: Subject: Re: [PATCH v2 10/39] x86/mm: Introduce _PAGE_COW To: "Edgecombe, Rick P" Cc: "kirill.shutemov@linux.intel.com" , "Hansen, Dave" , "bsingharora@gmail.com" , "hpa@zytor.com" , "Syromiatnikov, Eugene" , "peterz@infradead.org" , "rdunlap@infradead.org" , "keescook@chromium.org" , "Yu, Yu-cheng" , "dave.hansen@linux.intel.com" , "Eranian, Stephane" , "linux-mm@kvack.org" , "fweimer@redhat.com" , "nadav.amit@gmail.com" , "dethoma@microsoft.com" , "linux-arch@vger.kernel.org" , "kcc@google.com" , "bp@alien8.de" , "oleg@redhat.com" , "hjl.tools@gmail.com" , "Yang, Weijiang" , "Lutomirski, Andy" , "pavel@ucw.cz" , "arnd@arndb.de" , "Moreira, Joao" , "tglx@linutronix.de" , "mike.kravetz@oracle.com" , "x86@kernel.org" , "linux-doc@vger.kernel.org" , "jamorris@linux.microsoft.com" , "john.allen@amd.com" , "rppt@kernel.org" , "mingo@redhat.com" , "Shankar, Ravi V" , "corbet@lwn.net" , "linux-kernel@vger.kernel.org" , "linux-api@vger.kernel.org" , "gorcunov@gmail.com" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 3, 2022 at 11:36 PM Edgecombe, Rick P wrote: > On Mon, 2022-10-03 at 19:26 +0300, Kirill A . Shutemov wrote: > > On Thu, Sep 29, 2022 at 03:29:07PM -0700, Rick Edgecombe wrote: > > > +/* > > > + * Normally the Dirty bit is used to denote COW memory on x86. But > > > + * in the case of X86_FEATURE_SHSTK, the software COW bit is used, > > > + * since the Dirty=1,Write=0 will result in the memory being > > > treated > > > + * as shaodw stack by the HW. So when creating COW memory, a > > > software > > > + * bit is used _PAGE_BIT_COW. The following functions pte_mkcow() > > > and > > > + * pte_clear_cow() take a PTE marked conventially COW (Dirty=1) > > > and > > > + * transition it to the shadow stack compatible version of COW > > > (Cow=1). > > > + */ > > > + > > > +static inline pte_t pte_mkcow(pte_t pte) > > > +{ > > > + if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) > > > + return pte; > > > + > > > + pte = pte_clear_flags(pte, _PAGE_DIRTY); > > > + return pte_set_flags(pte, _PAGE_COW); > > > +} > > > + > > > +static inline pte_t pte_clear_cow(pte_t pte) > > > +{ > > > + /* > > > + * _PAGE_COW is unnecessary on !X86_FEATURE_SHSTK kernels. > > > + * See the _PAGE_COW definition for more details. > > > + */ > > > + if (!cpu_feature_enabled(X86_FEATURE_SHSTK)) > > > + return pte; > > > + > > > + /* > > > + * PTE is getting copied-on-write, so it will be dirtied > > > + * if writable, or made shadow stack if shadow stack and > > > + * being copied on access. Set they dirty bit for both > > > + * cases. > > > + */ > > > + pte = pte_set_flags(pte, _PAGE_DIRTY); > > > + return pte_clear_flags(pte, _PAGE_COW); > > > +} > > > > These X86_FEATURE_SHSTK checks make me uneasy. Maybe use the > > _PAGE_COW > > logic for all machines with 64-bit entries. It will get you much more > > coverage and more universal rules. > > Yes, I didn't like them either at first. The reasoning originally was > that _PAGE_COW is a bit more work and it might show up for some > benchmark. > > Looking at this again though, it is just a few more operations on > memory that is already getting touched either way. It must be a very > tiny amount of impact if any. I'm fine removing them. Having just one > set of logic around this would make it easier to reason about. > > Dave, any thoughts on this? But the rules wouldn't actually be universal - you'd still have to look at X86_FEATURE_SHSTK in code that wants to figure out whether a PTE is shadow stack (on a newer CPU) or readonly dirty (on an older CPU that can set dirty bits on non-present PTEs), right?