From nobody Thu Dec 25 19:38:36 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC8485C8F7 for ; Fri, 12 Jan 2024 05:53:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zX9g2kAv" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbedc37d66cso6534239276.3 for ; Thu, 11 Jan 2024 21:53:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705038782; x=1705643582; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CCgv2gQcPlkrLlc5vy0/ee6RBdfFJuXbxv5Hj/nh1sY=; b=zX9g2kAvzWZXADJe9boZamxxhp6dBhkhuJ8ZSdlh38ErrYYCLGkuSKa8ADra2aggDB 3h2sgFqHCA/Lczrwp9ecoGisncrHQfkuA4icFWzw3vzVOMn8zd6WlDYGcFpSI7JzdSQf 00vMvHfUgvuH+vLWxNVr/qBAz/ZoK/K4Zso6M4FkNtYRnZ8qIniXgO/ZChGK3kGSX6Ix cYxphXayCC7BYydACazVDiWPiI5Pi5adzHCVPqfdgsinBdJIRqzIND5mSPQDjoAAPb3X 75jl7OPkkniX108TmxiZ/DdC2voXvPRviwYNLUxS4r5nvt3+1XaPtIWpftrobXRpEhRo 0fvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705038782; x=1705643582; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CCgv2gQcPlkrLlc5vy0/ee6RBdfFJuXbxv5Hj/nh1sY=; b=DP8S07vet4iQUe6A+0xbB4nfM0JIsfKeFRkvPwW8My+L24UNDQOgkp5sOdcXCB+bu/ kbInftNv5hviKjU/xE+IiFZpwn+iy7v2J9FzlrGuMi7ae0k8t8QEGSZMRYU4Ot0RQYKR xiXnwkcfeikRO3do+k+Hlk+wwQP42jPXpvEf7wVbQZrIzMTbkIADLHu7bVyDkxEAhr0b A4KfOOyix6kTJ8Tk7HhmN0+p5sUViZIE7oLb0knvuhIlxiMPtV84pyH9/EBLJrRy0qGH cQcBpkqp6JDyEtGCnle/zQ6e421eJunTy178zzdasxeQ5ZZI+d8xVt8CFXNBSq6xJBCT Sv5Q== X-Gm-Message-State: AOJu0YzslIh9DubvqKedeMFqckrRKY+M0nbZT4/TuHQw+47mGZZKhgYt hwonjo9FKvWMAEIyIMnPm5rbwEsobr9VT34hzqq2jJM= X-Google-Smtp-Source: AGHT+IHzIdxRbUN3uUh2p4li/usVrD1CGbdlJlgyLfKsoH/zMy1VbGZJ/Dfi9+WcQoQmfD7YSqjGuTsHmLd/HH/O X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:43a4]) (user=vannapurve job=sendgmr) by 2002:a05:6902:1b81:b0:dbe:d426:c456 with SMTP id ei1-20020a0569021b8100b00dbed426c456mr10589ybb.4.1705038782481; Thu, 11 Jan 2024 21:53:02 -0800 (PST) Date: Fri, 12 Jan 2024 05:52:47 +0000 In-Reply-To: <20240112055251.36101-1-vannapurve@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240112055251.36101-1-vannapurve@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240112055251.36101-2-vannapurve@google.com> Subject: [RFC V1 1/5] swiotlb: Support allocating DMA memory from SWIOTLB From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, rientjes@google.com, bgardon@google.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, michael.roth@amd.com, kirill@shutemov.name, thomas.lendacky@amd.com, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, andrew.jones@linux.dev, corbet@lwn.net, hch@lst.de, m.szyprowski@samsung.com, bp@suse.de, rostedt@goodmis.org, iommu@lists.linux.dev, Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Modify SWIOTLB framework to allocate DMA memory always from SWIOTLB. CVMs use SWIOTLB buffers for bouncing memory when using dma_map_* APIs to setup memory for IO operations. SWIOTLB buffers are marked as shared once during early boot. Buffers allocated using dma_alloc_* APIs are allocated from kernel memory and then converted to shared during each API invocation. This patch ensures that such buffers are also allocated from already shared SWIOTLB regions. This allows enforcing alignment requirements on regions marked as shared. Signed-off-by: Vishal Annapurve --- include/linux/swiotlb.h | 17 +---------------- kernel/dma/direct.c | 4 ++-- kernel/dma/swiotlb.c | 5 +++-- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index ecde0312dd52..058901313405 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -17,6 +17,7 @@ struct scatterlist; #define SWIOTLB_VERBOSE (1 << 0) /* verbose initialization */ #define SWIOTLB_FORCE (1 << 1) /* force bounce buffering */ #define SWIOTLB_ANY (1 << 2) /* allow any memory for the buffer */ +#define SWIOTLB_ALLOC (1 << 4) /* force dma allocation through swiotlb */ =20 /* * Maximum allowable number of contiguous slabs to map, @@ -259,7 +260,6 @@ static inline phys_addr_t default_swiotlb_limit(void) =20 extern void swiotlb_print_info(void); =20 -#ifdef CONFIG_DMA_RESTRICTED_POOL struct page *swiotlb_alloc(struct device *dev, size_t size); bool swiotlb_free(struct device *dev, struct page *page, size_t size); =20 @@ -267,20 +267,5 @@ static inline bool is_swiotlb_for_alloc(struct device = *dev) { return dev->dma_io_tlb_mem->for_alloc; } -#else -static inline struct page *swiotlb_alloc(struct device *dev, size_t size) -{ - return NULL; -} -static inline bool swiotlb_free(struct device *dev, struct page *page, - size_t size) -{ - return false; -} -static inline bool is_swiotlb_for_alloc(struct device *dev) -{ - return false; -} -#endif /* CONFIG_DMA_RESTRICTED_POOL */ =20 #endif /* __LINUX_SWIOTLB_H */ diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 73c95815789a..a7d3266d3d83 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -78,7 +78,7 @@ bool dma_coherent_ok(struct device *dev, phys_addr_t phys= , size_t size) =20 static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size) { - if (!force_dma_unencrypted(dev)) + if (!force_dma_unencrypted(dev) || is_swiotlb_for_alloc(dev)) return 0; return set_memory_decrypted((unsigned long)vaddr, PFN_UP(size)); } @@ -87,7 +87,7 @@ static int dma_set_encrypted(struct device *dev, void *va= ddr, size_t size) { int ret; =20 - if (!force_dma_unencrypted(dev)) + if (!force_dma_unencrypted(dev) || is_swiotlb_for_alloc(dev)) return 0; ret =3D set_memory_encrypted((unsigned long)vaddr, PFN_UP(size)); if (ret) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 33d942615be5..a056d2f8b9ee 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -363,6 +363,7 @@ void __init swiotlb_init_remap(bool addressing_limit, u= nsigned int flags, =20 io_tlb_default_mem.force_bounce =3D swiotlb_force_bounce || (flags & SWIOTLB_FORCE); + io_tlb_default_mem.for_alloc =3D (flags & SWIOTLB_ALLOC); =20 #ifdef CONFIG_SWIOTLB_DYNAMIC if (!remap) @@ -1601,8 +1602,6 @@ static inline void swiotlb_create_debugfs_files(struc= t io_tlb_mem *mem, =20 #endif /* CONFIG_DEBUG_FS */ =20 -#ifdef CONFIG_DMA_RESTRICTED_POOL - struct page *swiotlb_alloc(struct device *dev, size_t size) { struct io_tlb_mem *mem =3D dev->dma_io_tlb_mem; @@ -1634,6 +1633,8 @@ bool swiotlb_free(struct device *dev, struct page *pa= ge, size_t size) return true; } =20 +#ifdef CONFIG_DMA_RESTRICTED_POOL + static int rmem_swiotlb_device_init(struct reserved_mem *rmem, struct device *dev) { --=20 2.43.0.275.g3460e3d667-goog From nobody Thu Dec 25 19:38:36 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF8A05C902 for ; Fri, 12 Jan 2024 05:53:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dc/1Xz32" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5eb6dba1796so115717227b3.1 for ; Thu, 11 Jan 2024 21:53:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705038786; x=1705643586; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lgNchHS++prR/h3S2HHor+FAVbRKiDRX1egmYCByUjw=; b=dc/1Xz32RIzMrp6QjoBemHExoNQ2PbmRylBdEo1+M89DYNTG7C/ty3KQ6Hg5hD1mG5 o0owqv4GYQ1dH0rMkWwm1IleZ0C2X8l65IVOJHsdgXPhONQa64kK2rsxMY69AMupWu3/ izH5TS6ts1zl89PfymLmlkUR1FLS2S4PJPlaGQwqhnLunbLBwfuAtoadIQNy/Zt/n/qF SeMAPL6kOHJo2V0WfDYQUUu/nazw3IAzF4+y8Knw2wJatJUAhIRQGKCl+gvhUvG23OX9 jZEyul7vpJ1hD256r6mMgvD7ATaaaRyf3PwhnyRYMxZfnoqaZyAwbqYXPHqlz3ceNo8E /gTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705038786; x=1705643586; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lgNchHS++prR/h3S2HHor+FAVbRKiDRX1egmYCByUjw=; b=wt7H4KjsqWtXDw5HUyxbOo7VpmkbWH02jHQ+6gs0kGEngSGd/OCOizcPXTkW8RelCV Wg/1Px9KM6e8gjlhcEEwn985+fLRf1wZ3zAbom4xPc/RdXpDFUi7XkNBHOduc4XeW6PR /ILlzZxzkrRwEACsqT4N6gh8StmUY8jBIQETjkWP32D+BcgLjEenKI1i0GJ80/DtDfBz y3n1bNfj3eyUxR8+UQU5bX151ZVKIbwbxYcttqqg7vUMUvVVvzoxX8KOdCnMgXCwOsvg 48jtvcUiZKisPpNCHBUvEzxJW4Lso/iFadj+l331aAiIRnjOqe1lpemw0loJGiS1AK0j 4B2w== X-Gm-Message-State: AOJu0YzqeaNbEHS8fgT8bXZq2WR7BgQ9pCBkseLwnGsjhhv40H4ktrg5 BfJ99pWSu9z0TlcMfsecZOojlk21hrBavTAXztUc/MU= X-Google-Smtp-Source: AGHT+IHuDn+1jvl5KUmKWmyATHHZ4Buh8nYfelV4isyKtZemvMzYuO4uYtBD4xP91JEeDNYKvgt54kGnaY4QS31H X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:43a4]) (user=vannapurve job=sendgmr) by 2002:a05:6902:1081:b0:dbd:b6cd:92cf with SMTP id v1-20020a056902108100b00dbdb6cd92cfmr144861ybu.11.1705038785851; Thu, 11 Jan 2024 21:53:05 -0800 (PST) Date: Fri, 12 Jan 2024 05:52:48 +0000 In-Reply-To: <20240112055251.36101-1-vannapurve@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240112055251.36101-1-vannapurve@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240112055251.36101-3-vannapurve@google.com> Subject: [RFC V1 2/5] swiotlb: Allow setting up default alignment of SWIOTLB region From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, rientjes@google.com, bgardon@google.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, michael.roth@amd.com, kirill@shutemov.name, thomas.lendacky@amd.com, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, andrew.jones@linux.dev, corbet@lwn.net, hch@lst.de, m.szyprowski@samsung.com, bp@suse.de, rostedt@goodmis.org, iommu@lists.linux.dev, Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow adjusting alignment of SWIOTLB memory. CVMs can use this framework to align the shared memory regions as needed. Signed-off-by: Vishal Annapurve --- include/linux/swiotlb.h | 5 +++++ kernel/dma/swiotlb.c | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 058901313405..450bd82cdb9f 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -206,6 +206,7 @@ size_t swiotlb_max_mapping_size(struct device *dev); bool is_swiotlb_allocated(void); bool is_swiotlb_active(struct device *dev); void __init swiotlb_adjust_size(unsigned long size); +void __init swiotlb_adjust_alignment(unsigned long alignment); phys_addr_t default_swiotlb_base(void); phys_addr_t default_swiotlb_limit(void); #else @@ -247,6 +248,10 @@ static inline void swiotlb_adjust_size(unsigned long s= ize) { } =20 +void __init swiotlb_adjust_alignment(unsigned long alignment) +{ +} + static inline phys_addr_t default_swiotlb_base(void) { return 0; diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index a056d2f8b9ee..eeab0607a028 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -97,6 +97,7 @@ static struct io_tlb_mem io_tlb_default_mem; #endif /* CONFIG_SWIOTLB_DYNAMIC */ =20 static unsigned long default_nslabs =3D IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIF= T; +static unsigned long default_alignment =3D PAGE_SIZE; static unsigned long default_nareas; =20 /** @@ -223,6 +224,11 @@ void __init swiotlb_adjust_size(unsigned long size) pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20); } =20 +void __init swiotlb_adjust_alignment(unsigned long alignment) +{ + default_alignment =3D alignment; +} + void swiotlb_print_info(void) { struct io_tlb_pool *mem =3D &io_tlb_default_mem.defpool; @@ -315,7 +321,7 @@ static void __init *swiotlb_memblock_alloc(unsigned lon= g nslabs, unsigned int flags, int (*remap)(void *tlb, unsigned long nslabs)) { - size_t bytes =3D PAGE_ALIGN(nslabs << IO_TLB_SHIFT); + size_t bytes =3D ALIGN(nslabs << IO_TLB_SHIFT, default_alignment); void *tlb; =20 /* @@ -324,9 +330,9 @@ static void __init *swiotlb_memblock_alloc(unsigned lon= g nslabs, * memory encryption. */ if (flags & SWIOTLB_ANY) - tlb =3D memblock_alloc(bytes, PAGE_SIZE); + tlb =3D memblock_alloc(bytes, default_alignment); else - tlb =3D memblock_alloc_low(bytes, PAGE_SIZE); + tlb =3D memblock_alloc_low(bytes, default_alignment); =20 if (!tlb) { pr_warn("%s: Failed to allocate %zu bytes tlb structure\n", --=20 2.43.0.275.g3460e3d667-goog From nobody Thu Dec 25 19:38:36 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D442A5C91A for ; Fri, 12 Jan 2024 05:53:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jmI06ffq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5ecfd153ccfso111528997b3.2 for ; Thu, 11 Jan 2024 21:53:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705038789; x=1705643589; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3jQidOKSMiiXlthXl3syuEmjTW2A6fx8EoTn/KG4frI=; b=jmI06ffqgLtkLrkFmuLL3CQ5tuHVBBemfT840F8Fm7gXfRcPewemmPxH25ZZw8aYWl uyLRVtW4Xy+diazx9eohunng8RfXzbyduFVhaU5DW01G2LEA8NSdEBYHTHGLNpPUc1xc V/xnYjVPLtFBcljybTDpt5cYidifoLir8LDNXzKAp2MswJCROXLq/2cEl8KKyzEdPgyX K/uFY0DdDfWu7bn7W6PwcwQSTULDmL/nB4s7Lc8Ku/IdvX46eCkTWjrtY59a/tq0RwKI sQTxiUbE7C6a0lBTkDu7AzzBGuYzt0RAOHvp8VmFwVGRNHNL/K2HOjQOHCATrxLEsgwb tE/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705038789; x=1705643589; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3jQidOKSMiiXlthXl3syuEmjTW2A6fx8EoTn/KG4frI=; b=WX1u7B3ARBi5u9K7QETm07+TCcEe6s5+Ymjw4s/Pen+JnfVeSfykuONT6SMjI4lqR4 o7CCqZcLwPBR3592p5JQu/7Q1m62NCwLwc4+oeXZoXYWZ4O1y0o3TujO7iexCWQeIPGW aM1ycEvCf2cDb4+PLp4Og2nqsmDFfcdZD+T0jCcIN7/6/2t69JvNMJTL25wy9U7zbim0 u/n1hpqoSPrPKGFis9EjF2UOLa+hvL/u66Go+utnDSM8WX8grIvSIKrV2br+oHgV1MA9 qWJGCd1gr0aPGp2ikhMIqmZjbv4/3v5g20LMZT8QJIkQK2uzAeuPNTcxkZ8G5P4M4K1H 3DNQ== X-Gm-Message-State: AOJu0YymoZrm6kCVMFhvZUHQh/pP8yC9TmsG5qys/Tq2n5TPniY3/cge mWTQsdq6uSeVeP6aYUWK4DJ8q55Ia2QbxpbyHiYfwbA= X-Google-Smtp-Source: AGHT+IHKtqhJvPuX99AmjEPzIekAgNij/RFYGidlE8shwCwScd2W0RcK+2eNPVzJk4tEIt4roua9lsBYL0xT4CjS X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:43a4]) (user=vannapurve job=sendgmr) by 2002:a05:6902:168c:b0:dbd:4683:21da with SMTP id bx12-20020a056902168c00b00dbd468321damr154755ybb.8.1705038789001; Thu, 11 Jan 2024 21:53:09 -0800 (PST) Date: Fri, 12 Jan 2024 05:52:49 +0000 In-Reply-To: <20240112055251.36101-1-vannapurve@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240112055251.36101-1-vannapurve@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240112055251.36101-4-vannapurve@google.com> Subject: [RFC V1 3/5] x86: CVMs: Enable dynamic swiotlb by default for CVMs From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, rientjes@google.com, bgardon@google.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, michael.roth@amd.com, kirill@shutemov.name, thomas.lendacky@amd.com, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, andrew.jones@linux.dev, corbet@lwn.net, hch@lst.de, m.szyprowski@samsung.com, bp@suse.de, rostedt@goodmis.org, iommu@lists.linux.dev, Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" CVMs used SWIOTLB for non-trusted IO using dma_map_* APIs. This series will ensure that dma_alloc_* API to also allocate from SWIOTLB pools. Initial size of SWIOTLB pool is decided using heuristics and has been working with CVM usecases so far. It's better to allow SWIOTLB to scale up on demand rather than keeping the size fixed to avoid failures with possibly increased usage of SWIOTLB with dma_alloc_* APIs allocating from SWIOTLB pools. This should also help in future with more devices getting used from CVMs for non-trusted IO. Signed-off-by: Vishal Annapurve --- arch/x86/Kconfig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 1566748f16c4..035c8a022c4c 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -884,6 +884,7 @@ config INTEL_TDX_GUEST select X86_MEM_ENCRYPT select X86_MCE select UNACCEPTED_MEMORY + select SWIOTLB_DYNAMIC help Support running as a guest under Intel TDX. Without this support, the guest kernel can not boot or run under TDX. @@ -1534,6 +1535,7 @@ config AMD_MEM_ENCRYPT select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select UNACCEPTED_MEMORY + select SWIOTLB_DYNAMIC help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory --=20 2.43.0.275.g3460e3d667-goog From nobody Thu Dec 25 19:38:36 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6B9E5D73B for ; Fri, 12 Jan 2024 05:53:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RcsIPG81" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5f69158f32eso81476217b3.2 for ; Thu, 11 Jan 2024 21:53:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705038792; x=1705643592; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sOaEOFbZAWEH5Yjsdlubc01KHjJyWCBCS9BwuOvGQrw=; b=RcsIPG81Ilrha4dJO2ocI4bqi/hjF3gm47HEp7RSsSRIBe3B66ePiq/CTf5inG61Qk VmiPx+db4Z2hpOf0dcWXJdq30IRAoPXyPyoL1TgMwHQOm8786kV5ufW67wuMVCpYvC2K U7IWFKGnRfNF8FOwdWRgfFVBkJ/B6ccTgJT3bUXPri5uEtU7QutMQxVZJhix/XAWSL9f B0LW640sUADbfBVV603pXzQONCbL5aMcP+32chS+c/lMdEnJB+A+RIJPUxERXyrmiISG ST56WKneWlwUX0r0KkxGw3FFBf7jscncJiu8HJrn1JRiXO3NRZglzLnoCaaXPGVqNrIW 4TqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705038792; x=1705643592; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sOaEOFbZAWEH5Yjsdlubc01KHjJyWCBCS9BwuOvGQrw=; b=V8gvZuES1BIV1yC3eIIRhXwNXFWSSCKRfsnlO15gbm2o+xvBUYnxKvyLH6qwSCXh6d dddCicq7T0+6sucRPXKK5mXOwyyHnlW0YNalwGe+RnWrWGBZ4v9Kyy5kw1bwpy6nkXD2 0OU/kyLFLzFzMbNhnq35DHoJlI7jtfIxz0liktNX/7jm53H6h5CzZ35XQ7K4GX3IHb/2 XTEEgOfmIf/5izbZv09IB/r3+916xOnR9uqVLAGey6SROwOwjVYat9lcIIPgwxE3kzb/ WjKeKGZgn41Wx12Z/+qzZycLkENxh05Ro+IHlnBc8w5jxGkpNs7SEqIJh5DnqDoz2Iqb Yj2A== X-Gm-Message-State: AOJu0YzIk8yyAK+VjC14Zjyb/pHHlX6hD28FtXa+hOsBvO6vAo6w+JHD i6l2trI2k5SLP6X7/hwvyuJicVorv3AQFXuUZGvCj8E= X-Google-Smtp-Source: AGHT+IGaBbJ5M4pHi7k+543Q/p59waeE7UFHvcz2IdcmC08A81gveyAh20QzVYUNlkq6+5OlavoIfF0F2/qyiJ/k X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:43a4]) (user=vannapurve job=sendgmr) by 2002:a81:7102:0:b0:5fb:914f:207a with SMTP id m2-20020a817102000000b005fb914f207amr352561ywc.6.1705038791836; Thu, 11 Jan 2024 21:53:11 -0800 (PST) Date: Fri, 12 Jan 2024 05:52:50 +0000 In-Reply-To: <20240112055251.36101-1-vannapurve@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240112055251.36101-1-vannapurve@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240112055251.36101-5-vannapurve@google.com> Subject: [RFC V1 4/5] x86: CVMs: Allow allocating all DMA memory from SWIOTLB From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, rientjes@google.com, bgardon@google.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, michael.roth@amd.com, kirill@shutemov.name, thomas.lendacky@amd.com, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, andrew.jones@linux.dev, corbet@lwn.net, hch@lst.de, m.szyprowski@samsung.com, bp@suse.de, rostedt@goodmis.org, iommu@lists.linux.dev, Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Changes include: 1) Allocate all DMA memory from SWIOTLB buffers. 2) Increase the size of SWIOTLB region to accommodate dma_alloc_* invocations. 3) Align SWIOLTB regions to 2M size. Signed-off-by: Vishal Annapurve --- arch/x86/kernel/pci-dma.c | 2 +- arch/x86/mm/mem_encrypt.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/pci-dma.c b/arch/x86/kernel/pci-dma.c index f323d83e40a7..3dcc3104b2a8 100644 --- a/arch/x86/kernel/pci-dma.c +++ b/arch/x86/kernel/pci-dma.c @@ -61,7 +61,7 @@ static void __init pci_swiotlb_detect(void) */ if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { x86_swiotlb_enable =3D true; - x86_swiotlb_flags |=3D SWIOTLB_FORCE; + x86_swiotlb_flags |=3D (SWIOTLB_FORCE | SWIOTLB_ALLOC); } } #else diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index c290c55b632b..0cf3365b051f 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -112,10 +112,14 @@ void __init mem_encrypt_setup_arch(void) * The percentage of guest memory used here for SWIOTLB buffers * is more of an approximation of the static adjustment which * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6% + * + * Extra 2% is added to accommodate the requirement of DMA allocations + * done using dma_alloc_* APIs. */ - size =3D total_mem * 6 / 100; - size =3D clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G); + size =3D total_mem * 8 / 100; + size =3D clamp_val(size, IO_TLB_DEFAULT_SIZE, (SZ_1G + SZ_256M)); swiotlb_adjust_size(size); + swiotlb_adjust_alignment(SZ_2M); =20 /* Set restricted memory access for virtio. */ virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); --=20 2.43.0.275.g3460e3d667-goog From nobody Thu Dec 25 19:38:36 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A42585D754 for ; Fri, 12 Jan 2024 05:53:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vannapurve.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z9k1mwSi" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5e9de9795dfso116557157b3.0 for ; Thu, 11 Jan 2024 21:53:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705038795; x=1705643595; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MxvkESmzviEYCBtp91snCvTXp4bYwC2oMLMP2RAwiuA=; b=Z9k1mwSi3Lrc7sKciPEyKLWtgn/dQi3Igqi+JSP+YZoi4eO+aTjh9Dd0d6+6EB80EE lxHol5Bn8YJ79P6KQjHoBGzBF4nX4nEZDLSqWMc9fkx+y7lo2SzJe9DsLZrXRTo0f346 M4gBmBmT75EIobbjPaN0TRNWod16RKAJWmtgg2Cx4jfe8iluEGCqF96krG3z8jZ6kVP/ h8aUWwzv93o1DCGlE80pI2ZVBTCG0uojo7In9TDtlmuNcJEwaTDq8PNMexaMACdePyZn k6NkCHIsRBq99NtpqLci+xKTsCnQO4ZWjaENDMNPg90JZKXvBaxwvHougTEEJ13FrF6L +Myg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705038795; x=1705643595; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MxvkESmzviEYCBtp91snCvTXp4bYwC2oMLMP2RAwiuA=; b=cfCHft7+FbUQ/Ym9tHJ+eaPFAl4NiHeu0TPgc82vReOePER5Xig9iD283XOG4fVbQC 1KQFVoS6X35DHETJkuFXD+uAsFwsqn5t6S9rLzIpwFQ3XqA27iduJlvtyIdl1OjeI5Zq U9EuUpAAVhsqoKgMuE4sv6blMj8bNAsB39u8oEtBxinA26EpD+BPlqVaJPzBQAfx0bZq kI9m7X6yVgH1UO/40c5fRQq2iyeqplLAoqB9r+c09y4Bo40RaWlpwnbmoQ6cpGZ1QgHU lvvaeO9FLAPcvJN6c8XXHwdxK5cgwQK5eh4jsnmy1EbH73Hr3L9iaAjFAn6xXVy5QSRs LybA== X-Gm-Message-State: AOJu0YwGAKVJYBxIND+NkQZ2QCGitbIarRKEZMDhmrB0g4dSDlvA6fIZ IXsqyGxZnXBsELS7x5K+9It6ErXeT36nNekrvXokeKU= X-Google-Smtp-Source: AGHT+IEQgbkriNOGK5mfwWNuA4LMZiYYrvGPT1vz3dpLuatntUClCmGcbrPmnPd+PTEtKaWhmKYQgGZpVUGSTNxw X-Received: from vannapurve2.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:43a4]) (user=vannapurve job=sendgmr) by 2002:a0d:e003:0:b0:5fb:7d88:a558 with SMTP id j3-20020a0de003000000b005fb7d88a558mr497217ywe.0.1705038794846; Thu, 11 Jan 2024 21:53:14 -0800 (PST) Date: Fri, 12 Jan 2024 05:52:51 +0000 In-Reply-To: <20240112055251.36101-1-vannapurve@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240112055251.36101-1-vannapurve@google.com> X-Mailer: git-send-email 2.43.0.275.g3460e3d667-goog Message-ID: <20240112055251.36101-6-vannapurve@google.com> Subject: [RFC V1 5/5] x86: CVMs: Ensure that memory conversions happen at 2M alignment From: Vishal Annapurve To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, rientjes@google.com, bgardon@google.com, seanjc@google.com, erdemaktas@google.com, ackerleytng@google.com, jxgao@google.com, sagis@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, michael.roth@amd.com, kirill@shutemov.name, thomas.lendacky@amd.com, dave.hansen@linux.intel.com, linux-coco@lists.linux.dev, chao.p.peng@linux.intel.com, isaku.yamahata@gmail.com, andrew.jones@linux.dev, corbet@lwn.net, hch@lst.de, m.szyprowski@samsung.com, bp@suse.de, rostedt@goodmis.org, iommu@lists.linux.dev, Vishal Annapurve Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Return error on conversion of memory ranges not aligned to 2M size. Signed-off-by: Vishal Annapurve --- arch/x86/mm/pat/set_memory.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index bda9f129835e..6f7b06a502f4 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2133,8 +2133,10 @@ static int __set_memory_enc_pgtable(unsigned long ad= dr, int numpages, bool enc) int ret; =20 /* Should not be working on unaligned addresses */ - if (WARN_ONCE(addr & ~PAGE_MASK, "misaligned address: %#lx\n", addr)) - addr &=3D PAGE_MASK; + if (WARN_ONCE(addr & ~HPAGE_MASK, "misaligned address: %#lx\n", addr) + || WARN_ONCE((numpages << PAGE_SHIFT) & ~HPAGE_MASK, + "misaligned numpages: %#lx\n", numpages)) + return -EINVAL; =20 memset(&cpa, 0, sizeof(cpa)); cpa.vaddr =3D &addr; --=20 2.43.0.275.g3460e3d667-goog