From patchwork Wed Apr 3 23:35:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616776 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 05ED9CD1288 for ; Wed, 3 Apr 2024 23:42:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C6B96B00AD; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 44F966B00AE; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A1E46B00AF; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0AC1C6B00AD for ; Wed, 3 Apr 2024 19:42:05 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CF3A31409E6 for ; Wed, 3 Apr 2024 23:42:04 +0000 (UTC) X-FDA: 81969846168.30.E236BD3 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf11.hostedemail.com (Postfix) with ESMTP id 0713540004 for ; Wed, 3 Apr 2024 23:42:02 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DwMpDvvb; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712187723; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ir+lgmmL0ZmHswiBuJpYbgwfILZ/3vCYSTqJIvKZkPQ=; b=wt/cscaFmVNOlIBzUVd15lqaUyTnxXc1Mw6j+OdbjB0bvssf0iMIVxWg3FTK/09gE1Hn+q 58qfiwfV6DJNP8Iic/C0p8sUtfwz+4fLJ4KSpV7qWbBoZyr1xxpxR/cHt4KS+rYy/DArB5 6zzwaO6qd9Czx5rhIVdU1gEWVc1i9cU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DwMpDvvb; dmarc=none; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187723; a=rsa-sha256; cv=none; b=Ii0lmQ0gltRCjFBp1jpg+ltDWjDkZVGF/6KitlpzY2bOW5w9enEygRIhTwsoz7JaU5LeZs 5EOKMSkUqxCOrmgbv0da+LC0IQ4WfniTuprGgskZzT/KSx5rRhCOUaWIyVGKmRJStbcT7V PIlb/7didZ5jKOnB6oDT9uC64bJlETU= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1e27fadbbe1so3263485ad.1 for ; Wed, 03 Apr 2024 16:42:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187722; x=1712792522; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ir+lgmmL0ZmHswiBuJpYbgwfILZ/3vCYSTqJIvKZkPQ=; b=DwMpDvvbVKNHPW01AewN7KAQ5NVIzXtrPQSoUdCyaI2DCFJ9YccZ5CCZRiiFMYQaCO d1t5dFd+Rq4J1djEjIVYNRlxbbRrmZHYo5j8L+B+x8k+ywGGcxhE7uFSlmfyH0P9vIQv teayj0juFO2fi6KridUEV5aYiZdlfnAc6KTeJNPWC2cQHikYWJmPQ2hfqQirmuGyYas5 +QLpe8TwZ8WUxIpbwfml2KIIuuceTprUDUT+vgvHLWL5HuAkjl4p0YsIIyou/pOxbpmg Gpgj0E9CjTZmvSLvJE/th87XCwgBtAzx6i74YG5oGVJhSH9IOguVTErPeMk0CfZ7vJ2a usLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187722; x=1712792522; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ir+lgmmL0ZmHswiBuJpYbgwfILZ/3vCYSTqJIvKZkPQ=; b=NZS/JQKlrjCdHl9dQJ1BwQMSKnZ+Xwf3SuO0kfYYCpvSSPcH9Vlk3Un6lSYftpudMX 9G+6bR8mn86xrt90qzVhifV19ObpIZ7ze1poTaFcMfCprfmRoITZHU8DTDq53N1Jalyv VgrUbcVsSPhmIskGLulv3AdC2eNfaP2S5INfKij3a32PF7ojlJjA+y68NSUghUM/xFju NJ9IPyRaCOwoi5qFOBM3Vtu0qfQ2D5URy9MEd4Dv0dA/AKQZLZI0a1gSC5B/MDIRfnCH mWxCrikGCfLc0RmoQ7Ny6eaKMR/8pL5KSDzRpNHkz0gDOX7dmaWLruFZ26dqcV96lr4W q4Lw== X-Forwarded-Encrypted: i=1; AJvYcCXyLS/Zvste9g6OZBwu7xAHpAXS9c3KxiSLD/nninFwrvmgwY24oyB7xhZAYdJWIw2EYiUMqIW55E2ZqPqRTbjAJZw= X-Gm-Message-State: AOJu0YxsaamxH5ptW+nvVHgQ9Y4Du7Q1sbKbCOc6MM3rWXsvr+9rwJ1E 1PF6EFaxHGrM2j1mhKtROLL/AatjFz0ZDfTJhvskUUtTK5eUNCfsLn2Wp92Ym/s= X-Google-Smtp-Source: AGHT+IE44MbJu6BIDOc3JQGIbnw05ABlT/b1420//7Gxn6oIy2GZnOOaOXzQhU0JUqBs5V+YKteclQ== X-Received: by 2002:a17:903:22ca:b0:1e2:3851:6b6a with SMTP id y10-20020a17090322ca00b001e238516b6amr860088plg.65.1712187721821; Wed, 03 Apr 2024 16:42:01 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:01 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, conor@kernel.org Cc: linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v3 12/29] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Date: Wed, 3 Apr 2024 16:35:00 -0700 Message-ID: <20240403234054.2020347-13-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: piei7i71yb6xka5mt4im85rutw4dnhwu X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0713540004 X-HE-Tag: 1712187722-255480 X-HE-Meta: U2FsdGVkX1+5EyMub2Apf1MJz67k2WBDHIl4SG5ZR5njbzUwCYuLXqVRaAh2js6p/1gqX86DyXUOA/FWAGhfLZufiF2j6NmWZ9SeB1TRNUP+S/BNmsJ0KwHBn3tGeLIZF6enasKAEhXKY+2oOjp/Sap+9dOKQpdzKAEJZz22owBZ3iRrokZ42HbaBtn/kzkkAXy2Skl7RdXsm7Vxt+Euwqn8UN+cmAPbU7B8h14ujrwRTrzbk/vhGW7fsJjajwPyebmhS/MZ/tymdCjyQIuX0qT7122NG7BNefTg9BQnBiJCZos5zPDo3AjAcWVuNQ354sMhcixZVFT8tAUa5pHe1Ce5UhoNjQGA97+XtBL2ZLQorkNc7uN6OuiibeAWr87fua20xS6xZ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fXo8zbGpsWbYO2mcS+vraud7yJdPi0hfm87K8MY607AR1XAs4kQmhd8a/es19QrS/vS7K6s8Yek/6hDHa4D 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: List-Subscribe: List-Unsubscribe: pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 6362407f1e83..9b837239d3e8 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -403,6 +403,10 @@ static inline pte_t pte_wrprotect(pte_t pte) /* static inline pte_t pte_mkread(pte_t pte) */ +struct vm_area_struct; +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define pte_mkwrite pte_mkwrite + static inline pte_t pte_mkwrite_novma(pte_t pte) { return __pte(pte_val(pte) | _PAGE_WRITE); @@ -694,6 +698,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); +#define pmd_mkwrite pmd_mkwrite + static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) { return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index ef887efcb679..c84ae2e0424d 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -142,3 +142,24 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma_is_shadow_stack(vma->vm_flags)) + return pte_mkwrite_shstk(pte); + + pte = pte_mkwrite_novma(pte); + + return pte; +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma_is_shadow_stack(vma->vm_flags)) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_mkwrite_novma(pmd); + + return pmd; +} +