From patchwork Fri Mar 29 04:44:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610052 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 2443FCD1283 for ; Fri, 29 Mar 2024 04:46:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB10D6B009F; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A61246B00A0; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88CCD6B00A1; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 69C916B009F for ; Fri, 29 Mar 2024 00:46:16 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F2074121161 for ; Fri, 29 Mar 2024 04:46:15 +0000 (UTC) X-FDA: 81948839910.10.32D8DF0 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 380EAA0003 for ; Fri, 29 Mar 2024 04:46:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DnY956JY; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687574; a=rsa-sha256; cv=none; b=OcP81TXL4Up4Yw/jTai/1OUl0DLuob6QVrzP5smWC3vd3dHC8hQID2vGfb5nh8OvfOVLza jX772Nwh+67x0RrkyLf99sjZ7zoB7nk+6ys/DS+QAN/fwNhW0VmKbnlabtQY6IK99c0Aqa xGOJ5PifuS11zAYizyiJIsNymragJyo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DnY956JY; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.174 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=1711687574; 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=+qNedlIktUfQrTR049/Z3OzWmqNS/siPqaKIdhPjXgQ=; b=EVGjHdwIHXYe8hNrCm3hKpi28Rr0MEHaSMffQ3mWOSYUhxS1UDcfpSR87KwNgFxet3PVXX 6NTOEflDU8PHzmaXYNv+S+X2eU/56RQZrUrxOIVOZPB+xIB9GILgEldryt1zBNNSNLj8Su zEXvDS0T7Ff/VfBT70BD6k0bGbADkCk= Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3c397193878so1046992b6e.3 for ; Thu, 28 Mar 2024 21:46:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687573; x=1712292373; 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=+qNedlIktUfQrTR049/Z3OzWmqNS/siPqaKIdhPjXgQ=; b=DnY956JYOiiEH8NMqCUuMcAf4oQKHqxsjYp8DHxWIDUPXmTnaBpY9oc3TwPygf6Qld cRAc8tktN3E+2eD1vrPxO4fwQ0ED2EO+ZhSCARPRXno+MVm2IT/y4J3r0/x3yeYDQk3K IavQVoC1+dNIPYM5ZwvUOWf39qYVCXoEITWlxvPxKdApjWQIbu9JZp4HUxeW7r2AsH+a Zs3uOO103tOHjJvCX6OgWUuuVNPGSq85zb58VIQot3hVL8SNNNLSf/Eci33DrTmfF/uY KzeYk8WrGbMx84CY6lNVgXyW8a5ZiLeN9imPkcol9FbdfvKHcgWItuPcOE7dPglV85ys IuZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687573; x=1712292373; 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=+qNedlIktUfQrTR049/Z3OzWmqNS/siPqaKIdhPjXgQ=; b=QAZjs6ieofuG7yzeFuCTf4MpY+WmfLAvH8CnPpTu1K3HUtDzJNTV5/JiPBoMhOg5Eq xIlGdP1bfhkEycRtISbliM73I8jPHRhcKW4M2JENSimb2e3xCZgn9K9YUqi63nH1DMG3 4tNYid2gXzGOCTTA0J2OuuoP5eg0q82G1rR+MnRyCMifIhcEdjWqWn7wbrP3hpWusp7y XfBTIa5H8MCk5t2grI3l0s2uM1SOuy281RUX44kqgkXXVULt3CwnIyG2GWExy0Vdb5zp zj2nTdbbQ5hMxJunC5+CMjDfsm4X8Jm4bn91tNfRWh+K4iGCfD9Nnh+2zn6Q7kZG77/w Z3Xw== X-Forwarded-Encrypted: i=1; AJvYcCXEeGuY1LkartvozYGdyNegNhfd/yIYKy4KHpgpiyB7m5WkQB7UwKHW4NKKySrQVM5665ehcD3+F/K2tvRex5af00Y= X-Gm-Message-State: AOJu0Yy1o8BJH3Yx6p8WH+ZKprlGm0jnD8icYss0z4jE4yccMKW8mXFx ojaLjopUpjVubOX8Pb/oti63hRk9IgfVDul7kzQCXA0NcpyDhzV3IYvhdsXHSTI= X-Google-Smtp-Source: AGHT+IHTQWx1tR1j7s3+pvyrI5hMSSRXmYpDonpnfYPB/9nbQQQRNm8eLeO96l4IIa6gD4IvOzseMg== X-Received: by 2002:a05:6808:398c:b0:3c3:d2ca:a4fe with SMTP id gq12-20020a056808398c00b003c3d2caa4femr1622664oib.7.1711687573325; Thu, 28 Mar 2024 21:46:13 -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.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:13 -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 08/27] mm: abstract shadow stack vma behind `arch_is_shadow_stack` Date: Thu, 28 Mar 2024 21:44:40 -0700 Message-Id: <20240329044459.3990638-9-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: rspam06 X-Rspamd-Queue-Id: 380EAA0003 X-Stat-Signature: 8r8383hq4ydeobt5e87ezkc7wc9b1wd7 X-HE-Tag: 1711687574-211905 X-HE-Meta: U2FsdGVkX1/X8JWRVNtlxj4fz6fzMshmX1RLdl8XNBDlHwqOpH742iAjOF02Wkr8tMBENwZ0IwmOnLMbd89RQc0Sn7Tw1kBnO3ivdFo26aWPhPABCQZADPDPh/Sp1lyV9MRLL5J0LjBNMlbLQDZ7uDnzZEMg2MPlRB0F7Mn5itmlJfVuyzOPV32wbIwrsv4fVyPq54KZ5dxQHsLpHK8umFAHSnJ2Yyw2Qi4gOjUP4SuZXEFaJHCf2RpRnHjcjeo13fVrzvsfMZ2lp9sIUzA46Ah0hiJhima1QUQ/cVNaXI7V08Ta+XvlNlV7tEGs+DJv+GXnW4ThCM8zxxOzYQtmZNOXz8eI3dWIhB6ksz90VM3xDfhau9/uYlPjOvi03let8/yM7+7Y17E9l3Gf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zZYTP51CsxDdQGe//DvdCKf6on/5GaHIFPnRPuVUELEedB7Akn57aP1VmVxU8X2qH3SuH4iUhWqnRK2Q6Wj8XcIY22Ciup2a56 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: x86 has used VM_SHADOW_STACK (alias to VM_HIGH_ARCH_5) to encode shadow stack VMA. VM_SHADOW_STACK is thus not possible on 32bit. Some arches may need a way to encode shadow stack on 32bit and 64bit both and they may encode this information differently in VMAs. This patch changes checks of VM_SHADOW_STACK flag in generic code to call to a function `arch_is_shadow_stack` which will return true if arch supports shadow stack and vma is shadow stack else stub returns false. There was a suggestion to name it as `vma_is_shadow_stack`. I preferred to keep `arch` prefix in there because it's each arch specific. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 12 +++++++++++- mm/gup.c | 5 +++-- mm/internal.h | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 225af437ecba..9e6a4fbfccac 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,6 +352,10 @@ extern unsigned int kobjsize(const void *objp); * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return (vm_flags & VM_SHADOW_STACK); +} #endif #ifdef CONFIG_RISCV_USER_CFI @@ -372,6 +376,12 @@ static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) #ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE + +static inline bool arch_is_shadow_stack(vm_flags_t vm_flags) +{ + return false; +} + #endif #if defined(CONFIG_X86) @@ -3482,7 +3492,7 @@ static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma) return stack_guard_gap; /* See reasoning around the VM_SHADOW_STACK definition */ - if (vma->vm_flags & VM_SHADOW_STACK) + if (vma->vm_flags && arch_is_shadow_stack(vma->vm_flags)) return PAGE_SIZE; return 0; diff --git a/mm/gup.c b/mm/gup.c index df83182ec72d..a96043429b31 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1053,7 +1053,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) !writable_file_mapping_allowed(vma, gup_flags)) return -EFAULT; - if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { + if (!(vm_flags & VM_WRITE) || arch_is_shadow_stack(vm_flags)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ @@ -1071,7 +1071,8 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) if (!is_cow_mapping(vm_flags)) return -EFAULT; } - } else if (!(vm_flags & VM_READ)) { + } else if (!(vm_flags & VM_READ) && !arch_is_shadow_stack(vm_flags)) { + /* reads allowed if its shadow stack vma */ if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* diff --git a/mm/internal.h b/mm/internal.h index f309a010d50f..005761d754f6 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -572,7 +572,7 @@ static inline bool is_exec_mapping(vm_flags_t flags) */ static inline bool is_stack_mapping(vm_flags_t flags) { - return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); + return ((flags & VM_STACK) == VM_STACK) || arch_is_shadow_stack(flags); } /*