scatterlist: make sgl_free null pointer safe
Subsystems (3)
Sagi Grimberg
maintainerPATCH 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
@@ -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
Sagi Grimberg
maintainerPATCH 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
@@ -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
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_freeis null safe, drop the superflous check.Signed-off-by Sagi Grimberg <[email protected]>--- drivers/nvme/target/tcp.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)drivers/nvme/target/tcp.c +3
-6@@ -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
Re: [PATCH 2/2] nvmet-tcp: remove superflous check on request sgl
In reply to Sagi GrimbergQuoted 3 lines · Sagi Grimberg
Now that
sgl_freeis null safe, drop the superflous check.Signed-off-by Sagi Grimberg <[email protected]>
Looks good,
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
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
@@ -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.
Signed-off-by Sagi Grimberg <[email protected]>--- lib/scatterlist.c | 3 +++ 1 file changed, 3 insertions(+)lib/scatterlist.c +0
-0
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
Sagi Grimberg
maintainerQuoted 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