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: 13871346 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 4A667D3ABF5 for ; Mon, 11 Nov 2024 20:54:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 837806B00B0; Mon, 11 Nov 2024 15:54:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E9CC6B00B1; Mon, 11 Nov 2024 15:54:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6122B6B00C1; Mon, 11 Nov 2024 15:54:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3D14B6B00B0 for ; Mon, 11 Nov 2024 15:54:22 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E97CF1A1B9E for ; Mon, 11 Nov 2024 20:54:21 +0000 (UTC) X-FDA: 82775015820.21.AEBE5F0 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf05.hostedemail.com (Postfix) with ESMTP id D5940100028 for ; Mon, 11 Nov 2024 20:53:02 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VCZaXPIR; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731358228; 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=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=wdF8zQfFsgCxNaQrVsEP/APt6bV4nXDNgLjXOFd5KUeWUgCIFwDCuCMJRRTuje8ftAnFjO OBIlf6eeA2XeVoEQ9ZKGKuIE0qPHxbrE+WQldYPqgItBZWiXFc0NvTU8znfjCgT7KfXQZ5 o4QPMuHcMTryfeQfjDOFYFXqEU5oquM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VCZaXPIR; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358228; a=rsa-sha256; cv=none; b=sxro3YpjVflbwD0+NnPzlhri/Yr86pq/uxS5h9CwIQbn2yYIwL4dPPATM9Q6KPOv9Wbitg qV6fnPvVywZD2lblM9PCC62F9d5+HDsRZ/4kQjKWNzwr5kkEsDYJzVrfAT7HQ1LD3kav6n uqsyLvdAPBDFqFMuSABrwZDQQCkFIkI= Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7f45ab88e7fso1007525a12.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=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=I6L9CXOfGC8FGkDgNo32YOnEr2hJuvgG2CDkhLFheFM=; b=VCZaXPIRrvo7n4kIsJmM6U/C3MyFIfnu5/D+mlSY3EGs0BOGvl6G7BPVYTUJW93a8u x0rg1MkuoUYp4w7/NswnsascjEc5Z9D0ASaWRp+XeWBZoI6Bz5EAoCWpziUFNBAjTg8c GXjOpVSO89dipzG/LTdvXcTjlkWePBDAiQUucZgb2yT61eosqc/5Av69GyJyMyXXEYrg WdwMbAsaIQohLsQ4kGgvIN4hb7HDrwvmQRLdgIorH+2TrkoFMsTGC6G7ootBBINPaHZh Z89Iylyo/JPhaPn4Mpcs41tPICfz+z4lzcStYCmBgstcPOcFVNNvm46iy7FDS5hdLiZu fcrA== 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=bryArB1JfpWD37QhluqPrl566by7kqTdUtmQ7zlFqtRWedq1wSlq8ISpjzE22LBQKV dGrkWPcIGB52KNalMDrUBJ1dQebIhRoimriNPpuMIPNy1OFuQNPTdP0BjQ4lEWzDgiYr D9Kn8lH2alrV4GVXwwmsb8cakfOsocAJSyrajb/qBPYOIB/zHwQ2ZiEes9u9IvKq0E78 vsPbMMKg1RLsV5jrIuryVOKQTfMzkIicrjJPFY9vycWuEgd9WDEgr536S+2TVxN/KZJn WXpqzTGpnRRYQACMKRPfnsstzAZ370xGZmc+h2zdRYsPdRK3NC/+Y+mXcL0OLeQe258W 2F0w== X-Forwarded-Encrypted: i=1; AJvYcCUPuJxp0YfHwvK5mVNS+ZMKcbP9UhK9E6Eu2ZG0ls8dwfRfsQL00gVOBrN7NDXsYJ87TFYr1P3POw==@kvack.org X-Gm-Message-State: AOJu0Yy7ErOo9jj8GenB6mEUmhKkApvy6smMdvlMPRVZ/CwenT5Y55q+ CrZUmUEqL1QLq4ZR9VLcAyu95ofL22Y11fizEze1EakV4RJwP1viV2LUYOIwM0Q= 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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D5940100028 X-Stat-Signature: knzc4a6b31zdcajjpwfy1d65jgkaepwx X-Rspam-User: X-HE-Tag: 1731358382-664576 X-HE-Meta: U2FsdGVkX1+MSBCyKcOKvgEStIUNSjz3Fb0M+03dIG+HSKEcQxFgkAwdSkwxdmjT7vXLwlLRwZ2SI3cT4UFxUbzDt2v144BEgKcyy8Ezy/O9PPcwgDKUL7zozHs0ce9ua5Fm0IpFGBT4K2VRpR+npKO1Tn+TJ0qA0vabr9zX1jNzr2M6GVDvymMQrLWfa/5D1+P9/IKLNR6mm9meIR76/y3OU/WJp+50Y0xEhCNTfel2vLfNyQLI90s7ITScd8rYpx9uPXOCIyENUfHUHNwWEtXKk2+OC76LgYrUY+PCVyPIQ16LhRK2IwAbikMXcpa/a8iOAnR8lTHrxtSCZ/S3BaRlbt+ODUXb4PLsarq2ANVKy+ONPW64ceFGZHQmO7cYc6ynzX7Q+JZGGbrSvydNhcvTZ5Pzl5viA4bAm2ePmwU3Y6da1Zo1uHk19ihHo4MVGFADZFhi3gOuFMnnQbz4kcKHE603GTzSoOLlmZc+NVxY2WSQJu4xtTPBs0Cspq+LSPe5SgyRcn+ZbiYRhx6iFjCOaSQ8asfgWxX1PwSgrDeepDOWtjKu32Zl35NRGRnOVXtJ0YQ871jWx2gIXOPRkPgJqd5cGgPCCcI36ggSoggvG9HWh+5VDypV5EkzR7G3wM+bLOVozDIJnX0icrzRduZIUn7yZDkjcgKatUHY00QsSh279wf3vw6gD17yn+nl3SE/rU+I6TC++zPODDYEPx42X4Ct3lAZQqanO1C1Of0l1lHgo/lmeyFcjYRFDn8HlL3a9qV9UFXdc5KLJKdMawwefMCOX5ex2xBwkOVSkr7by4oY9oGi2AzDmUWBn5id1enMyL4l16ulGtPv+Tz77eI276tCHNjHDuGIPp/CjJl3wb/RXhO0Vy7MtoKBR3y7D6kXwdbEAEiOzSEEciHRnP+1RsnuJc2vCyyT/ljq23/LCTgIdKRdnMIdWVnohxzX2edwzfkRH2vu8XI9pMC wq7gBOzA vnM6TXoqy9oxNNunNrEz1KJVgO93u5r5RD+pGCvsHuuo8ZxgAXHvk1O4d+CPNG6pTJiVNzy9dlDe7OBbIuzeCYgJv4wML1PHVhyY4vdKXl3FWk/hzTnh44520OMFxQ0Hid6fZImhohcyceGNKB+VGfBSP4uA6Q3ku9fQU7flWsExXSzf2oRy3F/34GiwPAp6z3dIGUre9ly4ZgFNI7GclC3Wl5+lZ/LGheTmQnWv2Ftj4gtJzcM/c6xVagGUO70irA9b6lDpBtBwfpJSxMLAUmVOWbbZihMENlzDYTCPbC1gdGOsPGrvrb081mc8zt/98XmBgBeLwYRsigPnzULphXdaYxPUAdm7lprJpOV0DtBQcFEKS7dRyYm9qQMgBQGdtVZJCVOMlrEHpAqJr4j5sPpRcPsmED7Y9qkEiI+NGpE8OCQyhaFsSeMrnnxe378iR7ZWIQNI+jVcLaD5NNcFNacvCPeAeSh4O79z9x+Pw4kaUGCXTAaNmNvPQtsqHq9+6adnc 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); +} +