Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1297962rwl; Fri, 24 Mar 2023 08:43:35 -0700 (PDT) X-Google-Smtp-Source: AKy350b8qNQj4MvuD2rm4M0iBJMSUcO/4cOq8rlb4bf4zk4S8WLuhIqcpNjEMho/iIRSCsBjqRrx X-Received: by 2002:a17:906:a04a:b0:91f:5845:4e3c with SMTP id bg10-20020a170906a04a00b0091f58454e3cmr3204757ejb.42.1679672615286; Fri, 24 Mar 2023 08:43:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679672615; cv=none; d=google.com; s=arc-20160816; b=irsDxRci4uG5dZuZ/3OT9+WxiyE5et4LpE4XtX5WraFHFXUWRCqXyErp6S2o2tHJNG Sus4QuyR5FqRiAqVixOxXsmQgZJ0zQCKRQJKQqMw3DiZUYXS48uy41A6uMH8oEKxa1nV 0DgZ/odf4K815dv0YYH3wrxNxADk665J95HaTx3nAup5H7eK4HfgaxOZfQnxsfG9EVxK lXmAQG1W/FqQ4E+k8F/Giu6fMVg2QUTBtNzzD2yolH5V1cj05vxvlRHNUViFR3/OZLGS o8u0ktrgYIQQM1iJmo4arEBtUT4qDgSNtQEakaRQ/Rxea2PST6tq7ONAVNQBm67JaCJj iRvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gKYp+kOgUX/GJB2EDtARo8NYPrwRtHr03kQCkOwXG8g=; b=abMgY0FW/YxM9Dh/AcsScLSPxiP88nEqzpQTuY5FZr8vJuTrLuUixSJjmk+EggdjDc CJdxix+2j+y/cLQow56W2SYKR4lwgYAsS6tID1Ng1ifgYC35xW7FEEMg3H19vs46oTBC m2g5idhNhALHaQ0kIbC2cJLdMT0R7VVoe8fcd9EB5JW/VFcKDJIOVP/uaQNl5Dn/NMVY HdqseorqPWeh8byWDr+MnkIsSO7LoxSHrcWKs+oe4U5yMNXRicmsOMal9VDmgFTC3YD6 7Ivp0D6jip1ilk2GaesFPCxu2B3qfVZSDlho/hV3REVqsz3jlQf1ew9AfhIKHEW8sWjl F8xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a7d1l5vy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id st11-20020a170907c08b00b0093b2c6244c4si8117150ejc.203.2023.03.24.08.43.11; Fri, 24 Mar 2023 08:43:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a7d1l5vy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232233AbjCXPlX (ORCPT + 99 others); Fri, 24 Mar 2023 11:41:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232249AbjCXPlI (ORCPT ); Fri, 24 Mar 2023 11:41:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 106C892 for ; Fri, 24 Mar 2023 08:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679672418; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gKYp+kOgUX/GJB2EDtARo8NYPrwRtHr03kQCkOwXG8g=; b=a7d1l5vyPQHUlU54EjEtuOhZkJBajSdLQMxeHJDp+RcekV3qUXYFi4bO3lFi4wFPJ6QGPY jmyQ5CKb+/1ppH9EEXEl2WbAOS0tCFIu8BTB428ob423dgMx8WV8wrFw9a2PB1AzUlrtu/ FOVef7uB7/Dx4ljptL5SHTPbH8F7lws= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-626-rYmB9r_DMLimDQocaKtEBA-1; Fri, 24 Mar 2023 11:40:12 -0400 X-MC-Unique: rYmB9r_DMLimDQocaKtEBA-1 Received: by mail-ed1-f71.google.com with SMTP id j21-20020a508a95000000b004fd82403c91so3762614edj.3 for ; Fri, 24 Mar 2023 08:40:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679672411; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gKYp+kOgUX/GJB2EDtARo8NYPrwRtHr03kQCkOwXG8g=; b=xY4dvlJVHU/Ik8rCXUXRjpWJp+MTkbpzNzvpyNhAyyqdp+WJlMZixHp3AcceGwB7iG OFsG1q6tieq3bnWSmcI5QhazFMLhqJ6awQWTuxWOFUagTvzc6rHbno3Y9CStEpjmdDh4 kkgDrsldKhuZ8lx9AoKxknabws4OEjBneuwKDBzGIUcY8D/ySbENT2LCl8QLJHdOgXK8 cKqhtWO2uVRF53vTRU9smlhkYtw/hf8YsctHgxTaL0hdxeYssaJ194FU+8yDlbE61Tn4 u6EpEqBh8CwovtpcOEpIegaHtOBAQ5dybpIbYxOmBYIzlQLrDPGEiZ72Wg1hTwJM5+Td niag== X-Gm-Message-State: AAQBX9fYF4m4C32H16t33EJlxBuYnGXav9QaDYv+qqf8oGDFguHfD5Wq LL3WZMGlr4hEgiQ0VY56D+gxj8byiIpSe7xOhx7r1WrVvWwbjFAvACzH0QGgjTooFLrZSPbOekh DF2S2k5PIe5X9ypx+F0fpYcq3 X-Received: by 2002:a17:907:161f:b0:932:7f5c:4bb2 with SMTP id hb31-20020a170907161f00b009327f5c4bb2mr3662670ejc.75.1679672410842; Fri, 24 Mar 2023 08:40:10 -0700 (PDT) X-Received: by 2002:a17:907:161f:b0:932:7f5c:4bb2 with SMTP id hb31-20020a170907161f00b009327f5c4bb2mr3662649ejc.75.1679672410627; Fri, 24 Mar 2023 08:40:10 -0700 (PDT) Received: from localhost.localdomain (host-82-53-134-98.retail.telecomitalia.it. [82.53.134.98]) by smtp.gmail.com with ESMTPSA id rk28-20020a170907215c00b00933b38505f9sm7857073ejb.152.2023.03.24.08.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:40:09 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko , Jason Wang , eperezma@redhat.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella Subject: [PATCH v4 9/9] vdpa_sim: add support for user VA Date: Fri, 24 Mar 2023 16:40:00 +0100 Message-Id: <20230324154000.47809-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230324153607.46836-1-sgarzare@redhat.com> References: <20230324153607.46836-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- Notes: v4: - checked `use_va` in vdpasim_work_fn() [Jason] - removed `va_enabled` variable now used only in the if condition 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 | 97 +++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 8 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 2b2e439a66f7..2c706bb18897 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,13 +93,20 @@ 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; - - vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, true, - (struct vring_desc *)(uintptr_t)vq->desc_addr, - (struct vring_avail *) - (uintptr_t)vq->driver_addr, - (struct vring_used *) - (uintptr_t)vq->device_addr); + struct vring_desc *desc = (struct vring_desc *) + (uintptr_t)vq->desc_addr; + struct vring_avail *avail = (struct vring_avail *) + (uintptr_t)vq->driver_addr; + struct vring_used *used = (struct vring_used *) + (uintptr_t)vq->device_addr; + + if (use_va && vdpasim->mm_bound) { + vringh_init_iotlb_va(&vq->vring, vdpasim->features, vq->num, + true, desc, avail, used); + } else { + vringh_init_iotlb(&vq->vring, vdpasim->features, vq->num, + true, desc, avail, used); + } vq->vring.last_avail_idx = last_avail_idx; @@ -130,8 +171,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 (use_va && mm) { + if (!mmget_not_zero(mm)) + return; + kthread_use_mm(mm); + } vdpasim->dev_attr.work_fn(vdpasim); + + if (use_va && mm) { + kthread_unuse_mm(mm); + mmput(mm); + } } struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, @@ -162,7 +215,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, vdpa = __vdpa_alloc_device(NULL, ops, dev_attr->ngroups, dev_attr->nas, dev_attr->alloc_size, - dev_attr->name, false); + dev_attr->name, use_va); if (IS_ERR(vdpa)) { ret = PTR_ERR(vdpa); goto err_alloc; @@ -582,6 +635,30 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid, return ret; } +static int vdpasim_bind_mm(struct vdpa_device *vdpa, struct mm_struct *mm) +{ + struct vdpasim *vdpasim = vdpa_to_sim(vdpa); + struct vdpasim_mm_work mm_work; + + mm_work.vdpasim = vdpasim; + mm_work.mm_to_bind = mm; + + vdpasim_worker_change_mm_sync(vdpasim, &mm_work); + + return mm_work.ret; +} + +static void vdpasim_unbind_mm(struct vdpa_device *vdpa) +{ + struct vdpasim *vdpasim = vdpa_to_sim(vdpa); + struct vdpasim_mm_work mm_work; + + mm_work.vdpasim = vdpasim; + mm_work.mm_to_bind = NULL; + + vdpasim_worker_change_mm_sync(vdpasim, &mm_work); +} + static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid, u64 iova, u64 size, u64 pa, u32 perm, void *opaque) @@ -678,6 +755,8 @@ static const struct vdpa_config_ops vdpasim_config_ops = { .set_group_asid = vdpasim_set_group_asid, .dma_map = vdpasim_dma_map, .dma_unmap = vdpasim_dma_unmap, + .bind_mm = vdpasim_bind_mm, + .unbind_mm = vdpasim_unbind_mm, .free = vdpasim_free, }; @@ -712,6 +791,8 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = { .get_iova_range = vdpasim_get_iova_range, .set_group_asid = vdpasim_set_group_asid, .set_map = vdpasim_set_map, + .bind_mm = vdpasim_bind_mm, + .unbind_mm = vdpasim_unbind_mm, .free = vdpasim_free, }; -- 2.39.2