Skip to contentSkip to conversation

scatterlist: make sgl_free null pointer safe

PATCH In review Sagi Grimberg 3 participants 6 messages Sep 20, 2019 – Sep 27, 2019

Subsystems (3)
lore
Share & tools
lore

Sagi Grimberg

author maintainer
#0 lore raw

PATCH 1/2 scatterlist: make sgl_free null pointer safe

Match other alloc/free routines by checking the sgl pointer first.

---
 lib/scatterlist.c | 3 +++
 1 file changed, 3 insertions(+)
lib/scatterlist.c +3 -0
index c2cf2c311b7d..fea25b035e67 100644
@@ -563,6 +563,9 @@ void sgl_free_n_order(struct scatterlist *sgl, int nents, int order)
 	struct page *page;
 	int i;
 
+	if (!sgl)
+		return;
+
 	for_each_sg(sgl, sg, nents, i) {
 		if (!sg)
 			break;
Signature · 7 lines
2.17.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
author maintainer
#1 lore raw

PATCH 2/2 nvmet-tcp: remove superflous check on request sgl

Now that sgl_free is null safe, drop the superflous check.

---
 drivers/nvme/target/tcp.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/nvme/target/tcp.c +4 -8
index bf4f03474e89..d535080b781f 100644
@@ -348,8 +348,7 @@ static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
 
 	return 0;
 err:
-	if (cmd->req.sg_cnt)
-		sgl_free(cmd->req.sg);
+	sgl_free(cmd->req.sg);
 	return NVME_SC_INTERNAL;
 }
 
@@ -554,8 +553,7 @@ static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd)
 
 	if (queue->nvme_sq.sqhd_disabled) {
 		kfree(cmd->iov);
-		if (cmd->req.sg_cnt)
-			sgl_free(cmd->req.sg);
+		sgl_free(cmd->req.sg);
 	}
 
 	return 1;
@@ -586,8 +584,7 @@ static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
 		return -EAGAIN;
 
 	kfree(cmd->iov);
-	if (cmd->req.sg_cnt)
-		sgl_free(cmd->req.sg);
+	sgl_free(cmd->req.sg);
 	cmd->queue->snd_cmd = NULL;
 	nvmet_tcp_put_cmd(cmd);
 	return 1;
@@ -1310,8 +1307,7 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
 	nvmet_req_uninit(&cmd->req);
 	nvmet_tcp_unmap_pdu_iovec(cmd);
 	kfree(cmd->iov);
-	if (cmd->req.sg_cnt)
-		sgl_free(cmd->req.sg);
+	sgl_free(cmd->req.sg);
 }
 
 static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
Signature · 7 lines
2.17.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#2 lore raw

Re: [PATCH 2/2] nvmet-tcp: remove superflous check on request sgl

Please have a look at the comment at patch #1.

If you look at the nvmet_req_free_sgl() we don't have such check since I think code assumes that sgl_free() is null safe.

If that is correct we don't need additional checks in the code anyway

Please correct me if I'm wrong.

Quoted 43 lines · Sagi Grimberg

Now that sgl_free is null safe, drop the superflous check.

---
  drivers/nvme/target/tcp.c | 12 ++++--------
  1 file changed, 4 insertions(+), 8 deletions(-)
drivers/nvme/target/tcp.c +3 -6
index bf4f03474e89..d535080b781f 100644
@@ -348,8 +348,7 @@ static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
 
  	return 0;
  err:
-	if (cmd->req.sg_cnt)
-		sgl_free(cmd->req.sg);
+	sgl_free(cmd->req.sg);
  	return NVME_SC_INTERNAL;
  }
 
@@ -554,8 +553,7 @@ static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd)
 
  	if (queue->nvme_sq.sqhd_disabled) {
  		kfree(cmd->iov);
-		if (cmd->req.sg_cnt)
-			sgl_free(cmd->req.sg);
+		sgl_free(cmd->req.sg);
  	}
 
  	return 1;
@@ -586,8 +584,7 @@ static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
  		return -EAGAIN;
 
  	kfree(cmd->iov);
-	if (cmd->req.sg_cnt)
-		sgl_free(cmd->req.sg);
+	sgl_free(cmd->req.sg);
  	cmd->queue->snd_cmd = NULL;
  	nvmet_tcp_put_cmd(cmd);
  	return 1;

