From patchwork Tue Oct 1 16:06:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13818416 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 A69F6CEACE4 for ; Tue, 1 Oct 2024 16:07:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26F282800A8; Tue, 1 Oct 2024 12:07:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F656280068; Tue, 1 Oct 2024 12:07:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 022AB2800A8; Tue, 1 Oct 2024 12:07:38 -0400 (EDT) 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 D8E1B280068 for ; Tue, 1 Oct 2024 12:07:38 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8C1A41613FA for ; Tue, 1 Oct 2024 16:07:38 +0000 (UTC) X-FDA: 82625513796.29.81A350E Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf05.hostedemail.com (Postfix) with ESMTP id A6808100003 for ; Tue, 1 Oct 2024 16:07:36 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XiM+mZD8; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 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=1727798730; 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=cWHjpqY48uN4UnK6KdtPB5JuLwjX+c/1+0qjXDmz45LCayLIjsFjjLPK7l5rxE6AKxfT8A ruic3EndrugE2C99yDXneFyMq4vRgzrVLGeHzOLq13kz3VO40sBJePuipMHKcn/3r/siVe Gd14HKnEWNPsNJ7IihwTuFIAs2DG49w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798730; a=rsa-sha256; cv=none; b=OqH2EDqum+SAyzDRNl14q+J9THZ1hlY86+aesTyeJGvAxwN3ZZG3uvltt14tMI96NlrUST M2GgjyBPz7sONIITXaVkqRIFufyFqXLoPaPnWDJ2ouPieGkCfOn5PhQ8LXx8R9SkwpFMmB nZYvE0uTvWCvjHR5v3OHnUVrCznjvBI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XiM+mZD8; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2e0a950e2f2so4552092a91.2 for ; Tue, 01 Oct 2024 09:07:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798855; x=1728403655; 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=XiM+mZD8hm+nzdLSOB1aDldaCy/YXPfxkPYlua7Z9riUIG5e4/AL4IIDRq9SHhgvkf S1E+ilYfCl7281x0ogFNPYg3rVu9lEmEdZJbLHk1uwXbOqmUAspimr7BEI6OK9bqnCUV SA2AzOLf/3bCA59LSlikgqJvkfHirFCWqYWAo1wIE5twKRLIyMhlOczyGb/9EIkILK9v l6EDudk9z4kAL06zGr+7qwLQzEr0ZehVFeys/WR7RWDSbeySq29lRF+Cwh09WrInaYJR vlgQgkze3yAiKUAxqdZY22hpYNY59vz8CMGvlI9Izqfa83HNaYPrOQs8/JKWFTC45KHI HOWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798855; x=1728403655; 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=AXWEZNoXOgezFfFkfv+TIkX7BXCKFWp+zSAd0CbOvrushgX3GN0x4c4gYtK/bDF4RU PoelVvuX9Y5QnD0vOhJUEAk6UD46iSYC8vqHHCqvxXpHQ4z3s3PH9R8nVHrCSK8g8pq+ plRoBQb4akrFxp6B/xrFYi91ZlUfMh+9h5594I9zKFumvwAVB2ZEJvC5K9jjfipJRIKm bm6TEElkfAMm/m7g5BBgvQxbZysM4IGsvMNb9aBarOqKmeIDITS27KdaVEyYoCCefRlV H1qjqoSEEIuy+b2pBTSEUFWFxdSyhGnEl8im1WuwJfOfBheB0pzakHFcNRsYwZDEm998 gAzQ== X-Forwarded-Encrypted: i=1; AJvYcCViQA3n+N2FrNQ+sVwMhhAzOXzBhUMf7B6PcBbJal3YswhSfFZlDh+E/Pu7iNwgdVibk/Mbj0EEpQ==@kvack.org X-Gm-Message-State: AOJu0Yx/gy87u4dAbXSlYE/xJ1M5BOmA7GJD96+dlmVDw+c5r9O4Tx7f vVoTWPR03gH79ariDz1hEHvcG4x3bySqVmYYPxh3BQ3r6v2AVRCki+H/zLYzf2g= X-Google-Smtp-Source: AGHT+IF8xhnYD/pqfXYfj6NnlMKMMkkR64MiBO6DNyNWgrVcZh53ylTJhkG5X1f7jZZJUwYOw4ZShQ== X-Received: by 2002:a17:90a:77c4:b0:2e0:74c9:ecea with SMTP id 98e67ed59e1d1-2e184681664mr241500a91.10.1727798855416; Tue, 01 Oct 2024 09:07:35 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:35 -0700 (PDT) From: Deepak Gupta Date: Tue, 01 Oct 2024 09:06:18 -0700 Subject: [PATCH 13/33] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241001-v5_user_cfi_series-v1-13-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@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-Queue-Id: A6808100003 X-Stat-Signature: qeiuhzikf8mycsffq74f448uq5hidk7k X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1727798856-652783 X-HE-Meta: U2FsdGVkX1/cTpRq5DcWRcpXGRWeui9yIJ4TKnXAgCky7iROwEPKzE8y3yy41yDmFt/UuWEZsZO/3o6oFgRggDrjujMiTMeun/PrHMwJpkGTHW0DNgWkf+RCrcJBueJ+nfbvqVg3WhN2O22/cABl1sTk2Eycyt1mSIZa+S0+TKzNkncK9bqBcb/E+N73/19rfoMdOQnyLVinm2FluMQMlOqzO+JeordWyOovw1PmwRUgvt4dRKLE6Pw4Sn6GGeSOOBw2ZmmCOfcK64V+A2j4/Qxh7y8pv6NWG//oqZE8qLo2H0qRn2nFe/dE/Fi94IXZGdvvkms/SoxcqGPSBvnvBOJN1RMsTKwmtrI2BrRqBXUyHsL84LI6VSca60qAGK6mzVrzLSyDKiYF5FS/S97T7llMsON04gkQLJhWx0PMfwhax001XnNBEy91AnGsxG+MBCsfAM2Oqwf6PuOv02s4V5EjYxq7H+7tyalPZfq0x68KijIg+muAb4PEF5iWy8WO5u5Ksaw+FMtpdg0Dhz6z2esXicwmGWkHrrGaNJvEdq/XUQrNUvLh3UgBa+ugACMGtsT9rVJRLzRJOULpFbwRhbPZsajH2iQgMm+h90DzBVHu7f+ntVI+2bcRuqmUN/HTO8JALkgV2mweYWh7tmpf/O57EioMpyYh4ST30IQxOjQC5mEVV8UISOuhtKjan+UBRcmQ7btVUk3O/cFfvgFThOXq6oxDptYggmww0nKFXhbSHie8V/6C1ml69v/dZzV8PGWNOGaA1WlvRKeMygQjhbqcNTe5ntNQ/hLEElwoJAWmEhhpcsgRgBp8x4SawQ4ZWXD8l9ue8jiQAgwLEMo8JmVindQB6pEWCG0s2/BFBD+1bgf1IWNT5pT8/QF35pR7vcbndEitis5nptkcjjtjB13fhIieyXS+wIx/oC+N2EtsAIeM4Khf/rZ/0Sidn/LQUXUoOjj2V6ayxVRWPUh Uc/E88T+ 8EpbgeFjlP1xclyp85nrn+Y9xMGtgt2cbkmwq5sCCpZkhDtPzc4bcNS4dVlGYvj2jPlqKjh+XkGgU9GQaxzig9FEoZ8ZZ+77rHSWUwIwp/Qw0RJab5Iw1WTkgcEOQCAumxmZAD8vnEqWOMZYAEmttiSGsRVn5SQ6pQSD0Q3V/WnBETdMEevvfIwQMgEk0/Eycbo+6NdrSep7BAJvd6J0DzHu89M6A29FX3zS5qGaRl+6idF/W3egZ1CWT0/MgEIHcXJQeG81sFzWTY+bQirNvLqcIJjY7aTIF0dNLwQVpeNDSziIFunGI5+lYmOVm1bRnCvxwydznJhZWeC4N6i+u3YGc2rci4sfSGiRgbxZSRBkGxsvKzw2xotD458AytVgGofpeYJZiXlG5JfAWCcvrImpk8SJi0SNf9v22EfDpJ+iL0dG4zPgoJTbVkCUUlw0secqIWs7fLYVXPLTOiHo2rdvqSItZbQPxEZzYYZ3TmRk+Xk23/2+5tqRjwgM4NgHhhGGQcRRbpr31h5iqAIlmfe4g+phDIQOsSse6BXCv8DrGv2bYXRa+k1DPWkauJ+wApvYL5FRIFoZHY0cCRUtUVJ15n+wvmev98MDDgbgsDJ3C7JWjeF4V/I0q2Q== 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); +} +