linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount
@ 2021-07-17 10:20 Xiyu Yang
  2021-07-18 20:20 ` kernel test robot
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Xiyu Yang @ 2021-07-17 10:20 UTC (permalink / raw)
  To: Michael S. Tsirkin, Jason Wang, kvm, virtualization, netdev,
	linux-kernel
  Cc: yuanxzhang, Xiyu Yang, Xin Tan

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(-)

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount
  2021-07-17 10:20 [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount Xiyu Yang
@ 2021-07-18 20:20 ` kernel test robot
  2021-07-18 22:38 ` kernel test robot
  2021-09-05 16:17 ` Michael S. Tsirkin
  2 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2021-07-18 20:20 UTC (permalink / raw)
  To: Xiyu Yang, Michael S. Tsirkin, Jason Wang, kvm, virtualization,
	netdev, linux-kernel
  Cc: kbuild-all, yuanxzhang, Xiyu Yang, Xin Tan

[-- Attachment #1: Type: text/plain, Size: 1993 bytes --]

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

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 33714 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount
  2021-07-17 10:20 [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount Xiyu Yang
  2021-07-18 20:20 ` kernel test robot
@ 2021-07-18 22:38 ` kernel test robot
  2021-09-05 16:17 ` Michael S. Tsirkin
  2 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2021-07-18 22:38 UTC (permalink / raw)
  To: Xiyu Yang, Michael S. Tsirkin, Jason Wang, kvm, virtualization,
	netdev, linux-kernel
  Cc: clang-built-linux, kbuild-all, yuanxzhang, Xiyu Yang, Xin Tan

[-- Attachment #1: Type: text/plain, Size: 2322 bytes --]

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

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 45926 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount
  2021-07-17 10:20 [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount Xiyu Yang
  2021-07-18 20:20 ` kernel test robot
  2021-07-18 22:38 ` kernel test robot
@ 2021-09-05 16:17 ` Michael S. Tsirkin
  2 siblings, 0 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2021-09-05 16:17 UTC (permalink / raw)
  To: Xiyu Yang
  Cc: Jason Wang, kvm, virtualization, netdev, linux-kernel,
	yuanxzhang, Xin Tan

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-09-05 16:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-17 10:20 [PATCH] vhost_net: Convert from atomic_t to refcount_t on vhost_net_ubuf_ref->refcount Xiyu Yang
2021-07-18 20:20 ` kernel test robot
2021-07-18 22:38 ` kernel test robot
2021-09-05 16:17 ` Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).