From patchwork Tue Oct 29 23:44:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855728 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 84D92D7494E for ; Tue, 29 Oct 2024 23:44:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E7E36B00A7; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 796106B00A8; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C3BC6B00A9; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3AE866B00A7 for ; Tue, 29 Oct 2024 19:44:50 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F3A9D12079E for ; Tue, 29 Oct 2024 23:44:49 +0000 (UTC) X-FDA: 82728271584.22.76E3039 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf05.hostedemail.com (Postfix) with ESMTP id 84B27100006 for ; Tue, 29 Oct 2024 23:44:00 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbwegUCY; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245358; a=rsa-sha256; cv=none; b=BOzB+7pE9n4abeOmhgd0TQLVr8idxy/VUKXrRXvyjpZAlvZIh/PIBMds+0EmTX3gmqofTt piBBBGGg0IG6ppIBgO9OBxUtFw/kZp+L22gn0TWW+gM6K8XltraTvU+jVvOlnDcu5rII/6 GLB9lXlq9AECvivtpfXVFqN2KxbKCL4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rbwegUCY; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245358; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=tNaKI2MXKBrK4BCWbxPXHG4CRaCRTDuIImLxE1Y7RakkyJLGZcqxUxn6B37oML4i0KdFhQ IzmRrC/HMyNUJmF8AZst2V/+8Dvt1V9gVDWw5bHpjyg2cKhah0xJiUoF+f4oDxG/aoFPpG KfxZupkCzmYTNmLLjKpL01t++6eCyAw= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71ec12160f6so4359747b3a.3 for ; Tue, 29 Oct 2024 16:44:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245487; x=1730850287; darn=kvack.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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=rbwegUCYW4F0z1+BbJ7wKfTIYUvXvNOJR1aI0gqTvLhGFup1/Xolcm5ucZ79UgUMga TzRtVfdzZzwNhbmTkZvdb6AzBd0cwq7VOdue0laujLzIWIgyXx8wc/QEiU1mRl5a0SFd lnBO4LNqFvVpgkfi7icCDdkEIm0ZSYx4934nWy5QG+tbKXD9sI047K1jpAtkjrfIvyA0 YyUspLqOE5Hp+HW9Lad5jCu3ZfZrF07qCNdZLO4xzK+oQ5YrMNaHtZBUWaQVlRbYP47d M0CK21fHgx9mKXKav/MrW9E1n84e7tCrM5kcjBhzqapjgXu9fw5w9gb12zJpLh7LBNLp XO8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245487; x=1730850287; 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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=SjlmNwtTtRtqvdj3JWf7VjDIaGk9yWCmihySAe+/y+i3klorHxiJL4aLI8jasCetKE ULiRbe0pktfkD1fA3VHg0OBuQfsPhA8o2SOr46L9c5MUNOihG5tCfF1IycbLBcvne2de mRs2sSVbOojcCJ30qZPyawx7Nhax3DezSBCTeYdJtblsOF4z5RR08UIByK6Z41T5BDio 6VW5tWLEH8A55xeNQZuR3kOXV6SxxAz3tD8uSXGfchpYJ2S80YtH4bnASWj1xuyXfPFw s/XCPWmSJ2N97Nu/pMU/pdR2DZqzphwQPVZTP2P/yP6lWBBpmk5UIV0r29G+55c/92CJ P/Gg== X-Forwarded-Encrypted: i=1; AJvYcCWFwvgULg1W/9h8qJMQUVBroHVMiFndctSTbCspzxLKWXYYmBtpuwkzG3sCH4Tpgr++utd0+M6D2w==@kvack.org X-Gm-Message-State: AOJu0YxUgwJoLe0BHpoyD3su83k4f0b0kZJImAPlwARqjWOLoBoaMVsd Ae6KCGHqfza9bVO0Ni7P1aIhARXFiTDZ3DXU/kKV0Ch84UR7rwxxTR0bIFdJil4= X-Google-Smtp-Source: AGHT+IGGMP4Pw+lp+LPCrok5EqwNoJFdizqNNUNEZiV3YZc6kA7wl06mzdMMPrxb/Znt26d5Smw70g== X-Received: by 2002:a05:6a20:c797:b0:1d4:fcd0:5bea with SMTP id adf61e73a8af0-1d9a83ab7f2mr18393767637.6.1730245486894; Tue, 29 Oct 2024 16:44:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:46 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:12 -0700 Subject: [PATCH v7 12/32] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-12-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 84B27100006 X-Stat-Signature: smmz3mb3acd3sopnm3rf1duq47zzbd9q X-Rspam-User: X-HE-Tag: 1730245440-505059 X-HE-Meta: U2FsdGVkX18ZpiTCeVoGOz5Ef+QZSodv05KylJflCf5XJpkDoBneczXKIztdS0Z2zilJl84AJGUFTIguBlfAVBttRvDHNQAmzbeQac6b3tC6kqsS5OcOip+HMNhGvOEV9/Ayheog/AiO2qhmUIsJdRIid7AgMH0uqtGel8KFmOM9aON6wbnyFV/D9w2BZ3izp0STqHHaKDkOg3lZjNbPZBaqcMX3wZBSFuho2W5A1DSUn7p5fQHJSi/heyjwRYRO58hiifZwc4kogr7mHhhh1JcoWkufg6qZbE64Eni1bWMN0vE77PaC6rJ6Vj2LGmIPQa4QUKILHSROxIHt+1VBtOGemOrleWlfscFWy+l/NiOxncM/eIxUOzSfTKr3HgMc+GD8Iz7Ts2STVvxLGuOco0OkWaLMQjkQ53YtkAAD7hiNFVY6jwM70fm+nhlXcxsUMPYff1NdL8IMhf3O6eYgQjHflNIWOZQnKNGqZ4IAaRFYkk4VX3ljt0OUJ/XaNmAPOo6fu2c0ysyc7r3bNCbLQhDoMvZiFzFzlWqrV3iYu2p3xKUvHBakdGnXD6PKtgoR8ZsV+8kUpOBT7TBI26Fvg455V/qVjMqNIKIn1RgNQO7vHtcA9spuyx0u/KFfWqQY5/CAdatwLgY97ac6Ti2UFD6GB02gd2+Z/HSzZ3zCk2Y6EDcBBHGDbGJE5dARuU0XipACImPbIBKa2EitdvAxeEhf4tiIO6hUnkhdjGdoFbtRoDYKZm5JLoefyDMxd9ch3HDcUdHM3Nw9HKsn57/wELsb+I8sEpd2yqC3yBGnh8ayGzc9+el4TEPZsvcgcHKAlEr7PBrz1QVBSqPXmYyGyTuUtfqaHQ6V8FyR+qPxpgPJttAf9fS/8BFm415I1Z4N1l4A8Y75sanwBVrEV1SXw4OM6s5ly4WFJbyYjvUo2kpuu7QTweDvDWeCbd8+H/IlhKwS58K8fP7kupLdU5p e6T9zJem 28ZQB6ZiwOQIk7+i9wpmzAQ97zOGgajWGG4KRIzI5XI2A+8MyY8foPfPVPY9xGKQqIGkuXJDoebW8HG0E9C7Fo59yAnHjAQIKZwfWSUFgqRRyp7g0BTXjN1i79gnAfa2YdMWxAU+hHO7Q02o0TWxQsIp//A4ISXfivjoLhGoV6rhhqMU6SPfnRHjjHmRqtHUZL/yee3Lkyaxp0bCsHs2cx9ap/Ib6GKrccS3abX1cUGbqojkPPd7cBtoHm17C1+6wUXRHhwcBy1px9aFbxrQV5y8vc35NM633PuhQbPQO/Lo0uQxhdM05Lve84KwFk3kdhAaPt0I3eyygYNkJzwNjoChM9D0JURR1Fb9fxQ50BCAyQBg5XZadRae0481D4ZAMn8NKAS/CcQi2T507XGN3usOLXvyIijj8gIhIDdIAWu4avanRAxN0PGGE3otxUDpMAsoqjFHkEN+z2O1nTNYfkQueX40CLr8vmxMts7NDH+wxx/5HpJcRCwkrCIC38LNE8a4G 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 | 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); +} +