drivers/nvme/target/tcp.c static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd) · line 1313

Quoted 9 lines
drivers/nvme/target/tcp.c +1 -2
@@ -1310,8 +1307,7 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
  	nvmet_req_uninit(&cmd->req);
  	nvmet_tcp_unmap_pdu_iovec(cmd);
  	kfree(cmd->iov);
-	if (cmd->req.sg_cnt)
-		sgl_free(cmd->req.sg);
+	sgl_free(cmd->req.sg);
  }
 
  static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] nvmet-tcp: remove superflous check on request sgl

In reply to Sagi Grimberg
Quoted 3 lines · Sagi Grimberg

Now that sgl_free is null safe, drop the superflous check.

Looks good,

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#4 lore raw
In reply to Sagi Grimberg

Unless I totally missed what you are trying to fix here, I think this case is already handled implicitly to avoid the code repetition inside for_each_sg() which makes :-

sgl_free()

	sgl_free_order()
		sgl_free_n_order()

code path NULL safe.

I did a quick test without applying your patch with some prints and it worked file following module:-

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)

{
	sgl_free(NULL);
	return 0;
}

void cleanup_module(void)
{

}

MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");

Prints :-

lib/scatterlist.c +0 -0
index c2cf2c3..56fa586 100644
@@ -563,13 +563,20 @@ void sgl_free_n_order(struct scatterlist *sgl, int 

nents, int order)

         struct page *page;
         int i;
+       pr_info("%s %d\n", __func__, __LINE__);
         for_each_sg(sgl, sg, nents, i) {
+               pr_info("%s %d\n", __func__, __LINE__);
                 if (!sg)
                         break;
+
+               pr_info("%s %d\n", __func__, __LINE__);
                 page = sg_page(sg);
                 if (page)
                         __free_pages(page, order);
+               pr_info("%s %d\n", __func__, __LINE__);
         }
+
+       pr_info("%s %d\n", __func__, __LINE__);
         kfree(sgl);

}
EXPORT_SYMBOL(sgl_free_n_order);
@@ -581,6 +588,7 @@ void sgl_free_n_order(struct scatterlist *sgl, int nents, int order)
*/
void sgl_free_order(struct scatterlist *sgl, int order)
{

+       pr_info("%s %d\n", __func__, __LINE__);
         sgl_free_n_order(sgl, INT_MAX, order);

}
EXPORT_SYMBOL(sgl_free_order);
@@ -591,6 +599,7 @@ void sgl_free_order(struct scatterlist *sgl, int order)
*/
void sgl_free(struct scatterlist *sgl)
{

+       pr_info("%s %d\n", __func__, __LINE__);
         sgl_free_order(sgl, 0);

}
EXPORT_SYMBOL(sgl_free);

Got the following output without any crash or warning :-

[  156.950073] sgl_free 602
[  156.950076] sgl_free_order 591
[  156.950078] sgl_free_n_order 566
[  156.950079] sgl_free_n_order 568
[  156.950081] sgl_free_n_order 579

Did I miss anything ?

Quoted 13 lines · Sagi Grimberg

Match other alloc/free routines by checking the sgl pointer first.

---
  lib/scatterlist.c | 3 +++
  1 file changed, 3 insertions(+)
lib/scatterlist.c +0 -0
index c2cf2c311b7d..fea25b035e67 100644

lib/scatterlist.c void sgl_free_n_order(struct scatterlist *sgl, int nents, int order) · line 571

Quoted 9 lines
lib/scatterlist.c +3 -0
@@ -563,6 +563,9 @@ void sgl_free_n_order(struct scatterlist *sgl, int nents, int order)
  	struct page *page;
  	int i;
 
+	if (!sgl)
+		return;
+
  	for_each_sg(sgl, sg, nents, i) {
  		if (!sg)
  			break;

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

author maintainer
#5 lore raw
Quoted 4 lines

Unless I totally missed what you are trying to fix here, I think this case is already handled implicitly to avoid the code repetition inside for_each_sg() which makes :-

You're right, the condition in the loop makes it null safe already.

So we can safely disregard the first patch.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme