From: Luigi Rizzo <lrizzo@google.com>
To: Marek Szyprowski <m.szyprowski@samsung.com>,
Robin Murphy <robin.murphy@arm.com>,
Willem de Bruijn <willemb@google.com>,
Kuniyuki Iwashima <kuniyu@google.com>,
"David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Luigi Rizzo <lrizzo@google.com>,
Luigi Rizzo <rizzo.unipi@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Dragos Tatulea <dtatulea@nvidia.com>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
netdev@vger.kernel.org, linux-mm@kvack.org,
iommu@lists.linux.dev, driver-core@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/5] swiotlb: enforce pool nareas and nslabs invariants
Date: Mon, 24 Aug 2026 15:29:28 +0000 [thread overview]
Message-ID: <20260824152932.1583506-2-lrizzo@google.com> (raw)
In-Reply-To: <20260824152932.1583506-1-lrizzo@google.com>
The SWIOTLB allocator relies on two runtime invariants across all
pool initialization paths:
1. pool->nareas must always be a power of two so that a slot's area
can be located efficiently via bitwise masking (index & (nareas - 1))
instead of integer division.
2. pool->nslabs must be a multiple of nareas * IO_TLB_SEGSIZE so that
each area contains an integer multiple of IO_TLB_SEGSIZE (default 128)
slots, preventing contiguous allocations from crossing area boundaries.
Enforce these invariants consistently during early boot, pool
initialization (swiotlb_init_io_tlb_pool), and restricted DMA pool setup.
Fixes: 8ac04063354a ("swiotlb: reduce the number of areas to match actual memory pool size")
Signed-off-by: Luigi Rizzo <lrizzo@google.com>
---
kernel/dma/swiotlb.c | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 1abd3e6146f45..8e4bd9d47735a 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -33,6 +33,7 @@
#include <linux/kmsan-checks.h>
#include <linux/iommu-helper.h>
#include <linux/init.h>
+#include <linux/log2.h>
#include <linux/memblock.h>
#include <linux/mm.h>
#include <linux/pfn.h>
@@ -176,7 +177,7 @@ static void swiotlb_adjust_nareas(unsigned int nareas)
static unsigned int limit_nareas(unsigned int nareas, unsigned long nslots)
{
if (nslots < nareas * IO_TLB_SEGSIZE)
- return nslots / IO_TLB_SEGSIZE;
+ return rounddown_pow_of_two(nslots / IO_TLB_SEGSIZE);
return nareas;
}
@@ -269,7 +270,16 @@ static void swiotlb_init_io_tlb_pool(struct io_tlb_pool *mem, phys_addr_t start,
unsigned long nslabs, bool late_alloc, unsigned int nareas)
{
void *vaddr = phys_to_virt(start);
- unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
+ unsigned long bytes, i;
+
+ /*
+ * If we have multiple areas, ensure each area's size is a multiple of
+ * IO_TLB_SEGSIZE slots by aligning the total pool size down.
+ */
+ if (nareas > 1)
+ nslabs = ALIGN_DOWN(nslabs, nareas * IO_TLB_SEGSIZE);
+
+ bytes = nslabs << IO_TLB_SHIFT;
mem->nslabs = nslabs;
mem->start = start;
@@ -1813,7 +1823,10 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
struct device *dev)
{
struct io_tlb_mem *mem = rmem->priv;
- unsigned long nslabs = rmem->size >> IO_TLB_SHIFT;
+ unsigned long nslabs = round_down(rmem->size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
+
+ if (!nslabs)
+ return -EINVAL;
/* Set Per-device io tlb area to one */
unsigned int nareas = 1;
--
2.55.0.766.g2966f0265a-goog
next prev parent reply other threads:[~2026-08-24 15:29 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260615234220.3946885-1-lrizzo@google.com>
[not found] ` <20260615172535.080cf94f@kernel.org>
[not found] ` <CAMOZA0KAHKsvA9yRcdrjG13S+=rJhw-Cvnw2WdLjGGY0azG0kw@mail.gmail.com>
2026-06-16 11:06 ` [PATCH] swiotlb: avoid double copy with swiotlb on tx socket Mostafa Saleh
2026-08-24 8:59 ` Dragos Tatulea
2026-08-24 15:32 ` Luigi Rizzo
2026-08-24 17:39 ` Dragos Tatulea
2026-08-25 16:33 ` Mostafa Saleh
2026-08-24 15:29 ` [PATCH v2 0/5] swiotlb: avoid swiotlb copy on network sockets Luigi Rizzo
2026-08-24 15:29 ` Luigi Rizzo [this message]
2026-08-24 15:29 ` [PATCH v2 2/5] swiotlb/mm: Implement SWIOTLB nocopy page allocator Luigi Rizzo
2026-08-24 16:05 ` Robin Murphy
2026-08-24 16:30 ` Luigi Rizzo
2026-08-24 17:38 ` Dragos Tatulea
2026-08-24 15:29 ` [PATCH v2 3/5] net/swiotlb: Track bounce device per socket Luigi Rizzo
2026-08-24 15:29 ` [PATCH v2 4/5] net: Divert socket allocations to SWIOTLB for nocopy TX Luigi Rizzo
2026-08-24 16:32 ` Randy Dunlap
2026-08-24 15:29 ` [PATCH v2 5/5] swiotlb: Implement RX nocopy with fast recycling eviction Luigi Rizzo
2026-08-24 17:38 ` Dragos Tatulea
2026-08-24 17:37 ` [PATCH v2 0/5] swiotlb: avoid swiotlb copy on network sockets Dragos Tatulea
2026-08-25 8:03 ` [syzbot ci] " syzbot ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260824152932.1583506-2-lrizzo@google.com \
--to=lrizzo@google.com \
--cc=akpm@linux-foundation.org \
--cc=davem@davemloft.net \
--cc=david@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=dtatulea@nvidia.com \
--cc=edumazet@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=iommu@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=m.szyprowski@samsung.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rafael@kernel.org \
--cc=rizzo.unipi@gmail.com \
--cc=robin.murphy@arm.com \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox