Re: [PATCH v3 8/8] vdpa_sim: add support for user VA

Stefano Garzarella
2023-03-24T14:48:01Z

On Fri, Mar 24, 2023 at 11:49:32AM +0800, Jason Wang wrote:
>
>在 2023/3/21 23:48, Stefano Garzarella 写道:
>>The new "use_va" module parameter (default: true) is used in
>>vdpa_alloc_device() to inform the vDPA framework that the device
>>supports VA.
>>
>>vringh is initialized to use VA only when "use_va" is true and the
>>user's mm has been bound. So, only when the bus supports user VA
>>(e.g. vhost-vdpa).
>>
>>vdpasim_mm_work_fn work is used to serialize the binding to a new
>>address space when the .bind_mm callback is invoked, and unbinding
>>when the .unbind_mm callback is invoked.
>>
>>Call mmget_not_zero()/kthread_use_mm() inside the worker function
>>to pin the address space only as long as needed, following the
>>documentation of mmget() in include/linux/sched/mm.h:
>>
>> * Never use this function to pin this address space for an
>> * unbounded/indefinite amount of time.
>>
>>Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
>>---
>>
>>Notes:
>> v3:
>> - called mmget_not_zero() before kthread_use_mm() [Jason]
>> As the documentation of mmget() in include/linux/sched/mm.h says:
>> * Never use this function to pin this address space for an
>> * unbounded/indefinite amount of time.
>> I moved mmget_not_zero/kthread_use_mm inside the worker function,
>> this way we pin the address space only as long as needed.
>> This is similar to what vfio_iommu_type1_dma_rw_chunk() does in
>> drivers/vfio/vfio_iommu_type1.c
>> - simplified the mm bind/unbind [Jason]
>> - renamed vdpasim_worker_change_mm_sync() [Jason]
>> - fix commit message (s/default: false/default: true)
>> v2:
>> - `use_va` set to true by default [Eugenio]
>> - supported the new unbind_mm callback [Jason]
>> - removed the unbind_mm call in vdpasim_do_reset() [Jason]
>> - avoided to release the lock while call kthread_flush_work() since we
>> are now using a mutex to protect the device state
>>
>> drivers/vdpa/vdpa_sim/vdpa_sim.h | 1 +
>> drivers/vdpa/vdpa_sim/vdpa_sim.c | 80 +++++++++++++++++++++++++++++++-
>> 2 files changed, 79 insertions(+), 2 deletions(-)
>>
>>diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h
>>index 4774292fba8c..3a42887d05d9 100644
>>--- a/drivers/vdpa/vdpa_sim/vdpa_sim.h
>>+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h
>>@@ -59,6 +59,7 @@ struct vdpasim {
>> struct vdpasim_virtqueue *vqs;
>> struct kthread_worker *worker;
>> struct kthread_work work;
>>+ struct mm_struct *mm_bound;
>> struct vdpasim_dev_attr dev_attr;
>> /* mutex to synchronize virtqueue state */
>> struct mutex mutex;
>>diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
>>index ab4cfb82c237..23c891cdcd54 100644
>>--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
>>+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
>>@@ -35,10 +35,44 @@ module_param(max_iotlb_entries, int, 0444);
>> MODULE_PARM_DESC(max_iotlb_entries,
>> "Maximum number of iotlb entries for each address space. 0 means unlimited. (default: 2048)");
>>+static bool use_va = true;
>>+module_param(use_va, bool, 0444);
>>+MODULE_PARM_DESC(use_va, "Enable/disable the device's ability to use VA");
>>+
>> #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
>> #define VDPASIM_QUEUE_MAX 256
>> #define VDPASIM_VENDOR_ID 0
>>+struct vdpasim_mm_work {
>>+ struct kthread_work work;
>>+ struct vdpasim *vdpasim;
>>+ struct mm_struct *mm_to_bind;
>>+ int ret;
>>+};
>>+
>>+static void vdpasim_mm_work_fn(struct kthread_work *work)
>>+{
>>+ struct vdpasim_mm_work *mm_work =
>>+ container_of(work, struct vdpasim_mm_work, work);
>>+ struct vdpasim *vdpasim = mm_work->vdpasim;
>>+
>>+ mm_work->ret = 0;
>>+
>>+ //TODO: should we attach the cgroup of the mm owner?
>>+ vdpasim->mm_bound = mm_work->mm_to_bind;
>>+}
>>+
>>+static void vdpasim_worker_change_mm_sync(struct vdpasim *vdpasim,
>>+ struct vdpasim_mm_work *mm_work)
>>+{
>>+ struct kthread_work *work = &mm_work->work;
>>+
>>+ kthread_init_work(work, vdpasim_mm_work_fn);
>>+ kthread_queue_work(vdpasim->worker, work);
>>+
>>+ kthread_flush_work(work);
>>+}
>>+
>> static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
>> {
>> return container_of(vdpa, struct vdpasim, vdpa);
>>@@ -59,8 +93,10 @@ static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
>> {
>> struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
>> uint16_t last_avail_idx = vq->vring.last_avail_idx;
>>+ bool va_enabled = use_va && vdpasim->mm_bound;
>>- vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, true, false,
>>+ vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, true,
>>+ va_enabled,
>> (struct vring_desc *)(uintptr_t)vq->desc_addr,
>> (struct vring_avail *)
>> (uintptr_t)vq->driver_addr,
>>@@ -130,8 +166,20 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops;
>> static void vdpasim_work_fn(struct kthread_work *work)
>> {
>> struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
>>+ struct mm_struct *mm = vdpasim->mm_bound;
>>+
>>+ if (mm) {
>>+ if (!mmget_not_zero(mm))
>>+ return;
>
>
>Do we need to check use_va here.

Yep, right!

>
>Other than this
>
>Acked-by: Jason Wang <jasowang@redhat.com>

Thanks for the reviews,
Stefano