From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D5B2C433DB for ; Mon, 1 Feb 2021 06:37:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 12B1764E06 for ; Mon, 1 Feb 2021 06:37:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232260AbhBAGhC (ORCPT ); Mon, 1 Feb 2021 01:37:02 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:51905 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231940AbhBAGdD (ORCPT ); Mon, 1 Feb 2021 01:33:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612161086; 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=KQnIWQf9C6+iWPjav1rsxJB9F4geXbdqF7FYdnK6OQI=; b=DULUdzNf5P9xJ1XiC7azAQGEbKgY/CjGkI4Wc0zYBbFzxs0NsIHjHJm2GAbWYE22YrMOnk a4LcXXuoVMwwx60SsfX+Y4HTKWXLw+9SNojGRRlys7zq69z0ffs10st1ToqimzUDyL6L2O 0PIgw8/9OV1vJZjKpXWZt+t/JozmnNA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-229-wcxT8NBqOf2e-cmHFTfOkw-1; Mon, 01 Feb 2021 01:31:24 -0500 X-MC-Unique: wcxT8NBqOf2e-cmHFTfOkw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 286191842140; Mon, 1 Feb 2021 06:31:23 +0000 (UTC) Received: from [10.72.13.120] (ovpn-13-120.pek2.redhat.com [10.72.13.120]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3F25210016F5; Mon, 1 Feb 2021 06:31:13 +0000 (UTC) Subject: Re: [PATCH RFC v2 02/10] vringh: add 'iotlb_lock' to synchronize iotlb accesses To: Stefano Garzarella Cc: virtualization@lists.linux-foundation.org, Xie Yongji , "Michael S. Tsirkin" , Laurent Vivier , Stefan Hajnoczi , linux-kernel@vger.kernel.org, Max Gurtovoy , kvm@vger.kernel.org References: <20210128144127.113245-1-sgarzare@redhat.com> <20210128144127.113245-3-sgarzare@redhat.com> <017f6e69-b2ec-aed0-5920-a389199e4cf9@redhat.com> <20210129091850.gatf3ih3knw2p4l4@steredhat> From: Jason Wang Message-ID: <40e6c307-b3e0-6bc1-05a1-804500b6fe3f@redhat.com> Date: Mon, 1 Feb 2021 14:31:12 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210129091850.gatf3ih3knw2p4l4@steredhat> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/1/29 下午5:18, Stefano Garzarella wrote: > On Fri, Jan 29, 2021 at 03:43:40PM +0800, Jason Wang wrote: >> >> On 2021/1/28 下午10:41, Stefano Garzarella wrote: >>> Usually iotlb accesses are synchronized with a spinlock. >>> Let's request it as a new parameter in vringh_set_iotlb() and >>> hold it when we navigate the iotlb in iotlb_translate() to avoid >>> race conditions with any new additions/deletions of ranges from >>> the ioltb. >> >> >> Patch looks fine but I wonder if this is the best approach comparing >> to do locking by the caller. > > Initially I tried to hold the lock in the vdpasim_blk_work(), but since > we have a lot of different functions for vringh, I opted to take the > lock at the beginning and release it at the end. > Also because several times I went to see if that call used > iotlb_translate or not. > > This could be a problem for example if we have multiple workers to > handle multiple queues. > > Also, some functions are quite long (e.g. vringh_getdesc_iotlb) and > holding the lock for that long could reduce parallelism. > > For these reasons I thought it was better to hide everything from the > caller who doesn't have to worry about which function calls > iotlb_translate() and thus hold the lock. Fine with me. Acked-by: Jason Wang Thanks > > Thanks, > Stefano > >> >> Thanks >> >> >>> >>> Signed-off-by: Stefano Garzarella >>> --- >>>  include/linux/vringh.h           | 6 +++++- >>>  drivers/vdpa/vdpa_sim/vdpa_sim.c | 3 ++- >>>  drivers/vhost/vringh.c           | 9 ++++++++- >>>  3 files changed, 15 insertions(+), 3 deletions(-) >>> >>> diff --git a/include/linux/vringh.h b/include/linux/vringh.h >>> index 59bd50f99291..9c077863c8f6 100644 >>> --- a/include/linux/vringh.h >>> +++ b/include/linux/vringh.h >>> @@ -46,6 +46,9 @@ struct vringh { >>>      /* IOTLB for this vring */ >>>      struct vhost_iotlb *iotlb; >>> +    /* spinlock to synchronize IOTLB accesses */ >>> +    spinlock_t *iotlb_lock; >>> + >>>      /* The function to call to notify the guest about added buffers */ >>>      void (*notify)(struct vringh *); >>>  }; >>> @@ -258,7 +261,8 @@ static inline __virtio64 cpu_to_vringh64(const >>> struct vringh *vrh, u64 val) >>>  #if IS_REACHABLE(CONFIG_VHOST_IOTLB) >>> -void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb); >>> +void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb, >>> +              spinlock_t *iotlb_lock); >>>  int vringh_init_iotlb(struct vringh *vrh, u64 features, >>>                unsigned int num, bool weak_barriers, >>> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c >>> b/drivers/vdpa/vdpa_sim/vdpa_sim.c >>> index 2183a833fcf4..53238989713d 100644 >>> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c >>> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c >>> @@ -284,7 +284,8 @@ struct vdpasim *vdpasim_create(struct >>> vdpasim_dev_attr *dev_attr) >>>          goto err_iommu; >>>      for (i = 0; i < dev_attr->nvqs; i++) >>> -        vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu); >>> +        vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu, >>> +                 &vdpasim->iommu_lock); >>>      ret = iova_cache_get(); >>>      if (ret) >>> diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c >>> index 85d85faba058..f68122705719 100644 >>> --- a/drivers/vhost/vringh.c >>> +++ b/drivers/vhost/vringh.c >>> @@ -1074,6 +1074,8 @@ static int iotlb_translate(const struct vringh >>> *vrh, >>>      int ret = 0; >>>      u64 s = 0; >>> +    spin_lock(vrh->iotlb_lock); >>> + >>>      while (len > s) { >>>          u64 size, pa, pfn; >>> @@ -1103,6 +1105,8 @@ static int iotlb_translate(const struct vringh >>> *vrh, >>>          ++ret; >>>      } >>> +    spin_unlock(vrh->iotlb_lock); >>> + >>>      return ret; >>>  } >>> @@ -1262,10 +1266,13 @@ EXPORT_SYMBOL(vringh_init_iotlb); >>>   * vringh_set_iotlb - initialize a vringh for a ring with IOTLB. >>>   * @vrh: the vring >>>   * @iotlb: iotlb associated with this vring >>> + * @iotlb_lock: spinlock to synchronize the iotlb accesses >>>   */ >>> -void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb) >>> +void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb, >>> +              spinlock_t *iotlb_lock) >>>  { >>>      vrh->iotlb = iotlb; >>> +    vrh->iotlb_lock = iotlb_lock; >>>  } >>>  EXPORT_SYMBOL(vringh_set_iotlb); >> >