From patchwork Mon Feb 13 04:53:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13137860 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 1DF0AC64EC7 for ; Mon, 13 Feb 2023 04:54:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7871B6B0075; Sun, 12 Feb 2023 23:54:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 737536B007B; Sun, 12 Feb 2023 23:54:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B13C6B007D; Sun, 12 Feb 2023 23:54:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 483646B0075 for ; Sun, 12 Feb 2023 23:54:21 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 090AE12131D for ; Mon, 13 Feb 2023 04:54:21 +0000 (UTC) X-FDA: 80461052322.21.833A52E Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf27.hostedemail.com (Postfix) with ESMTP id 437AE40007 for ; Mon, 13 Feb 2023 04:54:19 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=K5WRT8kv; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.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=1676264059; 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=8YCem4BrJJv8uMiBliK+eUgJsgA757fo9l+siWv19bQ=; b=xrWCELWy/WiuoYSEJnUrNRI8+l+p+c/jR0FKvoLQDcMCI3vE8X2yfri6OA+Msq3/W8kuDx jUpWllAhKGlnLVN5X3ll381HIXn4EU4aYqxk/QkE9hRDNhdrLuR6aXW55xPAG6eD8mvrpO UImGEQEGAynE530ZpArtWeExHZj5s4w= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=K5WRT8kv; dmarc=none; spf=pass (imf27.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676264059; a=rsa-sha256; cv=none; b=XOj8KaZb+QkRIox6T6wSAcm0XvoxJMQ7iFe5/Y6pgQgIX1EvfnoyDTzRpCFYqxmeiQ7xK6 r5LGAc35wdQZtV6D+4ZNH9/ISADsx8VZqRIm7WVSOtlDpw5s78uWh2S1pO8pbFgIOprpST TTiLn35Dvu0fpDgE8Av4YmTYEGndLVg= Received: by mail-pl1-f170.google.com with SMTP id h4so4467638pll.9 for ; Sun, 12 Feb 2023 20:54:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; 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=8YCem4BrJJv8uMiBliK+eUgJsgA757fo9l+siWv19bQ=; b=K5WRT8kvkrTRnxFXNxNWmZZe1eWQeiMi1qunE1LIKywvna8j3v5SzYl1jChj3QeZjI oNQVdHcccQpG2WuR1gzQg9kq/+0VuzgdVWD7JtRLTBXEFTOX1GEgMl7e5WBjD6OfQPdd 71LTAu0tFhni4ZSn88ImId4fpVYObX1bY6JW37HXCp7hvg9l286KFURTu9ooi1NBKqLt u26MG8kM25n8svLfFhRQp8B3l5+xVXSXRfyHCQXaBio+kKCjC+Q8QLGXOyPk+PiaEIGE UxpBMq4IRtaJ3U/MEkWjmDf10E4N70sMBA71dAXlsLakRYCt8pcisJr17siP90QETbWq 4NMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=8YCem4BrJJv8uMiBliK+eUgJsgA757fo9l+siWv19bQ=; b=QZ3NJJme6DgZq2ZQsjkqJrmLnXk01AXpy3d3l6XrCd+ohWj42HEuRs1pePEv0rO5BB Jyl62+RquC5vbFp4lPvFnetX4OXGBk1iBSmnRTegdKHrh9hNt8WND5YAxj5qK0Db/yKj HqoQDzYxUUo93r1OkaRPO2K55hpAnMGKPAiHyV/4i/Y8YpPol33Yagv0aCoREsXe9bEv 7PLJ04VhySDo1hR7Jt+HyntTV5/9XF970hFsHG2xa+u0r98FdBhvoWHmRl/8LSSGsoZW oZax09p3u+u32ANLXTCrNIsTrauOqDQKjB0Zwbk6wPQ6wEcvUpU7cZeC8Pg832425I6l UcVg== X-Gm-Message-State: AO0yUKVqgfNmrwFmZSNdfewgMcYfIQ+yx9VeYUy7s39a9CKHanhvovVl nkDZkNQ0jMIa77a55LLImbPmiw== X-Google-Smtp-Source: AK7set9FL/syEzV/2KBqufzNCOsrJUvHtlRk/LJL1zmvxtRWPEp1GVPCimz+TSI3kDQUud1c6m89VA== X-Received: by 2002:a17:902:e74c:b0:199:2a36:6c3f with SMTP id p12-20020a170902e74c00b001992a366c3fmr27035701plf.6.1676264058174; Sun, 12 Feb 2023 20:54:18 -0800 (PST) Received: from debug.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id e5-20020a170902784500b00189e7cb8b89sm7078303pln.127.2023.02.12.20.54.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Feb 2023 20:54:17 -0800 (PST) From: Deepak Gupta To: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Andrew Morton Cc: Deepak Gupta , linux-mm@kvack.org Subject: [PATCH v1 RFC Zisslpcfi 11/20] mmu: maybe_mkwrite updated to manufacture shadow stack PTEs Date: Sun, 12 Feb 2023 20:53:40 -0800 Message-Id: <20230213045351.3945824-12-debug@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230213045351.3945824-1-debug@rivosinc.com> References: <20230213045351.3945824-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 437AE40007 X-Stat-Signature: oc4j1pe7bqsmr1y6p3bstcegoin3itrg X-HE-Tag: 1676264059-895284 X-HE-Meta: U2FsdGVkX1/zRVTudh6TfyulD7R60RistfQIgzKxIoKAcln6r/Tt1E3kWDuIfSsv4zeS5aNXgHJKSTafxbxF/zQvExXznjyxGjXpxtk74ODVsjUPRWBoZTLYFzHaetY8F02RUkGdb0GuqvxpET/snGezdGc85vxYb6zzdOvASeJxnwqu15FHhhnucccbTylm/CkzoIQxrjpQE3nPKpVkq/U/Dg6cRuax4TPDvw0ziwwvjm9jntszgAtS8LpA6eiIqn1JtLvNaDlHCAIMoEoeDbzEaT5VJ7oh99SO3s1Y9YrAZYhUUtL3QW0Jcp7pyVVDxZP4YBI4I9n0o6fhghkWJWbI/RZ1yp5GebNb1ie53yXs4jtw/bNMVZFR6Y+HyYfGWoSe5HBHfQpOgyuw9fmxBrqBt24i7Pbzcbqgpw3Kl+ud1iqIdHgFZVVwsexz8rpQHuIl22DCALW+cjXv5m2TjuEE7VhrCqyURoSkFQXXmxxfFeZzyos7JwNRs6QfO9fpkPY7aCuKAl9Yajr5Aztqa7PEBDnj3VmsZIgIS7W3qn/5cBcKl9JP3rM3mGXQCuatcvvDH1ogTrpPyJsKRYoBBY1QlIZGpe/I/5ziqtWlyK4LYOZe494BdHZp8J31OybIq9KA9JDRAx07yndW/WrOPezC+w4wSB5thydAVJlwAKYGWO/KdQQ3OD82PYZkkAd5gCOQ3JwrIDGkweGIoVPWa7cMj76OWFx+G6cq6ZR3vRgkN+xc+MuFHl151h4AuEblAYBb2k3JpJWwFikAOjNgH9SF0hMXINCS2kbBKxvlrcx8Bb++ENpdrCxW0B8QwPik/W3D0mkdqqxK2COByya5605SEyeuIcDiWbodRNKV96150mu1uNHY5nwbU6aYJDJqOhHJPpYq9380YRSX7UajMLhN1QshoPSMR/yFM4UfXUo0h7cdmmZ2Ea0FleTi/K6KM7GfsPPXHVcJ1eA1E0e jiO4Byan NsldfBDPw0a7xTOQtXzEP5GHEKfa1zsdltio1g2GngzIF5DQbiuNcGtIxggcGSBfVg2WkOrp9tMwq2ByvSONGDIkOWQQRTKPtEdco5/OB6B/rLap3QGFbM2ytRzi2YWIyY65IQWm7KRwd5Ap2FykFz5hKMyXD/ipXQFvH+r5Ltg1YBE+5wifiQ6LHQx7Qo3g0qC2G3WjcIxtFefZbxlmP6mpeqN+jm/4XRrj9+3czW2H59HK3VLTxM8Bofoz2EJMB667hiMqH0rTdLW1+S44inxF0XX7XSWFRNJQeCiOsWMDeH1PWP5HxXozC4I5ee2iQeds8xuqhP8W9tGqCvuYp1f7qV/5JOBc0pB5+3h/QZ8232u4jcu73DwU7v0N7WCUmCLHQv/DnTcpzyFcafTKm8gzO6P7SOX8AdZGDcWH6LJL9Wq2pd9XsVhspyNxbTTcFGXHm1P1wmAeGFdP4ceEtFRllCRua2gc7W3Ph8UgkKxzpoTuejpSdaUQKXVPgwtN2+HMY 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: maybe_mkwrite creates PTEs with WRITE encodings for underlying arch if VM_WRITE is turned on in vma->vm_flags. Shadow stack memory is a write- able memory except it can only be written by certain specific instructions. This patch allows maybe_mkwrite to create shadow stack PTEs if vma is shadow stack VMA. Each arch can define which combination of VMA flags means a shadow stack. Additionally pte_mkshdwstk must be provided by arch specific PTE construction headers to create shadow stack PTEs. (in arch specific pgtable.h). This patch provides dummy/stub pte_mkshdwstk if CONFIG_USER_SHADOW_STACK is not selected. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 23 +++++++++++++++++++++-- include/linux/pgtable.h | 4 ++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f857163ac89..a7705bc49bfe 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1093,6 +1093,21 @@ static inline unsigned long thp_size(struct page *page) void free_compound_page(struct page *page); #ifdef CONFIG_MMU + +#ifdef CONFIG_USER_SHADOW_STACK +bool arch_is_shadow_stack_vma(struct vm_area_struct *vma); +#endif + +static inline bool +is_shadow_stack_vma(struct vm_area_struct *vma) +{ +#ifdef CONFIG_USER_SHADOW_STACK + return arch_is_shadow_stack_vma(vma); +#else + return false; +#endif +} + /* * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when * servicing faults for write access. In the normal case, do always want @@ -1101,8 +1116,12 @@ void free_compound_page(struct page *page); */ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) { - if (likely(vma->vm_flags & VM_WRITE)) - pte = pte_mkwrite(pte); + if (likely(vma->vm_flags & VM_WRITE)) { + if (unlikely(is_shadow_stack_vma(vma))) + pte = pte_mkshdwstk(pte); + else + pte = pte_mkwrite(pte); + } return pte; } diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 1159b25b0542..94b157218c73 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1736,4 +1736,8 @@ pgprot_t vm_get_page_prot(unsigned long vm_flags) \ } \ EXPORT_SYMBOL(vm_get_page_prot); +#ifndef CONFIG_USER_SHADOW_STACK +#define pte_mkshdwstk(pte) pte +#endif + #endif /* _LINUX_PGTABLE_H */