Skip to contentSkip to conversation

qla4xxx: Return -ENOMEM on memory allocation failure

PATCH In review Himangi Saraogi 5 participants 6 messages Jul 4, 2014 – Jul 7, 2014

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

PATCH 1/1 qla4xxx: Return -ENOMEM on memory allocation failure

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the Coccinelle semantic match that finds this problem is as follows:

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
identifier alloc;
@@

ret = 0
... when != ret = e1
*x = alloc(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/scsi/qla4xxx/ql4_os.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/qla4xxx/ql4_os.c +1 -0
index c5d9564..72ba671 100644
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 45 lines

-----Original Message-----
From: [email protected] [mailto:linux-scsi- [email protected]] On Behalf Of Himangi Saraogi
Sent: Friday, 04 July, 2014 1:28 PM
To: Vikas Chaudhary; [email protected]; James E.J. Bottomley; linux- [email protected]; [email protected]
Cc: [email protected]
Subject: [PATCH] qla4xxx: Return -ENOMEM on memory allocation failure

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the Coccinelle semantic match that finds this problem is as follows:

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
identifier alloc;
@@

ret = 0
... when != ret = e1
*x = alloc(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/scsi/qla4xxx/ql4_os.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/qla4xxx/ql4_os.c +0 -0
index c5d9564..72ba671 100644

drivers/scsi/qla4xxx/ql4_os.c static int qla4xxx_get_host_stats(struct Scsi_Host · line 1056

Quoted 7 lines
drivers/scsi/qla4xxx/ql4_os.c +1 -0
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host
 *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}

Also, the only caller of this function doesn't use the return value - it's overwritten by another function call:

drivers/scsi/scsi_transport_iscsi.c:

                err = transport->get_host_stats(shost, buf, host_stats_size);

                actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
                skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
                nlhhost_stats->nlmsg_len = actual_size;

                err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
                                          GFP_KERNEL);

---
Rob Elliott HP Server Storage

#2 lore raw
Quoted 57 lines · Robert (Server Storage)
Quoted 45 lines

-----Original Message-----
From: [email protected] [mailto:linux-scsi- [email protected]] On Behalf Of Himangi Saraogi
Sent: Friday, 04 July, 2014 1:28 PM
To: Vikas Chaudhary; [email protected]; James E.J. Bottomley; linux- [email protected]; [email protected]
Cc: [email protected]
Subject: [PATCH] qla4xxx: Return -ENOMEM on memory allocation failure

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the Coccinelle semantic match that finds this problem is as follows:

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
identifier alloc;
@@

ret = 0
... when != ret = e1
*x = alloc(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/scsi/qla4xxx/ql4_os.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/qla4xxx/ql4_os.c +0 -0
index c5d9564..72ba671 100644

drivers/scsi/qla4xxx/ql4_os.c static int qla4xxx_get_host_stats(struct Scsi_Host · line 1056

Quoted 7 lines
drivers/scsi/qla4xxx/ql4_os.c +1 -0
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host
 *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}

Also, the only caller of this function doesn't use the return value - it's overwritten by another function call:

drivers/scsi/scsi_transport_iscsi.c:

Quoted 8 lines
                err = transport->get_host_stats(shost, buf, host_stats_size);

                actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
                skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
                nlhhost_stats->nlmsg_len = actual_size;

                err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
                                          GFP_KERNEL);

Maybe that is intentional? If get_host_stats fails, eg because of a lack of memory, the worst that will happen is that a region of the buffer will be 0. If the loop is done again, there seems to be a risk of an infinite loop.

julia

#3 lore raw
Quoted 67 lines · Julia Lawall
Quoted 63 lines · Robert (Server Storage)
Quoted 45 lines

-----Original Message-----
From: [email protected] [mailto:linux-scsi- [email protected]] On Behalf Of Himangi Saraogi
Sent: Friday, 04 July, 2014 1:28 PM
To: Vikas Chaudhary; [email protected]; James E.J. Bottomley; linux- [email protected]; [email protected]
Cc: [email protected]
Subject: [PATCH] qla4xxx: Return -ENOMEM on memory allocation failure

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the Coccinelle semantic match that finds this problem is as follows:

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
identifier alloc;
@@

ret = 0
... when != ret = e1
*x = alloc(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/scsi/qla4xxx/ql4_os.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/qla4xxx/ql4_os.c +0 -0
index c5d9564..72ba671 100644

drivers/scsi/qla4xxx/ql4_os.c static int qla4xxx_get_host_stats(struct Scsi_Host · line 1056

Quoted 7 lines
drivers/scsi/qla4xxx/ql4_os.c +1 -0
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host
 *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}

Also, the only caller of this function doesn't use the return value - it's overwritten by another function call:

drivers/scsi/scsi_transport_iscsi.c:

                err = transport->get_host_stats(shost, buf, host_stats_size);

                actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
                skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
                nlhhost_stats->nlmsg_len = actual_size;
Quoted 7 lines
Quoted 2 lines

err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
GFP_KERNEL);

Maybe that is intentional? If get_host_stats fails, eg because of a lack of memory, the worst that will happen is that a region of the buffer will be 0. If the loop is done again, there seems to be a risk of an infinite loop.

Or one could jump out of the function completely, as on failure of alloc_skb.

julia

#4 lore raw
In reply to Julia Lawall
Quoted 63 lines · Julia Lawall
Quoted 59 lines · Robert (Server Storage)
Quoted 45 lines

-----Original Message-----
From: [email protected] [mailto:linux-scsi- [email protected]] On Behalf Of Himangi Saraogi
Sent: Friday, 04 July, 2014 1:28 PM
To: Vikas Chaudhary; [email protected]; James E.J. Bottomley; linux- [email protected]; [email protected]
Cc: [email protected]
Subject: [PATCH] qla4xxx: Return -ENOMEM on memory allocation failure

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the Coccinelle semantic match that finds this problem is as follows:

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
identifier alloc;
@@

ret = 0
... when != ret = e1
*x = alloc(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/scsi/qla4xxx/ql4_os.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/qla4xxx/ql4_os.c +0 -0
index c5d9564..72ba671 100644

drivers/scsi/qla4xxx/ql4_os.c static int qla4xxx_get_host_stats(struct Scsi_Host · line 1056

Quoted 7 lines
drivers/scsi/qla4xxx/ql4_os.c +1 -0
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host
 *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}

Also, the only caller of this function doesn't use the return value - it's overwritten by another function call:

drivers/scsi/scsi_transport_iscsi.c:

                err = transport->get_host_stats(shost, buf, host_stats_size);
Quoted 8 lines
Quoted 6 lines
                actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
                skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
                nlhhost_stats->nlmsg_len = actual_size;

                err = iscsi_multicast_skb(skbhost_stats, ISCSI_NL_GRP_ISCSID,
                                          GFP_KERNEL);

Maybe that is intentional?

I think it was a mistake. There is also one more issue in qla4xxx_get_host_stats where it is returning an internal qlogic specific error value. This patch should fix all issues.

[PATCH] qla4xxx/iscsi: fix get_host_stats error propagation

This patch fixes 2 bugs.

1. qla4xxx was not always returning -EXYZ error codes when qla4xxx_get_host_stats failed.

2. iscsi_get_host_stats was dropping the error code returned by drivers like qla4xxx.

drivers/scsi/qla4xxx/ql4_os.c +2 -0
index 459b9f7..e4dc3e0 100644
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}
 
@@ -1058,6 +1059,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len)
 	if (ret != QLA_SUCCESS) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to retrieve iscsi stats\n");
