From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (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 6791B43B3EA for ; Mon, 24 Aug 2026 15:29:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787585380; cv=none; b=UCMWfl5Zu3gjtw7Z35CHFu+Ms2cltkiChlIdMIt8jRPtefgx0iLCe4VV1+qrCe1RvtpvMXD1nYuees5C1flntddLUooR4sTGppycnL0lz/xqcmLIUWWtLFqHYABDnSaJxpZPmOemAnFkrAndshB0pXrInitgagn+8kZoBGVePrI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787585380; c=relaxed/simple; bh=wb3aYsExEyGsCbKmxC8jyjPeXblGtpkc9Lib1zp0eB4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ROWZW15a+jfmURoSQiosibkowXGRnMnotR+/w1VnOeo+Ozw6fwYvTad51P4q8CBHaCZPaQna90l5m7oSmuup3gN4gAwwwrfmvspQSNU5J1XSXgkmwrWPV28Ox9PUeMNvuJUbb3BLRSHcBfnpjfifXFkhTRtalVYkKqvHcUDyMYs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--lrizzo.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=g4fd5Xw3; arc=none smtp.client-ip=209.85.218.71 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--lrizzo.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="g4fd5Xw3" Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-c20e5890680so124434566b.1 for ; Mon, 24 Aug 2026 08:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787585376; x=1788190176; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=XxtYpttAkkFeNCs4heKfMrvlAQRBvH9LjM7sWbysPCs=; b=g4fd5Xw3tT6PsyB9wPLh4cY+UVPtPQKp+SUS9vm6SlnYKBo64ca3wWLVPMmLoYQzsE 5W/19tBu6I2RMP/iEwTP5maFjLeF/Crg9rmo6ayAtRiuDayooSRdtbqBCkdUfk1tA6XJ 9+62vFswcPs1sK9nZtXvJwKTwoYB1HwY/MZDXenF2aj7a9OMRIFxO3m2JGU/5hb43sA/ iYVIRdw7ldYyTJAvNON+mtX3c3frhiwAV3rVY1LS7BR9UA4xtPDoMoj4UIyWLJagCfrs j0stKsn6YGIqg/M1K3m2+M9QLc2TdYBe23YYhSO1z23tCJrSF7ZunGhoev1kef9eeu2E JP8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787585376; x=1788190176; h=content-type: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:content-type; bh=XxtYpttAkkFeNCs4heKfMrvlAQRBvH9LjM7sWbysPCs=; b=LDiivCQYY0JZFrfKGCjwxYrlP0Uy6IczeSuSf6pgjvFFUEGM0h/nieOIRYpHhd3pNN FDIms6YLBvFmMKpshURTKVK45RfCsOrtAu60MCaj27JOD4OsXEAjEl3XG5RUfTphgbUI oeXybqZNwAGqE4H03CqUJ3GEMuyaP6eokJuuxygk6AVS6XnJDW1JR7u84fDTOwZ8aMVg yCQ57IfyDf4txhIwYemZrj+Y5Cxr3VLrt4ERr/axEOJlPwcAc2Ada3dLOngF3DhnNhK2 +3aIS4VOgoirdJIcrppjShM1zLkZERLUOJ6eqqODcmkuw3itwknWjVD8Ok6jGUlAd5yX Wrdw== X-Forwarded-Encrypted: i=1; AHgh+RpFcKeDAYiIrMGF/W+CWkLcfDn0dLFpk+j4hItLezJrfRXaQjfmud1gPk/iOlpaH+xkC6bQLFuRlewvdZw=@vger.kernel.org X-Gm-Message-State: AFuF++nKfNmj96hULu68Z5dBqKLRNpvYs7hyZRQgw9duvvaQ97wmllO2 y/5LvFxNebVJ2QRTDn8fg/lx2DgmuNWmxCsHSF3Ff4lwqtTu+JOhNx8fAiJ0XowsvE5HrfTtO2c rm7tMcQ== X-Received: from ejcfb18.prod.google.com ([2002:a17:907:3a12:b0:c21:94a2:c168]) (user=lrizzo job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:c245:b0:c24:6f95:4f0e with SMTP id a640c23a62f3a-c2492719d94mr2282288466b.18.1787585376193; Mon, 24 Aug 2026 08:29:36 -0700 (PDT) Date: Mon, 24 Aug 2026 15:29:28 +0000 In-Reply-To: <20260824152932.1583506-1-lrizzo@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260615234220.3946885-1-lrizzo@google.com> <20260824152932.1583506-1-lrizzo@google.com> X-Mailer: git-send-email 2.55.0.766.g2966f0265a-goog Message-ID: <20260824152932.1583506-2-lrizzo@google.com> Subject: [PATCH v2 1/5] swiotlb: enforce pool nareas and nslabs invariants From: Luigi Rizzo To: Marek Szyprowski , Robin Murphy , Willem de Bruijn , Kuniyuki Iwashima , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Luigi Rizzo , Luigi Rizzo Cc: Greg Kroah-Hartman , Dragos Tatulea , "Rafael J . Wysocki" , Andrew Morton , David Hildenbrand , netdev@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux.dev, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 --- 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 #include #include +#include #include #include #include @@ -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