Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp630843lqc; Fri, 8 Mar 2024 07:29:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXKftrjmDd4Bh6BNc1SnwH8Ve9aKyAJm4Ouoytz4K19OJo/+tic2sywmObFVd7PJORdTnKJrvJqoiDJLb6haxkuc1r7AeugK7y2+2yYbQ== X-Google-Smtp-Source: AGHT+IFvQ/AKzyYYDCmXutbsMMA2MfCrjH3hdbyZjmhtQsg0dRCWIudSoavxavmFxf3cx4Vwjep1 X-Received: by 2002:a05:622a:648:b0:42e:a6d3:8822 with SMTP id a8-20020a05622a064800b0042ea6d38822mr311866qtb.35.1709911772522; Fri, 08 Mar 2024 07:29:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709911772; cv=pass; d=google.com; s=arc-20160816; b=Eo/KKJZL8Y/i3Pjb7yOomqBEhjESC/9uabl66jd7g0107j+BwPcufZimN3DXbUoQCr HzBODz2g+tABnq6y+zxCYNlUPdgATuIfePwnz6yitSBtUlTomqYmfukpa8yomNPqovox uo8gpAQrlDvgo/f2O2g1DvmQzfLOEAuWnF5B6GGczZq2hpcZKZXXokTqM4ZLou1ZuR1P 4jNZF7bVZ12Owk2gjv7wFuIwUqw4CebIgnqPI52xn2AIl4AZYWzVgMYQB1yaohMF4qxi uwFyiIhOZuFjQ7tK/mEAcwbZRYvlrEKDDpHvCATlaC0L/yHF3vwLaPQsZUQDsEY/+8kx ILUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=z306nxRjUkanpQy+db/pJfCu4m4bh3T/sLBDYVeUVK8=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=u2XNNWH7BB+EDyEM0Sk0osL8RKPnFX5Dtb1WC7ySTvf9jFEBtgnhx61CHe9XmRSJB3 NICql5Ndb7rwJ9zTngwSs8IpYjTndjZ6aRyy1X/t4qUiAhRM1ZkJ6y0EdNbSIAnS8YaA WClkl10+J9eHAmSva/J6lc2MmYHJ+dTAMxsM2mnLKmpl7IJp3DOwVgg1bREpYTax99mB CP3l8U0FlVX/JD1gCFNd5V1W8gggUP3JSE9J200DxUfZ6PmCIiHYUszVxB55fUb2NhyO IWF0SvvCf9B/pCIQ343XUpOd3lpB0eNpR7TZb9S8bCTtLaJ/usGdUpLgMaeTx9lcPnT9 S2Tg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iTUGVGZz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-97234-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97234-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j18-20020ac85c52000000b0042ebbe19dd5si7887163qtj.73.2024.03.08.07.29.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 07:29:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97234-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iTUGVGZz; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-97234-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97234-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 45ED41C21894 for ; Fri, 8 Mar 2024 15:29:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 442631EF1E; Fri, 8 Mar 2024 15:28:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iTUGVGZz" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AD1F1EEFD; Fri, 8 Mar 2024 15:28:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709911730; cv=none; b=FnqfgdJzvhV1wqIy+os+vJWSsRNqXSoD1HupzZtuo5/5oWqoGz78Vb5Ox2nko7gMfG2v/iI30lAGcGmMnTJXH67edzlymDWya/2gVpYmiJKQPQVioQdcR3e3922aWF3XsZzUxzQ8ITeekWKxniZQ+ahlLZSLtYlCO9O3rgUkdu8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709911730; c=relaxed/simple; bh=n76N/QmTxWzDYO3g7VF7EKTPs6uGGLuTDor+G+M6gCk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k9plknX4LUBMv30vGIditcy70Ljq/4+U29OJAX9X+95KKqNUpPYg303dmvZGsvNN7a7DZ4ijrEBDmtXssey+portSir27zqHK6p7aMKwflq/jWaVamkOmcYoPspR5e9PGV14WtN8hwPs1pPJXcmFBWS0g/PO5PwC3ZQOqk5vkAE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iTUGVGZz; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3782C433F1; Fri, 8 Mar 2024 15:28:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709911730; bh=n76N/QmTxWzDYO3g7VF7EKTPs6uGGLuTDor+G+M6gCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iTUGVGZz5UkT+wKearzCBeceKmqF7KhauGIuliGwu3VwgUiqOjnIygC569njryNLU 3O3iSRsZrDckfRk45+HSxH6MhWSLe2lOxbhh46wWJiXqNiHppDomb6uXKyBiC7WfDR MiCdBsEV7z6XSOvyKwof/V6kW1pS4swtLTQW7xSOotAsbV/NiScHmuB4XY5MLXPHOl Kc6mpbe2FQejG55weRLA5aEHN/DRC9nqkdlhIIPSr9h0KpfnIkV9Y2dITdw3aTGLSd GAIrHnBfr+Rc6dASfIa0RWsfEHCgZ/KEg+Cskl818j00KlRj5gioBBjX0ZfYRFmfbu 4+pmM9+zWELZQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: kernel-team@android.com, Will Deacon , iommu@lists.linux.dev, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Dexuan Cui , Nicolin Chen , Michael Kelley Subject: [PATCH v6 4/6] swiotlb: Fix alignment checks when both allocation and DMA masks are present Date: Fri, 8 Mar 2024 15:28:27 +0000 Message-Id: <20240308152829.25754-5-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240308152829.25754-1-will@kernel.org> References: <20240308152829.25754-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Nicolin reports that swiotlb buffer allocations fail for an NVME device behind an IOMMU using 64KiB pages. This is because we end up with a minimum allocation alignment of 64KiB (for the IOMMU to map the buffer safely) but a minimum DMA alignment mask corresponding to a 4KiB NVME page (i.e. preserving the 4KiB page offset from the original allocation). If the original address is not 4KiB-aligned, the allocation will fail because swiotlb_search_pool_area() erroneously compares these unmasked bits with the 64KiB-aligned candidate allocation. Tweak swiotlb_search_pool_area() so that the DMA alignment mask is reduced based on the required alignment of the allocation. Fixes: 82612d66d51d ("iommu: Allow the dma-iommu api to use bounce buffers") Reported-by: Nicolin Chen Link: https://lore.kernel.org/r/cover.1707851466.git.nicolinc@nvidia.com Tested-by: Nicolin Chen Reviewed-by: Michael Kelley Signed-off-by: Will Deacon --- kernel/dma/swiotlb.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index c20324fba814..c381a7ed718f 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -981,8 +981,7 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(dev, pool->start) & boundary_mask; unsigned long max_slots = get_max_slots(boundary_mask); - unsigned int iotlb_align_mask = - dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); + unsigned int iotlb_align_mask = dma_get_min_align_mask(dev); unsigned int nslots = nr_slots(alloc_size), stride; unsigned int offset = swiotlb_align_offset(dev, orig_addr); unsigned int index, slots_checked, count = 0, i; @@ -993,6 +992,14 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool BUG_ON(!nslots); BUG_ON(area_index >= pool->nareas); + /* + * Ensure that the allocation is at least slot-aligned and update + * 'iotlb_align_mask' to ignore bits that will be preserved when + * offsetting into the allocation. + */ + alloc_align_mask |= (IO_TLB_SIZE - 1); + iotlb_align_mask &= ~alloc_align_mask; + /* * For mappings with an alignment requirement don't bother looping to * unaligned slots once we found an aligned one. -- 2.44.0.278.ge034bb2e1d-goog