2023-12-24 19:56:40

by Markus Elfring

[permalink] [raw]
Subject: [PATCH] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

From: Markus Elfring <[email protected]>
Date: Sun, 24 Dec 2023 20:42:19 +0100

The kfree() function was called in up to two cases by the
__virtio_crypto_akcipher_do_req() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

* Adjust jump targets.

* Delete two initialisations which became unnecessary
with this refactoring.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
index 2621ff8a9376..15dbe67f0d31 100644
--- a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
+++ b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
@@ -224,11 +224,11 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
struct virtio_crypto *vcrypto = ctx->vcrypto;
struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
- void *src_buf = NULL, *dst_buf = NULL;
+ void *src_buf, *dst_buf = NULL;
unsigned int num_out = 0, num_in = 0;
int node = dev_to_node(&vcrypto->vdev->dev);
unsigned long flags;
- int ret = -ENOMEM;
+ int ret;
bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;

@@ -239,7 +239,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
/* src data */
src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
if (!src_buf)
- goto err;
+ return -ENOMEM;

if (verify) {
/* for verify operation, both src and dst data work as OUT direction */
@@ -254,7 +254,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
/* dst data */
dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
if (!dst_buf)
- goto err;
+ goto free_src;

sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
sgs[num_out + num_in++] = &dstdata_sg;
@@ -277,9 +277,9 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
return 0;

err:
- kfree(src_buf);
kfree(dst_buf);
-
+free_src;
+ kfree(src_buf);
return -ENOMEM;
}

--
2.43.0



2023-12-26 00:54:37

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

Hi Markus,

kernel test robot noticed the following build errors:

