From patchwork Sun Mar 12 11:26:06 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: 13171305 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 5122BC6FD1F for ; Sun, 12 Mar 2023 11:27:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E20438E000D; Sun, 12 Mar 2023 07:27:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD0218E0008; Sun, 12 Mar 2023 07:27:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C71928E000D; Sun, 12 Mar 2023 07:27:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B8A198E0008 for ; Sun, 12 Mar 2023 07:27:01 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8A44B140905 for ; Sun, 12 Mar 2023 11:27:01 +0000 (UTC) X-FDA: 80560019442.26.9CCAA03 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf17.hostedemail.com (Postfix) with ESMTP id 2A7AD40006 for ; Sun, 12 Mar 2023 11:26:58 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DX0oH4A4; spf=none (imf17.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.31) 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=1678620419; 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=eNMbej0AjDCem+sQF9VbalutOkSMPsnwphNJOw0msfI=; b=5+XfuqyLomHUN0TW/Bb9U3AUPgPxz/vjqMKSV0w5zyNTE1OvBBDlvRu3LwA/t1p5Ie8YhG W4R1iC9HgqLsLtRB0jjRdtYG0ilEIZsnT9XaA06ixmhXuBmwYx/YhAvWap53EdqMZX8CSk z7xrqg+JIaYZeUwy6G3YrhHabLigHIg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DX0oH4A4; spf=none (imf17.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.31) 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=1678620419; a=rsa-sha256; cv=none; b=oBS8hjoWiZTUBBS5Vqb+6C/eCEJkIRihPNoGuyCjG8QWqvQQYlcIw0dQOddjHZ36o6LCYX dB3deW3FFoB0yqourPKKJ+I/9HjAYiubS4GELoxKANjGgzg9ozB7N1gVIDSbnVB8GwRptT 79PWugC713mvVic1E+nCdvtfhazYhLU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678620419; x=1710156419; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pXZFCCXe+PDES5Btd5MhuDoEZZ+VyMVykxFB/NLFXd8=; b=DX0oH4A4hgc/FPe/UEPIUie3vgKKxGXpzjDpQjZOBiaxp8jgCVIiQh+8 2jVIH+LWlj55pQs9ZOkAX5laH1oRTZJs6SUrfaimZPEcqFbawD+Svxofo t+KvYOXC8MvJDm/fCph3atb8+CJxhU9z4wp11q4nd+k+iPAi2CRHpMc9w NZucrD9ZXs56vapSlqDdsL9gI5shFMOLYySB3XTtjTJEFvG0OtR7r1g8a CgwCmoALIKBbcC3z7Pdd4lOC4ruREcWW0FMVg2qcj7duXbRxiPxiUUeZj 7SQmiy1UXGtxGrGBR1FjrOFZ3p5jLB1e6c4lE0AN/lh5DJuV7HADqbwbi A==; X-IronPort-AV: E=McAfee;i="6500,9779,10646"; a="399577565" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="399577565" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.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="671580733" X-IronPort-AV: E=Sophos;i="5.98,254,1673942400"; d="scan'208";a="671580733" Received: from nmoazzen-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.219.215]) by orsmga007-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 C3FDC10D7B9; 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 11/17] x86/mm/iommu/sva: Make LAM and SVA mutually exclusive Date: Sun, 12 Mar 2023 14:26:06 +0300 Message-Id: <20230312112612.31869-12-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-Rspamd-Queue-Id: 2A7AD40006 X-Stat-Signature: 3pkf1jb31i3qtpekr1b7jntc868y61ty X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1678620418-475892 X-HE-Meta: U2FsdGVkX1+MbzDj2JikHjxl38U5Qf+rmhH6d9q2kRDOp2Q25rBntagv7HEQ9w5+zKw/QbhB9mlHTW4AXhhie1l76tx89zBL6kYvkri1Vmwq1dQCUHJeSQetbJSBdweYNe1KigK05RJD+c0XduNOiJm8e3FaQlEc94PQIsB6HyAku8/LQ4EAaY7a8JKDnvsJ+DP2st3rqrFqElf57YgEDzOVoTw2b8Vyl0B6VIv9FyEdXVAKlZYKUy7+lrWZrSvB2hFhtLoLFcrweQG8obDvt6tiLHfMMNtJwgJlvogL136lngfmiXnx3gT86MsaM5xllgKpsN01oX54b5jDAfn9EA8CvkO3048X0NqIKMZTRHPZ4vBFvHUCk5E1yP+sWOIwurjgqPGCqFD/AsvbSAffotz3sXR88UGk9QT8FkDSJ+UteYsBE2pmz0DNvisKH2gHHhT8SwwpiZBjfl8SVq40OpFxihVPYBnrQ9/5dSR+TarIjzu7IGRz4qYYVTBYAradf7wX7xO9+wcIqAlQlJSjT3rw4UhtW2tkFmfRntzkH4Ig31fTWjQ322pfKKMpVhm10fFp6HJzE+LHlRxSztsRgg5tH5Eb1qEhtMfyg7IcFRk2xNbXVtq3LNye3iGeu+w9X0DNJAN+C7sdAsvwZzHU8aN23jZhrEQN0YuhKWqk2LKxwZkTGIDbZKerXFy/nLjni1RmOUBrKcMXWgHtuphX+9rrY1xBxKjGH9iyTUoYT05+hCAEuk/xf9DO96rzqTqR7oyyviWZXpFanRADgoKRIBveNFQpGuT5HXWicFUUJI79pR4OqBUDvDVbXp6IpchA0Bgmdd7q0x6NSAPrsJBSqz83R6dXIWHbwa3OTSdWkG5LnGlv7d5VGjhAlK/YJyPjt7SpPKpcFaBEovEA8eEdSr5kbnE12DY23AqPR293cwoNiG1nFYXT1nkXotvUhezc2p8QNjVJlHpF9w+YoDA GZ/mhnbd HWLDEV+dMJAGQ+nAWp3R492qIuf3JveXnD9X+MRHWkuzaOGfeoWno6UXBXP9JMn/cWf5QFVUxxxqihybPIp8PqQCuZKrA0aO0TeA4Jw9GjgyzjsJWsZH+Yp7PMiEOstx6hAIsZ1SYttx+/5sfaXNmLfk2ME1AZVvc8qHuOT3nzQzN3IX0x6M4gOtd+MT9KtuGmVRsms6lt8CdKMpjyDvN3n2QJpLvhdj1SqCY0cokm0QoGSyQQsNund9AS6su5Fz2O2wTFZ5+umekpzo2J1PgHP564tBRVCMu1uqE 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 Acked-by: Peter Zijlstra (Intel) --- 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 06eaaf75d572..4c396e9a384f 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -115,6 +115,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 88aae519c8f8..eda826a956df 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -756,6 +756,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; @@ -878,6 +882,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