Skip to contentSkip to conversation

vhost: It's better to use size_t for the 3rd parameter of vhost_exceeds_weight()

PATCH In review wangxu 3 participants 6 messages Sep 23, 2019 – Sep 25, 2019

Subsystems (5)
lore
Share & tools
lore

wangxu

author
#0 lore raw

PATCH 1/1 vhost: It's better to use size_t for the 3rd parameter of vhost_exceeds_weight()

From: Wang Xu <[email protected]>

Caller of vhost_exceeds_weight(..., total_len) in drivers/vhost/net.c usually pass size_t total_len, which may be affected by rx/tx package.

---
 drivers/vhost/vhost.c | 4 ++--
 drivers/vhost/vhost.h | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +2 -2
index 36ca2cf..159223a 100644
@@ -412,7 +412,7 @@ static void vhost_dev_free_iovecs(struct vhost_dev *dev)
 }
 
 bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
-			  int pkts, int total_len)
+			  int pkts, size_t total_len)
 {
 	struct vhost_dev *dev = vq->dev;
 
@@ -454,7 +454,7 @@ static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
 
 void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
-		    int iov_limit, int weight, int byte_weight)
+		    int iov_limit, int weight, size_t byte_weight)
 {
 	struct vhost_virtqueue *vq;
 	int i;
drivers/vhost/vhost.h +4 -3
index e9ed272..8d80389d 100644
@@ -172,12 +172,13 @@ struct vhost_dev {
 	wait_queue_head_t wait;
 	int iov_limit;
 	int weight;
-	int byte_weight;
+	size_t byte_weight;
 };
 
-bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
+bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts,
+			  size_t total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
-		    int nvqs, int iov_limit, int weight, int byte_weight);
+		    int nvqs, int iov_limit, int weight, size_t byte_weight);
 long vhost_dev_set_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
 long vhost_dev_check_owner(struct vhost_dev *);
Signature · 1 line
1.8.5.6
#1 lore raw
Quoted 6 lines · wangxu

From: Wang Xu <[email protected]>

Caller of vhost_exceeds_weight(..., total_len) in drivers/vhost/net.c usually pass size_t total_len, which may be affected by rx/tx package.

Puts a bit more pressure on the register file ...
why do we care? Is there some way that it can
exceed INT_MAX?

Quoted 32 lines
---
 drivers/vhost/vhost.c | 4 ++--
 drivers/vhost/vhost.h | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +2 -2
index 36ca2cf..159223a 100644
@@ -412,7 +412,7 @@ static void vhost_dev_free_iovecs(struct vhost_dev *dev)
 }
 
 bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
-			  int pkts, int total_len)
+			  int pkts, size_t total_len)
 {
 	struct vhost_dev *dev = vq->dev;
 
@@ -454,7 +454,7 @@ static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
 
 void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
-		    int iov_limit, int weight, int byte_weight)
+		    int iov_limit, int weight, size_t byte_weight)
 {
 	struct vhost_virtqueue *vq;
 	int i;
drivers/vhost/vhost.h +0 -0
index e9ed272..8d80389d 100644

drivers/vhost/vhost.h struct vhost_dev { · line 177

Quoted 7 lines
drivers/vhost/vhost.h +1 -1
@@ -172,12 +172,13 @@ struct vhost_dev {
 	wait_queue_head_t wait;
 	int iov_limit;
 	int weight;
-	int byte_weight;
+	size_t byte_weight;
 };
 

This just costs extra memory, and value is never large, so I don't think this matters.

Quoted 11 lines
drivers/vhost/vhost.h +3 -2
-bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
+bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts,
+			  size_t total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
-		    int nvqs, int iov_limit, int weight, int byte_weight);
+		    int nvqs, int iov_limit, int weight, size_t byte_weight);
 long vhost_dev_set_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
 long vhost_dev_check_owner(struct vhost_dev *);
Signature · 1 line
1.8.5.6
#2 lore raw
In reply to wangxu
Quoted 6 lines · wangxu

From: Wang Xu <[email protected]>

Caller of vhost_exceeds_weight(..., total_len) in drivers/vhost/net.c usually pass size_t total_len, which may be affected by rx/tx package.

