diff mbox series

vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount

Message ID 1626517230-42920-1-git-send-email-xiyuyang19@fudan.edu.cn
State New
Headers show
Series vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount | expand

Commit Message

Xiyu Yang July 17, 2021, 10:20 a.m. UTC
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 <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 drivers/vhost/net.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Comments

kernel test robot July 18, 2021, 8:20 p.m. UTC | #1
Hi Xiyu,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on vhost/linux-next]
[also build test WARNING on v5.14-rc1 next-20210716]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Xiyu-Yang/vhost_net-Convert-from-atomic_t-to-refcount_t-on-vhost_net_ubuf_ref-refcount/20210718-101502
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: i386-randconfig-a015-20210718 (attached as .config)
compiler: gcc-10 (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/8aaf246d79e6bb03b71d9a1a13289f919aa34215
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Xiyu-Yang/vhost_net-Convert-from-atomic_t-to-refcount_t-on-vhost_net_ubuf_ref-refcount/20210718-101502
        git checkout 8aaf246d79e6bb03b71d9a1a13289f919aa34215
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   drivers/vhost/net.c: In function 'vhost_net_ubuf_put':
>> drivers/vhost/net.c:253:2: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

     253 |  int r = refcount_read(&ubufs->refcount);
         |  ^~~


vim +253 drivers/vhost/net.c

   249	
   250	static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
   251	{
   252		refcount_dec(&ubufs->refcount);
 > 253		int r = refcount_read(&ubufs->refcount);

   254		if (unlikely(!r))
   255			wake_up(&ubufs->wait);
   256		return r;
   257	}
   258	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot July 18, 2021, 10:38 p.m. UTC | #2
Hi Xiyu,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on vhost/linux-next]
[also build test WARNING on v5.14-rc1 next-20210716]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Xiyu-Yang/vhost_net-Convert-from-atomic_t-to-refcount_t-on-vhost_net_ubuf_ref-refcount/20210718-101502
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: arm64-randconfig-r021-20210718 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 5d5b08761f944d5b9822d582378333cc4b36a0a7)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm64 cross compiling tool for clang build
        # apt-get install binutils-aarch64-linux-gnu
        # https://github.com/0day-ci/linux/commit/8aaf246d79e6bb03b71d9a1a13289f919aa34215
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Xiyu-Yang/vhost_net-Convert-from-atomic_t-to-refcount_t-on-vhost_net_ubuf_ref-refcount/20210718-101502
        git checkout 8aaf246d79e6bb03b71d9a1a13289f919aa34215
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/vhost/net.c:253:6: warning: ISO C90 forbids mixing declarations and code [-Wdeclaration-after-statement]

           int r = refcount_read(&ubufs->refcount);
               ^
   1 warning generated.


vim +253 drivers/vhost/net.c

   249	
   250	static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
   251	{
   252		refcount_dec(&ubufs->refcount);
 > 253		int r = refcount_read(&ubufs->refcount);

   254		if (unlikely(!r))
   255			wake_up(&ubufs->wait);
   256		return r;
   257	}
   258	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
Michael S. Tsirkin Sept. 5, 2021, 4:17 p.m. UTC | #3
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 <xiyuyang19@fudan.edu.cn>

> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>


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 <linux/refcount.h>

>  #include <linux/compat.h>

>  #include <linux/eventfd.h>

>  #include <linux/vhost.h>

> @@ -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
diff mbox series

Patch

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 <linux/refcount.h>
 #include <linux/compat.h>
 #include <linux/eventfd.h>
 #include <linux/vhost.h>
@@ -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);
 	}
 }