From patchwork Fri Nov 11 21:58:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 19013 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp985427wru; Fri, 11 Nov 2022 13:59:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Oo9pY/JIoPRV+LPzZhQhSwp3Ushr8OHqN8N1H0ZIVOx8lg4ZY5R8pvZwRr2ZfcUggoQNC X-Received: by 2002:a17:906:a1d2:b0:78d:b7b3:8115 with SMTP id bx18-20020a170906a1d200b0078db7b38115mr3133288ejb.699.1668203993568; Fri, 11 Nov 2022 13:59:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668203993; cv=none; d=google.com; s=arc-20160816; b=O/spyt68ISJn9+q8r9p2NJcrUUmqDgSNc6vELVvHrCr6zzCJLaiFiEEHkRRjY1AJI+ gdNhW9j+RfpOOiki1zUaOK6MxYWOmSERjeeWgOrax+BnD1vweJ3jfssAFlw6Y+DT+ly6 INBQkfeKdFifQm8V4zyAgojv1j2MwcdM7rJx9g4wpm64QqzfWGNGaNIeR+F+OVYUhf3q 7tM037WBzaJzG2WtMR4CNzhLy1sKSN2eLCWqmJR0XaFTcBuzYl4Pr6golhId6dCMInEA lEB9zjv3QqDBxBAJ4/rn5WvuMcrvjoSRjNldmSyvmi+4CNF1JWy7L9Xs4MZ2/FjeX6pC glXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=BCUnFSEgXqFPUUW5UerPVfg/BV4gyo2gPkqS6wbdVxw=; b=eQHO9yxBIgF4H4DYfmc3vVK95DlFYpo3el41YGNus5hqolkQvg430GzQ7mwujv7njg TbVveLKiJmudsoH78Z3PMVCjPGOz7dMa3Qp5/XLZtrjHqpIQdvCXvBAFZAuspl+JFn9R 084It8jugbV/6kYsvuDF82b379/XvbmM3U1i6O9o7v2FGw/fpA04k5WPbb3/0A+27dJv x3wDfeSis1qc1TNPBZM/jv46XL0sJDPAGvUhcMb/6NDYIQ04DIw/ztWDwFL4HdpdzU97 jW316qxVeOm53b1J1J0iQ54g6DRg5990DM0KloqdP2ceGlyY/t9ykFTSlM4IoWWTQ7k5 azpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Z7sGRkvG; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z1-20020a50cd01000000b0044ee4ec8915si2812923edi.39.2022.11.11.13.59.26; Fri, 11 Nov 2022 13:59:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Z7sGRkvG; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234246AbiKKV7G (ORCPT + 99 others); Fri, 11 Nov 2022 16:59:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233927AbiKKV6c (ORCPT ); Fri, 11 Nov 2022 16:58:32 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CCEEDE85; Fri, 11 Nov 2022 13:58:31 -0800 (PST) Date: Fri, 11 Nov 2022 21:58:28 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668203909; h=from:from:sender:sender:reply-to: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; bh=BCUnFSEgXqFPUUW5UerPVfg/BV4gyo2gPkqS6wbdVxw=; b=Z7sGRkvGKlpiO/4in/mRL4MPB68FNH+jzdqm8oT/I2x+S2BK2FnGHDKwjFdxTNp9BIk0lb zLwgy9efPsepHTFoEricyVXYYnWYT/7SZJ2SKMYY9VKQntjF7XyQHrkLemEWmrkzMDvWBK UmVqswnffmI4ZKvrPuzJ5TleNPumbYzI2NUhLX5nYOpddRTsAK5OHCggwoOdssJ2dP5pBA ZnIwA28T9HFnOu9l68n4efm3MyREZbbGhO0PkVDaYKqse+3SL4n4WVXhAJkQ45h8Emo6ZA XEL2yBOOkJ5qPtI5bl+KfLEKpnaz9OAK8922PPVYDufOigmzT/8lSaWI5whF9Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668203909; h=from:from:sender:sender:reply-to: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; bh=BCUnFSEgXqFPUUW5UerPVfg/BV4gyo2gPkqS6wbdVxw=; b=oHgC8xMUmW4UnN/tjkxUJVAM8wqD6FZVwfaDE4HyZNdW+LTkqIusD0hLfRGwmGgQTCA+Zw lhVxmcK0P776HZBA== From: "tip-bot2 for Kirill A. Shutemov" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/mm] x86/mm: Provide arch_prctl() interface for LAM Cc: "Kirill A. Shutemov" , Dave Hansen , Alexander Potapenko , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <166820390853.4906.11890562076114153914.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749238670370884531?= X-GMAIL-MSGID: =?utf-8?q?1749238670370884531?= The following commit has been merged into the x86/mm branch of tip: Commit-ID: 6b38f4411021e3e63f541925c3cb5d7fed983ca1 Gitweb: https://git.kernel.org/tip/6b38f4411021e3e63f541925c3cb5d7fed983ca1 Author: Kirill A. Shutemov AuthorDate: Wed, 09 Nov 2022 19:51:31 +03:00 Committer: Dave Hansen CommitterDate: Fri, 11 Nov 2022 13:28:08 -08:00 x86/mm: Provide arch_prctl() interface for LAM Add a couple 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 Signed-off-by: Dave Hansen Reviewed-by: Alexander Potapenko Acked-by: Peter Zijlstra (Intel) Tested-by: Alexander Potapenko Link: https://lore.kernel.org/all/20221109165140.9137-8-kirill.shutemov%40linux.intel.com --- arch/x86/include/uapi/asm/prctl.h | 4 ++- arch/x86/kernel/process_64.c | 71 +++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index 500b96e..a31e27b 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_64.c b/arch/x86/kernel/process_64.c index 6b3418b..b8f2558 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -743,6 +743,66 @@ static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr) } #endif +static void enable_lam_func(void *mm) +{ + struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm); + unsigned long lam_mask; + unsigned long cr3; + + if (loaded_mm != mm) + return; + + lam_mask = READ_ONCE(loaded_mm->context.lam_cr3_mask); + + /* + * Update CR3 to get LAM active on the CPU. + * + * This might not actually need to update CR3 if a context switch + * happened between updating 'lam_cr3_mask' and running this IPI + * handler. Update it unconditionally for simplicity. + */ + cr3 = __read_cr3(); + cr3 &= ~(X86_CR3_LAM_U48 | X86_CR3_LAM_U57); + cr3 |= lam_mask; + write_cr3(cr3); + set_tlbstate_cr3_lam_mask(lam_mask); +} + +#define LAM_U57_BITS 6 + +static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits) +{ + int ret = 0; + + if (!cpu_feature_enabled(X86_FEATURE_LAM)) + return -ENODEV; + + if (mmap_write_lock_killable(mm)) + return -EINTR; + + /* Already enabled? */ + if (mm->context.lam_cr3_mask) { + ret = -EBUSY; + goto out; + } + + if (!nr_bits) { + ret = -EINVAL; + goto out; + } else if (nr_bits <= LAM_U57_BITS) { + mm->context.lam_cr3_mask = X86_CR3_LAM_U57; + mm->context.untag_mask = ~GENMASK(62, 57); + } else { + ret = -EINVAL; + goto out; + } + + on_each_cpu_mask(mm_cpumask(mm), enable_lam_func, mm, true); +out: + mmap_write_unlock(mm); + return ret; +} + long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) { int ret = 0; @@ -830,7 +890,16 @@ 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 - + 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); default: ret = -EINVAL; break;