Puts a bit more pressure on the register file ...
why do we care? Is there some way that it can
exceed INT_MAX?

Quoted 32 lines
---
 drivers/vhost/vhost.c | 4 ++--
 drivers/vhost/vhost.h | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c +2 -2
index 36ca2cf..159223a 100644
@@ -412,7 +412,7 @@ static void vhost_dev_free_iovecs(struct vhost_dev *dev)
 }
 
 bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
-			  int pkts, int total_len)
+			  int pkts, size_t total_len)
 {
 	struct vhost_dev *dev = vq->dev;
 
@@ -454,7 +454,7 @@ static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
 
 void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
-		    int iov_limit, int weight, int byte_weight)
+		    int iov_limit, int weight, size_t byte_weight)
 {
 	struct vhost_virtqueue *vq;
 	int i;
drivers/vhost/vhost.h +0 -0
index e9ed272..8d80389d 100644

drivers/vhost/vhost.h struct vhost_dev { · line 177

Quoted 7 lines
drivers/vhost/vhost.h +1 -1
@@ -172,12 +172,13 @@ struct vhost_dev {
 	wait_queue_head_t wait;
 	int iov_limit;
 	int weight;
-	int byte_weight;
+	size_t byte_weight;
 };
 

This just costs extra memory, and value is never large, so I don't think this matters.

Quoted 11 lines
drivers/vhost/vhost.h +3 -2
-bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
+bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts,
+			  size_t total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
-		    int nvqs, int iov_limit, int weight, int byte_weight);
+		    int nvqs, int iov_limit, int weight, size_t byte_weight);
 long vhost_dev_set_owner(struct vhost_dev *dev);
 bool vhost_dev_has_owner(struct vhost_dev *dev);
 long vhost_dev_check_owner(struct vhost_dev *);
Signature · 1 line
1.8.5.6
#3 lore raw

Hi Michael

	Thanks for your fast reply.

	As the following code, the 2nd branch of iov_iter_advance() does not check if i->count < size, when this happens, i->count -= size may cause len exceed INT_MAX, and then total_len exceed INT_MAX.

	handle_tx_copy() ->
		get_tx_bufs(..., &len, ...) ->
			init_iov_iter() ->
				iov_iter_advance(iter, ...) 	// has 3 branches: 
					pipe_advance() 	 	// has checked the size: if (unlikely(i->count < size)) size = i->count;
					iov_iter_is_discard() ... 	// no check.
					iterate_and_advance() 	//has checked: if (unlikely(i->count < n)) n = i->count;
				return iov_iter_count(iter);

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Monday, September 23, 2019 4:07 PM
To: wangxu (AE) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] vhost: It's better to use size_t for the 3rd parameter of vhost_exceeds_weight()

Quoted 6 lines · wangxu

From: Wang Xu <[email protected]>

Caller of vhost_exceeds_weight(..., total_len) in drivers/vhost/net.c usually pass size_t total_len, which may be affected by rx/tx package.

Puts a bit more pressure on the register file ... why do we care? Is there some way that it can exceed INT_MAX?

Quoted 33 lines
---
 drivers/vhost/vhost.c | 4 ++--
 drivers/vhost/vhost.h | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c index renamed from drivers/vhost/vhost.c +0 -0

36ca2cf..159223a 100644

drivers/vhost/vhost.c +2 -2
@@ -412,7 +412,7 @@ static void vhost_dev_free_iovecs(struct vhost_dev 
 *dev)  }
 
 bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
