From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 273F61940A1 for ; Tue, 1 Jul 2025 01:14:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332466; cv=none; b=mtpsbeVmRwg3iKl0Z6wUQ8b2nnD0TwDsWY9IdHKIkGALbf0vS1TQ2LiO3UBVTtPUd/kvc7HwQRZuRwCMOcXqoLn4bMcqAPrHtak23O/zXLumEYpyD7DZ1nPSLmgPerKMvw8JmA91rZn8Q9GMjjfRmBc7pqzc5ENOUmuiVGxth8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332466; c=relaxed/simple; bh=eBvYFg9tq7pmKh3r3mOIic3BXAndXFSpP1qIufBgdC8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eKgOi9/2o5z5ihbw4ikDA8YkGbvzkCJhxbKPpma/0cmeSwSn7HeJCyC6rp4ybhKdUjf6xHzgHz6uSBF+tdJc72uuqW0dXLDBBU66IZyHsAop5OeRCnym4DTBTC4OFrjqY7Ie7ntyFSugIunZJfzZPWf2teyc2W68xj22YKXZZZ4= 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=XluP8WkC; arc=none smtp.client-ip=170.10.129.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="XluP8WkC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332461; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o4ZnRreTmlOViqHLXW0Hu2Tg5DRBen9ZU6hQ3CIkd+k=; b=XluP8WkC2WVE8sZzslb5VXx8/VpzJn/ozGbV8U/ax6hOWCnQwmy14ComNYLVP6tFj9FBnq utSq22GvcOqa9DEojP4f+NQ7u88dj5eaTZAJ24Eehx50zIuGmh6pbbPos8G481qDlmLqlk 6NVC7Cnc6TvqLbOTmECxJJtC402/Vh0= Received: from mx-prod-mc-04.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-393-m1FC4r0dPOu7JZ5a74WwAA-1; Mon, 30 Jun 2025 21:14:17 -0400 X-MC-Unique: m1FC4r0dPOu7JZ5a74WwAA-1 X-Mimecast-MFC-AGG-ID: m1FC4r0dPOu7JZ5a74WwAA_1751332456 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7C50D1954215; Tue, 1 Jul 2025 01:14:16 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 39EC030001B1; Tue, 1 Jul 2025 01:14:10 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 1/9] virtio_ring: constify virtqueue pointer for DMA helpers Date: Tue, 1 Jul 2025 09:13:53 +0800 Message-ID: <20250701011401.74851-2-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" This patch consities virtqueue point for DMA helpers. Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 25 +++++++++++++------------ include/linux/virtio.h | 12 ++++++------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index b784aab66867..291d93d4a613 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -3141,12 +3141,12 @@ EXPORT_SYMBOL_GPL(virtqueue_get_vring); * * return DMA address. Caller should check that by virtqueue_dma_mapping_e= rror(). */ -dma_addr_t virtqueue_dma_map_single_attrs(struct virtqueue *_vq, void *ptr, +dma_addr_t virtqueue_dma_map_single_attrs(const struct virtqueue *_vq, voi= d *ptr, size_t size, enum dma_data_direction dir, unsigned long attrs) { - struct vring_virtqueue *vq =3D to_vvq(_vq); + const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (!vq->use_dma_api) { kmsan_handle_dma(virt_to_page(ptr), offset_in_page(ptr), size, dir); @@ -3168,11 +3168,12 @@ EXPORT_SYMBOL_GPL(virtqueue_dma_map_single_attrs); * Unmap the address that is mapped by the virtqueue_dma_map_* APIs. * */ -void virtqueue_dma_unmap_single_attrs(struct virtqueue *_vq, dma_addr_t ad= dr, +void virtqueue_dma_unmap_single_attrs(const struct virtqueue *_vq, + dma_addr_t addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { - struct vring_virtqueue *vq =3D to_vvq(_vq); + const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (!vq->use_dma_api) return; @@ -3188,9 +3189,9 @@ EXPORT_SYMBOL_GPL(virtqueue_dma_unmap_single_attrs); * * Returns 0 means dma valid. Other means invalid dma address. */ -int virtqueue_dma_mapping_error(struct virtqueue *_vq, dma_addr_t addr) +int virtqueue_dma_mapping_error(const struct virtqueue *_vq, dma_addr_t ad= dr) { - struct vring_virtqueue *vq =3D to_vvq(_vq); + const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (!vq->use_dma_api) return 0; @@ -3209,9 +3210,9 @@ EXPORT_SYMBOL_GPL(virtqueue_dma_mapping_error); * * return bool */ -bool virtqueue_dma_need_sync(struct virtqueue *_vq, dma_addr_t addr) +bool virtqueue_dma_need_sync(const struct virtqueue *_vq, dma_addr_t addr) { - struct vring_virtqueue *vq =3D to_vvq(_vq); + const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (!vq->use_dma_api) return false; @@ -3232,12 +3233,12 @@ EXPORT_SYMBOL_GPL(virtqueue_dma_need_sync); * the DMA address really needs to be synchronized * */ -void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq, +void virtqueue_dma_sync_single_range_for_cpu(const struct virtqueue *_vq, dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir) { - struct vring_virtqueue *vq =3D to_vvq(_vq); + const struct vring_virtqueue *vq =3D to_vvq(_vq); struct device *dev =3D vring_dma_dev(vq); =20 if (!vq->use_dma_api) @@ -3258,12 +3259,12 @@ EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_f= or_cpu); * Before calling this function, use virtqueue_dma_need_sync() to confirm = that * the DMA address really needs to be synchronized */ -void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq, +void virtqueue_dma_sync_single_range_for_device(const struct virtqueue *_v= q, dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir) { - struct vring_virtqueue *vq =3D to_vvq(_vq); + const struct vring_virtqueue *vq =3D to_vvq(_vq); struct device *dev =3D vring_dma_dev(vq); =20 if (!vq->use_dma_api) diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 64cb4b04be7a..8c0a3165e754 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -259,18 +259,18 @@ void unregister_virtio_driver(struct virtio_driver *d= rv); module_driver(__virtio_driver, register_virtio_driver, \ unregister_virtio_driver) =20 -dma_addr_t virtqueue_dma_map_single_attrs(struct virtqueue *_vq, void *ptr= , size_t size, +dma_addr_t virtqueue_dma_map_single_attrs(const struct virtqueue *_vq, voi= d *ptr, size_t size, enum dma_data_direction dir, unsigned long attrs); -void virtqueue_dma_unmap_single_attrs(struct virtqueue *_vq, dma_addr_t ad= dr, +void virtqueue_dma_unmap_single_attrs(const struct virtqueue *_vq, dma_add= r_t addr, size_t size, enum dma_data_direction dir, unsigned long attrs); -int virtqueue_dma_mapping_error(struct virtqueue *_vq, dma_addr_t addr); +int virtqueue_dma_mapping_error(const struct virtqueue *_vq, dma_addr_t ad= dr); =20 -bool virtqueue_dma_need_sync(struct virtqueue *_vq, dma_addr_t addr); -void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq, dma_ad= dr_t addr, +bool virtqueue_dma_need_sync(const struct virtqueue *_vq, dma_addr_t addr); +void virtqueue_dma_sync_single_range_for_cpu(const struct virtqueue *_vq, = dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir); -void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq, dma= _addr_t addr, +void virtqueue_dma_sync_single_range_for_device(const struct virtqueue *_v= q, dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir); =20 --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 8A2EF197A6C for ; Tue, 1 Jul 2025 01:14:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332472; cv=none; b=cPmqgNQjukzjI41af4cK0b9X/pTtg5mhnDmdO5kH7bQg9/2D90KuICOek4QBssrVy9UwZXPB4IawIDC/6ZMl7OSR4r5y1JvXrtYBatHPizxQOiv0hOrVFEjHZzrOhQEl3a2ppBNFHUQJDeCayPdMPI2zSwG1+9N7tG1vozkHVJY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332472; c=relaxed/simple; bh=rjcmnDhxSJw+MvMwFg+91jmRX4tBaB+vRVTeQNiXTcE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AAsA1v1u85hq0dC3XRZLfw9sc2I+iUkVjakwSREDDOxUTN0at/gOAjwAkJhUWtvcnACimsmG2I/AoIDy5u7LawAQZLEdJdQQWLVL6XASABXzBa400zdO31gmVy6QqylGLMUeiBLjjjxn7pHVPf5G/KR05ElGxRTCT8tE0vJmBHI= 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=fSVwLXs6; arc=none smtp.client-ip=170.10.129.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="fSVwLXs6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6kbR3eYQb0FlsO0demrZEJQtyuEYA1enmKkQtGgvdec=; b=fSVwLXs6CeWVvvJ+1pKIjI4yj0Uep24WukEM1IJGIBuWvT98xIn+jH4AvTP9574adHJNOv 84CcwxvfBwjepd/SlVPAs4tkxfMX40zRt4Yv+7YVz4CzmZsR/dKw38snkaPWWQLhkwUP9y HV7Q0MwnZKO6p0gCrc7qgHRi2w+Nt+s= Received: from mx-prod-mc-02.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-211-AHLyKEmNNNC1Zmk4pD38KA-1; Mon, 30 Jun 2025 21:14:24 -0400 X-MC-Unique: AHLyKEmNNNC1Zmk4pD38KA-1 X-Mimecast-MFC-AGG-ID: AHLyKEmNNNC1Zmk4pD38KA_1751332463 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C3DA41956086; Tue, 1 Jul 2025 01:14:22 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8190E30001B5; Tue, 1 Jul 2025 01:14:17 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 2/9] virtio_ring: switch to use dma_{map|unmap}_page() Date: Tue, 1 Jul 2025 09:13:54 +0800 Message-ID: <20250701011401.74851-3-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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. 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 291d93d4a613..04d88502a685 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_v= irtqueue *vq, if (!vq->use_dma_api) return (dma_addr_t)virt_to_phys(cpu_addr); =20 - 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); } =20 static int vring_mapping_error(const struct vring_virtqueue *vq, @@ -451,22 +451,14 @@ static unsigned int vring_unmap_one_split(const struc= t 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; =20 - 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); =20 out: return extra->next; @@ -1276,20 +1268,13 @@ static void vring_unmap_extra_packed(const struct v= ring_virtqueue *vq, if (flags & VRING_DESC_F_INDIRECT) { if (!vq->use_dma_api) return; + } else if (!vring_need_unmap_buffer(vq, extra)) + return; =20 - 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); } =20 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_= sg, @@ -3153,7 +3138,13 @@ dma_addr_t virtqueue_dma_map_single_attrs(const stru= ct virtqueue *_vq, void *ptr return (dma_addr_t)virt_to_phys(ptr); } =20 - 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); =20 @@ -3178,7 +3169,7 @@ void virtqueue_dma_unmap_single_attrs(const struct vi= rtqueue *_vq, if (!vq->use_dma_api) return; =20 - 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); =20 --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 2A7DC1A0BC5 for ; Tue, 1 Jul 2025 01:14:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332477; cv=none; b=NSBaoyX2l3jZafbtbhqHXdeXXRmw69zC8+J3QmbOdIeenAS6Z+ek4HH4BeaWmn6Fxno9lllLLQzUiok0ETItl4dCWJwbY5ZQ/sFAGnu78v54x1puw7/XiWhU+a+TMGJ+qfKoYfi/kjExg+6rnvAUeeotU3lwErVVPyGUxzs+EKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332477; c=relaxed/simple; bh=6IcBgKkww0Gtd8wYRB6Zo/1cLdz+spKU1SQlpcQLEr4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e1YOnISet7FExsM/vCv5v8bnQL0/uebHgKfk2nc5YIYJYaV9zBDcJOyqz56F2jznVsNZ2QVFiY7iFXv4UZ7BoE7MYh4Fap8cj2kjDK2wFQXbgsD3aeNvxuE6Zn90TRnW83NbagRg2RRwNLI9q+IGqPLxkhheHAQarcdos+jttRY= 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=Q77KHeSU; arc=none smtp.client-ip=170.10.129.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="Q77KHeSU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332473; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d1YJAWiVeR1Z3d/6uV73THC6e7egT1xA04L6cikCEnw=; b=Q77KHeSUV9PcuntG28Hgni9AGxyZ9iTHp2nx0rbapXfHL4QiVzCeLKUf2KPwnYKI/0U7EA KGiGPcRV4rdSTKedWGhIsrrkO8E8ClNAiadpKOsh0g9hEe/uTn3qwF3YLoyBH0mQoynpfr hqnL7aKKRX4Kqb6q3Ent5tlqc4Da1i8= Received: from mx-prod-mc-02.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-471-0r5JGp3COEaCrzoYgwK-oA-1; Mon, 30 Jun 2025 21:14:30 -0400 X-MC-Unique: 0r5JGp3COEaCrzoYgwK-oA-1 X-Mimecast-MFC-AGG-ID: 0r5JGp3COEaCrzoYgwK-oA_1751332469 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 16B1F1955EC3; Tue, 1 Jul 2025 01:14:29 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 261B630001B1; Tue, 1 Jul 2025 01:14:23 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 3/9] virtio: rename dma helpers Date: Tue, 1 Jul 2025 09:13:55 +0800 Message-ID: <20250701011401.74851-4-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Following patch will introduce virtio mapping fucntion to avoid abusing DMA API for device that doesn't do DMA. To ease the introduction, this patch rename "dma" to "map" for the current dma mapping helpers. Signed-off-by: Jason Wang --- drivers/net/virtio_net.c | 28 ++++----- drivers/virtio/virtio_ring.c | 114 +++++++++++++++++------------------ include/linux/virtio.h | 12 ++-- 3 files changed, 77 insertions(+), 77 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index e53ba600605a..39bcb85335d5 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -913,7 +913,7 @@ static void virtnet_rq_unmap(struct receive_queue *rq, = void *buf, u32 len) if (dma->need_sync && len) { offset =3D buf - (head + sizeof(*dma)); =20 - virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr, + virtqueue_map_sync_single_range_for_cpu(rq->vq, dma->addr, offset, len, DMA_FROM_DEVICE); } @@ -921,8 +921,8 @@ static void virtnet_rq_unmap(struct receive_queue *rq, = void *buf, u32 len) if (dma->ref) return; =20 - virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len, - DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); + virtqueue_unmap_single_attrs(rq->vq, dma->addr, dma->len, + DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); put_page(page); } =20 @@ -989,13 +989,13 @@ static void *virtnet_rq_alloc(struct receive_queue *r= q, u32 size, gfp_t gfp) =20 dma->len =3D alloc_frag->size - sizeof(*dma); =20 - addr =3D virtqueue_dma_map_single_attrs(rq->vq, dma + 1, - dma->len, DMA_FROM_DEVICE, 0); - if (virtqueue_dma_mapping_error(rq->vq, addr)) + addr =3D virtqueue_map_single_attrs(rq->vq, dma + 1, + dma->len, DMA_FROM_DEVICE, 0); + if (virtqueue_map_mapping_error(rq->vq, addr)) return NULL; =20 dma->addr =3D addr; - dma->need_sync =3D virtqueue_dma_need_sync(rq->vq, addr); + dma->need_sync =3D virtqueue_map_need_sync(rq->vq, addr); =20 /* Add a reference to dma to prevent the entire dma from * being released during error handling. This reference @@ -5892,9 +5892,9 @@ static int virtnet_xsk_pool_enable(struct net_device = *dev, if (!rq->xsk_buffs) return -ENOMEM; =20 - hdr_dma =3D virtqueue_dma_map_single_attrs(sq->vq, &xsk_hdr, vi->hdr_len, - DMA_TO_DEVICE, 0); - if (virtqueue_dma_mapping_error(sq->vq, hdr_dma)) { + hdr_dma =3D virtqueue_map_single_attrs(sq->vq, &xsk_hdr, vi->hdr_len, + DMA_TO_DEVICE, 0); + if (virtqueue_map_mapping_error(sq->vq, hdr_dma)) { err =3D -ENOMEM; goto err_free_buffs; } @@ -5923,8 +5923,8 @@ static int virtnet_xsk_pool_enable(struct net_device = *dev, err_rq: xsk_pool_dma_unmap(pool, 0); err_xsk_map: - virtqueue_dma_unmap_single_attrs(rq->vq, hdr_dma, vi->hdr_len, - DMA_TO_DEVICE, 0); + virtqueue_unmap_single_attrs(rq->vq, hdr_dma, vi->hdr_len, + DMA_TO_DEVICE, 0); err_free_buffs: kvfree(rq->xsk_buffs); return err; @@ -5951,8 +5951,8 @@ static int virtnet_xsk_pool_disable(struct net_device= *dev, u16 qid) =20 xsk_pool_dma_unmap(pool, 0); =20 - virtqueue_dma_unmap_single_attrs(sq->vq, sq->xsk_hdr_dma_addr, - vi->hdr_len, DMA_TO_DEVICE, 0); + virtqueue_unmap_single_attrs(sq->vq, sq->xsk_hdr_dma_addr, + vi->hdr_len, DMA_TO_DEVICE, 0); kvfree(rq->xsk_buffs); =20 return err; diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 04d88502a685..5961e77db6dc 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -166,7 +166,7 @@ struct vring_virtqueue { bool packed_ring; =20 /* Is DMA API used? */ - bool use_dma_api; + bool use_map_api; =20 /* Can we use weak barriers? */ bool weak_barriers; @@ -268,7 +268,7 @@ static bool virtqueue_use_indirect(const struct vring_v= irtqueue *vq, * unconditionally on data path. */ =20 -static bool vring_use_dma_api(const struct virtio_device *vdev) +static bool vring_use_map_api(const struct virtio_device *vdev) { if (!virtio_has_dma_quirk(vdev)) return true; @@ -291,14 +291,14 @@ static bool vring_use_dma_api(const struct virtio_dev= ice *vdev) static bool vring_need_unmap_buffer(const struct vring_virtqueue *vring, const struct vring_desc_extra *extra) { - return vring->use_dma_api && (extra->addr !=3D DMA_MAPPING_ERROR); + return vring->use_map_api && (extra->addr !=3D DMA_MAPPING_ERROR); } =20 size_t virtio_max_dma_size(const struct virtio_device *vdev) { size_t max_segment_size =3D SIZE_MAX; =20 - if (vring_use_dma_api(vdev)) + if (vring_use_map_api(vdev)) max_segment_size =3D dma_max_mapping_size(vdev->dev.parent); =20 return max_segment_size; @@ -309,7 +309,7 @@ static void *vring_alloc_queue(struct virtio_device *vd= ev, size_t size, dma_addr_t *dma_handle, gfp_t flag, struct device *dma_dev) { - if (vring_use_dma_api(vdev)) { + if (vring_use_map_api(vdev)) { return dma_alloc_coherent(dma_dev, size, dma_handle, flag); } else { @@ -343,7 +343,7 @@ static void vring_free_queue(struct virtio_device *vdev= , size_t size, void *queue, dma_addr_t dma_handle, struct device *dma_dev) { - if (vring_use_dma_api(vdev)) + if (vring_use_map_api(vdev)) dma_free_coherent(dma_dev, size, queue, dma_handle); else free_pages_exact(queue, PAGE_ALIGN(size)); @@ -372,7 +372,7 @@ static int vring_map_one_sg(const struct vring_virtqueu= e *vq, struct scatterlist =20 *len =3D sg->length; =20 - if (!vq->use_dma_api) { + if (!vq->use_map_api) { /* * If DMA is not used, KMSAN doesn't know that the scatterlist * is initialized by the hardware. Explicitly check/unpoison it @@ -402,17 +402,17 @@ static dma_addr_t vring_map_single(const struct vring= _virtqueue *vq, void *cpu_addr, size_t size, enum dma_data_direction direction) { - if (!vq->use_dma_api) + if (!vq->use_map_api) return (dma_addr_t)virt_to_phys(cpu_addr); =20 - return virtqueue_dma_map_single_attrs(&vq->vq, cpu_addr, - size, direction, 0); + return virtqueue_map_single_attrs(&vq->vq, cpu_addr, + size, direction, 0); } =20 static int vring_mapping_error(const struct vring_virtqueue *vq, dma_addr_t addr) { - if (!vq->use_dma_api) + if (!vq->use_map_api) return 0; =20 return dma_mapping_error(vring_dma_dev(vq), addr); @@ -449,7 +449,7 @@ static unsigned int vring_unmap_one_split(const struct = vring_virtqueue *vq, flags =3D extra->flags; =20 if (flags & VRING_DESC_F_INDIRECT) { - if (!vq->use_dma_api) + if (!vq->use_map_api) goto out; } else if (!vring_need_unmap_buffer(vq, extra)) goto out; @@ -782,7 +782,7 @@ static void detach_buf_split(struct vring_virtqueue *vq= , unsigned int head, =20 extra =3D (struct vring_desc_extra *)&indir_desc[num]; =20 - if (vq->use_dma_api) { + if (vq->use_map_api) { for (j =3D 0; j < num; j++) vring_unmap_one_split(vq, &extra[j]); } @@ -1150,7 +1150,7 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, vq->broken =3D false; #endif vq->dma_dev =3D dma_dev; - vq->use_dma_api =3D vring_use_dma_api(vdev); + vq->use_map_api =3D vring_use_map_api(vdev); =20 vq->indirect =3D virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && !context; @@ -1266,7 +1266,7 @@ static void vring_unmap_extra_packed(const struct vri= ng_virtqueue *vq, flags =3D extra->flags; =20 if (flags & VRING_DESC_F_INDIRECT) { - if (!vq->use_dma_api) + if (!vq->use_map_api) return; } else if (!vring_need_unmap_buffer(vq, extra)) return; @@ -1351,7 +1351,7 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, desc[i].addr =3D cpu_to_le64(addr); desc[i].len =3D cpu_to_le32(len); =20 - if (unlikely(vq->use_dma_api)) { + if (unlikely(vq->use_map_api)) { extra[i].addr =3D premapped ? DMA_MAPPING_ERROR : addr; extra[i].len =3D len; extra[i].flags =3D n < out_sgs ? 0 : VRING_DESC_F_WRITE; @@ -1373,7 +1373,7 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, sizeof(struct vring_packed_desc)); vq->packed.vring.desc[head].id =3D cpu_to_le16(id); =20 - if (vq->use_dma_api) { + if (vq->use_map_api) { vq->packed.desc_extra[id].addr =3D addr; vq->packed.desc_extra[id].len =3D total_sg * sizeof(struct vring_packed_desc); @@ -1515,7 +1515,7 @@ static inline int virtqueue_add_packed(struct virtque= ue *_vq, desc[i].len =3D cpu_to_le32(len); desc[i].id =3D cpu_to_le16(id); =20 - if (unlikely(vq->use_dma_api)) { + if (unlikely(vq->use_map_api)) { vq->packed.desc_extra[curr].addr =3D premapped ? DMA_MAPPING_ERROR : addr; vq->packed.desc_extra[curr].len =3D len; @@ -1650,7 +1650,7 @@ static void detach_buf_packed(struct vring_virtqueue = *vq, vq->free_head =3D id; vq->vq.num_free +=3D state->num; =20 - if (unlikely(vq->use_dma_api)) { + if (unlikely(vq->use_map_api)) { curr =3D id; for (i =3D 0; i < state->num; i++) { vring_unmap_extra_packed(vq, @@ -1668,7 +1668,7 @@ static void detach_buf_packed(struct vring_virtqueue = *vq, if (!desc) return; =20 - if (vq->use_dma_api) { + if (vq->use_map_api) { len =3D vq->packed.desc_extra[id].len; num =3D len / sizeof(struct vring_packed_desc); =20 @@ -2121,7 +2121,7 @@ static struct virtqueue *__vring_new_virtqueue_packed= (unsigned int index, #endif vq->packed_ring =3D true; vq->dma_dev =3D dma_dev; - vq->use_dma_api =3D vring_use_dma_api(vdev); + vq->use_map_api =3D vring_use_map_api(vdev); =20 vq->indirect =3D virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && !context; @@ -2429,7 +2429,7 @@ struct device *virtqueue_dma_dev(struct virtqueue *_v= q) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - if (vq->use_dma_api) + if (vq->use_map_api) return vring_dma_dev(vq); else return NULL; @@ -3114,7 +3114,7 @@ const struct vring *virtqueue_get_vring(const struct = virtqueue *vq) EXPORT_SYMBOL_GPL(virtqueue_get_vring); =20 /** - * virtqueue_dma_map_single_attrs - map DMA for _vq + * virtqueue_map_single_attrs - map DMA for _vq * @_vq: the struct virtqueue we're talking about. * @ptr: the pointer of the buffer to do dma * @size: the size of the buffer to do dma @@ -3124,16 +3124,16 @@ EXPORT_SYMBOL_GPL(virtqueue_get_vring); * The caller calls this to do dma mapping in advance. The DMA address can= be * passed to this _vq when it is in pre-mapped mode. * - * return DMA address. Caller should check that by virtqueue_dma_mapping_e= rror(). + * return DMA address. Caller should check that by virtqueue_mapping_error= (). */ -dma_addr_t virtqueue_dma_map_single_attrs(const struct virtqueue *_vq, voi= d *ptr, - size_t size, - enum dma_data_direction dir, - unsigned long attrs) +dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *p= tr, + size_t size, + enum dma_data_direction dir, + unsigned long attrs) { const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - if (!vq->use_dma_api) { + if (!vq->use_map_api) { kmsan_handle_dma(virt_to_page(ptr), offset_in_page(ptr), size, dir); return (dma_addr_t)virt_to_phys(ptr); } @@ -3146,85 +3146,85 @@ dma_addr_t virtqueue_dma_map_single_attrs(const str= uct virtqueue *_vq, void *ptr 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); +EXPORT_SYMBOL_GPL(virtqueue_map_single_attrs); =20 /** - * virtqueue_dma_unmap_single_attrs - unmap DMA for _vq + * virtqueue_unmap_single_attrs - unmap map for _vq * @_vq: the struct virtqueue we're talking about. * @addr: the dma address to unmap * @size: the size of the buffer * @dir: DMA direction * @attrs: DMA Attrs * - * Unmap the address that is mapped by the virtqueue_dma_map_* APIs. + * Unmap the address that is mapped by the virtqueue_map_* APIs. * */ -void virtqueue_dma_unmap_single_attrs(const struct virtqueue *_vq, - dma_addr_t addr, - size_t size, enum dma_data_direction dir, - unsigned long attrs) +void virtqueue_unmap_single_attrs(const struct virtqueue *_vq, + dma_addr_t addr, + size_t size, enum dma_data_direction dir, + unsigned long attrs) { const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - if (!vq->use_dma_api) + if (!vq->use_map_api) return; =20 dma_unmap_page_attrs(vring_dma_dev(vq), addr, size, dir, attrs); } -EXPORT_SYMBOL_GPL(virtqueue_dma_unmap_single_attrs); +EXPORT_SYMBOL_GPL(virtqueue_unmap_single_attrs); =20 /** - * virtqueue_dma_mapping_error - check dma address + * virtqueue_map_mapping_error - check dma address * @_vq: the struct virtqueue we're talking about. * @addr: DMA address * * Returns 0 means dma valid. Other means invalid dma address. */ -int virtqueue_dma_mapping_error(const struct virtqueue *_vq, dma_addr_t ad= dr) +int virtqueue_map_mapping_error(const struct virtqueue *_vq, dma_addr_t ad= dr) { const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - if (!vq->use_dma_api) + if (!vq->use_map_api) return 0; =20 return dma_mapping_error(vring_dma_dev(vq), addr); } -EXPORT_SYMBOL_GPL(virtqueue_dma_mapping_error); +EXPORT_SYMBOL_GPL(virtqueue_map_mapping_error); =20 /** - * virtqueue_dma_need_sync - check a dma address needs sync + * virtqueue_map_need_sync - check a dma address needs sync * @_vq: the struct virtqueue we're talking about. * @addr: DMA address * - * Check if the dma address mapped by the virtqueue_dma_map_* APIs needs t= o be + * Check if the dma address mapped by the virtqueue_map_* APIs needs to be * synchronized * * return bool */ -bool virtqueue_dma_need_sync(const struct virtqueue *_vq, dma_addr_t addr) +bool virtqueue_map_need_sync(const struct virtqueue *_vq, dma_addr_t addr) { const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - if (!vq->use_dma_api) + if (!vq->use_map_api) return false; =20 return dma_need_sync(vring_dma_dev(vq), addr); } -EXPORT_SYMBOL_GPL(virtqueue_dma_need_sync); +EXPORT_SYMBOL_GPL(virtqueue_map_need_sync); =20 /** - * virtqueue_dma_sync_single_range_for_cpu - dma sync for cpu + * virtqueue_map_sync_single_range_for_cpu - map sync for cpu * @_vq: the struct virtqueue we're talking about. * @addr: DMA address * @offset: DMA address offset * @size: buf size for sync * @dir: DMA direction * - * Before calling this function, use virtqueue_dma_need_sync() to confirm = that + * Before calling this function, use virtqueue_map_need_sync() to confirm = that * the DMA address really needs to be synchronized * */ -void virtqueue_dma_sync_single_range_for_cpu(const struct virtqueue *_vq, +void virtqueue_map_sync_single_range_for_cpu(const struct virtqueue *_vq, dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir) @@ -3232,25 +3232,25 @@ void virtqueue_dma_sync_single_range_for_cpu(const = struct virtqueue *_vq, const struct vring_virtqueue *vq =3D to_vvq(_vq); struct device *dev =3D vring_dma_dev(vq); =20 - if (!vq->use_dma_api) + if (!vq->use_map_api) return; =20 dma_sync_single_range_for_cpu(dev, addr, offset, size, dir); } -EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_cpu); +EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_cpu); =20 /** - * virtqueue_dma_sync_single_range_for_device - dma sync for device + * virtqueue_map_sync_single_range_for_device - map sync for device * @_vq: the struct virtqueue we're talking about. * @addr: DMA address * @offset: DMA address offset * @size: buf size for sync * @dir: DMA direction * - * Before calling this function, use virtqueue_dma_need_sync() to confirm = that + * Before calling this function, use virtqueue_map_need_sync() to confirm = that * the DMA address really needs to be synchronized */ -void virtqueue_dma_sync_single_range_for_device(const struct virtqueue *_v= q, +void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_v= q, dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir) @@ -3258,12 +3258,12 @@ void virtqueue_dma_sync_single_range_for_device(con= st struct virtqueue *_vq, const struct vring_virtqueue *vq =3D to_vvq(_vq); struct device *dev =3D vring_dma_dev(vq); =20 - if (!vq->use_dma_api) + if (!vq->use_map_api) return; =20 dma_sync_single_range_for_device(dev, addr, offset, size, dir); } -EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_device); +EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_device); =20 MODULE_DESCRIPTION("Virtio ring implementation"); MODULE_LICENSE("GPL"); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 8c0a3165e754..0371b500ed19 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -259,18 +259,18 @@ void unregister_virtio_driver(struct virtio_driver *d= rv); module_driver(__virtio_driver, register_virtio_driver, \ unregister_virtio_driver) =20 -dma_addr_t virtqueue_dma_map_single_attrs(const struct virtqueue *_vq, voi= d *ptr, size_t size, +dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *p= tr, size_t size, enum dma_data_direction dir, unsigned long attrs); -void virtqueue_dma_unmap_single_attrs(const struct virtqueue *_vq, dma_add= r_t addr, +void virtqueue_unmap_single_attrs(const struct virtqueue *_vq, dma_addr_t = addr, size_t size, enum dma_data_direction dir, unsigned long attrs); -int virtqueue_dma_mapping_error(const struct virtqueue *_vq, dma_addr_t ad= dr); +int virtqueue_map_mapping_error(const struct virtqueue *_vq, dma_addr_t ad= dr); =20 -bool virtqueue_dma_need_sync(const struct virtqueue *_vq, dma_addr_t addr); -void virtqueue_dma_sync_single_range_for_cpu(const struct virtqueue *_vq, = dma_addr_t addr, +bool virtqueue_map_need_sync(const struct virtqueue *_vq, dma_addr_t addr); +void virtqueue_map_sync_single_range_for_cpu(const struct virtqueue *_vq, = dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir); -void virtqueue_dma_sync_single_range_for_device(const struct virtqueue *_v= q, dma_addr_t addr, +void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_v= q, dma_addr_t addr, unsigned long offset, size_t size, enum dma_data_direction dir); =20 --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 137831A3161 for ; Tue, 1 Jul 2025 01:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332482; cv=none; b=KpZGcPsVxqklK85TVU6sc7WEZn9AAawvJ1tv0YWDS1To6Jqa+LLF12DqoUAENFLW8f2YF3YJ2sx9726XJhYnoKaPQR/MAfatRRAlNWq/4SVDS6B1X0Ngr6CbAXNkjooslfBNCGtRHaGB+RXeaCSGJ3/NPCLxw7IEjKm/kn5ITkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332482; c=relaxed/simple; bh=T0ai9cn5CT3is2Nf5c3Dp0OeH6S0W9l+9WFC+WPyVAw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JhSBRvKUBCiQS7asCVmlbKxXB4m+9K6wGpjDGp01+K3XmLqGM0VJjR+7IBs6sd9HY2B7VCC5bFR0ORgxIHfwihaRBPqPgqFrZxm35qzHQiQCWZMd9D/GSxq8WAU0tW3qN2Mjur4o7QNfqkPVJE4Hmh+D2sEgPvseTqQnIhEz2ik= 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=YhxURyBN; arc=none smtp.client-ip=170.10.129.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="YhxURyBN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332479; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IAHHsMWCNQsLyebIszuF+Xbmat525vL7K5J/FgE589E=; b=YhxURyBNYFckm2Q5lEgGDYG5mcVkYQHXbgdOu+AHqVKQxV/HLmRAM/5ad+vL3N6LS0lBMp heEm2GPTgiNOabhTXJcez/vDv/S4zY3wWninhabiLiBx59dvi32O6IyZaqGGTlQ/BLeEb6 hZor7SkYJ497EKyLU88depx38RIfcJg= 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-551-s_tJMqd_OyqhjpME8QLF5g-1; Mon, 30 Jun 2025 21:14:36 -0400 X-MC-Unique: s_tJMqd_OyqhjpME8QLF5g-1 X-Mimecast-MFC-AGG-ID: s_tJMqd_OyqhjpME8QLF5g_1751332475 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 5D63C1944AAB; Tue, 1 Jul 2025 01:14:35 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 48C0430001B5; Tue, 1 Jul 2025 01:14:29 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 4/9] virtio: rename dma_dev to map_token Date: Tue, 1 Jul 2025 09:13:56 +0800 Message-ID: <20250701011401.74851-5-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Following patch will introduce the mapping operations for virtio device. So this patch rename dma_dev to map_token to match the rework. The idea is the allow the transport layer to pass device specific mapping token which will be used as a parameter for the virtio mapping operations. Signed-off-by: Jason Wang --- drivers/net/virtio_net.c | 4 +- drivers/virtio/virtio_ring.c | 130 +++++++++++++++++------------------ drivers/virtio/virtio_vdpa.c | 2 +- include/linux/virtio.h | 2 +- include/linux/virtio_ring.h | 6 +- 5 files changed, 72 insertions(+), 72 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 39bcb85335d5..43711e4cc381 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -5879,10 +5879,10 @@ static int virtnet_xsk_pool_enable(struct net_devic= e *dev, * But vq->dma_dev allows every vq has the respective dma dev. So I * check the dma dev of vq and sq is the same dev. */ - if (virtqueue_dma_dev(rq->vq) !=3D virtqueue_dma_dev(sq->vq)) + if (virtqueue_map_token(rq->vq) !=3D virtqueue_map_token(sq->vq)) return -EINVAL; =20 - dma_dev =3D virtqueue_dma_dev(rq->vq); + dma_dev =3D virtqueue_map_token(rq->vq); if (!dma_dev) return -EINVAL; =20 diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 5961e77db6dc..5f17f8d91f1a 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -210,8 +210,8 @@ struct vring_virtqueue { /* DMA, allocation, and size information */ bool we_own_ring; =20 - /* Device used for doing DMA */ - struct device *dma_dev; + /* Transport specific token used for doing map */ + void *map_token; =20 #ifdef DEBUG /* They're supposed to lock for us. */ @@ -307,10 +307,10 @@ EXPORT_SYMBOL_GPL(virtio_max_dma_size); =20 static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, dma_addr_t *dma_handle, gfp_t flag, - struct device *dma_dev) + void *map_token) { if (vring_use_map_api(vdev)) { - return dma_alloc_coherent(dma_dev, size, + return dma_alloc_coherent(map_token, size, dma_handle, flag); } else { void *queue =3D alloc_pages_exact(PAGE_ALIGN(size), flag); @@ -341,22 +341,22 @@ static void *vring_alloc_queue(struct virtio_device *= vdev, size_t size, =20 static void vring_free_queue(struct virtio_device *vdev, size_t size, void *queue, dma_addr_t dma_handle, - struct device *dma_dev) + void *map_token) { if (vring_use_map_api(vdev)) - dma_free_coherent(dma_dev, size, queue, dma_handle); + dma_free_coherent(map_token, size, queue, dma_handle); else free_pages_exact(queue, PAGE_ALIGN(size)); } =20 /* - * The DMA ops on various arches are rather gnarly right now, and - * making all of the arch DMA ops work on the vring device itself + * The map ops on various arches are rather gnarly right now, and + * making all of the arch map ops work on the vring device itself * is a mess. */ -static struct device *vring_dma_dev(const struct vring_virtqueue *vq) +static void *vring_map_token(const struct vring_virtqueue *vq) { - return vq->dma_dev; + return vq->map_token; } =20 /* Map one sg entry. */ @@ -388,11 +388,11 @@ static int vring_map_one_sg(const struct vring_virtqu= eue *vq, struct scatterlist * the way it expects (we don't guarantee that the scatterlist * will exist for the lifetime of the mapping). */ - *addr =3D dma_map_page(vring_dma_dev(vq), + *addr =3D dma_map_page(vring_map_token(vq), sg_page(sg), sg->offset, sg->length, direction); =20 - if (dma_mapping_error(vring_dma_dev(vq), *addr)) + if (dma_mapping_error(vring_map_token(vq), *addr)) return -ENOMEM; =20 return 0; @@ -415,7 +415,7 @@ static int vring_mapping_error(const struct vring_virtq= ueue *vq, if (!vq->use_map_api) return 0; =20 - return dma_mapping_error(vring_dma_dev(vq), addr); + return dma_mapping_error(vring_map_token(vq), addr); } =20 static void virtqueue_init(struct vring_virtqueue *vq, u32 num) @@ -454,7 +454,7 @@ static unsigned int vring_unmap_one_split(const struct = vring_virtqueue *vq, } else if (!vring_need_unmap_buffer(vq, extra)) goto out; =20 - dma_unmap_page(vring_dma_dev(vq), + dma_unmap_page(vring_map_token(vq), extra->addr, extra->len, (flags & VRING_DESC_F_WRITE) ? @@ -1056,12 +1056,12 @@ static int vring_alloc_state_extra_split(struct vri= ng_virtqueue_split *vring_spl } =20 static void vring_free_split(struct vring_virtqueue_split *vring_split, - struct virtio_device *vdev, struct device *dma_dev) + struct virtio_device *vdev, void *map_token) { vring_free_queue(vdev, vring_split->queue_size_in_bytes, vring_split->vring.desc, vring_split->queue_dma_addr, - dma_dev); + map_token); =20 kfree(vring_split->desc_state); kfree(vring_split->desc_extra); @@ -1072,7 +1072,7 @@ static int vring_alloc_queue_split(struct vring_virtq= ueue_split *vring_split, u32 num, unsigned int vring_align, bool may_reduce_num, - struct device *dma_dev) + void *map_token) { void *queue =3D NULL; dma_addr_t dma_addr; @@ -1088,7 +1088,7 @@ static int vring_alloc_queue_split(struct vring_virtq= ueue_split *vring_split, queue =3D vring_alloc_queue(vdev, vring_size(num, vring_align), &dma_addr, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, - dma_dev); + map_token); if (queue) break; if (!may_reduce_num) @@ -1102,7 +1102,7 @@ static int vring_alloc_queue_split(struct vring_virtq= ueue_split *vring_split, /* Try to get a single page. You are my only hope! */ queue =3D vring_alloc_queue(vdev, vring_size(num, vring_align), &dma_addr, GFP_KERNEL | __GFP_ZERO, - dma_dev); + map_token); } if (!queue) return -ENOMEM; @@ -1126,7 +1126,7 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, bool (*notify)(struct virtqueue *), void (*callback)(struct virtqueue *), const char *name, - struct device *dma_dev) + void *map_token) { struct vring_virtqueue *vq; int err; @@ -1149,7 +1149,7 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, #else vq->broken =3D false; #endif - vq->dma_dev =3D dma_dev; + vq->map_token =3D map_token; vq->use_map_api =3D vring_use_map_api(vdev); =20 vq->indirect =3D virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && @@ -1187,21 +1187,21 @@ static struct virtqueue *vring_create_virtqueue_spl= it( bool (*notify)(struct virtqueue *), void (*callback)(struct virtqueue *), const char *name, - struct device *dma_dev) + void *map_token) { struct vring_virtqueue_split vring_split =3D {}; struct virtqueue *vq; int err; =20 err =3D vring_alloc_queue_split(&vring_split, vdev, num, vring_align, - may_reduce_num, dma_dev); + may_reduce_num, map_token); if (err) return NULL; =20 vq =3D __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barrie= rs, - context, notify, callback, name, dma_dev); + context, notify, callback, name, map_token); if (!vq) { - vring_free_split(&vring_split, vdev, dma_dev); + vring_free_split(&vring_split, vdev, map_token); return NULL; } =20 @@ -1220,7 +1220,7 @@ static int virtqueue_resize_split(struct virtqueue *_= vq, u32 num) err =3D vring_alloc_queue_split(&vring_split, vdev, num, vq->split.vring_align, vq->split.may_reduce_num, - vring_dma_dev(vq)); + vring_map_token(vq)); if (err) goto err; =20 @@ -1238,7 +1238,7 @@ static int virtqueue_resize_split(struct virtqueue *_= vq, u32 num) return 0; =20 err_state_extra: - vring_free_split(&vring_split, vdev, vring_dma_dev(vq)); + vring_free_split(&vring_split, vdev, vring_map_token(vq)); err: virtqueue_reinit_split(vq); return -ENOMEM; @@ -1271,7 +1271,7 @@ static void vring_unmap_extra_packed(const struct vri= ng_virtqueue *vq, } else if (!vring_need_unmap_buffer(vq, extra)) return; =20 - dma_unmap_page(vring_dma_dev(vq), + dma_unmap_page(vring_map_token(vq), extra->addr, extra->len, (flags & VRING_DESC_F_WRITE) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); @@ -1947,25 +1947,25 @@ static struct vring_desc_extra *vring_alloc_desc_ex= tra(unsigned int num) =20 static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, struct virtio_device *vdev, - struct device *dma_dev) + void *map_token) { if (vring_packed->vring.desc) vring_free_queue(vdev, vring_packed->ring_size_in_bytes, vring_packed->vring.desc, vring_packed->ring_dma_addr, - dma_dev); + map_token); =20 if (vring_packed->vring.driver) vring_free_queue(vdev, vring_packed->event_size_in_bytes, vring_packed->vring.driver, vring_packed->driver_event_dma_addr, - dma_dev); + map_token); =20 if (vring_packed->vring.device) vring_free_queue(vdev, vring_packed->event_size_in_bytes, vring_packed->vring.device, vring_packed->device_event_dma_addr, - dma_dev); + map_token); =20 kfree(vring_packed->desc_state); kfree(vring_packed->desc_extra); @@ -1973,7 +1973,7 @@ static void vring_free_packed(struct vring_virtqueue_= packed *vring_packed, =20 static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_p= acked, struct virtio_device *vdev, - u32 num, struct device *dma_dev) + u32 num, void *map_token) { struct vring_packed_desc *ring; struct vring_packed_desc_event *driver, *device; @@ -1985,7 +1985,7 @@ static int vring_alloc_queue_packed(struct vring_virt= queue_packed *vring_packed, ring =3D vring_alloc_queue(vdev, ring_size_in_bytes, &ring_dma_addr, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, - dma_dev); + map_token); if (!ring) goto err; =20 @@ -1998,7 +1998,7 @@ static int vring_alloc_queue_packed(struct vring_virt= queue_packed *vring_packed, driver =3D vring_alloc_queue(vdev, event_size_in_bytes, &driver_event_dma_addr, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, - dma_dev); + map_token); if (!driver) goto err; =20 @@ -2009,7 +2009,7 @@ static int vring_alloc_queue_packed(struct vring_virt= queue_packed *vring_packed, device =3D vring_alloc_queue(vdev, event_size_in_bytes, &device_event_dma_addr, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, - dma_dev); + map_token); if (!device) goto err; =20 @@ -2021,7 +2021,7 @@ static int vring_alloc_queue_packed(struct vring_virt= queue_packed *vring_packed, return 0; =20 err: - vring_free_packed(vring_packed, vdev, dma_dev); + vring_free_packed(vring_packed, vdev, map_token); return -ENOMEM; } =20 @@ -2097,7 +2097,7 @@ static struct virtqueue *__vring_new_virtqueue_packed= (unsigned int index, bool (*notify)(struct virtqueue *), void (*callback)(struct virtqueue *), const char *name, - struct device *dma_dev) + void *map_token) { struct vring_virtqueue *vq; int err; @@ -2120,7 +2120,7 @@ static struct virtqueue *__vring_new_virtqueue_packed= (unsigned int index, vq->broken =3D false; #endif vq->packed_ring =3D true; - vq->dma_dev =3D dma_dev; + vq->map_token =3D map_token; vq->use_map_api =3D vring_use_map_api(vdev); =20 vq->indirect =3D virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && @@ -2158,18 +2158,18 @@ static struct virtqueue *vring_create_virtqueue_pac= ked( bool (*notify)(struct virtqueue *), void (*callback)(struct virtqueue *), const char *name, - struct device *dma_dev) + void *map_token) { struct vring_virtqueue_packed vring_packed =3D {}; struct virtqueue *vq; =20 - if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev)) + if (vring_alloc_queue_packed(&vring_packed, vdev, num, map_token)) return NULL; =20 vq =3D __vring_new_virtqueue_packed(index, &vring_packed, vdev, weak_barr= iers, - context, notify, callback, name, dma_dev); + context, notify, callback, name, map_token); if (!vq) { - vring_free_packed(&vring_packed, vdev, dma_dev); + vring_free_packed(&vring_packed, vdev, map_token); return NULL; } =20 @@ -2185,7 +2185,7 @@ static int virtqueue_resize_packed(struct virtqueue *= _vq, u32 num) struct virtio_device *vdev =3D _vq->vdev; int err; =20 - if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_dma_dev(vq))) + if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_map_token(vq= ))) goto err_ring; =20 err =3D vring_alloc_state_extra_packed(&vring_packed); @@ -2202,7 +2202,7 @@ static int virtqueue_resize_packed(struct virtqueue *= _vq, u32 num) return 0; =20 err_state_extra: - vring_free_packed(&vring_packed, vdev, vring_dma_dev(vq)); + vring_free_packed(&vring_packed, vdev, vring_map_token(vq)); err_ring: virtqueue_reinit_packed(vq); return -ENOMEM; @@ -2420,21 +2420,21 @@ int virtqueue_add_inbuf_premapped(struct virtqueue = *vq, EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_premapped); =20 /** - * virtqueue_dma_dev - get the dma dev + * virtqueue_map_token - get the transport specific map token * @_vq: the struct virtqueue we're talking about. * - * Returns the dma dev. That can been used for dma api. + * Returns the map token. That can been used for map api. */ -struct device *virtqueue_dma_dev(struct virtqueue *_vq) +void *virtqueue_map_token(struct virtqueue *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (vq->use_map_api) - return vring_dma_dev(vq); + return vring_map_token(vq); else return NULL; } -EXPORT_SYMBOL_GPL(virtqueue_dma_dev); +EXPORT_SYMBOL_GPL(virtqueue_map_token); =20 /** * virtqueue_kick_prepare - first half of split virtqueue_kick call. @@ -2727,7 +2727,7 @@ struct virtqueue *vring_create_virtqueue( } EXPORT_SYMBOL_GPL(vring_create_virtqueue); =20 -struct virtqueue *vring_create_virtqueue_dma( +struct virtqueue *vring_create_virtqueue_map( unsigned int index, unsigned int num, unsigned int vring_align, @@ -2738,19 +2738,19 @@ struct virtqueue *vring_create_virtqueue_dma( bool (*notify)(struct virtqueue *), void (*callback)(struct virtqueue *), const char *name, - struct device *dma_dev) + void *map_token) { =20 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) return vring_create_virtqueue_packed(index, num, vring_align, vdev, weak_barriers, may_reduce_num, - context, notify, callback, name, dma_dev); + context, notify, callback, name, map_token); =20 return vring_create_virtqueue_split(index, num, vring_align, vdev, weak_barriers, may_reduce_num, - context, notify, callback, name, dma_dev); + context, notify, callback, name, map_token); } -EXPORT_SYMBOL_GPL(vring_create_virtqueue_dma); +EXPORT_SYMBOL_GPL(vring_create_virtqueue_map); =20 /** * virtqueue_resize - resize the vring of vq @@ -2886,19 +2886,19 @@ static void vring_free(struct virtqueue *_vq) vq->packed.ring_size_in_bytes, vq->packed.vring.desc, vq->packed.ring_dma_addr, - vring_dma_dev(vq)); + vring_map_token(vq)); =20 vring_free_queue(vq->vq.vdev, vq->packed.event_size_in_bytes, vq->packed.vring.driver, vq->packed.driver_event_dma_addr, - vring_dma_dev(vq)); + vring_map_token(vq)); =20 vring_free_queue(vq->vq.vdev, vq->packed.event_size_in_bytes, vq->packed.vring.device, vq->packed.device_event_dma_addr, - vring_dma_dev(vq)); + vring_map_token(vq)); =20 kfree(vq->packed.desc_state); kfree(vq->packed.desc_extra); @@ -2907,7 +2907,7 @@ static void vring_free(struct virtqueue *_vq) vq->split.queue_size_in_bytes, vq->split.vring.desc, vq->split.queue_dma_addr, - vring_dma_dev(vq)); + vring_map_token(vq)); } } if (!vq->packed_ring) { @@ -3143,7 +3143,7 @@ dma_addr_t virtqueue_map_single_attrs(const struct vi= rtqueue *_vq, void *ptr, "rejecting DMA map of vmalloc memory\n")) return DMA_MAPPING_ERROR; =20 - return dma_map_page_attrs(vring_dma_dev(vq), virt_to_page(ptr), + return dma_map_page_attrs(vring_map_token(vq), virt_to_page(ptr), offset_in_page(ptr), size, dir, attrs); } EXPORT_SYMBOL_GPL(virtqueue_map_single_attrs); @@ -3169,7 +3169,7 @@ void virtqueue_unmap_single_attrs(const struct virtqu= eue *_vq, if (!vq->use_map_api) return; =20 - dma_unmap_page_attrs(vring_dma_dev(vq), addr, size, dir, attrs); + dma_unmap_page_attrs(vring_map_token(vq), addr, size, dir, attrs); } EXPORT_SYMBOL_GPL(virtqueue_unmap_single_attrs); =20 @@ -3187,7 +3187,7 @@ int virtqueue_map_mapping_error(const struct virtqueu= e *_vq, dma_addr_t addr) if (!vq->use_map_api) return 0; =20 - return dma_mapping_error(vring_dma_dev(vq), addr); + return dma_mapping_error(vring_map_token(vq), addr); } EXPORT_SYMBOL_GPL(virtqueue_map_mapping_error); =20 @@ -3208,7 +3208,7 @@ bool virtqueue_map_need_sync(const struct virtqueue *= _vq, dma_addr_t addr) if (!vq->use_map_api) return false; =20 - return dma_need_sync(vring_dma_dev(vq), addr); + return dma_need_sync(vring_map_token(vq), addr); } EXPORT_SYMBOL_GPL(virtqueue_map_need_sync); =20 @@ -3230,7 +3230,7 @@ void virtqueue_map_sync_single_range_for_cpu(const st= ruct virtqueue *_vq, enum dma_data_direction dir) { const struct vring_virtqueue *vq =3D to_vvq(_vq); - struct device *dev =3D vring_dma_dev(vq); + struct device *dev =3D vring_map_token(vq); =20 if (!vq->use_map_api) return; @@ -3256,7 +3256,7 @@ void virtqueue_map_sync_single_range_for_device(const= struct virtqueue *_vq, enum dma_data_direction dir) { const struct vring_virtqueue *vq =3D to_vvq(_vq); - struct device *dev =3D vring_dma_dev(vq); + struct device *dev =3D vring_map_token(vq); =20 if (!vq->use_map_api) return; diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c index 1f60c9d5cb18..59b53032f1e2 100644 --- a/drivers/virtio/virtio_vdpa.c +++ b/drivers/virtio/virtio_vdpa.c @@ -205,7 +205,7 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsign= ed int index, dma_dev =3D ops->get_vq_dma_dev(vdpa, index); else dma_dev =3D vdpa_get_dma_dev(vdpa); - vq =3D vring_create_virtqueue_dma(index, max_num, align, vdev, + vq =3D vring_create_virtqueue_map(index, max_num, align, vdev, true, may_reduce_num, ctx, notify, callback, name, dma_dev); if (!vq) { diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 0371b500ed19..3812661d3761 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -74,7 +74,7 @@ int virtqueue_add_sgs(struct virtqueue *vq, void *data, gfp_t gfp); =20 -struct device *virtqueue_dma_dev(struct virtqueue *vq); +void *virtqueue_map_token(struct virtqueue *vq); =20 bool virtqueue_kick(struct virtqueue *vq); =20 diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h index 9b33df741b63..a995bca6785f 100644 --- a/include/linux/virtio_ring.h +++ b/include/linux/virtio_ring.h @@ -79,9 +79,9 @@ struct virtqueue *vring_create_virtqueue(unsigned int ind= ex, =20 /* * Creates a virtqueue and allocates the descriptor ring with per - * virtqueue DMA device. + * virtqueue mapping operations. */ -struct virtqueue *vring_create_virtqueue_dma(unsigned int index, +struct virtqueue *vring_create_virtqueue_map(unsigned int index, unsigned int num, unsigned int vring_align, struct virtio_device *vdev, @@ -91,7 +91,7 @@ struct virtqueue *vring_create_virtqueue_dma(unsigned int= index, bool (*notify)(struct virtqueue *vq), void (*callback)(struct virtqueue *vq), const char *name, - struct device *dma_dev); + void *map_token); =20 /* * Creates a virtqueue with a standard layout but a caller-allocated --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 AD2DB1B0421 for ; Tue, 1 Jul 2025 01:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332489; cv=none; b=GFKZsZLNWFSYnWWqxr4fp3NJwTxkHQQSk9XXgLStUP5/9OeqLVWrFILoeUclbegWIxWJMWZWKfcoKjlpWHcPRykQc3VMky/OsD26xPtkoSR4BxnkJxTKee3bSJ42J6b3QdWMDddmJMPjKdpbBG7wzsI3jF/E1R0XhZd87rHwNBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332489; c=relaxed/simple; bh=YSsBe6LdTHYtKVga5PocRlqM8qR4L0H+XWsQj2ddj0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IIfWFErzQIxSKr06oewrltHZ8VDCuY4uOGNp8QaTnhcqrSMl6/si/baCxMTZsjpVb+Lb2EvddWKjAY3l5mkpQz+zJ/kPy1BmUIA39KU+GDO7DCj2UeowfP4oCJzDisJmWLTVp7Yd9yaZU3+bUU5uXap6NGRSk9aTmRebgOPGb3A= 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=GvJAe/j9; arc=none smtp.client-ip=170.10.129.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="GvJAe/j9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332486; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rmeRRCSnBG4kFkIf6ISy+E1tN0sQsjbYYUh5C2Zc5HE=; b=GvJAe/j9YvaVNlMOrhSzcLXTALZi6jZq7mOG9kEVT389qfLLrnjdJoufJIwpBzRVNDwSAW A8TrhJDvhqmatXEVVHOQCGsqdFwdQEEwAiRfUb5DapMmimL2wbG5YOQAJNnRQ2rrinEFeD q/b2TxIQVQAj7i+3XpJ/ehhYt69a/Z0= Received: from mx-prod-mc-04.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-651-2jqd0ZQYNAGGBQ9BJPFcoQ-1; Mon, 30 Jun 2025 21:14:43 -0400 X-MC-Unique: 2jqd0ZQYNAGGBQ9BJPFcoQ-1 X-Mimecast-MFC-AGG-ID: 2jqd0ZQYNAGGBQ9BJPFcoQ_1751332482 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D7C151978F63; Tue, 1 Jul 2025 01:14:41 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6322E30001B1; Tue, 1 Jul 2025 01:14:35 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 5/9] virtio_ring: rename dma_handle to map_handle Date: Tue, 1 Jul 2025 09:13:57 +0800 Message-ID: <20250701011401.74851-6-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Following patch will introduce virtio map opreations which means the address is not necessarily used for DMA. Let's rename the dma_handle to map_handle first. Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 5f17f8d91f1a..04e754874bec 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -306,18 +306,18 @@ size_t virtio_max_dma_size(const struct virtio_device= *vdev) EXPORT_SYMBOL_GPL(virtio_max_dma_size); =20 static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, - dma_addr_t *dma_handle, gfp_t flag, + dma_addr_t *map_handle, gfp_t flag, void *map_token) { if (vring_use_map_api(vdev)) { return dma_alloc_coherent(map_token, size, - dma_handle, flag); + map_handle, flag); } else { void *queue =3D alloc_pages_exact(PAGE_ALIGN(size), flag); =20 if (queue) { phys_addr_t phys_addr =3D virt_to_phys(queue); - *dma_handle =3D (dma_addr_t)phys_addr; + *map_handle =3D (dma_addr_t)phys_addr; =20 /* * Sanity check: make sure we dind't truncate @@ -330,7 +330,7 @@ static void *vring_alloc_queue(struct virtio_device *vd= ev, size_t size, * warning and abort if we end up with an * unrepresentable address. */ - if (WARN_ON_ONCE(*dma_handle !=3D phys_addr)) { + if (WARN_ON_ONCE(*map_handle !=3D phys_addr)) { free_pages_exact(queue, PAGE_ALIGN(size)); return NULL; } @@ -340,11 +340,11 @@ static void *vring_alloc_queue(struct virtio_device *= vdev, size_t size, } =20 static void vring_free_queue(struct virtio_device *vdev, size_t size, - void *queue, dma_addr_t dma_handle, + void *queue, dma_addr_t map_handle, void *map_token) { if (vring_use_map_api(vdev)) - dma_free_coherent(map_token, size, queue, dma_handle); + dma_free_coherent(map_token, size, queue, map_handle); else free_pages_exact(queue, PAGE_ALIGN(size)); } --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 8753518FDBD for ; Tue, 1 Jul 2025 01:14:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332495; cv=none; b=Cqqy6LYYNi8FXDdvhGd1gmXIEfH0PQG5n7qjsizDhH7NBxvX3TSt+T9vXz7mHn/xvW4O6sQNZuCr1s+Xz80iBZxzz9J1VTRLFF9SkhrCtSqZ8/AgAitfjgwdHyuuIIS3OHIftyZkUGtP2Gg0IIRlCMIil6xqQsQGb2OODSPrWf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332495; c=relaxed/simple; bh=5+RQrjGqfvpLMxvdQFKa4+gMSGK+Oyecnk8SSuroZ6M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OiHOvXmTSprKIV2rXViPn9P/zlqPSe2NPQp1R01QtP+V810eeA7aNGX1hLVnsE7PT+Phns7zKEW9Mi8/T+g/xKsEtLSZXatxzTncVvAsIIGqYsmRK3tLWUzf4a0olCtlcejSFrIL0pNDC0NDsUFB2Jkv21oT0eCNXSq2miV9ZBM= 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=C+iGQ2/x; arc=none smtp.client-ip=170.10.129.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="C+iGQ2/x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332492; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XTi9YmbqNjL74cZYZZ9G9WFkBTYPywebnwXraBYliLs=; b=C+iGQ2/xZzHBAOWYGmjT0PKjrOZgycuCUFgJ+OGbqw+QezeEwaplP1V5OJcTotfw/YTpcJ tGpK5lbDjcDEyEWNslBPheDC5CddTvZMQ+TRmtKx8X0ZErV0I3Bysvs2wQFJAG3N9ZoL+T FfvcpkAhd4+lvWGzibAtLz6COMHd//c= Received: from mx-prod-mc-04.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-465-DZ3PhQ6kOZefXtmkeVw5cw-1; Mon, 30 Jun 2025 21:14:49 -0400 X-MC-Unique: DZ3PhQ6kOZefXtmkeVw5cw-1 X-Mimecast-MFC-AGG-ID: DZ3PhQ6kOZefXtmkeVw5cw_1751332488 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EC4AF19560AD; Tue, 1 Jul 2025 01:14:47 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E393230001B1; Tue, 1 Jul 2025 01:14:42 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 6/9] virtio: introduce map ops in virtio core Date: Tue, 1 Jul 2025 09:13:58 +0800 Message-ID: <20250701011401.74851-7-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" This patch introduces map operations for virtio device. Virtio use to use DMA API which is not necessarily the case since some devices doesn't do DMA. Instead of using tricks and abusing DMA API, let's simply abstract the current mapping logic into a virtio specific mapping operations. For the device or transport that doesn't do DMA, they can implement their own mapping logic without the need to trick DMA core. In this case the map_token is opaque to the virtio core that will be passed back to the transport or device specific map operations. For other devices, DMA API will still be used, so map token will still be the dma device to minimize the changeset and performance impact. The mapping operations are abstract as a independent structure instead of reusing virtio_config_ops. This allows the transport can simply reuse the structure for lower layers. A set of new mapping helpers were introduced for the device that want to do mapping by themselves. Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 174 +++++++++++++++++++++++++++++----- include/linux/virtio.h | 22 +++++ include/linux/virtio_config.h | 68 +++++++++++++ 3 files changed, 238 insertions(+), 26 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 04e754874bec..40b2f526832e 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -298,8 +298,14 @@ size_t virtio_max_dma_size(const struct virtio_device = *vdev) { size_t max_segment_size =3D SIZE_MAX; =20 - if (vring_use_map_api(vdev)) - max_segment_size =3D dma_max_mapping_size(vdev->dev.parent); + if (vring_use_map_api(vdev)) { + if (vdev->map) + max_segment_size =3D + vdev->map->max_mapping_size(vdev->dev.parent); + else + max_segment_size =3D + dma_max_mapping_size(vdev->dev.parent); + } =20 return max_segment_size; } @@ -310,8 +316,8 @@ static void *vring_alloc_queue(struct virtio_device *vd= ev, size_t size, void *map_token) { if (vring_use_map_api(vdev)) { - return dma_alloc_coherent(map_token, size, - map_handle, flag); + return virtqueue_map_alloc_coherent(vdev, map_token, size, + map_handle, flag); } else { void *queue =3D alloc_pages_exact(PAGE_ALIGN(size), flag); =20 @@ -344,7 +350,8 @@ static void vring_free_queue(struct virtio_device *vdev= , size_t size, void *map_token) { if (vring_use_map_api(vdev)) - dma_free_coherent(map_token, size, queue, map_handle); + virtqueue_map_free_coherent(vdev, map_token, size, + queue, map_handle); else free_pages_exact(queue, PAGE_ALIGN(size)); } @@ -388,9 +395,9 @@ static int vring_map_one_sg(const struct vring_virtqueu= e *vq, struct scatterlist * the way it expects (we don't guarantee that the scatterlist * will exist for the lifetime of the mapping). */ - *addr =3D dma_map_page(vring_map_token(vq), - sg_page(sg), sg->offset, sg->length, - direction); + *addr =3D virtqueue_map_page_attrs(&vq->vq, sg_page(sg), + sg->offset, sg->length, + direction, 0); =20 if (dma_mapping_error(vring_map_token(vq), *addr)) return -ENOMEM; @@ -454,11 +461,12 @@ static unsigned int vring_unmap_one_split(const struc= t vring_virtqueue *vq, } else if (!vring_need_unmap_buffer(vq, extra)) goto out; =20 - dma_unmap_page(vring_map_token(vq), - extra->addr, - extra->len, - (flags & VRING_DESC_F_WRITE) ? - DMA_FROM_DEVICE : DMA_TO_DEVICE); + virtqueue_unmap_page_attrs(&vq->vq, + extra->addr, + extra->len, + (flags & VRING_DESC_F_WRITE) ? + DMA_FROM_DEVICE : DMA_TO_DEVICE, + 0); =20 out: return extra->next; @@ -1271,10 +1279,11 @@ static void vring_unmap_extra_packed(const struct v= ring_virtqueue *vq, } else if (!vring_need_unmap_buffer(vq, extra)) return; =20 - dma_unmap_page(vring_map_token(vq), - extra->addr, extra->len, - (flags & VRING_DESC_F_WRITE) ? - DMA_FROM_DEVICE : DMA_TO_DEVICE); + virtqueue_unmap_page_attrs(&vq->vq, + extra->addr, extra->len, + (flags & VRING_DESC_F_WRITE) ? + DMA_FROM_DEVICE : DMA_TO_DEVICE, + 0); } =20 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_= sg, @@ -3113,6 +3122,105 @@ const struct vring *virtqueue_get_vring(const struc= t virtqueue *vq) } EXPORT_SYMBOL_GPL(virtqueue_get_vring); =20 +/** + * virtqueue_map_alloc_coherent - alloc coherent mapping + * @vdev: the virtio device we are talking to + * @token: device specific mapping token + * @size: the size of the buffer + * @map_handle: the pointer to the mapped adress + * @gfp: allocation flag (GFP_XXX) + * + * return virtual address or NULL on error + */ +void *virtqueue_map_alloc_coherent(struct virtio_device *vdev, + void *map_token, size_t size, + dma_addr_t *map_handle, gfp_t gfp) +{ + if (vdev->map) + return vdev->map->alloc(map_token, size, map_handle, gfp); + else + return dma_alloc_coherent(map_token, size, + map_handle, gfp); +} +EXPORT_SYMBOL_GPL(virtqueue_map_alloc_coherent); + +/** + * virtqueue_map_free_coherent - free coherent mapping + * @vdev: the virtio device we are talking to + * @token: device specific mapping token + * @size: the size of the buffer + * @map_handle: the mapped address that needs to be freed + * + */ +void virtqueue_map_free_coherent(struct virtio_device *vdev, + void *map_token, size_t size, void *vaddr, + dma_addr_t map_handle) +{ + if (vdev->map) + vdev->map->free(map_token, size, vaddr, map_handle, 0); + else + dma_free_coherent(map_token, size, vaddr, map_handle); +} +EXPORT_SYMBOL_GPL(virtqueue_map_free_coherent); + +/** + * virtqueue_map_page_attrs - map a page to the device + * @_vq: the virtqueue we are talking to + * @page: the page that will be mapped by the device + * @offset: the offset in the page for a buffer + * @size: the buffer size + * @dir: mapping direction + * @attrs: mapping attributes + * + * Returns mapped address. Caller should check that by virtqueue_mapping_e= rror(). + */ +dma_addr_t virtqueue_map_page_attrs(const struct virtqueue *_vq, + struct page *page, + unsigned long offset, + size_t size, + enum dma_data_direction dir, + unsigned long attrs) +{ + const struct vring_virtqueue *vq =3D to_vvq(_vq); + struct virtio_device *vdev =3D _vq->vdev; + void *map_token =3D vring_map_token(vq); + + if (vdev->map) + return vdev->map->map_page(map_token, + page, offset, size, + dir, attrs); + + return dma_map_page_attrs(map_token, + page, offset, size, + dir, attrs); +} +EXPORT_SYMBOL_GPL(virtqueue_map_page_attrs); + +/** + * virtqueue_unmap_page_attrs - map a page to the device + * @_vq: the virtqueue we are talking to + * @map_handle: the mapped address + * @size: the buffer size + * @dir: mapping direction + * @attrs: unmapping attributes + */ +void virtqueue_unmap_page_attrs(const struct virtqueue *_vq, + dma_addr_t map_handle, + size_t size, enum dma_data_direction dir, + unsigned long attrs) +{ + const struct vring_virtqueue *vq =3D to_vvq(_vq); + struct virtio_device *vdev =3D _vq->vdev; + void *map_token =3D vring_map_token(vq); + + if (vdev->map) + vdev->map->unmap_page(map_token, map_handle, + size, dir, attrs); + else + dma_unmap_page_attrs(map_token, map_handle, size, dir, attrs); +} +EXPORT_SYMBOL_GPL(virtqueue_unmap_page_attrs); + /** * virtqueue_map_single_attrs - map DMA for _vq * @_vq: the struct virtqueue we're talking about. @@ -3124,7 +3232,7 @@ EXPORT_SYMBOL_GPL(virtqueue_get_vring); * The caller calls this to do dma mapping in advance. The DMA address can= be * passed to this _vq when it is in pre-mapped mode. * - * return DMA address. Caller should check that by virtqueue_mapping_error= (). + * return mapped address. Caller should check that by virtqueue_mapping_er= ror(). */ dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *p= tr, size_t size, @@ -3143,8 +3251,8 @@ dma_addr_t virtqueue_map_single_attrs(const struct vi= rtqueue *_vq, void *ptr, "rejecting DMA map of vmalloc memory\n")) return DMA_MAPPING_ERROR; =20 - return dma_map_page_attrs(vring_map_token(vq), virt_to_page(ptr), - offset_in_page(ptr), size, dir, attrs); + return virtqueue_map_page_attrs(&vq->vq, virt_to_page(ptr), + offset_in_page(ptr), size, dir, attrs); } EXPORT_SYMBOL_GPL(virtqueue_map_single_attrs); =20 @@ -3169,7 +3277,7 @@ void virtqueue_unmap_single_attrs(const struct virtqu= eue *_vq, if (!vq->use_map_api) return; =20 - dma_unmap_page_attrs(vring_map_token(vq), addr, size, dir, attrs); + virtqueue_unmap_page_attrs(_vq, addr, size, dir, attrs); } EXPORT_SYMBOL_GPL(virtqueue_unmap_single_attrs); =20 @@ -3204,11 +3312,16 @@ EXPORT_SYMBOL_GPL(virtqueue_map_mapping_error); bool virtqueue_map_need_sync(const struct virtqueue *_vq, dma_addr_t addr) { const struct vring_virtqueue *vq =3D to_vvq(_vq); + struct virtio_device *vdev =3D _vq->vdev; + void *token =3D vring_map_token(vq); =20 if (!vq->use_map_api) return false; =20 - return dma_need_sync(vring_map_token(vq), addr); + if (vdev->map) + return vdev->map->need_sync(token, addr); + else + return dma_need_sync(token, addr); } EXPORT_SYMBOL_GPL(virtqueue_map_need_sync); =20 @@ -3230,12 +3343,16 @@ void virtqueue_map_sync_single_range_for_cpu(const = struct virtqueue *_vq, enum dma_data_direction dir) { const struct vring_virtqueue *vq =3D to_vvq(_vq); - struct device *dev =3D vring_map_token(vq); + struct virtio_device *vdev =3D _vq->vdev; + void *token =3D vring_map_token(vq); =20 if (!vq->use_map_api) return; =20 - dma_sync_single_range_for_cpu(dev, addr, offset, size, dir); + if (vdev->map) + vdev->map->sync_single_for_cpu(token, addr + offset, size, dir); + else + dma_sync_single_range_for_cpu(token, addr, offset, size, dir); } EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_cpu); =20 @@ -3256,12 +3373,17 @@ void virtqueue_map_sync_single_range_for_device(con= st struct virtqueue *_vq, enum dma_data_direction dir) { const struct vring_virtqueue *vq =3D to_vvq(_vq); - struct device *dev =3D vring_map_token(vq); + struct virtio_device *vdev =3D _vq->vdev; + void *token =3D vring_map_token(vq); =20 if (!vq->use_map_api) return; =20 - dma_sync_single_range_for_device(dev, addr, offset, size, dir); + if (vdev->map) + vdev->map->sync_single_for_device(token, addr + offset, + size, dir); + else + dma_sync_single_range_for_device(token, addr, offset, size, dir); } EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_device); =20 diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 3812661d3761..6e8e9b350d05 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -158,6 +158,7 @@ struct virtio_device { struct virtio_device_id id; const struct virtio_config_ops *config; const struct vringh_config_ops *vringh_config; + const struct virtio_map_ops *map; struct list_head vqs; u64 features; void *priv; @@ -259,6 +260,27 @@ void unregister_virtio_driver(struct virtio_driver *dr= v); module_driver(__virtio_driver, register_virtio_driver, \ unregister_virtio_driver) =20 + +void *virtqueue_map_alloc_coherent(struct virtio_device *vdev, + void *map_token, size_t size, + dma_addr_t *dma_handle, gfp_t gfp); + +void virtqueue_map_free_coherent(struct virtio_device *vdev, + void *map_token, size_t size, void *vaddr, + dma_addr_t dma_handle); + +dma_addr_t virtqueue_map_page_attrs(const struct virtqueue *_vq, + struct page *page, + unsigned long offset, + size_t size, + enum dma_data_direction dir, + unsigned long attrs); + +void virtqueue_unmap_page_attrs(const struct virtqueue *_vq, + dma_addr_t dma_handle, + size_t size, enum dma_data_direction dir, + unsigned long attrs); + dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *p= tr, size_t size, enum dma_data_direction dir, unsigned long attrs); void virtqueue_unmap_single_attrs(const struct virtqueue *_vq, dma_addr_t = addr, diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index b3e1d30c765b..706ebf7cb389 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -133,6 +133,74 @@ struct virtio_config_ops { int (*enable_vq_after_reset)(struct virtqueue *vq); }; =20 +/** + * struct virtio_map_ops - operations for mapping buffer for a virtio devi= ce + * Note: For transport that has its own mapping logic it must + * implements all of the operations + * @map_page: map a buffer to the device + * token: device specific mapping token + * page: the page that will be mapped by the device + * offset: the offset in the page for a buffer + * size: the buffer size + * dir: mapping direction + * attrs: mapping attributes + * Returns: the mapped address + * @unmap_page: unmap a buffer from the device + * token: device specific mapping token + * map_handle: the mapped address + * size: the buffer size + * dir: mapping direction + * attrs: unmapping attributes + * @sync_single_for_cpu: sync a single buffer from device to cpu + * token: device specific mapping token + * map_handle: the mapping adress to sync + * size: the size of the buffer + * dir: synchronization direction + * @sync_single_for_device: sync a single buffer from cpu to device + * token: device specific mapping token + * map_handle: the mapping adress to sync + * size: the size of the buffer + * dir: synchronization direction + * @alloc: alloc a coherent buffer mapping + * token: device specific mapping token + * size: the size of the buffer + * map_handle: the mapping adress to sync + * gfp: allocation flag (GFP_XXX) + * Returns: virtual address of the allocated buffer + * @free: free a coherent buffer mapping + * token: device specific mapping token + * size: the size of the buffer + * vaddr: virtual address of the buffer + * map_handle: the mapping adress to sync + * attrs: unmapping attributes + * @need_sync: if the buffer needs synchronization + * token: device specific mapping token + * map_handle: the mapped address + * Returns: whether the buffer needs synchronization + * @max_mapping_size: get the maximum buffer size that can be mapped + * token: device specific mapping token + * Returns: the maximum buffer size that can be mapped + */ +struct virtio_map_ops { + dma_addr_t (*map_page)(void *token, struct page *page, + unsigned long offset, size_t size, + enum dma_data_direction dir, unsigned long attrs); + void (*unmap_page)(void *token, dma_addr_t map_handle, + size_t size, enum dma_data_direction dir, + unsigned long attrs); + void (*sync_single_for_cpu)(void *token, dma_addr_t map_handle, + size_t size, enum dma_data_direction dir); + void (*sync_single_for_device)(void *token, + dma_addr_t map_handle, size_t size, + enum dma_data_direction dir); + void *(*alloc)(void *token, size_t size, + dma_addr_t *map_handle, gfp_t gfp); + void (*free)(void *token, size_t size, void *vaddr, + dma_addr_t map_handle, unsigned long attrs); + bool (*need_sync)(void *token, dma_addr_t map_handle); + size_t (*max_mapping_size)(void *token); +}; + /* If driver didn't advertise the feature, it will never appear. */ void virtio_check_driver_offered_feature(const struct virtio_device *vdev, unsigned int fbit); --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 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 1536A1A0712 for ; Tue, 1 Jul 2025 01:14:58 +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=1751332500; cv=none; b=kBOGU2y5PCnRlWxGNuiKtUvICQnMx7CRLIyFudUxhjWal6aGWKpOMOhNoGua2h70kxHILMyeS2MB7LGlAq09kiQU9eaBxRr5zlKUYeVwDLz6pe6SzhTuUm/zNNrmPZ4w1mes86Y02EWEhLBP0hP+qne8cmQSVs2P7eEHMNRmlf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332500; c=relaxed/simple; bh=w8Q7p2/jymUTLxEN4p15BtUVOFpy1++JTaS420OfFYI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oe4RYPVSESq4Kk5KzY3J4YG/62Y7GVtn+xy2FzgU8EMHNFrimV8v2iYCip7uy7YneNX1b2f0JMSBwQvP5D2hBK5heY7RAZjx20zi8tX1iUYDErhz6M7I6BsnhS3xa1cAqEBvCfHkZZL78Me8FV3X/Hr4y4SS8dtRmcrUt3FtbB0= 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=Iy/4Z2Q8; 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="Iy/4Z2Q8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332497; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kYAOcmbEwBg2VfBZu4DP5Zw8EdQwqNIx4zWCkiCTB04=; b=Iy/4Z2Q8zcLplfB1Iygw1so4yndGktUiRi9QwjcuQwyA9V8qv/yU5AbOHGlBbiZmfSbt5x CKpvWl1r+bnvW8LmrYRq+AuvqYbpECGKTJ3wlS8LhnKkQWJPUv3v0GD6yryGjsMkF2WBpA W9plnjtN8D5dWaBs1pyRsjtcYpYMuB0= Received: from mx-prod-mc-03.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-382-WB4pCMVaMCaIy3CFLrfxMw-1; Mon, 30 Jun 2025 21:14:55 -0400 X-MC-Unique: WB4pCMVaMCaIy3CFLrfxMw-1 X-Mimecast-MFC-AGG-ID: WB4pCMVaMCaIy3CFLrfxMw_1751332494 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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3F38C18DA5CB; Tue, 1 Jul 2025 01:14:54 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F04E330001B1; Tue, 1 Jul 2025 01:14:48 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 7/9] vdpa: rename dma_dev to map_token Date: Tue, 1 Jul 2025 09:13:59 +0800 Message-ID: <20250701011401.74851-8-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Virtio core switches from DMA device to mapping token, let's do that as well for vDPA. Signed-off-by: Jason Wang --- drivers/vdpa/alibaba/eni_vdpa.c | 2 +- drivers/vdpa/ifcvf/ifcvf_main.c | 2 +- drivers/vdpa/octeon_ep/octep_vdpa_main.c | 2 +- drivers/vdpa/vdpa.c | 2 +- drivers/vdpa/vdpa_sim/vdpa_sim.c | 2 +- drivers/vdpa/vdpa_user/vduse_dev.c | 2 +- drivers/vdpa/virtio_pci/vp_vdpa.c | 2 +- drivers/vhost/vdpa.c | 4 ++-- drivers/virtio/virtio_vdpa.c | 12 ++++++------ include/linux/vdpa.h | 12 ++++++------ 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/drivers/vdpa/alibaba/eni_vdpa.c b/drivers/vdpa/alibaba/eni_vdp= a.c index ad7f3447fe90..34bf726dc660 100644 --- a/drivers/vdpa/alibaba/eni_vdpa.c +++ b/drivers/vdpa/alibaba/eni_vdpa.c @@ -496,7 +496,7 @@ static int eni_vdpa_probe(struct pci_dev *pdev, const s= truct pci_device_id *id) pci_set_master(pdev); pci_set_drvdata(pdev, eni_vdpa); =20 - eni_vdpa->vdpa.dma_dev =3D &pdev->dev; + eni_vdpa->vdpa.map_token =3D &pdev->dev; eni_vdpa->queues =3D eni_vdpa_get_num_queues(eni_vdpa); =20 eni_vdpa->vring =3D devm_kcalloc(&pdev->dev, eni_vdpa->queues, diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_mai= n.c index ccf64d7bbfaa..64d28ec97136 100644 --- a/drivers/vdpa/ifcvf/ifcvf_main.c +++ b/drivers/vdpa/ifcvf/ifcvf_main.c @@ -713,7 +713,7 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mde= v, const char *name, =20 ifcvf_mgmt_dev->adapter =3D adapter; adapter->pdev =3D pdev; - adapter->vdpa.dma_dev =3D &pdev->dev; + adapter->vdpa.map_token =3D &pdev->dev; adapter->vdpa.mdev =3D mdev; adapter->vf =3D vf; vdpa_dev =3D &adapter->vdpa; diff --git a/drivers/vdpa/octeon_ep/octep_vdpa_main.c b/drivers/vdpa/octeon= _ep/octep_vdpa_main.c index 9b49efd24391..42a4df4613dd 100644 --- a/drivers/vdpa/octeon_ep/octep_vdpa_main.c +++ b/drivers/vdpa/octeon_ep/octep_vdpa_main.c @@ -516,7 +516,7 @@ static int octep_vdpa_dev_add(struct vdpa_mgmt_dev *mde= v, const char *name, } =20 oct_vdpa->pdev =3D pdev; - oct_vdpa->vdpa.dma_dev =3D &pdev->dev; + oct_vdpa->vdpa.map_token =3D &pdev->dev; oct_vdpa->vdpa.mdev =3D mdev; oct_vdpa->oct_hw =3D oct_hw; vdpa_dev =3D &oct_vdpa->vdpa; diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index 8a372b51c21a..1cc4285ebd67 100644 --- a/drivers/vdpa/vdpa.c +++ b/drivers/vdpa/vdpa.c @@ -151,7 +151,7 @@ static void vdpa_release_dev(struct device *d) * Driver should use vdpa_alloc_device() wrapper macro instead of * using this directly. * - * Return: Returns an error when parent/config/dma_dev is not set or fail = to get + * Return: Returns an error when parent/config/map_token is not set or fai= l to get * ida. */ struct vdpa_device *__vdpa_alloc_device(struct device *parent, diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_= sim.c index c204fc8e471a..7c8e468f2f8c 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -272,7 +272,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr = *dev_attr, vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0], &vdpasim->iommu_lock); =20 - vdpasim->vdpa.dma_dev =3D dev; + vdpasim->vdpa.map_token =3D dev; =20 return vdpasim; =20 diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 6a9a37351310..7420e90488ef 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -2022,7 +2022,7 @@ static int vduse_dev_init_vdpa(struct vduse_dev *dev,= const char *name) return ret; } set_dma_ops(&vdev->vdpa.dev, &vduse_dev_dma_ops); - vdev->vdpa.dma_dev =3D &vdev->vdpa.dev; + vdev->vdpa.map_token =3D &vdev->vdpa.dev; vdev->vdpa.mdev =3D &vduse_mgmt->mgmt_dev; =20 return 0; diff --git a/drivers/vdpa/virtio_pci/vp_vdpa.c b/drivers/vdpa/virtio_pci/vp= _vdpa.c index 8787407f75b0..6e22e95245fa 100644 --- a/drivers/vdpa/virtio_pci/vp_vdpa.c +++ b/drivers/vdpa/virtio_pci/vp_vdpa.c @@ -520,7 +520,7 @@ static int vp_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev= , const char *name, =20 vp_vdpa_mgtdev->vp_vdpa =3D vp_vdpa; =20 - vp_vdpa->vdpa.dma_dev =3D &pdev->dev; + vp_vdpa->vdpa.map_token =3D &pdev->dev; vp_vdpa->queues =3D vp_modern_get_num_queues(mdev); vp_vdpa->mdev =3D mdev; =20 diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 5a49b5a6d496..732ed118c138 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -1320,7 +1320,7 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdpa = *v) { struct vdpa_device *vdpa =3D v->vdpa; const struct vdpa_config_ops *ops =3D vdpa->config; - struct device *dma_dev =3D vdpa_get_dma_dev(vdpa); + struct device *dma_dev =3D vdpa_get_map_token(vdpa); int ret; =20 /* Device want to do DMA by itself */ @@ -1355,7 +1355,7 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdpa = *v) static void vhost_vdpa_free_domain(struct vhost_vdpa *v) { struct vdpa_device *vdpa =3D v->vdpa; - struct device *dma_dev =3D vdpa_get_dma_dev(vdpa); + struct device *dma_dev =3D vdpa_get_map_token(vdpa); =20 if (v->domain) { iommu_detach_device(v->domain, dma_dev); diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c index 59b53032f1e2..cb68458cd809 100644 --- a/drivers/virtio/virtio_vdpa.c +++ b/drivers/virtio/virtio_vdpa.c @@ -147,7 +147,6 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsign= ed int index, { struct virtio_vdpa_device *vd_dev =3D to_virtio_vdpa_device(vdev); struct vdpa_device *vdpa =3D vd_get_vdpa(vdev); - struct device *dma_dev; const struct vdpa_config_ops *ops =3D vdpa->config; struct virtio_vdpa_vq_info *info; bool (*notify)(struct virtqueue *vq) =3D virtio_vdpa_notify; @@ -159,6 +158,7 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsign= ed int index, unsigned long flags; u32 align, max_num, min_num =3D 1; bool may_reduce_num =3D true; + void *map_token; int err; =20 if (!name) @@ -201,13 +201,13 @@ virtio_vdpa_setup_vq(struct virtio_device *vdev, unsi= gned int index, /* Create the vring */ align =3D ops->get_vq_align(vdpa); =20 - if (ops->get_vq_dma_dev) - dma_dev =3D ops->get_vq_dma_dev(vdpa, index); + if (ops->get_vq_map_token) + map_token =3D ops->get_vq_map_token(vdpa, index); else - dma_dev =3D vdpa_get_dma_dev(vdpa); + map_token =3D vdpa_get_map_token(vdpa); vq =3D vring_create_virtqueue_map(index, max_num, align, vdev, true, may_reduce_num, ctx, - notify, callback, name, dma_dev); + notify, callback, name, map_token); if (!vq) { err =3D -ENOMEM; goto error_new_virtqueue; @@ -497,7 +497,7 @@ static int virtio_vdpa_probe(struct vdpa_device *vdpa) if (!vd_dev) return -ENOMEM; =20 - vd_dev->vdev.dev.parent =3D vdpa_get_dma_dev(vdpa); + vd_dev->vdev.dev.parent =3D vdpa_get_map_token(vdpa); vd_dev->vdev.dev.release =3D virtio_vdpa_release_dev; vd_dev->vdev.config =3D &virtio_vdpa_config_ops; vd_dev->vdpa =3D vdpa; diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 2e7a30fe6b92..352ca5609c9a 100644 --- a/include/linux/vdpa.h +++ b/include/linux/vdpa.h @@ -70,7 +70,7 @@ struct vdpa_mgmt_dev; /** * struct vdpa_device - representation of a vDPA device * @dev: underlying device - * @dma_dev: the actual device that is performing DMA + * @map_token: the token passed to upper layer to be used for mappping * @driver_override: driver name to force a match; do not set directly, * because core frees it; use driver_set_override() to * set or clear it. @@ -87,7 +87,7 @@ struct vdpa_mgmt_dev; */ struct vdpa_device { struct device dev; - struct device *dma_dev; + void *map_token; const char *driver_override; const struct vdpa_config_ops *config; struct rw_semaphore cf_lock; /* Protects get/set config */ @@ -352,7 +352,7 @@ struct vdpa_map_file { * @vdev: vdpa device * @asid: address space identifier * Returns integer: success (0) or error (< 0) - * @get_vq_dma_dev: Get the dma device for a specific + * @get_vq_map_token: Get the map token for a specific * virtqueue (optional) * @vdev: vdpa device * @idx: virtqueue index @@ -436,7 +436,7 @@ struct vdpa_config_ops { int (*reset_map)(struct vdpa_device *vdev, unsigned int asid); int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group, unsigned int asid); - struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx); + struct device *(*get_vq_map_token)(struct vdpa_device *vdev, u16 idx); int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm); void (*unbind_mm)(struct vdpa_device *vdev); =20 @@ -520,9 +520,9 @@ static inline void vdpa_set_drvdata(struct vdpa_device = *vdev, void *data) dev_set_drvdata(&vdev->dev, data); } =20 -static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev) +static inline void *vdpa_get_map_token(struct vdpa_device *vdev) { - return vdev->dma_dev; + return vdev->map_token; } =20 static inline int vdpa_reset(struct vdpa_device *vdev, u32 flags) --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 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 644D41DE2A5 for ; Tue, 1 Jul 2025 01:15:06 +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=1751332508; cv=none; b=aEXiXkPqdoIb1pCfBITVLUSH8IRydpVcsBU3n7LVmGABaYCrIdNlG2SQpIoyRhbYZqbbn8uxUEnsmCtxQFZcal2q8hzeQu2ynuVSAdi0vKk+mS0rA5NZLy4CR9C71RzaorIomf2AKhT+OlfnI55NTQZqdnHkv5pZxkMQMRjcjB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332508; c=relaxed/simple; bh=55/NSFXrWdEpV1D/v39C2nQXFEA1ZVHNlRY+nPen2Bc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OK4Qd3xdxRzo/NANHHoPWpDqY01eLhxZ1DeqT2oRyCYPtfwSL880dRu7UVBt4Mhi05C3vp+PVlpkEKFspi/tzNMlKj6B3R+1vMQM0TqjRCfSFxCqXUVzLJSDXWxPJUTU+Naan+7lsQfWnDDJq11lPZiTlmhHRDp2ciI3wdi2jyM= 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=IO3JxkTJ; 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="IO3JxkTJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332505; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=je3b8PIs0E6FvZBuxFcoFdovw4S2zeNhIppefR0E2Og=; b=IO3JxkTJjLsNMlzeUS6ljNiw2xHq1iHOVJhULC4AUiNq+7eyC9AxzPSSpFSq/Uq6MFOLRi EbaXPMqYYAt68AHZBcRoAHTZ8BFCftD+98pRZSlEpULKJCOHPyb2JwqKd5hg9xIWkufN+q jGTL5Yn4yUc97WeteKl+XooVdJXkZ2Q= Received: from mx-prod-mc-04.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-592-58y2rdvZP9enLAN_heBbAQ-1; Mon, 30 Jun 2025 21:15:01 -0400 X-MC-Unique: 58y2rdvZP9enLAN_heBbAQ-1 X-Mimecast-MFC-AGG-ID: 58y2rdvZP9enLAN_heBbAQ_1751332500 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 935DF19560A2; Tue, 1 Jul 2025 01:15:00 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 866EE30001B5; Tue, 1 Jul 2025 01:14:54 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 8/9] vdpa: introduce map ops Date: Tue, 1 Jul 2025 09:14:00 +0800 Message-ID: <20250701011401.74851-9-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Virtio core allows the transport to provide device or transport specific mapping functions. This patch adds this support to vDPA. We can simply do this by allowing the vDPA parent to register a virtio_map_ops. Signed-off-by: Jason Wang --- drivers/vdpa/alibaba/eni_vdpa.c | 3 ++- drivers/vdpa/ifcvf/ifcvf_main.c | 3 ++- drivers/vdpa/octeon_ep/octep_vdpa_main.c | 4 ++-- drivers/vdpa/pds/vdpa_dev.c | 3 ++- drivers/vdpa/solidrun/snet_main.c | 4 ++-- drivers/vdpa/vdpa.c | 3 +++ drivers/vdpa/vdpa_sim/vdpa_sim.c | 2 +- drivers/vdpa/vdpa_user/iova_domain.c | 6 ++++++ drivers/vdpa/vdpa_user/iova_domain.h | 3 +++ drivers/vdpa/vdpa_user/vduse_dev.c | 3 ++- drivers/vdpa/virtio_pci/vp_vdpa.c | 3 ++- drivers/vhost/vdpa.c | 9 ++++++++- drivers/virtio/virtio_vdpa.c | 1 + include/linux/vdpa.h | 10 +++++++--- 14 files changed, 43 insertions(+), 14 deletions(-) diff --git a/drivers/vdpa/alibaba/eni_vdpa.c b/drivers/vdpa/alibaba/eni_vdp= a.c index 34bf726dc660..4ddf23065087 100644 --- a/drivers/vdpa/alibaba/eni_vdpa.c +++ b/drivers/vdpa/alibaba/eni_vdpa.c @@ -478,7 +478,8 @@ static int eni_vdpa_probe(struct pci_dev *pdev, const s= truct pci_device_id *id) return ret; =20 eni_vdpa =3D vdpa_alloc_device(struct eni_vdpa, vdpa, - dev, &eni_vdpa_ops, 1, 1, NULL, false); + dev, &eni_vdpa_ops, NULL, + 1, 1, NULL, false); if (IS_ERR(eni_vdpa)) { ENI_ERR(pdev, "failed to allocate vDPA structure\n"); return PTR_ERR(eni_vdpa); diff --git a/drivers/vdpa/ifcvf/ifcvf_main.c b/drivers/vdpa/ifcvf/ifcvf_mai= n.c index 64d28ec97136..1bc22020d0cc 100644 --- a/drivers/vdpa/ifcvf/ifcvf_main.c +++ b/drivers/vdpa/ifcvf/ifcvf_main.c @@ -705,7 +705,8 @@ static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mde= v, const char *name, vf =3D &ifcvf_mgmt_dev->vf; pdev =3D vf->pdev; adapter =3D vdpa_alloc_device(struct ifcvf_adapter, vdpa, - &pdev->dev, &ifc_vdpa_ops, 1, 1, NULL, false); + &pdev->dev, &ifc_vdpa_ops, + NULL, 1, 1, NULL, false); if (IS_ERR(adapter)) { IFCVF_ERR(pdev, "Failed to allocate vDPA structure"); return PTR_ERR(adapter); diff --git a/drivers/vdpa/octeon_ep/octep_vdpa_main.c b/drivers/vdpa/octeon= _ep/octep_vdpa_main.c index 42a4df4613dd..bb4a68b6cce5 100644 --- a/drivers/vdpa/octeon_ep/octep_vdpa_main.c +++ b/drivers/vdpa/octeon_ep/octep_vdpa_main.c @@ -508,8 +508,8 @@ static int octep_vdpa_dev_add(struct vdpa_mgmt_dev *mde= v, const char *name, u64 device_features; int ret; =20 - oct_vdpa =3D vdpa_alloc_device(struct octep_vdpa, vdpa, &pdev->dev, &octe= p_vdpa_ops, 1, 1, - NULL, false); + oct_vdpa =3D vdpa_alloc_device(struct octep_vdpa, vdpa, &pdev->dev, &octe= p_vdpa_ops, + NULL, 1, 1, NULL, false); if (IS_ERR(oct_vdpa)) { dev_err(&pdev->dev, "Failed to allocate vDPA structure for octep vdpa de= vice"); return PTR_ERR(oct_vdpa); diff --git a/drivers/vdpa/pds/vdpa_dev.c b/drivers/vdpa/pds/vdpa_dev.c index 301d95e08596..d2a017697827 100644 --- a/drivers/vdpa/pds/vdpa_dev.c +++ b/drivers/vdpa/pds/vdpa_dev.c @@ -632,7 +632,8 @@ static int pds_vdpa_dev_add(struct vdpa_mgmt_dev *mdev,= const char *name, } =20 pdsv =3D vdpa_alloc_device(struct pds_vdpa_device, vdpa_dev, - dev, &pds_vdpa_ops, 1, 1, name, false); + dev, &pds_vdpa_ops, NULL, + 1, 1, name, false); if (IS_ERR(pdsv)) { dev_err(dev, "Failed to allocate vDPA structure: %pe\n", pdsv); return PTR_ERR(pdsv); diff --git a/drivers/vdpa/solidrun/snet_main.c b/drivers/vdpa/solidrun/snet= _main.c index 55ec51c17ab3..46f1743eb9f5 100644 --- a/drivers/vdpa/solidrun/snet_main.c +++ b/drivers/vdpa/solidrun/snet_main.c @@ -1008,8 +1008,8 @@ static int snet_vdpa_probe_vf(struct pci_dev *pdev) } =20 /* Allocate vdpa device */ - snet =3D vdpa_alloc_device(struct snet, vdpa, &pdev->dev, &snet_config_op= s, 1, 1, NULL, - false); + snet =3D vdpa_alloc_device(struct snet, vdpa, &pdev->dev, &snet_config_op= s, + NULL, 1, 1, NULL, false); if (!snet) { SNET_ERR(pdev, "Failed to allocate a vdpa device\n"); ret =3D -ENOMEM; diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c index 1cc4285ebd67..2715ffcda585 100644 --- a/drivers/vdpa/vdpa.c +++ b/drivers/vdpa/vdpa.c @@ -142,6 +142,7 @@ static void vdpa_release_dev(struct device *d) * initialized but before registered. * @parent: the parent device * @config: the bus operations that is supported by this device + * @map: the map opeartions that is supported by this device * @ngroups: number of groups supported by this device * @nas: number of address spaces supported by this device * @size: size of the parent structure that contains private data @@ -156,6 +157,7 @@ static void vdpa_release_dev(struct device *d) */ struct vdpa_device *__vdpa_alloc_device(struct device *parent, const struct vdpa_config_ops *config, + const struct virtio_map_ops *map, unsigned int ngroups, unsigned int nas, size_t size, const char *name, bool use_va) @@ -187,6 +189,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *= parent, vdev->dev.release =3D vdpa_release_dev; vdev->index =3D err; vdev->config =3D config; + vdev->map =3D map; vdev->features_valid =3D false; vdev->use_va =3D use_va; vdev->ngroups =3D ngroups; diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_= sim.c index 7c8e468f2f8c..89a795e2a44b 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -215,7 +215,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr = *dev_attr, else ops =3D &vdpasim_config_ops; =20 - vdpa =3D __vdpa_alloc_device(NULL, ops, + vdpa =3D __vdpa_alloc_device(NULL, ops, NULL, dev_attr->ngroups, dev_attr->nas, dev_attr->alloc_size, dev_attr->name, use_va); diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/= iova_domain.c index 58116f89d8da..019f3305c0ac 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -506,6 +506,12 @@ void vduse_domain_free_coherent(struct vduse_iova_doma= in *domain, size_t size, free_pages_exact(phys_to_virt(pa), size); } =20 +bool vduse_domain_need_sync(struct vduse_iova_domain *domain, + dma_addr_t dma_addr) +{ + return dma_addr < domain->bounce_size; +} + static vm_fault_t vduse_domain_mmap_fault(struct vm_fault *vmf) { struct vduse_iova_domain *domain =3D vmf->vma->vm_private_data; diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/= iova_domain.h index 7f3f0928ec78..846572b95c23 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -70,6 +70,9 @@ void vduse_domain_free_coherent(struct vduse_iova_domain = *domain, size_t size, void *vaddr, dma_addr_t dma_addr, unsigned long attrs); =20 +bool vduse_domain_need_sync(struct vduse_iova_domain *domain, + dma_addr_t dma_addr); + void vduse_domain_reset_bounce_map(struct vduse_iova_domain *domain); =20 int vduse_domain_add_user_bounce_pages(struct vduse_iova_domain *domain, diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 7420e90488ef..64bc39722007 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -2009,7 +2009,8 @@ static int vduse_dev_init_vdpa(struct vduse_dev *dev,= const char *name) return -EEXIST; =20 vdev =3D vdpa_alloc_device(struct vduse_vdpa, vdpa, dev->dev, - &vduse_vdpa_config_ops, 1, 1, name, true); + &vduse_vdpa_config_ops, NULL, + 1, 1, name, true); if (IS_ERR(vdev)) return PTR_ERR(vdev); =20 diff --git a/drivers/vdpa/virtio_pci/vp_vdpa.c b/drivers/vdpa/virtio_pci/vp= _vdpa.c index 6e22e95245fa..395996ec4608 100644 --- a/drivers/vdpa/virtio_pci/vp_vdpa.c +++ b/drivers/vdpa/virtio_pci/vp_vdpa.c @@ -511,7 +511,8 @@ static int vp_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev= , const char *name, int ret, i; =20 vp_vdpa =3D vdpa_alloc_device(struct vp_vdpa, vdpa, - dev, &vp_vdpa_ops, 1, 1, name, false); + dev, &vp_vdpa_ops, NULL, + 1, 1, name, false); =20 if (IS_ERR(vp_vdpa)) { dev_err(dev, "vp_vdpa: Failed to allocate vDPA structure\n"); diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 732ed118c138..4932271899ea 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -1320,13 +1320,20 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdp= a *v) { struct vdpa_device *vdpa =3D v->vdpa; const struct vdpa_config_ops *ops =3D vdpa->config; - struct device *dma_dev =3D vdpa_get_map_token(vdpa); + const struct virtio_map_ops *map =3D vdpa->map; + struct device *dma_dev; int ret; =20 /* Device want to do DMA by itself */ if (ops->set_map || ops->dma_map) return 0; =20 + if (map) { + dev_warn(&v->dev, "Can't allocate a domian, device use vendor specific m= appings\n"); + return -EINVAL; + } + + dma_dev =3D vdpa_get_map_token(vdpa); if (!device_iommu_capable(dma_dev, IOMMU_CAP_CACHE_COHERENCY)) { dev_warn_once(&v->dev, "Failed to allocate domain, device is not IOMMU cache coherent ca= pable\n"); diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c index cb68458cd809..286b60ce3637 100644 --- a/drivers/virtio/virtio_vdpa.c +++ b/drivers/virtio/virtio_vdpa.c @@ -500,6 +500,7 @@ static int virtio_vdpa_probe(struct vdpa_device *vdpa) vd_dev->vdev.dev.parent =3D vdpa_get_map_token(vdpa); vd_dev->vdev.dev.release =3D virtio_vdpa_release_dev; vd_dev->vdev.config =3D &virtio_vdpa_config_ops; + vd_dev->vdev.map =3D vdpa->map; vd_dev->vdpa =3D vdpa; INIT_LIST_HEAD(&vd_dev->virtqueues); spin_lock_init(&vd_dev->lock); diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h index 352ca5609c9a..cb51b7e2e569 100644 --- a/include/linux/vdpa.h +++ b/include/linux/vdpa.h @@ -75,6 +75,7 @@ struct vdpa_mgmt_dev; * because core frees it; use driver_set_override() to * set or clear it. * @config: the configuration ops for this device. + * @map: the map ops for this device * @cf_lock: Protects get and set access to configuration layout. * @index: device index * @features_valid: were features initialized? for legacy guests @@ -90,6 +91,7 @@ struct vdpa_device { void *map_token; const char *driver_override; const struct vdpa_config_ops *config; + const struct virtio_map_ops *map; struct rw_semaphore cf_lock; /* Protects get/set config */ unsigned int index; bool features_valid; @@ -446,6 +448,7 @@ struct vdpa_config_ops { =20 struct vdpa_device *__vdpa_alloc_device(struct device *parent, const struct vdpa_config_ops *config, + const struct virtio_map_ops *map, unsigned int ngroups, unsigned int nas, size_t size, const char *name, bool use_va); @@ -457,6 +460,7 @@ struct vdpa_device *__vdpa_alloc_device(struct device *= parent, * @member: the name of struct vdpa_device within the @dev_struct * @parent: the parent device * @config: the bus operations that is supported by this device + * @map: the map operations that is supported by this device * @ngroups: the number of virtqueue groups supported by this device * @nas: the number of address spaces * @name: name of the vdpa device @@ -464,10 +468,10 @@ struct vdpa_device *__vdpa_alloc_device(struct device= *parent, * * Return allocated data structure or ERR_PTR upon error */ -#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas= , \ - name, use_va) \ +#define vdpa_alloc_device(dev_struct, member, parent, config, map, \ + ngroups, nas, name, use_va) \ container_of((__vdpa_alloc_device( \ - parent, config, ngroups, nas, \ + parent, config, map, ngroups, nas, \ (sizeof(dev_struct) + \ BUILD_BUG_ON_ZERO(offsetof( \ dev_struct, member))), name, use_va)), \ --=20 2.34.1 From nobody Wed Oct 8 07:35:01 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 A345718E25 for ; Tue, 1 Jul 2025 01:15:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332512; cv=none; b=XVMLH6Jf+dm/3ha10Eyuyi992BtD9l2F1VH1d00SdD+BhwMCyzT7owEu3Z/6nuaKyyPGK+cZ/F7zWnpHI+qan4fsPfwOMwtte/mv4BDsu/S4wiuh6MQbZj6r4rlLrdCu+ozvhhElImc5k6qEWRrWedXOk0MIljIOrpWtL51XcuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751332512; c=relaxed/simple; bh=VUVsBfN9svwjFZAwqjMlXux23lDyc72bG0JSXdyitQI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ovKPrj7n7fWKAqtjChT3GC57U7IrRyIbFAJFUHh7lqQHa9Xk4HjfYW4bvgxW3ArpIVqf7hdEr66e1dks2BkwYWTMxTJfW4lFwbvssvWRmMivfRPe8omL+SI9JGtwwI/gf4GjKKWulWwkFVzzEGfiBU7GGvKLqf5U5azu7J8KW6A= 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=QgRYUPRU; arc=none smtp.client-ip=170.10.129.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="QgRYUPRU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751332509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ztKCE441E4iY+w5SsH1xNzKVblctvlPQOmwjwNGLiFI=; b=QgRYUPRUIQSpO1QdzFT7LI6e/yCOOz6bWtpHzCcYKYcm2NCnspBTrA1vJoIIgS3X4shkea 2/5jFBzNeFpOr6XbCl5ipdU0IyTfRLzZhqHqLEg/870ZeCjASMikcFyoW3/iGl9DEf7o2U v8bVqhWe11liAfOh4O70blngf9+Na+g= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-663-XtNNRmT3N0WeBEmXCozKYA-1; Mon, 30 Jun 2025 21:15:08 -0400 X-MC-Unique: XtNNRmT3N0WeBEmXCozKYA-1 X-Mimecast-MFC-AGG-ID: XtNNRmT3N0WeBEmXCozKYA_1751332506 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CDC1918002EC; Tue, 1 Jul 2025 01:15:06 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C6F4030001B1; Tue, 1 Jul 2025 01:15:01 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, hch@infradead.org, xieyongji@bytedance.com Subject: [PATCH 9/9] vduse: switch to use virtio map API instead of DMA API Date: Tue, 1 Jul 2025 09:14:01 +0800 Message-ID: <20250701011401.74851-10-jasowang@redhat.com> In-Reply-To: <20250701011401.74851-1-jasowang@redhat.com> References: <20250701011401.74851-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Lacking the support of device specific mapping supported in virtio, VDUSE must trick the DMA API in order to make virtio-vdpa transport work. This is done by advertising vDPA device as dma device with a VDUSE specific dma_ops even if it doesn't do DMA at all. This will be fixed by this patch. Thanks to the new mapping operations support by virtio and vDPA. VDUSE can simply switch to advertise its specific mappings operations to virtio via virtio-vdpa then DMA API is not needed for VDUSE any more. Signed-off-by: Jason Wang --- drivers/vdpa/vdpa_user/iova_domain.c | 2 +- drivers/vdpa/vdpa_user/iova_domain.h | 2 +- drivers/vdpa/vdpa_user/vduse_dev.c | 31 ++++++++++++++++------------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/drivers/vdpa/vdpa_user/iova_domain.c b/drivers/vdpa/vdpa_user/= iova_domain.c index 019f3305c0ac..8ea311692545 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.c +++ b/drivers/vdpa/vdpa_user/iova_domain.c @@ -447,7 +447,7 @@ void vduse_domain_unmap_page(struct vduse_iova_domain *= domain, =20 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain, size_t size, dma_addr_t *dma_addr, - gfp_t flag, unsigned long attrs) + gfp_t flag) { struct iova_domain *iovad =3D &domain->consistent_iovad; unsigned long limit =3D domain->iova_limit; diff --git a/drivers/vdpa/vdpa_user/iova_domain.h b/drivers/vdpa/vdpa_user/= iova_domain.h index 846572b95c23..a2316571671f 100644 --- a/drivers/vdpa/vdpa_user/iova_domain.h +++ b/drivers/vdpa/vdpa_user/iova_domain.h @@ -64,7 +64,7 @@ void vduse_domain_unmap_page(struct vduse_iova_domain *do= main, =20 void *vduse_domain_alloc_coherent(struct vduse_iova_domain *domain, size_t size, dma_addr_t *dma_addr, - gfp_t flag, unsigned long attrs); + gfp_t flag); =20 void vduse_domain_free_coherent(struct vduse_iova_domain *domain, size_t s= ize, void *vaddr, dma_addr_t dma_addr, diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 64bc39722007..f86d7111e103 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -814,51 +814,55 @@ static const struct vdpa_config_ops vduse_vdpa_config= _ops =3D { .free =3D vduse_vdpa_free, }; =20 -static void vduse_dev_sync_single_for_device(struct device *dev, +static void vduse_dev_sync_single_for_device(void *token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; =20 vduse_domain_sync_single_for_device(domain, dma_addr, size, dir); } =20 -static void vduse_dev_sync_single_for_cpu(struct device *dev, +static void vduse_dev_sync_single_for_cpu(void *token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; =20 vduse_domain_sync_single_for_cpu(domain, dma_addr, size, dir); } =20 -static dma_addr_t vduse_dev_map_page(struct device *dev, struct page *page, +static dma_addr_t vduse_dev_map_page(void *token, struct page *page, unsigned long offset, size_t size, enum dma_data_direction dir, unsigned long attrs) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; =20 return vduse_domain_map_page(domain, page, offset, size, dir, attrs); } =20 -static void vduse_dev_unmap_page(struct device *dev, dma_addr_t dma_addr, +static void vduse_dev_unmap_page(void *token, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; =20 return vduse_domain_unmap_page(domain, dma_addr, size, dir, attrs); } =20 -static void *vduse_dev_alloc_coherent(struct device *dev, size_t size, - dma_addr_t *dma_addr, gfp_t flag, - unsigned long attrs) +static void *vduse_dev_alloc_coherent(void *token, size_t size, + dma_addr_t *dma_addr, gfp_t flag) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; unsigned long iova; @@ -866,7 +870,7 @@ static void *vduse_dev_alloc_coherent(struct device *de= v, size_t size, =20 *dma_addr =3D DMA_MAPPING_ERROR; addr =3D vduse_domain_alloc_coherent(domain, size, - (dma_addr_t *)&iova, flag, attrs); + (dma_addr_t *)&iova, flag); if (!addr) return NULL; =20 @@ -875,25 +879,27 @@ static void *vduse_dev_alloc_coherent(struct device *= dev, size_t size, return addr; } =20 -static void vduse_dev_free_coherent(struct device *dev, size_t size, +static void vduse_dev_free_coherent(void *token, size_t size, void *vaddr, dma_addr_t dma_addr, unsigned long attrs) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; =20 vduse_domain_free_coherent(domain, size, vaddr, dma_addr, attrs); } =20 -static size_t vduse_dev_max_mapping_size(struct device *dev) +static size_t vduse_dev_max_mapping_size(void *token) { + struct device *dev =3D token; struct vduse_dev *vdev =3D dev_to_vduse(dev); struct vduse_iova_domain *domain =3D vdev->domain; =20 return domain->bounce_size; } =20 -static const struct dma_map_ops vduse_dev_dma_ops =3D { +static const struct virtio_map_ops vduse_map_ops =3D { .sync_single_for_device =3D vduse_dev_sync_single_for_device, .sync_single_for_cpu =3D vduse_dev_sync_single_for_cpu, .map_page =3D vduse_dev_map_page, @@ -2009,7 +2015,7 @@ static int vduse_dev_init_vdpa(struct vduse_dev *dev,= const char *name) return -EEXIST; =20 vdev =3D vdpa_alloc_device(struct vduse_vdpa, vdpa, dev->dev, - &vduse_vdpa_config_ops, NULL, + &vduse_vdpa_config_ops, &vduse_map_ops, 1, 1, name, true); if (IS_ERR(vdev)) return PTR_ERR(vdev); @@ -2022,7 +2028,6 @@ static int vduse_dev_init_vdpa(struct vduse_dev *dev,= const char *name) put_device(&vdev->vdpa.dev); return ret; } - set_dma_ops(&vdev->vdpa.dev, &vduse_dev_dma_ops); vdev->vdpa.map_token =3D &vdev->vdpa.dev; vdev->vdpa.mdev =3D &vduse_mgmt->mgmt_dev; =20 --=20 2.34.1