From patchwork Mon Nov 11 20:53:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871380 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A80D4D3ABF7 for ; Mon, 11 Nov 2024 21:06:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=X/2OWOTmMMxumpM+SiyMosT08VugchrIYPwWQ0h1fPo=; b=PO8zG4cYQ8jnkU aIMCnKBxueEod2GhO8VouwUi0fkkBAYr4jwZzY+hk4zXYs7srU9Nqd5El3ZK4cmO0QztpcxIrpnU5 rZ2/Uj30YD6gKSDfhy0OvRQuY38/myqXPZhyKERa2wtgybW5jOSBB3qcqAajsIvpZF1SQZTQD6D38 01zb+TQrcQh8LYgb48vewp7w50GpAKXQCPpor6bNYeUO+PIkR3lE45taBIceMFOEqWbLjbcRURr7z cfyerfign7CyTru/WzZj6Dpa/07lIbW5W5baY4UEGXXqqUJY2ymmmva+0CLpwfWKsUKG8K2iN3vSY my87npgGcorI29CqtImQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tAbbt-00000001IvT-1igE; Mon, 11 Nov 2024 21:06:09 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tAbQR-00000001Fsy-3jyH for linux-riscv@lists.infradead.org; Mon, 11 Nov 2024 20:54:24 +0000 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-7f45ab88e7fso1007524a12.1 for ; Mon, 11 Nov 2024 12:54:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358459; x=1731963259; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=BdmqWsDcjXF4nBF0ugyXrNKHtHfZsDWJpDZiv2C4G2NMytFc3Lcp2FGcQiEFq9+PxN JkWddFv0g+IdW+aCd0FNyLL0O5sfwUXhtk7SdaUSDF+fxdnfjFyF9G/+wXYDWV91sfnE xRpe8XZPTecDoQYMPU5T8VVJQEYk1+EJVqNY0enSHiRRPQ8/bQZC+2r8Sm+EyH5kauUj w8hpnKLW0z9msbXhq6Aq8Rp8tHLm876sAHgCe1fqnuGkzBS/9VWBKeQl58DZhpkf+KN7 FlafRPYwstP36Q+/YulmlvxgOCt1GkE80yscXHVsp88jFStQLpFzlTesijj5PfymLp2G CAEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358459; x=1731963259; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=dWOJ62XH7fTpEEuZ7GqTdL8jCppDWffMHh0M8478AiENsEPDKANuSyDOASSVu94p44 af8ft/wf1anJlAgiBlT9NxJCvNPKc92MaTmZJg9vXCodCZEL++Y4Jyf7YC8tSPMql5Y0 lq7o/9AzHU4SWpPNDKVajTvrf9RxF+h0R7ISn+kav/vufnR0DhGwjAg2xzJeMftTRCW0 odteM3dauOkV1REJR2e7y4/QcMktJ4kK3E+xpzO2O8OHy7o9aof7s4aboZZP0XvrAhqH mDkZF7Cwn7/EZY0fabfQd06EUVkjfhE29tBtJHzdyL0/BdvtMQHIIxukX2rUvg43ihSc AYhQ== X-Forwarded-Encrypted: i=1; AJvYcCVsm989kTCCOlmXN3/WUvaPu5FMA4d/2MSzMy/+cqXpfD9sUcjyUZOh924qyV10h3Fv3XSxZ+rzQg6pxw==@lists.infradead.org X-Gm-Message-State: AOJu0YyX5w8WsZG9TkrMWU8SbjlvlkVoRcZAiNT6gRuApca2eK6kLloZ 8rPyxR1m9frGOS1x20mnsIeLLz+uLcsTPGbW7oYk9i9tiiW/znzaoGvxBQZsnAc= X-Google-Smtp-Source: AGHT+IEjvMy0kDgdhVCgK7uRX+xkeiQXZuQFJUruwKRcfTXpudULSRLKQcZE75uv6i6SPaD/tVV5LA== X-Received: by 2002:a17:90b:52c3:b0:2e2:e5d0:dabc with SMTP id 98e67ed59e1d1-2e9b171635bmr19132907a91.11.1731358458754; Mon, 11 Nov 2024 12:54:18 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:18 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:54 -0800 Subject: [PATCH v8 09/29] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-9-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241111_125420_125162_CB93D571 X-CRM114-Status: GOOD ( 11.10 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 2c6edc8d04a3..7963ab11d924 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -416,6 +416,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); @@ -738,6 +742,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 4ae67324f992..be5d38546bb3 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -155,3 +155,20 @@ 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->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite_novma(pte); +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite_novma(pmd); +} +