-			  int pkts, int total_len)
+			  int pkts, size_t total_len)
 {
 	struct vhost_dev *dev = vq->dev;
 
@@ -454,7 +454,7 @@ static size_t vhost_get_desc_size(struct 
 vhost_virtqueue *vq,
 
 void vhost_dev_init(struct vhost_dev *dev,
 		    struct vhost_virtqueue **vqs, int nvqs,
-		    int iov_limit, int weight, int byte_weight)
+		    int iov_limit, int weight, size_t byte_weight)
 {
 	struct vhost_virtqueue *vq;
drivers/vhost/vhost.c +0 -0
 	int i;
drivers/vhost/vhost.h index renamed from drivers/vhost/vhost.h +0 -0

e9ed272..8d80389d 100644

drivers/vhost/vhost.h struct vhost_dev { · line 177

Quoted 7 lines
drivers/vhost/vhost.h +1 -1
@@ -172,12 +172,13 @@ struct vhost_dev {
 	wait_queue_head_t wait;
 	int iov_limit;
 	int weight;
-	int byte_weight;
+	size_t byte_weight;
 };
 

This just costs extra memory, and value is never large, so I don't think this matters.

Quoted 12 lines
drivers/vhost/vhost.h +3 -2
-bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int 
 total_len);
+bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts,
+			  size_t total_len);
 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
-		    int nvqs, int iov_limit, int weight, int byte_weight);
+		    int nvqs, int iov_limit, int weight, size_t byte_weight);
 long vhost_dev_set_owner(struct vhost_dev *dev);  bool 

vhost_dev_has_owner(struct vhost_dev *dev); long
vhost_dev_check_owner(struct vhost_dev *);
--
1.8.5.6

#4 lore raw
Quoted 12 lines · wangxu (AE)

Hi Michael

	Thanks for your fast reply.

	As the following code, the 2nd branch of iov_iter_advance() does not check if i->count < size, when this happens, i->count -= size may cause len exceed INT_MAX, and then total_len exceed INT_MAX.

	handle_tx_copy() ->
		get_tx_bufs(..., &len, ...) ->
			init_iov_iter() ->
				iov_iter_advance(iter, ...) 	// has 3 branches:
					pipe_advance() 	 	// has checked the size: if (unlikely(i->count < size)) size = i->count;
					iov_iter_is_discard() ... 	// no check.

Yes, but I don't think we use ITER_DISCARD.

Thanks

Quoted 64 lines
					iterate_and_advance() 	//has checked: if (unlikely(i->count < n)) n = i->count;
				return iov_iter_count(iter);

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Monday, September 23, 2019 4:07 PM
To: wangxu (AE) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] vhost: It's better to use size_t for the 3rd parameter of vhost_exceeds_weight()

Quoted 6 lines · wangxu

From: Wang Xu <[email protected]>

Caller of vhost_exceeds_weight(..., total_len) in drivers/vhost/net.c usually pass size_t total_len, which may be affected by rx/tx package.

Puts a bit more pressure on the register file ... why do we care? Is there some way that it can exceed INT_MAX?

Quoted 33 lines
---
  drivers/vhost/vhost.c | 4 ++--
  drivers/vhost/vhost.h | 7 ++++---
  2 files changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c index renamed from drivers/vhost/vhost.c +0 -0

36ca2cf..159223a 100644

drivers/vhost/vhost.c +2 -2
@@ -412,7 +412,7 @@ static void vhost_dev_free_iovecs(struct vhost_dev
 *dev)  }
 
  bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
-			  int pkts, int total_len)
+			  int pkts, size_t total_len)
  {
  	struct vhost_dev *dev = vq->dev;
 
@@ -454,7 +454,7 @@ static size_t vhost_get_desc_size(struct
 vhost_virtqueue *vq,
 
  void vhost_dev_init(struct vhost_dev *dev,
  		    struct vhost_virtqueue **vqs, int nvqs,
-		    int iov_limit, int weight, int byte_weight)
+		    int iov_limit, int weight, size_t byte_weight)
  {
  	struct vhost_virtqueue *vq;

int i;

drivers/vhost/vhost.h index renamed from drivers/vhost/vhost.h +0 -0

e9ed272..8d80389d 100644

drivers/vhost/vhost.h struct vhost_dev { · line 177

Quoted 7 lines
drivers/vhost/vhost.h +1 -1
@@ -172,12 +172,13 @@ struct vhost_dev {
  	wait_queue_head_t wait;
  	int iov_limit;
  	int weight;
-	int byte_weight;
+	size_t byte_weight;
  };
 

This just costs extra memory, and value is never large, so I don't think this matters.

Quoted 12 lines
Quoted 8 lines
drivers/vhost/vhost.h +3 -2
-bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int
 total_len);
+bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts,
+			  size_t total_len);
  void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
-		    int nvqs, int iov_limit, int weight, int byte_weight);
+		    int nvqs, int iov_limit, int weight, size_t byte_weight);
  long vhost_dev_set_owner(struct vhost_dev *dev);  bool
