Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp3320456pxb; Sun, 29 Aug 2021 22:03:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw88c/c26rN8r0gntJowQX7PRisSOA4D01q4aDup/zNr2gk2BaTBqVmTzyq34oUclfc+bZf X-Received: by 2002:a05:6402:27c6:: with SMTP id c6mr22328935ede.111.1630299791533; Sun, 29 Aug 2021 22:03:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630299791; cv=none; d=google.com; s=arc-20160816; b=Z3V19CWm//CSsxEjKU13H9ABOqF7VXooQXzn7cyIhuoyUmCp61UVAfpym84AspGdjB ImU4P4fRRDYmVBjRiO2LjgzvovMxk8B4pO2pvVlAqeIIdRZYugo0C1EYmAWJfi776Ydz o+3kVKRTe30/F0Jt56FCjKSRq4UZwi7m1d4PQOEq3WI9QVQEuQB++hlpk5RuZfqDiaAI vI3Mnri08MPkfSy/7GOIhkI+Ud9Np4AM0vieDkOtR3lv4vNxsItTZtZxcAA02B2MhT+i L8XG/Daf45+eA0i0e+x7rXkFlDgMA2mbkAebVWYkxVxmcbkSGxizaqxoH31wHJIziy2k LaZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qJyHOvrGmzqaEJuP2+sKnDXbmHMGvrcvxnYLz7HnOUs=; b=bOfG+fsMWaXtzV81YB1A3zZqR2koVDUwEFZzZx1ZBh2RkFAmfdtJvVEaqtSQPdRp+G OqK5cZpFDMe9vqhftOlEh6fA8fAzCcjMVPsmvbLWlyRJnl4XOVHF5jEMuS8hpwiyB+YD eKE72RQB1s4Tvlwapv7aL2i5+HyUfGVzozxtkQMtkujKhRNUGYSKWtlfm58qoGZsX39x wiItHRAJ1kEOkV4bRJlFfQJJ1wBWa1EuOqDIF5lwBN1DFC3ThxPtbQTIvAa24xDPFX0G lKXzVnvLz4t+N6ZUxFBxVg7dQHMrYT1dumO47GF4Ztwll98aAW5ux4dgRSfzHFnPzKd8 uzGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bkvIYEfQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r24si14383952ejy.432.2021.08.29.22.02.47; Sun, 29 Aug 2021 22:03:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bkvIYEfQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232412AbhH3FBe (ORCPT + 99 others); Mon, 30 Aug 2021 01:01:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231722AbhH3FBb (ORCPT ); Mon, 30 Aug 2021 01:01:31 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65BADC0612E7 for ; Sun, 29 Aug 2021 22:00:35 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id c4so7815111plh.7 for ; Sun, 29 Aug 2021 22:00:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qJyHOvrGmzqaEJuP2+sKnDXbmHMGvrcvxnYLz7HnOUs=; b=bkvIYEfQm3cuzqyPdrzZ2ZneZYaPfTukGW2BJJadyr2kbZx+YpkCKMmOkAzIzXdZYW X2MKOBnkdETzOlc6jT9LPUxlooD9cEMNHybcrlFBU4wF8WldUOn12WDIXTumngjGvQrL 911JGkwyyXlKa5Df1VNSsr8eIw+FYNJ1F76V8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qJyHOvrGmzqaEJuP2+sKnDXbmHMGvrcvxnYLz7HnOUs=; b=WtEjBo9a2UCpjZsv4TNZsUo6tAL62/xqYZ2GePWcGqaERu48Kw8VyiHJPaKetYOpLg ZuW7jDXQpDYXGfT1bKMbsp7taShwSkJnPSs3Dv4ftEMGACegZbw6244VrsB8uuoL/7xj YJ5nXlhVyIYJywUjYr7MKYB5xgcxKWR/BxyI2MHHYOrpSqYhtNRz2DwQkMfq2It1Y5AA q/JKOvxPivNLnsoLopTC6jVo/FB4zWpihOsTsMhYmksY2ZrMmGWC6ABYBtWEQpEIsJJ5 URiv1HMYkcMkNNmld5Z2CivQXkxFwIIDUFww6SHZD5JzE1zOBf4qdxz5yp2djwYx6qmM ylPQ== X-Gm-Message-State: AOAM5313XY/IpKlGNVGG6BTMusCzsm+POhfMehPVDLW/2bYtQiqG7FuW 1R5bQXey7I7ubxKM1Aqori1CTg== X-Received: by 2002:a17:90a:a087:: with SMTP id r7mr37215191pjp.84.1630299634969; Sun, 29 Aug 2021 22:00:34 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:28a5:1b9f:af1d:5542]) by smtp.gmail.com with UTF8SMTPSA id k189sm2785564pfd.76.2021.08.29.22.00.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Aug 2021 22:00:34 -0700 (PDT) From: David Stevens X-Google-Original-From: David Stevens To: Robin Murphy , Christoph Hellwig Cc: Joerg Roedel , Will Deacon , Lu Baolu , Tom Murphy , Rajat Jain , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v7 6/7] swiotlb: support aligned swiotlb buffers Date: Mon, 30 Aug 2021 13:59:24 +0900 Message-Id: <20210830045925.4163412-7-stevensd@google.com> X-Mailer: git-send-email 2.33.0.259.gc128427fd7-goog In-Reply-To: <20210830045925.4163412-1-stevensd@google.com> References: <20210830045925.4163412-1-stevensd@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Stevens Add an argument to swiotlb_tbl_map_single that specifies the desired alignment of the allocated buffer. This is used by dma-iommu to ensure the buffer is aligned to the iova granule size when using swiotlb with untrusted sub-granule mappings. This addresses an issue where adjacent slots could be exposed to the untrusted device if IO_TLB_SIZE < iova granule < PAGE_SIZE. Signed-off-by: David Stevens Reviewed-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 4 ++-- drivers/xen/swiotlb-xen.c | 2 +- include/linux/swiotlb.h | 3 ++- kernel/dma/swiotlb.c | 11 +++++++---- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 714bec7a53c2..9b8c17c3d29b 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -817,8 +817,8 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, size_t padding_size; aligned_size = iova_align(iovad, size); - phys = swiotlb_tbl_map_single(dev, phys, size, - aligned_size, dir, attrs); + phys = swiotlb_tbl_map_single(dev, phys, size, aligned_size, + iova_mask(iovad), dir, attrs); if (phys == DMA_MAPPING_ERROR) return DMA_MAPPING_ERROR; diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 24d11861ac7d..8b03d2c93428 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -382,7 +382,7 @@ static dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page, */ trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force); - map = swiotlb_tbl_map_single(dev, phys, size, size, dir, attrs); + map = swiotlb_tbl_map_single(dev, phys, size, size, 0, dir, attrs); if (map == (phys_addr_t)DMA_MAPPING_ERROR) return DMA_MAPPING_ERROR; diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 216854a5e513..93d82e43eb3a 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -44,7 +44,8 @@ extern void __init swiotlb_update_mem_attributes(void); phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys, size_t mapping_size, size_t alloc_size, - enum dma_data_direction dir, unsigned long attrs); + unsigned int alloc_aligned_mask, enum dma_data_direction dir, + unsigned long attrs); extern void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index e50df8d8f87e..d4c45d8cd1fa 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -427,7 +427,7 @@ static unsigned int wrap_index(struct io_tlb_mem *mem, unsigned int index) * allocate a buffer from that IO TLB pool. */ static int find_slots(struct device *dev, phys_addr_t orig_addr, - size_t alloc_size) + size_t alloc_size, unsigned int alloc_align_mask) { struct io_tlb_mem *mem = io_tlb_default_mem; unsigned long boundary_mask = dma_get_seg_boundary(dev); @@ -450,6 +450,7 @@ static int find_slots(struct device *dev, phys_addr_t orig_addr, stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1; if (alloc_size >= PAGE_SIZE) stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT)); + stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1); spin_lock_irqsave(&mem->lock, flags); if (unlikely(nslots > mem->nslabs - mem->used)) @@ -504,7 +505,8 @@ static int find_slots(struct device *dev, phys_addr_t orig_addr, phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, size_t mapping_size, size_t alloc_size, - enum dma_data_direction dir, unsigned long attrs) + unsigned int alloc_align_mask, enum dma_data_direction dir, + unsigned long attrs) { struct io_tlb_mem *mem = io_tlb_default_mem; unsigned int offset = swiotlb_align_offset(dev, orig_addr); @@ -524,7 +526,8 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, return (phys_addr_t)DMA_MAPPING_ERROR; } - index = find_slots(dev, orig_addr, alloc_size + offset); + index = find_slots(dev, orig_addr, + alloc_size + offset, alloc_align_mask); if (index == -1) { if (!(attrs & DMA_ATTR_NO_WARN)) dev_warn_ratelimited(dev, @@ -636,7 +639,7 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size, swiotlb_force); - swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, dir, + swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir, attrs); if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR) return DMA_MAPPING_ERROR; -- 2.33.0.259.gc128427fd7-goog