Skip to contentSkip to conversation

block: rnbd: rnbd-srv: silence uninitialized variable warning

PATCH In review Brooke Basile 4 participants 7 messages Aug 18, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: rnbd: rnbd-srv: silence uninitialized variable warning

Clang warns:

	drivers/block/rnbd/rnbd-srv.c:150:6: warning: variable 'err' is used
	uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
        	if (IS_ERR(bio)) {
            	^~~~~~~~~~~
	drivers/block/rnbd/rnbd-srv.c:177:9: note: uninitialized use occurs here
		return err;
		^~~
	drivers/block/rnbd/rnbd-srv.c:126:9: note: initialize the variable 'err'
	to silence this warning
        	int err;
               	^
                	= 0

Silence this by replacing err with ret, returning ret = 0 upon success.

---
 drivers/block/rnbd/rnbd-srv.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/block/rnbd/rnbd-srv.c +4 -4
index 0fb94843a495..f515d1a048a9 100644
@@ -123,10 +123,10 @@ static int process_rdma(struct rtrs_srv *sess,
 	struct rnbd_io_private *priv;
 	struct rnbd_srv_sess_dev *sess_dev;
 	u32 dev_id;
-	int err;
 	struct rnbd_dev_blk_io *io;
 	struct bio *bio;
 	short prio;
+	int ret = 0;
 
 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
 	if (!priv)
@@ -138,7 +138,7 @@ static int process_rdma(struct rtrs_srv *sess,
 	if (IS_ERR(sess_dev)) {
 		pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n",
 				   srv_sess->sessname, dev_id);
-		err = -ENOTCONN;
+		ret = -ENOTCONN;
 		goto err;
 	}
 
@@ -168,13 +168,13 @@ static int process_rdma(struct rtrs_srv *sess,
 
 	submit_bio(bio);
 
-	return 0;
+	return ret;
 
 sess_dev_put:
 	rnbd_put_sess_dev(sess_dev);
 err:
 	kfree(priv);
-	return err;
+	return ret;
 }
 
 static void destroy_device(struct rnbd_srv_dev *dev)
Signature · 1 line
2.28.0
#1 lore raw
Quoted 66 lines · Brooke Basile

Clang warns:

	drivers/block/rnbd/rnbd-srv.c:150:6: warning: variable 'err' is used
	uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
        	if (IS_ERR(bio)) {
            	^~~~~~~~~~~
	drivers/block/rnbd/rnbd-srv.c:177:9: note: uninitialized use occurs here
		return err;
		^~~
	drivers/block/rnbd/rnbd-srv.c:126:9: note: initialize the variable 'err'
	to silence this warning
        	int err;
               	^
                	= 0

Silence this by replacing err with ret, returning ret = 0 upon success.

---
 drivers/block/rnbd/rnbd-srv.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/block/rnbd/rnbd-srv.c +4 -4
index 0fb94843a495..f515d1a048a9 100644
@@ -123,10 +123,10 @@ static int process_rdma(struct rtrs_srv *sess,
 	struct rnbd_io_private *priv;
 	struct rnbd_srv_sess_dev *sess_dev;
 	u32 dev_id;
-	int err;
 	struct rnbd_dev_blk_io *io;
 	struct bio *bio;
 	short prio;
+	int ret = 0;
 
 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
 	if (!priv)
@@ -138,7 +138,7 @@ static int process_rdma(struct rtrs_srv *sess,
 	if (IS_ERR(sess_dev)) {
 		pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n",
 				   srv_sess->sessname, dev_id);
-		err = -ENOTCONN;
+		ret = -ENOTCONN;
 		goto err;
 	}
 
@@ -168,13 +168,13 @@ static int process_rdma(struct rtrs_srv *sess,
 
 	submit_bio(bio);
 
-	return 0;
+	return ret;
 
 sess_dev_put:
 	rnbd_put_sess_dev(sess_dev);
 err:
 	kfree(priv);
-	return err;
+	return ret;
 }
 
 static void destroy_device(struct rnbd_srv_dev *dev)
Signature · 1 line
2.28.0

I don't think this is a proper fix since the root cause of the warning appears to be that we are ignoring the return value of rnbd_bio_map_kern. Should we not set err to that value like this (completely untested)?

Cheers,
Nathan

drivers/block/rnbd/rnbd-srv.c +2 -1
index 0fb94843a495..1b71cb2a885d 100644
@@ -148,7 +148,8 @@ static int process_rdma(struct rtrs_srv *sess,
 	/* Generate bio with pages pointing to the rdma buffer */
 	bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
 	if (IS_ERR(bio)) {
-		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", PTR_ERR(bio));
+		err = PTR_ERR(bio);
+		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", err);
 		goto sess_dev_put;
 	}
 
#2 lore raw
Quoted 74 lines · Nathan Chancellor
Quoted 64 lines · Brooke Basile

Clang warns:

      drivers/block/rnbd/rnbd-srv.c:150:6: warning: variable 'err' is used
      uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
              if (IS_ERR(bio)) {
              ^~~~~~~~~~~
      drivers/block/rnbd/rnbd-srv.c:177:9: note: uninitialized use occurs here
              return err;
              ^~~
      drivers/block/rnbd/rnbd-srv.c:126:9: note: initialize the variable 'err'
      to silence this warning
              int err;
                      ^
                      = 0

Silence this by replacing err with ret, returning ret = 0 upon success.

---
 drivers/block/rnbd/rnbd-srv.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/block/rnbd/rnbd-srv.c +4 -5
index 0fb94843a495..f515d1a048a9 100644
@@ -123,10 +123,10 @@ static int process_rdma(struct rtrs_srv *sess,
      struct rnbd_io_private *priv;
      struct rnbd_srv_sess_dev *sess_dev;
      u32 dev_id;
-     int err;
      struct rnbd_dev_blk_io *io;
      struct bio *bio;
      short prio;
+     int ret = 0;
 
      priv = kmalloc(sizeof(*priv), GFP_KERNEL);
      if (!priv)
@@ -138,7 +138,7 @@ static int process_rdma(struct rtrs_srv *sess,
      if (IS_ERR(sess_dev)) {
              pr_err_ratelimited("Got I/O request on session %s for unknown device id %d\n",
                                 srv_sess->sessname, dev_id);
-             err = -ENOTCONN;
+             ret = -ENOTCONN;
              goto err;
      }
 
@@ -168,13 +168,13 @@ static int process_rdma(struct rtrs_srv *sess,
 
      submit_bio(bio);
 
-     return 0;
+     return ret;
 
 sess_dev_put:
      rnbd_put_sess_dev(sess_dev);
 err:
      kfree(priv);
-     return err;
+     return ret;
 }
 
 static void destroy_device(struct rnbd_srv_dev *dev)
--
Quoted 2 lines

2.28.0

I don't think this is a proper fix since the root cause of the warning appears to be that we are ignoring the return value of rnbd_bio_map_kern. Should we not set err to that value like this (completely untested)?

Quoted 2 lines

Cheers,
Nathan

Thanks Nathan, thanks Brooke,

I agree with Nathan, the problem is we forgot to set the err before "goto sess_dev_put".
Nathan's patch is simpler, less code of change.

Quoted 6 lines
drivers/block/rnbd/rnbd-srv.c +0 -0
index 0fb94843a495..1b71cb2a885d 100644

drivers/block/rnbd/rnbd-srv.c static int process_rdma(struct rtrs_srv *sess, · line 155

Quoted 9 lines
drivers/block/rnbd/rnbd-srv.c +2 -1
@@ -148,7 +148,8 @@ static int process_rdma(struct rtrs_srv *sess,
        /* Generate bio with pages pointing to the rdma buffer */
        bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
        if (IS_ERR(bio)) {
-               rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", PTR_ERR(bio));
+               err = PTR_ERR(bio);
+               rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", err);
                goto sess_dev_put;
        }
 

Nathan, could you send a formal patch?

Thanks

#3 lore raw

PATCH 1/1 block/rnbd: Ensure err is always initialized in process_rdma

Clang warns:

drivers/block/rnbd/rnbd-srv.c:150:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]

        if (IS_ERR(bio)) {
            ^~~~~~~~~~~
drivers/block/rnbd/rnbd-srv.c:177:9: note: uninitialized use occurs here
        return err;
               ^~~
drivers/block/rnbd/rnbd-srv.c:150:2: note: remove the 'if' if its
condition is always false
        if (IS_ERR(bio)) {
        ^~~~~~~~~~~~~~~~~~
drivers/block/rnbd/rnbd-srv.c:126:9: note: initialize the variable 'err'
to silence this warning
        int err;
               ^
                = 0

1 warning generated.

err is indeed uninitialized when this statement is taken. Ensure that it is assigned the error value of bio before jumping to the error handling label.

Fixes 735d77d4fd28 ("rnbd: remove rnbd_dev_submit_io")
---
 drivers/block/rnbd/rnbd-srv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/rnbd/rnbd-srv.c +2 -1
index 0fb94843a495..1b71cb2a885d 100644
@@ -148,7 +148,8 @@ static int process_rdma(struct rtrs_srv *sess,
 	/* Generate bio with pages pointing to the rdma buffer */
 	bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
 	if (IS_ERR(bio)) {
-		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", PTR_ERR(bio));
+		err = PTR_ERR(bio);
+		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", err);
 		goto sess_dev_put;
 	}
 
Signature · 1 line
2.28.0
Acked-by on v1 1/1
#4 lore raw

Re: [PATCH] block/rnbd: Ensure err is always initialized in process_rdma

Quoted 22 lines · Nathan Chancellor

Clang warns:

drivers/block/rnbd/rnbd-srv.c:150:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]

        if (IS_ERR(bio)) {
            ^~~~~~~~~~~
drivers/block/rnbd/rnbd-srv.c:177:9: note: uninitialized use occurs here
        return err;
               ^~~
drivers/block/rnbd/rnbd-srv.c:150:2: note: remove the 'if' if its
condition is always false
        if (IS_ERR(bio)) {
        ^~~~~~~~~~~~~~~~~~
drivers/block/rnbd/rnbd-srv.c:126:9: note: initialize the variable 'err'
to silence this warning
        int err;
               ^
                = 0

1 warning generated.

Quoted 8 lines

err is indeed uninitialized when this statement is taken. Ensure that it is assigned the error value of bio before jumping to the error handling label.

Fixes 735d77d4fd28 ("rnbd: remove rnbd_dev_submit_io")

Thanks!

Quoted 21 lines
---
 drivers/block/rnbd/rnbd-srv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/rnbd/rnbd-srv.c +2 -2
index 0fb94843a495..1b71cb2a885d 100644
@@ -148,7 +148,8 @@ static int process_rdma(struct rtrs_srv *sess,
        /* Generate bio with pages pointing to the rdma buffer */
        bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
        if (IS_ERR(bio)) {
-               rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", PTR_ERR(bio));
+               err = PTR_ERR(bio);
+               rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", err);
                goto sess_dev_put;
        }
 
--

2.28.0

maintainer Applied
#5 lore raw

Re: [PATCH] block/rnbd: Ensure err is always initialized in process_rdma

In reply to Nathan Chancellor
Quoted 19 lines · Nathan Chancellor

Clang warns:

drivers/block/rnbd/rnbd-srv.c:150:6: warning: variable 'err' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]

        if (IS_ERR(bio)) {
            ^~~~~~~~~~~
drivers/block/rnbd/rnbd-srv.c:177:9: note: uninitialized use occurs here
        return err;
               ^~~
drivers/block/rnbd/rnbd-srv.c:150:2: note: remove the 'if' if its
condition is always false
        if (IS_ERR(bio)) {
        ^~~~~~~~~~~~~~~~~~
drivers/block/rnbd/rnbd-srv.c:126:9: note: initialize the variable 'err'
to silence this warning
        int err;
               ^
                = 0
Quoted 5 lines

1 warning generated.

err is indeed uninitialized when this statement is taken. Ensure that it is assigned the error value of bio before jumping to the error handling label.

Applied, thanks.

Signature · 1 line
Jens Axboe
#6 lore raw
In reply to Nathan Chancellor
Quoted 14 lines · Nathan Chancellor

I don't think this is a proper fix since the root cause of the warning appears to be that we are ignoring the return value of rnbd_bio_map_kern. Should we not set err to that value like this (completely untested)?

Cheers,
Nathan

drivers/block/rnbd/rnbd-srv.c +0 -0
index 0fb94843a495..1b71cb2a885d 100644

drivers/block/rnbd/rnbd-srv.c static int process_rdma(struct rtrs_srv *sess, · line 155

Quoted 9 lines
drivers/block/rnbd/rnbd-srv.c +2 -1
@@ -148,7 +148,8 @@ static int process_rdma(struct rtrs_srv *sess,
  	/* Generate bio with pages pointing to the rdma buffer */
  	bio = rnbd_bio_map_kern(data, sess_dev->rnbd_dev->ibd_bio_set, datalen, GFP_KERNEL);
  	if (IS_ERR(bio)) {
-		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", PTR_ERR(bio));
+		err = PTR_ERR(bio);
+		rnbd_srv_err(sess_dev, "Failed to generate bio, err: %ld\n", err);
  		goto sess_dev_put;
  	}
 

Ah, I see what you mean. Thanks for the fix!

Best,
Brooke Basile