[auto build test ERROR on herbert-cryptodev-2.6/master]
[also build test ERROR on herbert-crypto-2.6/master linus/master v6.7-rc7 next-20231222]
[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#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Markus-Elfring/crypto-virtio-Less-function-calls-in-__virtio_crypto_akcipher_do_req-after-error-detection/20231225-154431
base: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
patch link: https://lore.kernel.org/r/2413f22f-f0c3-45e0-9f6b-a551bdf0f54c%40web.de
patch subject: [PATCH] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection
config: arm-randconfig-001-20231225 (https://download.01.org/0day-ci/archive/20231226/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231226/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

>> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c:281:1: error: use of undeclared identifier 'free_src'; did you mean 'free_irq'?
free_src;
^~~~~~~~
free_irq
include/linux/interrupt.h:196:20: note: 'free_irq' declared here
extern const void *free_irq(unsigned int, void *);
^
>> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c:257:9: error: use of undeclared label 'free_src'
goto free_src;
^
>> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c:281:1: warning: expression result unused [-Wunused-value]
free_src;
^~~~~~~~
1 warning and 2 errors generated.


vim +281 drivers/crypto/virtio/virtio_crypto_akcipher_algs.c

218
219 static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request *vc_akcipher_req,
220 struct akcipher_request *req, struct data_queue *data_vq)
221 {
222 struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
223 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
224 struct virtio_crypto *vcrypto = ctx->vcrypto;
225 struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
226 struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
227 void *src_buf, *dst_buf = NULL;
228 unsigned int num_out = 0, num_in = 0;
229 int node = dev_to_node(&vcrypto->vdev->dev);
230 unsigned long flags;
231 int ret;
232 bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
233 unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
234
235 /* out header */
236 sg_init_one(&outhdr_sg, req_data, sizeof(*req_data));
237 sgs[num_out++] = &outhdr_sg;
238
239 /* src data */
240 src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
241 if (!src_buf)
242 return -ENOMEM;
243
244 if (verify) {
245 /* for verify operation, both src and dst data work as OUT direction */
246 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
247 sg_init_one(&srcdata_sg, src_buf, src_len);
248 sgs[num_out++] = &srcdata_sg;
249 } else {
250 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
251 sg_init_one(&srcdata_sg, src_buf, src_len);
252 sgs[num_out++] = &srcdata_sg;
253
254 /* dst data */
255 dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
256 if (!dst_buf)
> 257 goto free_src;
258
259 sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
260 sgs[num_out + num_in++] = &dstdata_sg;
261 }
262
263 vc_akcipher_req->src_buf = src_buf;
264 vc_akcipher_req->dst_buf = dst_buf;
265
266 /* in header */
267 sg_init_one(&inhdr_sg, &vc_req->status, sizeof(vc_req->status));
268 sgs[num_out + num_in++] = &inhdr_sg;
269
270 spin_lock_irqsave(&data_vq->lock, flags);
271 ret = virtqueue_add_sgs(data_vq->vq, sgs, num_out, num_in, vc_req, GFP_ATOMIC);
272 virtqueue_kick(data_vq->vq);
273 spin_unlock_irqrestore(&data_vq->lock, flags);
274 if (ret)
275 goto err;
276
277 return 0;
278
279 err:
280 kfree(dst_buf);
> 281 free_src;
282 kfree(src_buf);
283 return -ENOMEM;
284 }
285

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

2023-12-26 02:58:10

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

Hi Markus,

kernel test robot noticed the following build errors:

[auto build test ERROR on herbert-cryptodev-2.6/master]
[also build test ERROR on herbert-crypto-2.6/master linus/master v6.7-rc7 next-20231222]
[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#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Markus-Elfring/crypto-virtio-Less-function-calls-in-__virtio_crypto_akcipher_do_req-after-error-detection/20231225-154431
base: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
patch link: https://lore.kernel.org/r/2413f22f-f0c3-45e0-9f6b-a551bdf0f54c%40web.de
patch subject: [PATCH] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection
config: x86_64-rhel-8.3-bpf (https://download.01.org/0day-ci/archive/20231226/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231226/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/crypto/virtio/virtio_crypto_akcipher_algs.c: In function '__virtio_crypto_akcipher_do_req':
>> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c:281:1: error: 'free_src' undeclared (first use in this function)
281 | free_src;
| ^~~~~~~~
drivers/crypto/virtio/virtio_crypto_akcipher_algs.c:281:1: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c:257:25: error: label 'free_src' used but not defined
257 | goto free_src;
| ^~~~


vim +/free_src +281 drivers/crypto/virtio/virtio_crypto_akcipher_algs.c

218
219 static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request *vc_akcipher_req,
220 struct akcipher_request *req, struct data_queue *data_vq)
221 {
222 struct virtio_crypto_akcipher_ctx *ctx = vc_akcipher_req->akcipher_ctx;
223 struct virtio_crypto_request *vc_req = &vc_akcipher_req->base;
224 struct virtio_crypto *vcrypto = ctx->vcrypto;
225 struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
226 struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
227 void *src_buf, *dst_buf = NULL;
228 unsigned int num_out = 0, num_in = 0;
229 int node = dev_to_node(&vcrypto->vdev->dev);
230 unsigned long flags;
231 int ret;
232 bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
233 unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
234
235 /* out header */
236 sg_init_one(&outhdr_sg, req_data, sizeof(*req_data));
237 sgs[num_out++] = &outhdr_sg;
238
239 /* src data */
240 src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
241 if (!src_buf)
242 return -ENOMEM;
243
244 if (verify) {
245 /* for verify operation, both src and dst data work as OUT direction */
246 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
247 sg_init_one(&srcdata_sg, src_buf, src_len);
248 sgs[num_out++] = &srcdata_sg;
249 } else {
250 sg_copy_to_buffer(req->src, sg_nents(req->src), src_buf, src_len);
251 sg_init_one(&srcdata_sg, src_buf, src_len);
252 sgs[num_out++] = &srcdata_sg;
253
254 /* dst data */
255 dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
256 if (!dst_buf)
> 257 goto free_src;
258
259 sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
260 sgs[num_out + num_in++] = &dstdata_sg;
261 }
262
263 vc_akcipher_req->src_buf = src_buf;
264 vc_akcipher_req->dst_buf = dst_buf;
265
266 /* in header */
267 sg_init_one(&inhdr_sg, &vc_req->status, sizeof(vc_req->status));
268 sgs[num_out + num_in++] = &inhdr_sg;
269
270 spin_lock_irqsave(&data_vq->lock, flags);
271 ret = virtqueue_add_sgs(data_vq->vq, sgs, num_out, num_in, vc_req, GFP_ATOMIC);
272 virtqueue_kick(data_vq->vq);
273 spin_unlock_irqrestore(&data_vq->lock, flags);
274 if (ret)
275 goto err;
276
277 return 0;
278
279 err:
280 kfree(dst_buf);
> 281 free_src;
282 kfree(src_buf);
283 return -ENOMEM;
284 }
285

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

2023-12-26 10:13:14

by Markus Elfring

[permalink] [raw]
Subject: [PATCH v2] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

From: Markus Elfring <[email protected]>
Date: Tue, 26 Dec 2023 11:00:20 +0100

The kfree() function was called in up to two cases by the
__virtio_crypto_akcipher_do_req() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

* Adjust jump targets.

* Delete two initialisations which became unnecessary
with this refactoring.

Signed-off-by: Markus Elfring <[email protected]>
---

v2:
A typo was fixed for the delimiter of a label.

drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
index 2621ff8a9376..057da5bd8d30 100644
--- a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
+++ b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
@@ -224,11 +224,11 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
struct virtio_crypto *vcrypto = ctx->vcrypto;
struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
- void *src_buf = NULL, *dst_buf = NULL;
+ void *src_buf, *dst_buf = NULL;
unsigned int num_out = 0, num_in = 0;
int node = dev_to_node(&vcrypto->vdev->dev);
unsigned long flags;
- int ret = -ENOMEM;
+ int ret;
bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;

@@ -239,7 +239,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
/* src data */
src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
if (!src_buf)
- goto err;
+ return -ENOMEM;

if (verify) {
/* for verify operation, both src and dst data work as OUT direction */
@@ -254,7 +254,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
/* dst data */
dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
if (!dst_buf)
- goto err;
+ goto free_src;

sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
sgs[num_out + num_in++] = &dstdata_sg;
@@ -277,9 +277,9 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
return 0;

err:
- kfree(src_buf);
kfree(dst_buf);
-
+free_src:
+ kfree(src_buf);
return -ENOMEM;
}

--
2.43.0


2023-12-26 12:22:34

by Gonglei (Arei)

[permalink] [raw]
Subject: RE: [PATCH v2] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection



> -----Original Message-----
> From: Markus Elfring [mailto:[email protected]]
> Sent: Tuesday, December 26, 2023 6:12 PM
> To: kernel test robot <[email protected]>; [email protected];
> [email protected]; [email protected]; David S. Miller
> <[email protected]>; Gonglei (Arei) <[email protected]>; Herbert
> Xu <[email protected]>; Jason Wang <[email protected]>;
> Michael S. Tsirkin <[email protected]>; Xuan Zhuo
> <[email protected]>
> Cc: [email protected]; [email protected]; [email protected];
> LKML <[email protected]>; [email protected]
> Subject: [PATCH v2] crypto: virtio - Less function calls in
> __virtio_crypto_akcipher_do_req() after error detection
>
> From: Markus Elfring <[email protected]>
> Date: Tue, 26 Dec 2023 11:00:20 +0100
>
> The kfree() function was called in up to two cases by the
> __virtio_crypto_akcipher_do_req() function during error handling even if the
> passed variable contained a null pointer.
> This issue was detected by using the Coccinelle software.
>
> * Adjust jump targets.
>
> * Delete two initialisations which became unnecessary
> with this refactoring.
>
> Signed-off-by: Markus Elfring <[email protected]>
> ---
>
> v2:
> A typo was fixed for the delimiter of a label.
>
> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>

Reviewed-by: Gonglei <[email protected]>


Regards,
-Gonglei

2024-01-09 23:42:58

by Justin Stitt

[permalink] [raw]
Subject: Re: [PATCH v2] crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

Hi,

On Tue, Dec 26, 2023 at 11:12:23AM +0100, Markus Elfring wrote:
> From: Markus Elfring <[email protected]>
> Date: Tue, 26 Dec 2023 11:00:20 +0100
>
> The kfree() function was called in up to two cases by the
> __virtio_crypto_akcipher_do_req() function during error handling
> even if the passed variable contained a null pointer.
> This issue was detected by using the Coccinelle software.

If the script is short and simple would you mind, in the future,
including it below the fold -- this may help others do similar work down
the line -- Or you could also link to a git-managed version like what
Kees has been doing with his __counted_by patches [1].

>
> * Adjust jump targets.
>
> * Delete two initialisations which became unnecessary
> with this refactoring.
>
> Signed-off-by: Markus Elfring <[email protected]>
> ---

Nonetheless,

Reviewed-by: Justin Stitt <[email protected]>
>
> v2:
> A typo was fixed for the delimiter of a label.
>
> drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
> index 2621ff8a9376..057da5bd8d30 100644
> --- a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
> +++ b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
> @@ -224,11 +224,11 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
> struct virtio_crypto *vcrypto = ctx->vcrypto;
> struct virtio_crypto_op_data_req *req_data = vc_req->req_data;
> struct scatterlist *sgs[4], outhdr_sg, inhdr_sg, srcdata_sg, dstdata_sg;
> - void *src_buf = NULL, *dst_buf = NULL;
> + void *src_buf, *dst_buf = NULL;
> unsigned int num_out = 0, num_in = 0;
> int node = dev_to_node(&vcrypto->vdev->dev);
> unsigned long flags;
> - int ret = -ENOMEM;
> + int ret;
> bool verify = vc_akcipher_req->opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY;
> unsigned int src_len = verify ? req->src_len + req->dst_len : req->src_len;
>
> @@ -239,7 +239,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
> /* src data */
> src_buf = kcalloc_node(src_len, 1, GFP_KERNEL, node);
> if (!src_buf)
> - goto err;
> + return -ENOMEM;
>
> if (verify) {
> /* for verify operation, both src and dst data work as OUT direction */
> @@ -254,7 +254,7 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
> /* dst data */
> dst_buf = kcalloc_node(req->dst_len, 1, GFP_KERNEL, node);
> if (!dst_buf)
> - goto err;
> + goto free_src;
>
> sg_init_one(&dstdata_sg, dst_buf, req->dst_len);
> sgs[num_out + num_in++] = &dstdata_sg;
> @@ -277,9 +277,9 @@ static int __virtio_crypto_akcipher_do_req(struct virtio_crypto_akcipher_request
> return 0;
>
> err:
> - kfree(src_buf);
> kfree(dst_buf);
> -
> +free_src:
> + kfree(src_buf);
> return -ENOMEM;
> }
>
> --
> 2.43.0
>

[1]: https://lore.kernel.org/all/[email protected]/

Thanks
Justin

2024-01-10 07:47:29

by Markus Elfring

[permalink] [raw]
Subject: Re: crypto: virtio - Less function calls in __virtio_crypto_akcipher_do_req() after error detection

>> The kfree() function was called in up to two cases by the
>> __virtio_crypto_akcipher_do_req() function during error handling
>> even if the passed variable contained a null pointer.
>> This issue was detected by using the Coccinelle software.
>
> If the script is short and simple would you mind, in the future,
> including it below the fold -- this may help others do similar work down
> the line -- Or …

Would you like to take another look at the clarification approach
“Reconsidering kfree() calls for null pointers (with SmPL)”?
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-03/msg00096.html

Regards,
Markus