From patchwork Tue Oct 22 01:57:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13844962 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 18CF7D17157 for ; Tue, 22 Oct 2024 01:59:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A126A6B0092; Mon, 21 Oct 2024 21:59:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C1E26B0093; Mon, 21 Oct 2024 21:59:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 862FD6B0096; Mon, 21 Oct 2024 21:59:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 22C5F6B0092 for ; Mon, 21 Oct 2024 21:59:20 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A027A161C75 for ; Tue, 22 Oct 2024 01:59:01 +0000 (UTC) X-FDA: 82699580376.23.983B99F Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf10.hostedemail.com (Postfix) with ESMTP id 040FDC0019 for ; Tue, 22 Oct 2024 01:59:10 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=bPQzSfvh; spf=pass (imf10.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729562208; 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: references:dkim-signature; bh=e65ANAnizaofFjgpIDXOaUasil0HVOGTLixFGnIEx4s=; b=SXdNMa1ejm+4/U0WtOGo158i9SGue4HJN38VkmVY7HoFggIfQ7NATTKCJkXl1sXW+eRch3 pyipCrxn/zEJmpGAjUh0jf2H7vOZk6RtLWd0fSy6HI38WbwEPTSVmzG6Owt+X8JLOPI6CE NqrmjKcqGQy/Q5uYyTOJiFZAfaBLSsc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729562208; a=rsa-sha256; cv=none; b=jkdOiGe+q/YlQtkcB9nu2ozf/qjL1uJwrAAjr/lowxIKMcG4skxqAOu8wc6BlATSTQmWFB 4TY35cZSClURHOdaHkoriUeTkuWCrpBUtqEU8b0Uy+bemqARpZfhLGbm9/d56dTlNE9cI5 OdiIAGhsW0iNbvIGQbmr+YYNXYD98LI= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=sifive.com header.s=google header.b=bPQzSfvh; spf=pass (imf10.hostedemail.com: domain of samuel.holland@sifive.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=samuel.holland@sifive.com; dmarc=pass (policy=reject) header.from=sifive.com Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-20cdbe608b3so41712655ad.1 for ; Mon, 21 Oct 2024 18:59:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729562356; x=1730167156; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=e65ANAnizaofFjgpIDXOaUasil0HVOGTLixFGnIEx4s=; b=bPQzSfvhB5kNk1nCGNNT7au4AqkAIDZcF1wF9X2CzRObinCSA4z32DomHOu1YBx25Q Y4+OgSv8SexYx1uYZ59g5iRHi4NgVTeYjhmIRcd7aSRD+pKw5TDc7viMVwtuJ11oIzMW sFl/HWjg4RcjXypfBXGBmJwjQzUUeL6MDoM/k14Z7sQs89ZjFrqS2KA35I5vLwbp1a5C 2uWrh6h+o8x6asN4DgIyLTNz5QzhsfZ1ccNmGwd5aPn7xQeRJfeXrWsFWQdYpjVIRmWN Nxt4sncdEqigO+dgamyRIh2BkVRSgkH/n3knYGCVet2ELmME4L9brQDBbXsBsjG79aH3 fzpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729562356; x=1730167156; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=e65ANAnizaofFjgpIDXOaUasil0HVOGTLixFGnIEx4s=; b=nflW6WNNTH8qgEGYpBp+Lp+4go7aSzYKMlpx2MPpAiDlOeV/x6rJqYF/tWLQYWJe62 JhWZoWCaKnZ5UTB7XmzfwJ+kP7X1L3sBF20ndhcixO5WDf21YGPIt6ISYH4/G/3Nz/Nz d5JwxtvFNAlkWAwEk8KaXYeFIxPXQa6KTStEUsBtp5KhZaBC4gswKj92hgQv+cGTLP99 viOzy1vo5R2lrBv3/GdYBOVi0ahoSC3GevsIG1Vlcl4prThN2wOXkq9U2M+2EB1nD1l0 bn+u4K9ZYreMruaQerXyMJjUh+KCLWwVzsDbxvDoMiE/OHDltIWi6QgUcT/LXXhieLWo 9FzQ== X-Forwarded-Encrypted: i=1; AJvYcCWgJr82O+4Td1XLP3j3gEs4cA63Gu2iTawcXpM0NUwBw2I9UBHYh61PRsNDv54DgTsYLki73ze9bg==@kvack.org X-Gm-Message-State: AOJu0Yyk4HsaKZRtkUe4QI7inJJftRHB5mOWmZGWDF6vnVKNuBWfo7HD Tt88r+ZUIm8VI0wuXj//c0UZ5xsC/wUQ4GQgcF2yaJeF1KejCTq/hSDiky+74CI= X-Google-Smtp-Source: AGHT+IEXsyYj+KgjHmYUKoNKrCcxY4nPfOvA/Yp1hV7DCU8lwbKfMca2QJF0T6KgMVKIPzFYrhGr9A== X-Received: by 2002:a05:6a20:d81b:b0:1d9:20cf:2c24 with SMTP id adf61e73a8af0-1d96b71566fmr2363833637.29.1729562356331; Mon, 21 Oct 2024 18:59:16 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec132ffdcsm3600710b3a.46.2024.10.21.18.59.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 18:59:15 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com Cc: llvm@lists.linux.dev, Catalin Marinas , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Will Deacon , Evgenii Stepanov , Andrew Morton , linux-arm-kernel@lists.infradead.org, Samuel Holland Subject: [PATCH v2 0/9] kasan: RISC-V support for KASAN_SW_TAGS using pointer masking Date: Mon, 21 Oct 2024 18:57:08 -0700 Message-ID: <20241022015913.3524425-1-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 040FDC0019 X-Stat-Signature: huupwcb8ebh8j7ckgq1iwurucidsi6iq X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1729562350-325677 X-HE-Meta: U2FsdGVkX1+xr+HY+StTdOifzB9CRHd1VLHWEOPuyk4rRUC02q3ybB3HRaZNVuaKgnygeXpEBh9EArPhdCYamv01XbhExEdTJL0pqBdyP9IWToqfRK6494vfgCcSvX7qMFlTGUSXZqudN8PBKWWcYiHEmoAEIt5JqEQAcPeV3IhNhnacxTiv0YzI7n0YVi1iE/4yGvPTJC4NKRbZffhpMEvXVbzV497qQlYPyVN0pA1r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x6sh/oisyHkcSwabYOHp5yHwqDK4mDl8xGaiCUx9lJAaORVHDjxK4QV6HhVjFWnV+t6F/ze/PRmhGYy7EJTUoWBRIoi1mDqHrqJcnwV6W1NYM1jHQwwUPrycztuhJYM1ga78wfEOPxm4Rc7tZtWDJ6gEeWGaDVcj60r9Hv 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: This series implements support for software tag-based KASAN using the RISC-V pointer masking extension[1], which supports 7 and/or 16-bit tags. This implementation uses 7-bit tags, so it is compatible with either hardware mode. Patch 4 adds supports for KASAN_SW_TAGS with tag widths other than 8 bits. Pointer masking is an optional ISA extension, and it must be enabled using an SBI call to firmware on each CPU. If the SBI call fails on the boot CPU, KASAN is globally disabled. Patch 2 adds support for boot-time disabling of KASAN_SW_TAGS, and patch 3 adds support for runtime control of stack tagging. Patch 1 is an optimization that could be applied separately. It is included here because it affects the selection of KASAN_SHADOW_OFFSET. This implementation currently passes the KASAN KUnit test suite: # kasan: pass:64 fail:0 skip:9 total:73 # Totals: pass:64 fail:0 skip:9 total:73 ok 1 kasan One workaround is required to pass the vmalloc_percpu test. I have to shrink the initial percpu area to force the use of a KASAN-tagged percpu area in the test (depending on .config, this workaround is also needed on arm64 without this series applied, so it is not a new issue): masking, the kernel still boots successfully: kasan: test: Can't run KASAN tests with KASAN disabled # kasan: # failed to initialize (-1) not ok 1 kasan This series can be tested by applying patch series to LLVM[2] and QEMU[3], and using the master branch of OpenSBI[4]. [1]: https://github.com/riscv/riscv-j-extension/raw/d70011dde6c2/zjpm-spec.pdf [2]: https://github.com/SiFiveHolland/llvm-project/commits/up/riscv64-kernel-hwasan [3]: https://lore.kernel.org/qemu-devel/20240511101053.1875596-1-me@deliversmonkey.space/ [4]: https://github.com/riscv-software-src/opensbi/commit/1cb234b1c9ed Changes in v2: - Improve the explanation for how KASAN_SHADOW_END is derived - Update the range check in kasan_non_canonical_hook() - Split the generic and RISC-V parts of stack tag generation control to avoid breaking bisectability - Add a patch to call kasan_non_canonical_hook() on riscv - Fix build error with KASAN_GENERIC - Use symbolic definitons for SBI firmware features call - Update indentation in scripts/Makefile.kasan - Use kasan_params to set hwasan-generate-tags-with-calls=1 Clément Léger (1): riscv: Add SBI Firmware Features extension definitions Samuel Holland (8): kasan: sw_tags: Use arithmetic shift for shadow computation kasan: sw_tags: Check kasan_flag_enabled at runtime kasan: sw_tags: Support outline stack tag generation kasan: sw_tags: Support tag widths less than 8 bits riscv: mm: Log potential KASAN shadow alias riscv: Do not rely on KASAN to define the memory layout riscv: Align the sv39 linear map to 16 GiB riscv: Implement KASAN_SW_TAGS Documentation/arch/riscv/vm-layout.rst | 10 ++--- Documentation/dev-tools/kasan.rst | 14 +++--- arch/arm64/Kconfig | 10 ++--- arch/arm64/include/asm/kasan.h | 6 ++- arch/arm64/include/asm/memory.h | 17 ++++++- arch/arm64/include/asm/uaccess.h | 1 + arch/arm64/mm/kasan_init.c | 7 ++- arch/riscv/Kconfig | 4 +- arch/riscv/include/asm/cache.h | 4 ++ arch/riscv/include/asm/kasan.h | 29 +++++++++++- arch/riscv/include/asm/page.h | 21 +++++++-- arch/riscv/include/asm/pgtable.h | 6 +++ arch/riscv/include/asm/sbi.h | 28 ++++++++++++ arch/riscv/include/asm/tlbflush.h | 4 +- arch/riscv/kernel/setup.c | 6 +++ arch/riscv/kernel/smpboot.c | 8 +++- arch/riscv/lib/Makefile | 2 + arch/riscv/lib/kasan_sw_tags.S | 61 ++++++++++++++++++++++++++ arch/riscv/mm/fault.c | 3 ++ arch/riscv/mm/init.c | 2 +- arch/riscv/mm/kasan_init.c | 32 +++++++++++++- arch/riscv/mm/physaddr.c | 4 ++ include/linux/kasan-enabled.h | 15 +++---- include/linux/kasan-tags.h | 13 +++--- include/linux/kasan.h | 10 ++++- mm/kasan/hw_tags.c | 10 ----- mm/kasan/kasan.h | 2 + mm/kasan/report.c | 22 ++++++++-- mm/kasan/sw_tags.c | 9 ++++ mm/kasan/tags.c | 10 +++++ scripts/Makefile.kasan | 5 +++ scripts/gdb/linux/mm.py | 5 ++- 32 files changed, 313 insertions(+), 67 deletions(-) create mode 100644 arch/riscv/lib/kasan_sw_tags.S diff --git a/include/linux/percpu.h b/include/linux/percpu.h index b6321fc49159..26b97c79ad7c 100644 --- a/include/linux/percpu.h +++ b/include/linux/percpu.h @@ -43,7 +43,7 @@ #ifdef CONFIG_RANDOM_KMALLOC_CACHES #define PERCPU_DYNAMIC_SIZE_SHIFT 12 #else -#define PERCPU_DYNAMIC_SIZE_SHIFT 10 +#define PERCPU_DYNAMIC_SIZE_SHIFT 8 #endif When running with hardware or firmware that doesn't support pointer