Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp2131424pxb; Sun, 5 Sep 2021 09:27:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2IXdm0h6OcB+p8uhB95ToEm9t9WbvrRFwpVXUTCtREDWYQfuEc0VYtpBMEBKAMNuyChYH X-Received: by 2002:a05:6e02:ee1:: with SMTP id j1mr5587020ilk.61.1630859242728; Sun, 05 Sep 2021 09:27:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630859242; cv=none; d=google.com; s=arc-20160816; b=UG3jLmpBew3kBLLKVo6pFTPLF1ZM2o2RKmRrP/lKAqx7xeonxLzLHmz67BOX2fWJTg 3v+VFCrkrnozVo/IUsDF8/hRtrM00QuUHSQFN5VlbHL19Vm8ZP1ZplQTo+pFeOS28XUC X0rwauCS1Ck0UZScFZ72JvrLA8laUDcyQCTK/ANcYiQ3mggyiuD31TQberTwnbuhROWU ab6FmYweZOqDipwush9dbDprXSCt1Dig6xpjIRS54+2ousyRMoV//zvfekim0XUQ7lId PubGiLph6qlluXweHgrL6u84awsVLjXFUdFpOQWRqEip5ElC0F7VmFXVclGmJwf6CgP1 t2aA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=hW1sN5QGx3Kx2EPNmaIE4CRt6Il/ANBqcp567eWWlqk=; b=aW0121BcS6icnBbr0E8Q8z2q9ELz+AVLlf4tRf18S7z/pUQLr7iE+Ee3oo5cm39FFk sBpl5Tpci7E3aBtOalLL2DIEcg0OYn9trhfhtPU6ep4o6FeE0KligZDqH58ymwcIePG3 Y839Vbx6RZluGZChE36fhQ9amYDnDomKBqM/3A5ofWdiEpAXYSZyj7u1/TOSD9oXUycP n5OMFk5c9xTxgf3QL43rJrU25nkrsUI3ThYwM/1H+4b+itXXzuxqh5LGAWXBpW3PXAPy Y9AVoEZkxte2k3WQkxY5wmNBTDCFylCPbFpUeY/OEjXVgm9OucSw8B44r1wcphcE65oV ciEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NzkRj9Ti; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g7si2753995ilb.86.2021.09.05.09.27.11; Sun, 05 Sep 2021 09:27:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NzkRj9Ti; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S237898AbhIEQTH (ORCPT + 99 others); Sun, 5 Sep 2021 12:19:07 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:34599 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230387AbhIEQTG (ORCPT ); Sun, 5 Sep 2021 12:19:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630858682; 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: in-reply-to:in-reply-to:references:references; bh=hW1sN5QGx3Kx2EPNmaIE4CRt6Il/ANBqcp567eWWlqk=; b=NzkRj9Titj7saPXnb7z70ke5gZiqAyAknju4Az/6KZCzKUsLsU+WO2UodfHunzETTk4bFe OPI5/+0u66svGhm2/qnhTZ/YOnnR+e29aZBEKDsgjdGraHm0kYIOf6wwAJLoDEVNhEt0Zo l9jS6VRAJN4wAQDlGER5CecQxyyQPe8= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-390-PxAL8IZAOQKBMIp1Q-d1GQ-1; Sun, 05 Sep 2021 12:18:01 -0400 X-MC-Unique: PxAL8IZAOQKBMIp1Q-d1GQ-1 Received: by mail-wm1-f71.google.com with SMTP id r4-20020a1c4404000000b002e728beb9fbso2725806wma.9 for ; Sun, 05 Sep 2021 09:18:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=hW1sN5QGx3Kx2EPNmaIE4CRt6Il/ANBqcp567eWWlqk=; b=aXsoZ1serI4zvsmrnmpOytSuhsZkvuzb+EJuPp7RFBxSJKkSORPDecA8lyG6BIuFRV xkee5zo/erWd5xcKR2qXF+qjZWcxZQcbrnjxf9OzATe3WHqbnuZZxHejyf5l3pJd6tKl ltGM/H9xxRFAztP4dp/zswF9BxS61Zz9KKIJQ9PZc4Rx8x4287jxp9EQt8EXQhlWoZCZ m0Dg8OGgjg7ZM2gWaz6WCExQk+2owtLsr+N4+XBQIOildYD1yZXbUtDUfOz+33qXFr9f zVJLwvO9bfE+gMmtWKSUIl9AiMzYB3o1trkSNX2TcIkUEo2B0YW6C3tuFg4dyBIM0tB2 /jtg== X-Gm-Message-State: AOAM5321+H5tfdzbsA3ARtWBwdO8E143xlX68XVPw1Nuq25aCwU2gZoK cm0OZJluYU+q5Ga1fZGo7slVBUbVS1UWvfQ4wIKfLOe7ldAG9KzM/l3D0iq5qOqXXvRqquXQ+TM eMXYqIZed1zJHLFWMNb6DYn0i X-Received: by 2002:adf:e6c5:: with SMTP id y5mr9020252wrm.198.1630858680262; Sun, 05 Sep 2021 09:18:00 -0700 (PDT) X-Received: by 2002:adf:e6c5:: with SMTP id y5mr9020227wrm.198.1630858679962; Sun, 05 Sep 2021 09:17:59 -0700 (PDT) Received: from redhat.com ([2.55.131.183]) by smtp.gmail.com with ESMTPSA id n18sm4795503wmc.22.2021.09.05.09.17.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Sep 2021 09:17:58 -0700 (PDT) Date: Sun, 5 Sep 2021 12:17:55 -0400 From: "Michael S. Tsirkin" To: Xiyu Yang Cc: Jason Wang , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, yuanxzhang@fudan.edu.cn, Xin Tan Subject: Re: [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount Message-ID: <20210905121737-mutt-send-email-mst@kernel.org> References: <1626517230-42920-1-git-send-email-xiyuyang19@fudan.edu.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1626517230-42920-1-git-send-email-xiyuyang19@fudan.edu.cn> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jul 17, 2021 at 06:20:30PM +0800, Xiyu Yang wrote: > refcount_t type and corresponding API can protect refcounters from > accidental underflow and overflow and further use-after-free situations. > > Signed-off-by: Xiyu Yang > Signed-off-by: Xin Tan Pls resubmit after addressing the build bot comments. Thanks! > --- > drivers/vhost/net.c | 14 ++++++++------ > 1 file changed, 8 insertions(+), 6 deletions(-) > > diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c > index 6414bd5741b8..e23150ca7d4c 100644 > --- a/drivers/vhost/net.c > +++ b/drivers/vhost/net.c > @@ -5,6 +5,7 @@ > * virtio-net server in host kernel. > */ > > +#include > #include > #include > #include > @@ -92,7 +93,7 @@ struct vhost_net_ubuf_ref { > * 1: no outstanding ubufs > * >1: outstanding ubufs > */ > - atomic_t refcount; > + refcount_t refcount; > wait_queue_head_t wait; > struct vhost_virtqueue *vq; > }; > @@ -240,7 +241,7 @@ vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy) > ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL); > if (!ubufs) > return ERR_PTR(-ENOMEM); > - atomic_set(&ubufs->refcount, 1); > + refcount_set(&ubufs->refcount, 1); > init_waitqueue_head(&ubufs->wait); > ubufs->vq = vq; > return ubufs; > @@ -248,7 +249,8 @@ vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy) > > static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs) > { > - int r = atomic_sub_return(1, &ubufs->refcount); > + refcount_dec(&ubufs->refcount); > + int r = refcount_read(&ubufs->refcount); > if (unlikely(!r)) > wake_up(&ubufs->wait); > return r; > @@ -257,7 +259,7 @@ static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs) > static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs) > { > vhost_net_ubuf_put(ubufs); > - wait_event(ubufs->wait, !atomic_read(&ubufs->refcount)); > + wait_event(ubufs->wait, !refcount_read(&ubufs->refcount)); > } > > static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs) > @@ -909,7 +911,7 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock) > ctl.ptr = ubuf; > msg.msg_controllen = sizeof(ctl); > ubufs = nvq->ubufs; > - atomic_inc(&ubufs->refcount); > + refcount_inc(&ubufs->refcount); > nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV; > } else { > msg.msg_control = NULL; > @@ -1384,7 +1386,7 @@ static void vhost_net_flush(struct vhost_net *n) > vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs); > mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); > n->tx_flush = false; > - atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1); > + refcount_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1); > mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex); > } > } > -- > 2.7.4