Quoted 4 lines

vhost_dev_has_owner(struct vhost_dev *dev); long
vhost_dev_check_owner(struct vhost_dev *);
--
1.8.5.6

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw
In reply to wangxu (AE)
Quoted 12 lines · wangxu (AE)

Hi Michael

	Thanks for your fast reply.

	As the following code, the 2nd branch of iov_iter_advance() does not check if i->count < size, when this happens, i->count -= size may cause len exceed INT_MAX, and then total_len exceed INT_MAX.

	handle_tx_copy() ->
		get_tx_bufs(..., &len, ...) ->
			init_iov_iter() ->
				iov_iter_advance(iter, ...) 	// has 3 branches:
					pipe_advance() 	 	// has checked the size: if (unlikely(i->count < size)) size = i->count;
					iov_iter_is_discard() ... 	// no check.

Yes, but I don't think we use ITER_DISCARD.

Thanks

Quoted 76 lines
					iterate_and_advance() 	//has checked: if (unlikely(i->count < n)) n = i->count;
				return iov_iter_count(iter);

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Monday, September 23, 2019 4:07 PM
To: wangxu (AE) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] vhost: It's better to use size_t for the 3rd parameter of vhost_exceeds_weight()

Quoted 6 lines · wangxu

From: Wang Xu <[email protected]>

Caller of vhost_exceeds_weight(..., total_len) in drivers/vhost/net.c usually pass size_t total_len, which may be affected by rx/tx package.

Puts a bit more pressure on the register file ... why do we care? Is there some way that it can exceed INT_MAX?

Quoted 33 lines
---
  drivers/vhost/vhost.c | 4 ++--
  drivers/vhost/vhost.h | 7 ++++---
  2 files changed, 6 insertions(+), 5 deletions(-)
drivers/vhost/vhost.c index renamed from drivers/vhost/vhost.c +0 -0

36ca2cf..159223a 100644

drivers/vhost/vhost.c +2 -2
@@ -412,7 +412,7 @@ static void vhost_dev_free_iovecs(struct vhost_dev
 *dev)  }
 
  bool vhost_exceeds_weight(struct vhost_virtqueue *vq,
-			  int pkts, int total_len)
+			  int pkts, size_t total_len)
  {
  	struct vhost_dev *dev = vq->dev;
 
@@ -454,7 +454,7 @@ static size_t vhost_get_desc_size(struct
 vhost_virtqueue *vq,
 
  void vhost_dev_init(struct vhost_dev *dev,
  		    struct vhost_virtqueue **vqs, int nvqs,
-		    int iov_limit, int weight, int byte_weight)
+		    int iov_limit, int weight, size_t byte_weight)
  {
  	struct vhost_virtqueue *vq;

int i;

drivers/vhost/vhost.h index renamed from drivers/vhost/vhost.h +0 -0

e9ed272..8d80389d 100644

drivers/vhost/vhost.h struct vhost_dev { · line 177

Quoted 7 lines
drivers/vhost/vhost.h +1 -1
@@ -172,12 +172,13 @@ struct vhost_dev {
  	wait_queue_head_t wait;
  	int iov_limit;
  	int weight;
-	int byte_weight;
+	size_t byte_weight;
  };
 

This just costs extra memory, and value is never large, so I don't think this matters.

Quoted 12 lines
drivers/vhost/vhost.h +3 -2
-bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int
 total_len);
+bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts,
+			  size_t total_len);
  void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
-		    int nvqs, int iov_limit, int weight, int byte_weight);
+		    int nvqs, int iov_limit, int weight, size_t byte_weight);
  long vhost_dev_set_owner(struct vhost_dev *dev);  bool

vhost_dev_has_owner(struct vhost_dev *dev); long
vhost_dev_check_owner(struct vhost_dev *);
--
1.8.5.6