From patchwork Fri Mar 29 04:44:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610055 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 03F6FCD1288 for ; Fri, 29 Mar 2024 04:46:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B9E76B00A5; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 819F76B00A7; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 643666B00A6; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) 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 4492D6B0083 for ; Fri, 29 Mar 2024 00:46:25 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CCF01C1131 for ; Fri, 29 Mar 2024 04:46:24 +0000 (UTC) X-FDA: 81948840288.25.38695EF Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by imf28.hostedemail.com (Postfix) with ESMTP id 08963C0009 for ; Fri, 29 Mar 2024 04:46:22 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=im6vyX6v; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 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=1711687583; 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=fEO9ZqLZNdtWaEHlnNa/VDfM4+2OUD4uT/m4nT0q2gI=; b=llD0779hNEPd5cvgO885GNst2q7ou19mRloYPf47z8qCmcL99VujTd9U2z7CehQicd5F/Z y0QOOMmkM8ClIJndzcMG3fBB+6NM+XWxNIcp23XREY0ASfZncUz99WyF5qbeqbwhT7pV7P T/yhr3UmtBzR5F11UjLgt3/2iXQSWxU= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=im6vyX6v; dmarc=none; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687583; a=rsa-sha256; cv=none; b=QFaculWVoYIObr0iZ/ApY2+U9xtA6uotj9GgIJbJA8JsRObGOLpI7JF5R7wlgTvppF3/un HbYlEM3rRhz/qqkuB3gl/c7QEq8qbqd7zpikxzASJGaxhKUfPriZ1sQcekQyoFh43cbriz z3sI6V3pZ8jFe5xB/wgCGb4QCJn8UfE= Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3c3bc867eaaso1151848b6e.1 for ; Thu, 28 Mar 2024 21:46:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687582; x=1712292382; 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=fEO9ZqLZNdtWaEHlnNa/VDfM4+2OUD4uT/m4nT0q2gI=; b=im6vyX6va8QDo51PH2Cn8tx24sh8T7RR41ykVkW3SxUvuhBHmupf9LTc0z5UQmGDcG 9SoEy2LOAc5UL6LUMTkFN6FHTImi2B6epMasas9svPZR6VBQcBuf1SmubhnkPARWNw9L OlCMMKlWywVbbJnTKRqgYCAkDDppeuV00LgAlIOTpyjhrhYnrAoycRsjY83pUs2fD4ut YbLtRzjtWD7bTSEmTbneHf8uajoaE9rjEjyT+YGK+2Q89cs1u6sQoF//FTjYIwC+0fkN sO+9PjOLIOCBOWqF2ZDwhYKiy+v0ZMji5byhQRXtgd7UcAzOCy0s73hKWlYsooAPJNuj zKcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687582; x=1712292382; 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=fEO9ZqLZNdtWaEHlnNa/VDfM4+2OUD4uT/m4nT0q2gI=; b=OgQTXT21eGGQkkw3TVJFu3ENeioQLURhmn5YeLQJRJsDdF9ORY6yN1bErJYnsUzX4O BB3ZBZB9dd6Ku1QJdTpJ4ABv6CC6YMKbNthi92Gjgs6ssdkNRoe0lMFKTeV1WWF/xEDC //CpLY6SlP+lpLwnWkJYJ6ARDlzdis9+McO40Pkd4kkFKRg7xAQH+J8j6PNAbABCUMoG XYwn8hZcokhx4/+3fTdMs26WceCSOaqUbAsHCpgsgSSKKjKlgvUpKw/zglAbLJcSmAgw NPgt6txjq3OnHw4F2+RknGqEfFk+nq0LuEnxszuUi1wqh5l5xxa+Hq15L4q9Fp+y2WRs nQZQ== X-Forwarded-Encrypted: i=1; AJvYcCWmkwIDGk7u4pYWq6WYcBGGXb5R11936XbhC6h4eFs+GIdTeYfwwDXjKN7+SiGZkXQHvHVXnaQ3iVkeLWWY4McAtN8= X-Gm-Message-State: AOJu0Yz52p0db9vOLBpF+NcaHLi9VDkQYgNEHINOyD+jJlVXSyJ/q0T6 C7KdKuCOLG42AD0S2FmQUUJV1d4kIOjF5/T0NrwTqF4cguZ1smd0bDiZ1L9ib74= X-Google-Smtp-Source: AGHT+IHkNGwGYsa0lxSVV6I7rhUYi4wQsB/IhRzR2mYXyandS5eg+YGt4M7UftNEIAsc9qZb2HdgcA== X-Received: by 2002:a05:6808:3385:b0:3c3:ebba:2cdc with SMTP id ce5-20020a056808338500b003c3ebba2cdcmr1352197oib.17.1711687582182; Thu, 28 Mar 2024 21:46:22 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:21 -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, palmer@sifive.com, conor@kernel.org, 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 Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, 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 v2 11/27] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Date: Thu, 28 Mar 2024 21:44:43 -0700 Message-Id: <20240329044459.3990638-12-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 08963C0009 X-Stat-Signature: 6hmu3nink98toubn6goj4jq14z8z61zz X-HE-Tag: 1711687582-515121 X-HE-Meta: U2FsdGVkX1/ddYYB+Ifbq4SAPxq9sgCXe4dTzf6p8oGpcjdECLDz6AQiI1tOpD6u6EKs9NzgekYgZy0Xd7iVjPgMe7OI6eXb6iwSIiYYS3DY3NAgvaYKKGcL4NeLIJc8oJZACPmUvoOmFDjN43/s6iIB5bBdgo4pQOW48CLCltrcb7BQLXcKqTf+7ZsoGjsIpyRvJY3o0PQ6gjcnA5qzCDmfolGV7TQ03JJOGkKCZ/0gTFOijPYevfgOOF/Z74AR0ksqxKNDZnXqZikCFi9PkbAa9uIkM6HMXjpQoWpOhr1O44x0cNlR2lNdtP5PSUWLYE3BB2X3/nboVjcEG8VEDYnWF99lp0q6v+P5rjwPHjX3eC2wCutHHLM7dJetHnCu14SA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bbu2WudhURwV8CHlCxx+GsNguHQxba9M1pMJz2iN+k0tTIq5/VCK51Ux1au+gqZakiTAEunFniRJi/ciBmuHyDsSzSTpd3w67EeDdTUe 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. On riscv, presence of only VM_WRITE in vma->vm_flags means it's a shadow stack. Signed-off-by: Deepak Gupta rebase with a30f0ca0fa31cdb2ac3d24b7b5be9e3ae75f4175 Implementation of pte_mkwrite and pmd_mkwrite on riscv Signed-off-by: Deepak Gupta --- 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..933c5f23ef73 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 (arch_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 (arch_is_shadow_stack(vma->vm_flags)) + return pmd_mkwrite_shstk(pmd); + + pmd = pmd_mkwrite_novma(pmd); + + return pmd; +} +