Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 0E4251B0F23 for ; Fri, 18 Jul 2025 02:41:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752806494; cv=none; b=fvhwdn4/S9P26NeNaUVXysWWpcymjeNl6wHFnt0xrTmHz7zaZRxYjmSIKxCTFnmbyzO5HvVpVubEEvr+Mz9CVBebbH9tRrf0pLblVSvnA6LtQ+A62LtCcBeoF2CpnEzLIDIOb7M21wbdyiDQDe9FpZHsBI/EViW4mrnyKkFILQk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752806494; c=relaxed/simple; bh=tKRAAZvIgcD9V7uzb3SLhO+RCydf8PsdTyyxa1wpOS4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=pHUhOidrtSPvowUZtdtkY5qIgE+zKfG3NmzfKEFnl0shWU2pEwC1Aj+Ov1Cx9LBqBbdBI59/oLIKmMWn4AXUwsAWBwXs6aduBH7VtyIjEATn9QxPKXT42y32j62Zs2QEvcqXO1pWN/E7XVrz6JGQld2GWQGrmA8Wn9nM2EF8AkU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Ep1LJZBT; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ep1LJZBT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752806492; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g/OxXeqbeKEs2+kXeL4cq4sS7WnEqGRjB8HxGWpN4Do=; b=Ep1LJZBTstz+m7a82ZTr6v+B4qhhIUNQGAfg7tlLcUycF9OCDP+UobYPk/SFBv9uF8PY56 11L+X/4yLDmSwCIqNq17+ibRq9KVW3frWHSkHsljUaBUTwuNR0x+LbsHAkmRoTs0bKzMCY eHeE5/x4UTD5xZm+WRFCidaYJN4NdFc= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-206-ldniAQEyNtCc3q4aNLPZXg-1; Thu, 17 Jul 2025 22:38:16 -0400 X-MC-Unique: ldniAQEyNtCc3q4aNLPZXg-1 X-Mimecast-MFC-AGG-ID: ldniAQEyNtCc3q4aNLPZXg_1752806295 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1E9B91956088; Fri, 18 Jul 2025 02:38:15 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.207]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A137830001B1; Fri, 18 Jul 2025 02:38:11 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, hch@infradead.org, Christoph Hellwig Subject: [PATCH V3 2/9] virtio_ring: switch to use dma_{map|unmap}_page() Date: Fri, 18 Jul 2025 10:37:51 +0800 Message-ID: <20250718023758.36866-3-jasowang@redhat.com> In-Reply-To: <20250718023758.36866-1-jasowang@redhat.com> References: <20250718023758.36866-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: V2rFbAupDoLFMo_u8jRDgpYEvJhVIJSfDxkfW16jCBg_1752806295 X-Mimecast-Originator: redhat.com Content-type: text/plain Content-Transfer-Encoding: 8bit This patch switches to use dma_{map|unmap}_page() to reduce the coverage of DMA operations. This would help for the following rework on the virtio map operations. Reviewed-by: Christoph Hellwig Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 55 +++++++++++++++--------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 103bad8cffff..75e5f6336c8d 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -405,8 +405,8 @@ static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, if (!vq->use_dma_api) return (dma_addr_t)virt_to_phys(cpu_addr); - return dma_map_single(vring_dma_dev(vq), - cpu_addr, size, direction); + return virtqueue_dma_map_single_attrs(&vq->vq, cpu_addr, + size, direction, 0); } static int vring_mapping_error(const struct vring_virtqueue *vq, @@ -451,22 +451,14 @@ static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, if (flags & VRING_DESC_F_INDIRECT) { if (!vq->use_dma_api) goto out; + } else if (!vring_need_unmap_buffer(vq, extra)) + goto out; - dma_unmap_single(vring_dma_dev(vq), - extra->addr, - extra->len, - (flags & VRING_DESC_F_WRITE) ? - DMA_FROM_DEVICE : DMA_TO_DEVICE); - } else { - if (!vring_need_unmap_buffer(vq, extra)) - goto out; - - dma_unmap_page(vring_dma_dev(vq), - extra->addr, - extra->len, - (flags & VRING_DESC_F_WRITE) ? - DMA_FROM_DEVICE : DMA_TO_DEVICE); - } + dma_unmap_page(vring_dma_dev(vq), + extra->addr, + extra->len, + (flags & VRING_DESC_F_WRITE) ? + DMA_FROM_DEVICE : DMA_TO_DEVICE); out: return extra->next; @@ -1276,20 +1268,13 @@ static void vring_unmap_extra_packed(const struct vring_virtqueue *vq, if (flags & VRING_DESC_F_INDIRECT) { if (!vq->use_dma_api) return; + } else if (!vring_need_unmap_buffer(vq, extra)) + return; - dma_unmap_single(vring_dma_dev(vq), - extra->addr, extra->len, - (flags & VRING_DESC_F_WRITE) ? - DMA_FROM_DEVICE : DMA_TO_DEVICE); - } else { - if (!vring_need_unmap_buffer(vq, extra)) - return; - - dma_unmap_page(vring_dma_dev(vq), - extra->addr, extra->len, - (flags & VRING_DESC_F_WRITE) ? - DMA_FROM_DEVICE : DMA_TO_DEVICE); - } + dma_unmap_page(vring_dma_dev(vq), + extra->addr, extra->len, + (flags & VRING_DESC_F_WRITE) ? + DMA_FROM_DEVICE : DMA_TO_DEVICE); } static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg, @@ -3161,7 +3146,13 @@ dma_addr_t virtqueue_dma_map_single_attrs(const struct virtqueue *_vq, void *ptr return (dma_addr_t)virt_to_phys(ptr); } - return dma_map_single_attrs(vring_dma_dev(vq), ptr, size, dir, attrs); + /* DMA must never operate on areas that might be remapped. */ + if (dev_WARN_ONCE(&_vq->vdev->dev, is_vmalloc_addr(ptr), + "rejecting DMA map of vmalloc memory\n")) + return DMA_MAPPING_ERROR; + + return dma_map_page_attrs(vring_dma_dev(vq), virt_to_page(ptr), + offset_in_page(ptr), size, dir, attrs); } EXPORT_SYMBOL_GPL(virtqueue_dma_map_single_attrs); @@ -3186,7 +3177,7 @@ void virtqueue_dma_unmap_single_attrs(const struct virtqueue *_vq, if (!vq->use_dma_api) return; - dma_unmap_single_attrs(vring_dma_dev(vq), addr, size, dir, attrs); + dma_unmap_page_attrs(vring_dma_dev(vq), addr, size, dir, attrs); } EXPORT_SYMBOL_GPL(virtqueue_dma_unmap_single_attrs); -- 2.42.0