From patchwork Wed Jan 11 12:37:30 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: 13096661 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 1844DC5479D for ; Wed, 11 Jan 2023 13:24:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DD25900007; Wed, 11 Jan 2023 08:24:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 36672940007; Wed, 11 Jan 2023 08:24:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F598900007; Wed, 11 Jan 2023 08:24:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 00C7E900002 for ; Wed, 11 Jan 2023 08:24:03 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CB532140C64 for ; Wed, 11 Jan 2023 13:24:03 +0000 (UTC) X-FDA: 80342586366.30.3A35F70 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by imf29.hostedemail.com (Postfix) with ESMTP id E8C7E120003 for ; Wed, 11 Jan 2023 13:24:01 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MCW3Am3j; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf29.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.24) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673443442; 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=4X3Yl3VDJMkgc/rTeU7ylnnNNVhA7d2827/V+2fvJYE=; b=c+uzRZ3we9MYF51a6qnomu3z3cn+1CGsuFANwTEGwerm9QNnv2Og5RNUUES803dzffqDJg drrg/qQnq+XEZ/wyw5vqXFLPAp61c9FKGTa5Kf/RlUBvgvTQZIuCa/LC5N7k06N6OgYVok Ch/BOvusxBZ4aaQXcT3/TX9hBZr9sBg= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=MCW3Am3j; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf29.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.24) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673443442; a=rsa-sha256; cv=none; b=lttiD+EFZhUUXWws+WbcL/sUg74PSL6tZY3v+So+hki+w3JhD7PkUp/Mq30JPzaz4D+iGa i/IRAsvMVKpDOSAxmSeEqgeZ6alHrnqtHduV2Ljy89id61Qy0VyY/C9oMAHKYk7Kyz4+tX leKdnfSmv0GcQ7LwbWHSXynSvvlCer0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673443442; x=1704979442; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C8PPsmcjUUuQBbdSz1HmIC78H3wDV2qhKG6GO472d7A=; b=MCW3Am3j+qXV7hqimglzTRbLiev6eiACzOqhrTxn41WwqdJa7L9GYVye zv10aVNVRSKOvldDcvgEkh2NT4YhvXYmRQRdOJRhfDfoC8i9O+7DcNItz JVfG0krXH3lbelrp6HbJqpx4tnp/GXLZLbhVxKx0aV7VZHS3h5n+YRHSy MobZiH9Uhrx5Xz3Pg5gBREb7z58j/hPRN4Qn/gXhAdnAuN9cjwTCnnSJR LpH7uyckcjg1LGSpPp/bdTIHm0bGEZ2EB42xVbdrBEItDEIDnI0bc1JjN uuVfO3afiWTpQWP3brT4luLQAsdn7bzQN68wsQpbd+o5AhABuqqhUbhAW g==; X-IronPort-AV: E=McAfee;i="6500,9779,10586"; a="324646094" X-IronPort-AV: E=Sophos;i="5.96,317,1665471600"; d="scan'208";a="324646094" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jan 2023 05:23:54 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10586"; a="720725481" X-IronPort-AV: E=Sophos;i="5.96,317,1665471600"; d="scan'208";a="720725481" Received: from bachaue1-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.37.250]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jan 2023 05:23:49 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 73247109CE1; Wed, 11 Jan 2023 15:37:41 +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: [PATCHv14 11/17] x86/mm/iommu/sva: Make LAM and SVA mutually exclusive Date: Wed, 11 Jan 2023 15:37:30 +0300 Message-Id: <20230111123736.20025-12-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20230111123736.20025-1-kirill.shutemov@linux.intel.com> References: <20230111123736.20025-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E8C7E120003 X-Stat-Signature: 3ortqenh9smh688t77aesou3extqbmq3 X-HE-Tag: 1673443441-18107 X-HE-Meta: U2FsdGVkX18snSRott9Xn2mttbWzrecIgNgizf7opFDS6JqXdXPgWQrKGwdcr5tPt4UUrtcgW1e256HQVBouFGhjSueKRWzn646FXpOlSWJ30Oan6lBE8NmY61kH+ydbQLs7L4VKQMeeKmnBHL4ub07zgkPTzm9B5f/WpT8nVcDFtUyOhzLyBu7NzZvAFeqlJEAQjZGDeVT0Juwtf9hZTcVpgx/RVeAZ65KG/5TtL9Un9MXWotHdR8PhcePMgUTY86ot4TRFsBQFqvWqVsChMyq8U0Wr1IWDyfMXtPelRYp3mA7X4WAyESb+6FjFtw70l35fgbobseP8JYnaN7GeCt3khAX4gK7FC1t7R+Uhw3NW6Dgqo3QEl3y6kmTfRPI2CW8YoVj0fb4BMgXVK51AYxlnMd5/Svc29K86NQ4sGQ4y4YYUc8LJoOHkLkvJKHDXMCcEUTEDPngj45UZvc3rOVBYlGn0CtCKE2Qp8v6VgV7UTC4MCxxTXn3jFGvXWooGErvmSmfUBVEMIjsiOmW/OsmA+rf0SLvKVQbetrJ2dvKvz5Opj4ipCCtS4vb5WSdQIiQduwtnJKB8MqQtTOB+7SjEcyOYVtFe6SeFEQNeC7B1oHQvzWGowRcLkn+AVM2JAoLcTOAYsHT8eUJgvQV4PY8WmFursjhvwNiyuBw5RiLm1edr/1d/ybMzw6IOEns9/evLYoLoawiIC0xLR03dE51LFYYYIM1qUp+A2MaLkmyejsRUzZCYg49l4277yUiJ+DAatBXtrBn+oh51wKZlvpHd39nnlWkUGscwQcuN4o7iTbne7xXKzLwLuhDEQSvlZzfjwBOguEWT4wRNIL44Lo1Eqdu8xi7wrOryODRU1NEG7jZHM8465ws8iVShvGszfXaKXOu3pFFLWYf4rZt8UbkMZBsIS5HU2H/1RtFY1t6eCsKR4DMD8xtFGxzXgMQOYu06ynXmFNU= 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: IOMMU and SVA-capable devices know nothing about LAM and only expect canonical addresses. An attempt to pass down tagged pointer will lead to address translation failure. By default do not allow to enable both LAM and use SVA in the same process. The new ARCH_FORCE_TAGGED_SVA arch_prctl() overrides the limitation. By using the arch_prctl() userspace takes responsibility to never pass tagged address to the device. Signed-off-by: Kirill A. Shutemov Reviewed-by: Ashok Raj Reviewed-by: Jacob Pan --- arch/x86/include/asm/mmu.h | 2 ++ arch/x86/include/asm/mmu_context.h | 6 ++++++ arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 7 +++++++ drivers/iommu/iommu-sva.c | 4 ++++ include/linux/mmu_context.h | 7 +++++++ 6 files changed, 27 insertions(+) diff --git a/arch/x86/include/asm/mmu.h b/arch/x86/include/asm/mmu.h index e80762e998ce..0da5c227f490 100644 --- a/arch/x86/include/asm/mmu.h +++ b/arch/x86/include/asm/mmu.h @@ -14,6 +14,8 @@ #define MM_CONTEXT_HAS_VSYSCALL 1 /* Do not allow changing LAM mode */ #define MM_CONTEXT_LOCK_LAM 2 +/* Allow LAM and SVA coexisting */ +#define MM_CONTEXT_FORCE_TAGGED_SVA 3 /* * x86 has arch-specific MMU state beyond what lives in mm_struct. diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 4b77c96da07f..6ffc42dfd59d 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -114,6 +114,12 @@ static inline void mm_reset_untag_mask(struct mm_struct *mm) mm->context.untag_mask = -1UL; } +#define arch_pgtable_dma_compat arch_pgtable_dma_compat +static inline bool arch_pgtable_dma_compat(struct mm_struct *mm) +{ + return !mm_lam_cr3_mask(mm) || + test_bit(MM_CONTEXT_FORCE_TAGGED_SVA, &mm->context.flags); +} #else static inline unsigned long mm_lam_cr3_mask(struct mm_struct *mm) diff --git a/arch/x86/include/uapi/asm/prctl.h b/arch/x86/include/uapi/asm/prctl.h index a31e27b95b19..eb290d89cb32 100644 --- a/arch/x86/include/uapi/asm/prctl.h +++ b/arch/x86/include/uapi/asm/prctl.h @@ -23,5 +23,6 @@ #define ARCH_GET_UNTAG_MASK 0x4001 #define ARCH_ENABLE_TAGGED_ADDR 0x4002 #define ARCH_GET_MAX_TAG_BITS 0x4003 +#define ARCH_FORCE_TAGGED_SVA 0x4004 #endif /* _ASM_X86_PRCTL_H */ diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 1b41c60ebf6e..0831d2be190f 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -759,6 +759,10 @@ static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits) if (current->mm != mm) return -EINVAL; + if (mm_valid_pasid(mm) && + !test_bit(MM_CONTEXT_FORCE_TAGGED_SVA, &mm->context.flags)) + return -EINTR; + if (mmap_write_lock_killable(mm)) return -EINTR; @@ -882,6 +886,9 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2) (unsigned long __user *)arg2); case ARCH_ENABLE_TAGGED_ADDR: return prctl_enable_tagged_addr(task->mm, arg2); + case ARCH_FORCE_TAGGED_SVA: + set_bit(MM_CONTEXT_FORCE_TAGGED_SVA, &task->mm->context.flags); + return 0; case ARCH_GET_MAX_TAG_BITS: if (!cpu_feature_enabled(X86_FEATURE_LAM)) return put_user(0, (unsigned long __user *)arg2); diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 4ee2929f0d7a..dd76a1a09cf7 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -2,6 +2,7 @@ /* * Helpers for IOMMU drivers implementing SVA */ +#include #include #include #include @@ -32,6 +33,9 @@ int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max) min == 0 || max < min) return -EINVAL; + if (!arch_pgtable_dma_compat(mm)) + return -EBUSY; + mutex_lock(&iommu_sva_lock); /* Is a PASID already associated with this mm? */ if (mm_valid_pasid(mm)) { diff --git a/include/linux/mmu_context.h b/include/linux/mmu_context.h index 14b9c1fa05c4..f2b7a3f04099 100644 --- a/include/linux/mmu_context.h +++ b/include/linux/mmu_context.h @@ -35,4 +35,11 @@ static inline unsigned long mm_untag_mask(struct mm_struct *mm) } #endif +#ifndef arch_pgtable_dma_compat +static inline bool arch_pgtable_dma_compat(struct mm_struct *mm) +{ + return true; +} +#endif + #endif