Skip to contentSkip to conversation

xen-netback: correctly check failed allocation

PATCH Applied Insu Yun 4 participants 11 messages Oct 15, 2015 – Oct 19, 2015

Subsystems (3)
lore
Share & tools
lore

Insu Yun

author
#0 lore raw

PATCH 1/1 xen-netback: correctly check failed allocation

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/xen-netback/xenbus.c +6 -0
index 929a6e7..56ebd82 100644
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
 	/* Use the number of queues requested by the frontend */
 	be->vif->queues = vzalloc(requested_num_queues *
 				  sizeof(struct xenvif_queue));
+	if (!be->vif->queues) {
+		xenbus_dev_fatal(dev, -ENOMEM,
+				 "allocating queues");
+		return;
+	}
+
 	be->vif->num_queues = requested_num_queues;
 	be->vif->stalled_queues = requested_num_queues;
 
Signature · 1 line
1.9.1
author
#1 lore raw

I changed patch with valid format.

Quoted 14 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

Quoted 9 lines
drivers/net/xen-netback/xenbus.c +2 -1
+       }
+
        be->vif->num_queues = requested_num_queues;
        be->vif->stalled_queues = requested_num_queues;
 
--

1.9.1

Signature · 2 lines
Regards
Insu Yun

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

The existing code is not perfect, but that doesn't mean we should follow bad example.

Wei.

Quoted 15 lines
Quoted 10 lines
drivers/net/xen-netback/xenbus.c +2 -1
+       }
+
        be->vif->num_queues = requested_num_queues;
        be->vif->stalled_queues = requested_num_queues;
 
--

1.9.1

Signature · 2 lines
Regards
Insu Yun
#8 lore raw
In reply to Insu Yun
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

The existing code is not perfect, but that doesn't mean we should follow bad example.

Wei.

Quoted 15 lines
Quoted 10 lines
drivers/net/xen-netback/xenbus.c +2 -1
+       }
+
        be->vif->num_queues = requested_num_queues;
        be->vif->stalled_queues = requested_num_queues;
 
--

1.9.1

Signature · 2 lines
Regards
Insu Yun
Acked-by
#3 lore raw
In reply to Wei Liu
Quoted 35 lines · Wei Liu
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

Quoted 6 lines

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

The existing code is not perfect, but that doesn't mean we should follow bad example.

And to be clear, I don't want to block this patch just because of this coding style thing. It's still an improvement and fix a real problem. So:

Acked-by
#4 lore raw
In reply to Wei Liu
Quoted 35 lines · Wei Liu
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

Quoted 6 lines

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

The existing code is not perfect, but that doesn't mean we should follow bad example.

And to be clear, I don't want to block this patch just because of this coding style thing. It's still an improvement and fix a real problem. So:

#5 lore raw
In reply to Wei Liu
Quoted 31 lines · Wei Liu
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

Quoted 7 lines

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

Using xenbus_dev_fatal(); return; throughout would be consistent and easy to reason about.

Also, the goto err path should raise a fatal error (instead of disconnecting). I also note that failures in the xen_net_read_rate(), xen_register_watchers() and read_xenbus_vif_flags() are also not handled.

David

#7 lore raw
In reply to Wei Liu
Quoted 31 lines · Wei Liu
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

Quoted 7 lines

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

Using xenbus_dev_fatal(); return; throughout would be consistent and easy to reason about.

Also, the goto err path should raise a fatal error (instead of disconnecting). I also note that failures in the xen_net_read_rate(), xen_register_watchers() and read_xenbus_vif_flags() are also not handled.

David

author
#6 lore raw
In reply to David Vrabel

For me, it seems more consistent using xen_bus_dev_fatal(); return; Because other codes also use them.

If you want to use goto, then feel free to make a patch for this issue.

Quoted 49 lines · David Vrabel
Quoted 31 lines · Wei Liu
Quoted 12 lines · Insu Yun

I changed patch with valid format.

Quoted 8 lines · Insu Yun

Since vzalloc can be failed in memory pressure,
writes -ENOMEM to xenstore to indicate error.

---
 drivers/net/xen-netback/xenbus.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
drivers/net/xen-netback/xenbus.c +0 -0
index 929a6e7..56ebd82 100644

drivers/net/xen-netback/xenbus.c static void connect(struct backend_info *be) · line 795

Quoted 8 lines
drivers/net/xen-netback/xenbus.c +4 -0
@@ -788,6 +788,12 @@ static void connect(struct backend_info *be)
        /* Use the number of queues requested by the frontend */
        be->vif->queues = vzalloc(requested_num_queues *
                                  sizeof(struct xenvif_queue));
+       if (!be->vif->queues) {
+               xenbus_dev_fatal(dev, -ENOMEM,
+                                "allocating queues");
+               return;
 

I didn't use goto err, because another error handling is not required

Quoted 7 lines

It's recommended in kernel coding style to use "goto" style error handling. I personally prefer that to arbitrary return in function body, too.

It's not a matter of whether another error handling is required or not, it's about cleaner code that is easy to reason about and consistent coding style.

Using xenbus_dev_fatal(); return; throughout would be consistent and easy to reason about.

Also, the goto err path should raise a fatal error (instead of disconnecting). I also note that failures in the xen_net_read_rate(), xen_register_watchers() and read_xenbus_vif_flags() are also not handled.

David

Signature · 2 lines
Regards
Insu Yun

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel