Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1070427lql; Tue, 12 Mar 2024 06:42:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUF3XczsPnjYhoXpGnd1sMIqlzasjsd9479qLgIVrqxUKFN1H2YAkTHiJ1Mny1xLimTYA3a6P3137rOCBRx7NR3lYIsCTJ8rkkd76PbWA== X-Google-Smtp-Source: AGHT+IHGB2rw8vfjP2GdR5EL4TiInRC3hLqbDt0Wrn5aec+jeplA3KvnKDGOFp7/gc3qaS8I936L X-Received: by 2002:a17:90b:3007:b0:29b:bff0:d424 with SMTP id hg7-20020a17090b300700b0029bbff0d424mr7710268pjb.14.1710250953995; Tue, 12 Mar 2024 06:42:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710250953; cv=pass; d=google.com; s=arc-20160816; b=Y00d5m3i12mIFx+cFweXjsxxJrZB5+LJZFU0vNDYPrSrpC8u4DB3cCgdCQh1tJEiiJ aG0x+8IyBHxst+XEKu/YBXBNrqPAQvFk+yyJZDJJWiRwVJ/TLD/j7nJecsVt/FxAXYg3 rIGwB4Azzjm3zhBS0VivL3ysoPAGsGzsNOTp2SUNXtFspvRA4oCo3DvUh/7oE/aZ2cqO 1WTyWzUx45thS3VgA5U+yaR5zzqNwYPb5GVHVhhcTYdqv5KbaesGbyMROVRCHb4yWsyn /DVuUOBj6aRCtNcTLrvqaKisOqk/mtRRVXR3wRpy2D4jwNxI/SvKg+0lE9anpnXZopXW 1zUA== 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:message-id:date:subject:cc:to :from; bh=lPmRzsQTpYuxWtES9FAzyDi87HjeTHqou0J5oaARr/I=; fh=eMmvRW7kPtkKNBOZVQDPWpzNNgorsXyt3w/9SNxQ+3Q=; b=NB7cfdZ3qFhFeE7+hlKpJ8nGRQpNF3E3aibr+TfmCCUa+ZEptuzSunILOYiNmGKr9K OKDzGOz2oriK9VYp4JKZtqJGFU2Eonc1Oj2MazfEZE2lCo5cFTjOmlr+LUnXgPs/P/iy YCukL2Hjl3Yxx1mLCkR/xMgjWkP4SBiWcfhhQZ06mdXL9QDzBln/O8VSWFLLGaamouYv yS0BNeYp1Xd+KOu/P5Y6hH4b2Hkv2FO3Dr5Z8q0mg6fWR2e/phVgASXJ+YQI7fL3foEA qERbpk4A3tjCGN6/zfbpCKHdTm0aao7Et+GzBnUkOD6nxT/FIXYgb7WrG0kbrAQG8TPK 1nDg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-100299-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100299-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h15-20020a17090a9c0f00b0029bc4ae394esi6866200pjp.92.2024.03.12.06.42.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 06:42:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100299-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-100299-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100299-linux.lists.archive=gmail.com@vger.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id A98C3B220EF for ; Tue, 12 Mar 2024 13:42:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E793E2AE66; Tue, 12 Mar 2024 13:42:25 +0000 (UTC) Received: from frasgout13.his.huawei.com (frasgout13.his.huawei.com [14.137.139.46]) (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 965B27A700 for ; Tue, 12 Mar 2024 13:42:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=14.137.139.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710250945; cv=none; b=MiWqcdO8Cr6WzXgBTEuSpFaH9vH61acfZ8pDC2LaFLsWmauH8dfV8KF/p2iM2jjB+X1FIG+Sf79wWKgU++MvSayUkjHi+soMTUxc4sl19Xn0pCFP0Ts/VbxUAk31tdgrRH/ar2VU142fuYAAvNnUSyeyOszi4bYVPv9gGdIc+iA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710250945; c=relaxed/simple; bh=4LOfLz//4stF1IyW6kG8Qzj1vCEfzfpLV6bnrTyxpQI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=MNFaBNCSDUyMnQuUBDvNvYW8fQW1W72JYWJi5tLTBQV7Ii4ou/8RbMaltTtaRLWMjjTdz1NcPit1LaU75tBSNpBfGdRMPpto0vNQHRE33hm/9MNPviHqWIBPthNsN76eyDJC5evEds178NWK2D+lT5v/3S/tkb1s56lKBTh2hJk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=14.137.139.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.18.186.29]) by frasgout13.his.huawei.com (SkyGuard) with ESMTP id 4TvDtr1lCPz9xsCv for ; Tue, 12 Mar 2024 21:26:28 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.47]) by mail.maildlp.com (Postfix) with ESMTP id 272F1140890 for ; Tue, 12 Mar 2024 21:42:09 +0800 (CST) Received: from huaweicloud.com (unknown [10.48.131.93]) by APP1 (Coremail) with SMTP id LxC2BwD3oBmmW_BlDi4zBA--.21282S2; Tue, 12 Mar 2024 14:42:08 +0100 (CET) From: Petr Tesarik To: Christoph Hellwig , Marek Szyprowski , Robin Murphy , Will Deacon , Michael Kelley , Nicolin Chen , iommu@lists.linux.dev (open list:DMA MAPPING HELPERS), linux-kernel@vger.kernel.org (open list) Cc: Roberto Sassu , Petr Tesarik Subject: [PATCH 1/1] swiotlb: extend buffer pre-padding to alloc_align_mask if necessary Date: Tue, 12 Mar 2024 14:41:49 +0100 Message-Id: <20240312134149.497-1-petrtesarik@huaweicloud.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:LxC2BwD3oBmmW_BlDi4zBA--.21282S2 X-Coremail-Antispam: 1UD129KBjvJXoW3Xw4rZr4xXw1fZw4fKrW5trb_yoW7XrW3pF 4fKa1rKFWDJFyxCanFka18KF1F934kAry5CF4S9ryY9r1kXF98XF98A34Yga4FqFWv9FW2 qFyrur40kF47Jr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBS14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26r1j6r1xM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4j 6F4UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4j6r 4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0 I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r 4UM4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628v n2kIc2xKxwCY1x0262kKe7AKxVW8ZVWrXwCY1x0264kExVAvwVAq07x20xyl42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE 42xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87 Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjTRWbyCUUUUU X-CM-SenderInfo: hshw23xhvd2x3n6k3tpzhluzxrxghudrp/ From: Petr Tesarik Allow a buffer pre-padding of up to alloc_align_mask. If the allocation alignment is bigger than IO_TLB_SIZE and min_align_mask covers any non-zero bits in the original address between IO_TLB_SIZE and alloc_align_mask, these bits are not preserved in the swiotlb buffer address. To fix this case, increase the allocation size and use a larger offset within the allocated buffer. As a result, extra padding slots may be allocated before the mapping start address. Set the orig_addr in these padding slots to INVALID_PHYS_ADDR, because they do not correspond to any CPU buffer and the data must never be synced. The padding slots should be automatically released when the buffer is unmapped. However, swiotlb_tbl_unmap_single() takes only the address of the DMA buffer slot, not the first padding slot. Save the number of padding slots in struct io_tlb_slot and use it to adjust the slot index in swiotlb_release_slots(), so all allocated slots are properly freed. Fixes: 2fd4fa5d3fb5 ("swiotlb: Fix alignment checks when both allocation and DMA masks are present") Link: https://lore.kernel.org/linux-iommu/20240311210507.217daf8b@meshulam.tesarici.cz/ Signed-off-by: Petr Tesarik --- kernel/dma/swiotlb.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 86fe172b5958..8ce11abc691f 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -69,11 +69,13 @@ * @alloc_size: Size of the allocated buffer. * @list: The free list describing the number of free entries available * from each index. + * @padding: Number of preceding padding slots. */ struct io_tlb_slot { phys_addr_t orig_addr; size_t alloc_size; unsigned int list; + unsigned int padding; }; static bool swiotlb_force_bounce; @@ -287,6 +289,7 @@ static void swiotlb_init_io_tlb_pool(struct io_tlb_pool *mem, phys_addr_t start, mem->nslabs - i); mem->slots[i].orig_addr = INVALID_PHYS_ADDR; mem->slots[i].alloc_size = 0; + mem->slots[i].padding = 0; } memset(vaddr, 0, bytes); @@ -1328,11 +1331,12 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, unsigned long attrs) { struct io_tlb_mem *mem = dev->dma_io_tlb_mem; - unsigned int offset = swiotlb_align_offset(dev, orig_addr); + unsigned int offset; struct io_tlb_pool *pool; unsigned int i; int index; phys_addr_t tlb_addr; + unsigned int padding; if (!mem || !mem->nslabs) { dev_warn_ratelimited(dev, @@ -1349,6 +1353,15 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, return (phys_addr_t)DMA_MAPPING_ERROR; } + /* + * Calculate buffer pre-padding within the allocated space. Use it to + * preserve the low bits of the original address according to device's + * min_align_mask. Limit the padding to alloc_align_mask or slot size + * (whichever is bigger); higher bits of the original address are + * preserved by selecting a suitable IO TLB slot. + */ + offset = orig_addr & dma_get_min_align_mask(dev) & + (alloc_align_mask | (IO_TLB_SIZE - 1)); index = swiotlb_find_slots(dev, orig_addr, alloc_size + offset, alloc_align_mask, &pool); if (index == -1) { @@ -1364,6 +1377,12 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, * This is needed when we sync the memory. Then we sync the buffer if * needed. */ + padding = 0; + while (offset >= IO_TLB_SIZE) { + pool->slots[index++].orig_addr = INVALID_PHYS_ADDR; + pool->slots[index].padding = ++padding; + offset -= IO_TLB_SIZE; + } for (i = 0; i < nr_slots(alloc_size + offset); i++) pool->slots[index + i].orig_addr = slot_addr(orig_addr, i); tlb_addr = slot_addr(pool->start, index) + offset; @@ -1385,12 +1404,16 @@ static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr) struct io_tlb_pool *mem = swiotlb_find_pool(dev, tlb_addr); unsigned long flags; unsigned int offset = swiotlb_align_offset(dev, tlb_addr); - int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT; - int nslots = nr_slots(mem->slots[index].alloc_size + offset); - int aindex = index / mem->area_nslabs; - struct io_tlb_area *area = &mem->areas[aindex]; + int index, nslots, aindex; + struct io_tlb_area *area; int count, i; + index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT; + index -= mem->slots[index].padding; + nslots = nr_slots(mem->slots[index].alloc_size + offset); + aindex = index / mem->area_nslabs; + area = &mem->areas[aindex]; + /* * Return the buffer to the free list by setting the corresponding * entries to indicate the number of contiguous entries available. @@ -1413,6 +1436,7 @@ static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr) mem->slots[i].list = ++count; mem->slots[i].orig_addr = INVALID_PHYS_ADDR; mem->slots[i].alloc_size = 0; + mem->slots[i].padding = 0; } /* -- 2.34.1