Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2508645rwb; Mon, 7 Nov 2022 14:16:16 -0800 (PST) X-Google-Smtp-Source: AMsMyM4JdXhjAWwcv4kr3a/ZKonZSX0OCWDOr3FogMTxG5VWRSC+KkIymlY5FQ7KQQceWSMpiQo2 X-Received: by 2002:a17:90a:74cb:b0:213:9b4c:ecc2 with SMTP id p11-20020a17090a74cb00b002139b4cecc2mr54960874pjl.154.1667859376603; Mon, 07 Nov 2022 14:16:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667859376; cv=none; d=google.com; s=arc-20160816; b=GPSXmKJj7UI0A8T5m7BnAS9DQGMvHClCL1Nis321vg9b1h1lMLpCbgB6Wj4KiRJMXE 1NJj5LxTQ02rzFM3VhTXxnXzwP9GSokflZGMRNjrJFnmd8NkUugf7d/t2UTy2BNR4CRB XlrSvqD6OSuZOHwSpqdaYYQH/zRoK6rJhDyjl/OVBT7WQ5Dog2GtRijJ/+OG9Oe/ovD9 WLHyGlrg+K5NCSx0wO6EMuyUoVJDIGPtZCiOlEDDC1zLWT71oU3iBgteW/4Y90QpXxQL EHsqlAINYHEhaBovMw8STZmPpxoGvgFt0KuJQNKoVRqeslmcta9bxBjlaSWR0rKVCpZE k8UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/AHPNiprwO0cZDCn9T6ZmQ2fOpYm/X1SoaXHRjW9+Sk=; b=0vSQVxSRxexx+D1Pxds31+TjPJo/KhIelygy1/WlIkAqEczq2pUH8KbrSyuOszOxMb /2ri9OTa81Xt2wMG10DqUyicnW3itOw9i46f44kwBPnoRizEUNGgXq6DWnVbkaeEtlBR aEIwv5/lEZcBfbDdmxgmySAIdyUoBNr+JO6EmB+LOja0P/ZgchfL71TwBNBjHgm5ecMy /cs4E+du8Chz031ZvbleWaNYatW/qeJD+0iC53b+/C727qXPRqvj12W6nOAjnSXMQjrT Z+Z4uHj0XdGaeVT9Qg4SeabmcJWdVV9rhcEk2xnnXtShnZEJPj5ivt1rUWBVYxh4HFN4 3lPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ACZhRB6Z; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pj12-20020a17090b4f4c00b00213bbb6751asi13039714pjb.112.2022.11.07.14.16.04; Mon, 07 Nov 2022 14:16:16 -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=@intel.com header.s=Intel header.b=ACZhRB6Z; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233506AbiKGVhb (ORCPT + 91 others); Mon, 7 Nov 2022 16:37:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233145AbiKGVh2 (ORCPT ); Mon, 7 Nov 2022 16:37:28 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18A9CD9 for ; Mon, 7 Nov 2022 13:37:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667857048; x=1699393048; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cWyRCW5aKXNWtiucVf5kkixxGLClq+OtEfuUo4LHoho=; b=ACZhRB6Zj8oClnlG2tsVALHsfQR/D6mBX4uTz0Fb7iWK1RV0Fr7W1X8+ QfgzCUuXxh3eTRDCnfjVXc1Zarxx5r3JaM0j4px8MeP4ZRNmsy4WxmCCo z1su85xbO0aoFovE842MQnrHT33IVFEIXxNRRACQEouovLYAkA8x7nTbd MwhU3ka1huRcpkPDwwvwV8tdhOItvghdkihoxSWydctmmsRAFSBO3l/p4 nyRjm+3eEpmP0s9ClMz/ppSdvXCxkJ9Pp9Qp9Sg/p2wX14Dqa8Ru/j/Lw PZvDUYnaoxJIhJCbV/HalqsJSPHpHUzmkStsekm6sdCLc8gGNEmDfzV9e g==; X-IronPort-AV: E=McAfee;i="6500,9779,10524"; a="396827877" X-IronPort-AV: E=Sophos;i="5.96,145,1665471600"; d="scan'208";a="396827877" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2022 13:37:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10524"; a="965326539" X-IronPort-AV: E=Sophos;i="5.96,145,1665471600"; d="scan'208";a="965326539" Received: from biancabe-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.251.218.140]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2022 13:37:23 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id E49C010964A; Tue, 8 Nov 2022 00:37:20 +0300 (+03) From: "Kirill A. Shutemov" To: kirill.shutemov@linux.intel.com Cc: ak@linux.intel.com, andreyknvl@gmail.com, ashok.raj@intel.com, bharata@amd.com, dave.hansen@linux.intel.com, dvyukov@google.com, glider@google.com, hjl.tools@gmail.com, jacob.jun.pan@linux.intel.com, kcc@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, luto@kernel.org, peterz@infradead.org, rick.p.edgecombe@intel.com, ryabinin.a.a@gmail.com, tarasmadan@google.com, x86@kernel.org Subject: [PATCHv11.1 07/16] x86/mm: Provide arch_prctl() interface for LAM Date: Tue, 8 Nov 2022 00:37:09 +0300 Message-Id: <20221107213709.28330-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221025001722.17466-8-kirill.shutemov@linux.intel.com> References: <20221025001722.17466-8-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE 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 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 Tested-by: Alexander Potapenko Reviewed-by: Alexander Potapenko Acked-by: Peter Zijlstra (Intel) --- 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 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_64.c b/arch/x86/kernel/process_64.c index 6b3418bff326..b8f2558a3aeb 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; -- 2.38.0