+		ret = -EIO;
 		goto exit_host_stats;
 	}
 	host_stats->mactx_frames = le64_to_cpu(ql_iscsi_stats->mac_tx_frames);
drivers/scsi/scsi_transport_iscsi.c +4 -0
index 0102a2d..ea2996a 100644
@@ -3467,6 +3467,10 @@ iscsi_get_host_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
 		memset(buf, 0, host_stats_size);
 
 		err = transport->get_host_stats(shost, buf, host_stats_size);
+		if (err) {
+			kfree(skbhost_stats);
+			goto exit_host_stats;
+		}
 
 		actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
 		skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
#5 lore raw
Quoted 134 lines · Mike Christie
Quoted 72 lines · Julia Lawall
Quoted 68 lines · Robert (Server Storage)
Quoted 49 lines

-----Original Message-----
From: [email protected] [mailto:linux-scsi- [email protected]] On Behalf Of Himangi Saraogi
Sent: Friday, 04 July, 2014 1:28 PM
To: Vikas Chaudhary; [email protected]; James E.J. Bottomley; linux-
[email protected]; [email protected]
Cc: [email protected]
Subject: [PATCH] qla4xxx: Return -ENOMEM on memory allocation failure

In this code, 0 is returned on memory allocation failure, even though other failures return -ENOMEM or other similar values.

