From patchwork Sun Mar 12 11:26:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 13171300 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 C861EC6FD1F for ; Sun, 12 Mar 2023 11:26:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 066CC8E0009; Sun, 12 Mar 2023 07:26:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 014B68E0008; Sun, 12 Mar 2023 07:26:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAB158E0009; Sun, 12 Mar 2023 07:26:39 -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 CA1A58E0001 for ; Sun, 12 Mar 2023 07:26:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 890B5AABD3 for ; Sun, 12 Mar 2023 11:26:39 +0000 (UTC) X-FDA: 80560018518.20.284F722 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf10.hostedemail.com (Postfix) with ESMTP id 7E093C0010 for ; Sun, 12 Mar 2023 11:26:37 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Va3ViAMf; spf=none (imf10.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678620397; 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=SEQWBU+QfGDCbgeszyGr05YNz6negMxo/uUD30yHObw=; b=U6ZbMx0jALHfZEP4BulD/IeXGSrc4E7XjoFOXRBuYSxPPMEwXlhrtP9IGyBxogq/6r8vCe CqN9NQOfr6PHe0+UAtXU97shfOjLoE2pwp+R+S0y8qGCTYN8zJ00v+E/iILnPSP/AWAp9D 3UxhAdwBqJojRi5B1M9QklcitidslMk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Va3ViAMf; spf=none (imf10.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678620397; a=rsa-sha256; cv=none; b=1efKqijY87vPIBAWP4qA9lWGUymOvesEXzUFFwoSLhUsGmaYUaaMGGpzqM8ms1TtYCljDv aL77qhygYnkjUNwiqexKNOsrfGbFlU+kxrZ7Y1z/rc679ml+K3ZmVDSwwOsskPLXErelVC 0i8ko5xt1XsiiC/X10OtSSAW+gIlHW8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678620397; x=1710156397; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HmyZhvrnaOzMtHk3YHlMvWmwvxMx1gT3mopz2/NGNMw=; b=Va3ViAMfYzai7fBFMcRJ6tcOqOUOZyLzJ9r/XvNRrI9zxrAjehzQveWL lNKUyhT/0gc8Pw9tXri95P7iAnqVKqP4EJxpaD6rJ7+E4a1/bvT+bc85Q 877/qKd2MuO0uPrdGVMGPQPun6KgDdBb/UsfuzUzEgZOSRecq3BdmCpPa mJR0qHARsvryphPOcxKgDxVMDPwuK12aO0pZemniS1WhR/KcGAQmoIiZ+ O5UV/fcC38soTWXbXpgMP/1kOAFNh1KRowaHPZcz4w7rLbYlDA3YnkGQT 5q6limzoZEUSi2xWD71wr1ToOgt1f8gEoC/9c1zn8+9/Ht96jKJBCa1LN g==; X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="339349967" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="339349967" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2023 04:26:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="852438033" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="852438033" Received: from nmoazzen-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.219.215]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2023 04:26:30 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id A42E010D7B6; Sun, 12 Mar 2023 14:26:19 +0300 (+03) From: "Kirill A. Shutemov" To: Dave Hansen , Andy Lutomirski , Peter Zijlstra Cc: x86@kernel.org, Kostya Serebryany , Andrey Ryabinin , Andrey Konovalov , Alexander Potapenko , Taras Madan , Dmitry Vyukov , "H . J . Lu" , Andi Kleen , Rick Edgecombe , Bharata B Rao , Jacob Pan , Ashok Raj , Linus Torvalds , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv16 08/17] x86/mm: Provide arch_prctl() interface for LAM Date: Sun, 12 Mar 2023 14:26:03 +0300 Message-Id: <20230312112612.31869-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230312112612.31869-1-kirill.shutemov@linux.intel.com> References: <20230312112612.31869-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: c3gq4t4k654ywqhbrqbejxmmzc9g9dqz X-Rspamd-Queue-Id: 7E093C0010 X-HE-Tag: 1678620397-640870 X-HE-Meta: U2FsdGVkX19v9hbT8DXMG+y/i3AQ9uk/cmwNxZGeLBHd8c+KizlCx32W/tL6qWo3mFScqS7/1bWR81JTwHcckJ1no7Zqldbqdyo2eisyyv5/qroqUilTnSgkNL24+W1fBzfK8P7xP27f7+BoY8fmF6slgoY+/KBmUC+kb9vk3+jdG5OJDMmwAXKFslY5lSb0i65+XRW4O2lcPhb5k5rMEvUIE4PHmgJ3fpGnaa4XpYcuby4iAsU9Z2PmB6lhjgU3uHKq46M71xIUMv0LTjHDKVFFABho+9xlroN0OkM2fxTQbs0U30iE9a72y7GcgktmJ2QT6BtcJc6SMDno6eeKn6q/aZ+kOLP618scidYYGjiTRa2Z/G4vq9leP67xsKHm27OGctULHr0hIkw5tF8Pk6gTxNbG8fN1z5JkA/hEcIrlL1/5rxDXrUJJDFcCeyVr+zdSN391kHc5jlgY/ls24vw5hC42oTGs04v8IiKiH+xaOEisXGME0QF/NR3BGzn8rMNAdicLiB/whVJ8iFzsigM5Io4RW+rIwQYSrO3+QMFFqMFv4tG1WK8l441JxGm0sQAi4U9LEjukbJKSovEzRhX+6n68RpJalM1ktpizntXysCNwl76rkXoIZH1BccLE//B9Jou6hq1IvmZ5OchVYWkvY14+2BTKdhYMt0I7zk5CtimnQO2YKB32GWBf1OIZ/skBRbmifP+VkoTPMbggpx5cS4tlcdasycx5NKf/Q1rP2wPRnlomCFZL7//xR67ASi+XfwqZaQMYm7RqOdAT2ffZiIxfJUDjON05o+Mqt8QztPahw9DO3WdX2fYoRxpy6PaJOZl2Y6MGS52szbZvueQ5px48oxnxk9U7PY6ZI5YQSn4BKpNwoprkrHdS2rMm7/b7ewabyMhpcqOohV8QaHVpN9/vY7S8CtPYD/ltv63YOrZFNXgbRLQpznsfhKex8s2DpzV794B65V5ZqqT +MWToqHf DaDbgjhbYsnlioyffNsG/yq9lPk17tcor666ap1P8xdHwEMuenqSpp8tkkVtqZZb7c98GtfnOIfS3ekxBSHLNb/6YqiWm1kxHiXIzGUNeaZO5upPNh7oyWeOLJd3duDbOCq36Pu83fFma5rVsWqKqYlxK4t5po74Fc+bxvaIslgNyfMRieN18M+1qecnLzO9eWkSG+F1rcRmkDY9T3n+Ty1bzuH09jpFJNVYzYsJ63wjK4Kb8Dpjd2OB9T8Krub4wA/IsqKyPS2mhmxTQxrb+744wYaylwNZXdDe3uJOQCXA/QEzyLddbZoR1rOonH/3MbThjDEWXN6FcR99RudbkpL7lIm+JHOzXt3E5MB0ZXAspF5w= 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: Add a few of arch_prctl() handles: - ARCH_ENABLE_TAGGED_ADDR enabled LAM. The argument is required number of tag bits. It is rounded up to the nearest LAM mode that can provide it. For now only LAM_U57 is supported, with 6 tag bits. - ARCH_GET_UNTAG_MASK returns untag mask. It can indicates where tag bits located in the address. - ARCH_GET_MAX_TAG_BITS returns the maximum tag bits user can request. Zero if LAM is not supported. Signed-off-by: Kirill A. Shutemov Acked-by: Peter Zijlstra (Intel) Tested-by: Alexander Potapenko --- arch/x86/include/asm/mmu.h | 2 ++ arch/x86/include/uapi/asm/prctl.h | 4 +++ arch/x86/kernel/process.c | 3 ++ arch/x86/kernel/process_64.c | 55 ++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/mmu.h b/arch/x86/include/asm/mmu.h index 9cac8c45a647..e80762e998ce 100644 --- a/arch/x86/include/asm/mmu.h +++ b/arch/x86/include/asm/mmu.h @@ -12,6 +12,8 @@ #define MM_CONTEXT_UPROBE_IA32 0 /* vsyscall page is accessible on this MM */ #define MM_CONTEXT_HAS_VSYSCALL 1 +/* Do not allow changing LAM mode */ +#define MM_CONTEXT_LOCK_LAM 2 /* * x86 has arch-specific MMU state beyond what lives in mm_struct. diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 500b96e71f18..a31e27b95b19 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -20,4 +20,8 @@ #define ARCH_MAP_VDSO_32 0x2002 #define ARCH_MAP_VDSO_64 0x2003 +#define ARCH_GET_UNTAG_MASK 0x4001 +#define ARCH_ENABLE_TAGGED_ADDR 0x4002 +#define ARCH_GET_MAX_TAG_BITS 0x4003 + #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index bbc8c4c6e360..50d950771371 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -163,6 +163,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) savesegment(es, p->thread.es); savesegment(ds, p->thread.ds); + + if (p->mm && (clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM) + set_bit(MM_CONTEXT_LOCK_LAM, &p->mm->context.flags); #else p->thread.sp0 = (unsigned long) (childregs + 1); savesegment(gs, p->thread.gs); diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 8b06034e8c70..88aae519c8f8 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -743,6 +743,48 @@ static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr) } #endif +#ifdef CONFIG_ADDRESS_MASKING + +#define LAM_U57_BITS 6 + +static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits) +{ + if (!cpu_feature_enabled(X86_FEATURE_LAM)) + return -ENODEV; + + /* PTRACE_ARCH_PRCTL */ + if (current->mm != mm) + return -EINVAL; + + if (mmap_write_lock_killable(mm)) + return -EINTR; + + if (test_bit(MM_CONTEXT_LOCK_LAM, &mm->context.flags)) { + mmap_write_unlock(mm); + return -EBUSY; + } + + if (!nr_bits) { + mmap_write_unlock(mm); + return -EINVAL; + } else if (nr_bits <= LAM_U57_BITS) { + mm->context.lam_cr3_mask = X86_CR3_LAM_U57; + mm->context.untag_mask = ~GENMASK(62, 57); + } else { + mmap_write_unlock(mm); + return -EINVAL; + } + + write_cr3(__read_cr3() | mm->context.lam_cr3_mask); + set_tlbstate_lam_mode(mm); + set_bit(MM_CONTEXT_LOCK_LAM, &mm->context.flags); + + mmap_write_unlock(mm); + + return 0; +} +#endif + long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) { int ret = 0; @@ -830,7 +872,18 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) case ARCH_MAP_VDSO_64: return prctl_map_vdso(&vdso_image_64, arg2); #endif - +#ifdef CONFIG_ADDRESS_MASKING + case ARCH_GET_UNTAG_MASK: + return put_user(task->mm->context.untag_mask, + (unsigned long __user *)arg2); + case ARCH_ENABLE_TAGGED_ADDR: + return prctl_enable_tagged_addr(task->mm, arg2); + case ARCH_GET_MAX_TAG_BITS: + if (!cpu_feature_enabled(X86_FEATURE_LAM)) + return put_user(0, (unsigned long __user *)arg2); + else + return put_user(LAM_U57_BITS, (unsigned long __user *)arg2); +#endif default: ret = -EINVAL; break;