A simplified version of the Coccinelle semantic match that finds this problem is as follows:

// <smpl>
@@
expression ret;
expression x,e1,e2,e3;
identifier alloc;
@@

ret = 0
... when != ret = e1
*x = alloc(...)
... when != ret = e2
if (x == NULL) { ... when != ret = e3
return ret;
}
// </smpl>

---
 drivers/scsi/qla4xxx/ql4_os.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/qla4xxx/ql4_os.c +0 -0
index c5d9564..72ba671 100644
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct

Scsi_Host
*shost, char *buf, int len)

drivers/scsi/qla4xxx/ql4_os.c static int qla4xxx_get_host_stats(struct Scsi_Host *shost, char *buf, int len) · line 1055

Quoted 6 lines
drivers/scsi/qla4xxx/ql4_os.c +1 -0
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}

Also, the only caller of this function doesn't use the return value - it's overwritten by another function call:

drivers/scsi/scsi_transport_iscsi.c:

                err = transport->get_host_stats(shost, buf,

host_stats_size);

actual_size = nlmsg_total_size(sizeof(*ev) +

host_stats_size);
                skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));
                nlhhost_stats->nlmsg_len = actual_size;
Quoted 5 lines
Quoted 3 lines

err = iscsi_multicast_skb(skbhost_stats,
ISCSI_NL_GRP_ISCSID,

                                          GFP_KERNEL);

Maybe that is intentional?

I think it was a mistake. There is also one more issue in qla4xxx_get_host_stats where it is returning an internal qlogic specific error value. This patch should fix all issues.

[PATCH] qla4xxx/iscsi: fix get_host_stats error propagation

This patch fixes 2 bugs.

1. qla4xxx was not always returning -EXYZ error codes when qla4xxx_get_host_stats failed.

2. iscsi_get_host_stats was dropping the error code returned by drivers like qla4xxx.

drivers/scsi/qla4xxx/ql4_os.c +2 -0
index 459b9f7..e4dc3e0 100644
@@ -1050,6 +1050,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host
 *shost, char *buf, int len)
 	if (!ql_iscsi_stats) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to allocate memory for iscsi stats\n");
+		ret = -ENOMEM;
 		goto exit_host_stats;
 	}
@@ -1058,6 +1059,7 @@ static int qla4xxx_get_host_stats(struct Scsi_Host
 *shost, char *buf, int len)
 	if (ret != QLA_SUCCESS) {
 		ql4_printk(KERN_ERR, ha,
 			   "Unable to retrieve iscsi stats\n");
+		ret = -EIO;
 		goto exit_host_stats;
 	}
	host_stats->mactx_frames = le64_to_cpu(ql_iscsi_stats->mac_tx_frames);
drivers/scsi/scsi_transport_iscsi.c +4 -0
index 0102a2d..ea2996a 100644
@@ -3467,6 +3467,10 @@ iscsi_get_host_stats(struct iscsi_transport
 *transport, struct nlmsghdr *nlh)
 		memset(buf, 0, host_stats_size);
 
 		err = transport->get_host_stats(shost, buf, host_stats_size);
+		if (err) {
+			kfree(skbhost_stats);
+			goto exit_host_stats;
+		}
 
 		actual_size = nlmsg_total_size(sizeof(*ev) + host_stats_size);
Quoted 1 line
		skb_trim(skbhost_stats, NLMSG_ALIGN(actual_size));