CodingStyle: add some more error handling guidelines
Subsystems (2)
Michael S. Tsirkin
maintainerPATCH 1/1 CodingStyle: add some more error handling guidelines
commit commit ea04036032edda6f771c1381d03832d2ed0f6c31 ("CodingStyle:
add some more error handling guidelines") suggests never naming goto labels after the goto location - that is the error that is handled.
But it's actually pretty common and IMHO it's a reasonable style provided each error gets its own label, and each label comes after the matching cleanup:
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;Provides some symmetry and makes it easy to add more cases as code calling goto does not need to worry about how is the error handled.
--- tools/virtio/ringtest/main.h | 4 +++- Documentation/CodingStyle | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-)
tools/virtio/ringtest/main.h +3 -1
@@ -80,7 +80,9 @@ extern unsigned ring_size;/* Is there a portable way to do this? */ #if defined(__x86_64__) || defined(__i386__) -#define cpu_relax () asm ("rep; nop" ::: "memory") +#define cpu_relax () do { \ + asm ("rep; nop" ::: "memory"); \ +} while (0)#else #define cpu_relax () assert(0)#endif
Documentation/CodingStyle +42 -2
56 lines not on this page · raw message
Signature · 1 line
MST
Jonathan Corbet
maintainerQuoted 8 lines · On Mon, 22 Aug 2016 16:57:46 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
commit commit
ea04036032edda6f771c1381d03832d2ed0f6c31("CodingStyle:
add some more error handling guidelines") suggests never naming goto labels after the goto location - that is the error that is handled.But it's actually pretty common and IMHO it's a reasonable style provided each error gets its own label, and each label comes after the matching cleanup:
Quoted 16 lines
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;Hmm, I've never encountered that style, but I've never gone looking for it either. I find it a little confusing to detach a label from the code it will run. Is this really something we want to encourage? I kind of think this one needs some acks before I can consider it.
Quoted 5 lines
tools/virtio/ringtest/main.h +0
-0
tools/virtio/ringtest/main.h extern unsigned ring_size; · line 88
Quoted 10 lines
tools/virtio/ringtest/main.h +3
-1@@ -80,7 +80,9 @@ extern unsigned ring_size;/* Is there a portable way to do this? */ #if defined(__x86_64__) || defined(__i386__) -#define cpu_relax () asm ("rep; nop" ::: "memory") +#define cpu_relax () do { \ + asm ("rep; nop" ::: "memory"); \ +} while (0)#else #define cpu_relax () assert(0)#endif
This hunk seems somehow unrelated, either that or I really haven't understood the proposal :)
jon
Jonathan Corbet
maintainerQuoted 8 lines · On Mon, 22 Aug 2016 16:57:46 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
commit commit
ea04036032edda6f771c1381d03832d2ed0f6c31("CodingStyle:
add some more error handling guidelines") suggests never naming goto labels after the goto location - that is the error that is handled.But it's actually pretty common and IMHO it's a reasonable style provided each error gets its own label, and each label comes after the matching cleanup:
Quoted 16 lines
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;Hmm, I've never encountered that style, but I've never gone looking for it either. I find it a little confusing to detach a label from the code it will run. Is this really something we want to encourage? I kind of think this one needs some acks before I can consider it.
Quoted 5 lines
tools/virtio/ringtest/main.h +0
-0
tools/virtio/ringtest/main.h extern unsigned ring_size; · line 88
Quoted 10 lines
tools/virtio/ringtest/main.h +3
-1@@ -80,7 +80,9 @@ extern unsigned ring_size;/* Is there a portable way to do this? */ #if defined(__x86_64__) || defined(__i386__) -#define cpu_relax () asm ("rep; nop" ::: "memory") +#define cpu_relax () do { \ + asm ("rep; nop" ::: "memory"); \ +} while (0)#else #define cpu_relax () assert(0)#endif
This hunk seems somehow unrelated, either that or I really haven't understood the proposal :)
jon
Michael S. Tsirkin
maintainerQuoted 26 lines · Jonathan Corbet
Quoted 23 lines · On Mon, 22 Aug 2016 16:57:46 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
commit commit
ea04036032edda6f771c1381d03832d2ed0f6c31("CodingStyle:
add some more error handling guidelines") suggests never naming goto labels after the goto location - that is the error that is handled.But it's actually pretty common and IMHO it's a reasonable style provided each error gets its own label, and each label comes after the matching cleanup:
foo = kmalloc(SIZE, GFP_KERNEL); if (!foo) goto err_foo; foo->bar = kmalloc(SIZE, GFP_KERNEL); if (!foo->bar) goto err_bar; ... kfree(foo->bar); err_bar: kfree(foo); err_foo:
Quoted 6 lines
Quoted 1 line
return ret;Hmm, I've never encountered that style, but I've never gone looking for it either. I find it a little confusing to detach a label from the code it will run. Is this really something we want to encourage? I kind of think this one needs some acks before I can consider it.
The point is really naming label for the part of init that failed (and so needs to be skipped), rather than the part that will run. Adding empty lines is not the point - does it look better like this?
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;I don't know whether there are examples outside code that I wrote myself, e.g. in vhost_dev_set_owner. I find that it's helpful since it avoids churn if more allocations are added.
Quoted 19 lines
Quoted 5 lines
tools/virtio/ringtest/main.h +0
-0tools/virtio/ringtest/main.h extern unsigned ring_size; · line 88
Quoted 10 lines
tools/virtio/ringtest/main.h +3
-1@@ -80,7 +80,9 @@ extern unsigned ring_size;/* Is there a portable way to do this? */ #if defined(__x86_64__) || defined(__i386__) -#define cpu_relax () asm ("rep; nop" ::: "memory") +#define cpu_relax () do { \ + asm ("rep; nop" ::: "memory"); \ +} while (0)#else #define cpu_relax () assert(0)#endif This hunk seems somehow unrelated, either that or I really haven't understood the proposal :)
Quoted 1 line
jon
Ouch, that's unrelated, sorry.
Signature · 1 line
MST
Michael S. Tsirkin
maintainerQuoted 26 lines · Jonathan Corbet
Quoted 23 lines · On Mon, 22 Aug 2016 16:57:46 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
commit commit
ea04036032edda6f771c1381d03832d2ed0f6c31("CodingStyle:
add some more error handling guidelines") suggests never naming goto labels after the goto location - that is the error that is handled.But it's actually pretty common and IMHO it's a reasonable style provided each error gets its own label, and each label comes after the matching cleanup:
foo = kmalloc(SIZE, GFP_KERNEL); if (!foo) goto err_foo; foo->bar = kmalloc(SIZE, GFP_KERNEL); if (!foo->bar) goto err_bar; ... kfree(foo->bar); err_bar: kfree(foo); err_foo:
Quoted 6 lines
Quoted 1 line
return ret;Hmm, I've never encountered that style, but I've never gone looking for it either. I find it a little confusing to detach a label from the code it will run. Is this really something we want to encourage? I kind of think this one needs some acks before I can consider it.
The point is really naming label for the part of init that failed (and so needs to be skipped), rather than the part that will run. Adding empty lines is not the point - does it look better like this?
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;I don't know whether there are examples outside code that I wrote myself, e.g. in vhost_dev_set_owner. I find that it's helpful since it avoids churn if more allocations are added.
Quoted 19 lines
Quoted 5 lines
tools/virtio/ringtest/main.h +0
-0tools/virtio/ringtest/main.h extern unsigned ring_size; · line 88
Quoted 10 lines
tools/virtio/ringtest/main.h +3
-1@@ -80,7 +80,9 @@ extern unsigned ring_size;/* Is there a portable way to do this? */ #if defined(__x86_64__) || defined(__i386__) -#define cpu_relax () asm ("rep; nop" ::: "memory") +#define cpu_relax () do { \ + asm ("rep; nop" ::: "memory"); \ +} while (0)#else #define cpu_relax () assert(0)#endif This hunk seems somehow unrelated, either that or I really haven't understood the proposal :)
Quoted 1 line
jon
Ouch, that's unrelated, sorry.
Signature · 1 line
MST
vhost_dev_set_owner() is an example of why come-from labels are bad style.
devel/drivers/vhost/vhost.c
473 /* Caller should have device mutex */
474 long vhost_dev_set_owner(struct vhost_dev *dev)
475 { 476 struct task_struct *worker;
477 int err;
478
479 /* Is there an owner already? */
480 if (vhost_dev_has_owner(dev)) {
481 err = -EBUSY;
482 goto err_mm;
What does goto err_mm do? It's actually a do-nothing goto. It would be easier to read as a direct return. Why is it called err_mm? Because originally the condition was:
if (dev->mm) {
err = -EBUSY;
goto err_mm;
}We've changed the code but didn't update the label so it's slightly confusing unless you know how vhost_dev_has_owner() is implemented.
483 }
484
485 /* No owner, become one */
486 dev->mm = get_task_mm(current);
487 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
488 if (IS_ERR(worker)) {
489 err = PTR_ERR(worker);
490 goto err_worker;
491 }
492
493 dev->worker = worker;
494 wake_up_process(worker); /* avoid contributing to loadavg */
495
496 err = vhost_attach_cgroups(dev);
497 if (err)
498 goto err_cgroup;
499
500 err = vhost_dev_alloc_iovecs(dev);
501 if (err)
502 goto err_cgroup;This name doesn't make sense because it's a come-from label which is used twice. Some people do:
if (err) goto err_iovecs;
503
504 return 0;
Then they add two labels here:
err_iovecs: err_cgroup: kthread_stop(worker);
But if you base the label name on the label location then it makes sense. goto stop_kthread; goto err_mmput;.
505 err_cgroup: 506 kthread_stop(worker); 507 dev->worker = NULL; 508 err_worker: 509 if (dev->mm) 510 mmput(dev->mm); 511 dev->mm = NULL; 512 err_mm: 513 return err; 514 }
regards,
dan carpenter
Michael S. Tsirkin
maintainerQuoted 49 lines · Dan Carpenter
vhost_dev_set_owner()is an example of why come-from labels are bad style.
devel/drivers/vhost/vhost.c473 /* Caller should have device mutex */ 474 long vhost_dev_set_owner(struct vhost_dev *dev) 475 { 476 struct task_struct *worker; 477 int err; 478 479 /* Is there an owner already? */ 480 if (vhost_dev_has_owner(dev)) { 481 err = -EBUSY; 482 goto err_mm;What does goto
err_mmdo? It's actually a do-nothing goto. It would be easier to read as a direct return. Why is it callederr_mm? Because originally the condition was:if (dev->mm) { err = -EBUSY; goto err_mm; }We've changed the code but didn't update the label so it's slightly confusing unless you know how
vhost_dev_has_owner()is implemented.483 } 484 485 /* No owner, become one */ 486 dev->mm = get_task_mm(current); 487 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); 488 if (IS_ERR(worker)) { 489 err = PTR_ERR(worker); 490 goto err_worker; 491 } 492 493 dev->worker = worker; 494 wake_up_process(worker); /* avoid contributing to loadavg */ 495 496 err = vhost_attach_cgroups(dev); 497 if (err) 498 goto err_cgroup; 499 500 err = vhost_dev_alloc_iovecs(dev); 501 if (err) 502 goto err_cgroup;
Quoted 8 lines
This name doesn't make sense because it's a come-from label which is used twice. Some people do:
if (err) goto err_iovecs;503 504 return 0;
Right and the current CodingStyle text seems to discourage this.
Quoted 5 lines
Then they add two labels here:
err_iovecs: err_cgroup: kthread_stop(worker);
Definitely good points above, I'll fix them up.
Quoted 14 lines
But if you base the label name on the label location then it makes sense. goto
stop_kthread; gotoerr_mmput;.505 err_cgroup: 506 kthread_stop(worker); 507 dev->worker = NULL; 508 err_worker: 509 if (dev->mm) 510 mmput(dev->mm); 511 dev->mm = NULL; 512 err_mm: 513 return err; 514 }
Quoted 2 lines
regards,
dan carpenter
OK, I'll consider this, thanks!
Signature · 1 line
MST
Michael S. Tsirkin
maintainerQuoted 49 lines · Dan Carpenter
vhost_dev_set_owner()is an example of why come-from labels are bad style.
devel/drivers/vhost/vhost.c473 /* Caller should have device mutex */ 474 long vhost_dev_set_owner(struct vhost_dev *dev) 475 { 476 struct task_struct *worker; 477 int err; 478 479 /* Is there an owner already? */ 480 if (vhost_dev_has_owner(dev)) { 481 err = -EBUSY; 482 goto err_mm;What does goto
err_mmdo? It's actually a do-nothing goto. It would be easier to read as a direct return. Why is it callederr_mm? Because originally the condition was:if (dev->mm) { err = -EBUSY; goto err_mm; }We've changed the code but didn't update the label so it's slightly confusing unless you know how
vhost_dev_has_owner()is implemented.483 } 484 485 /* No owner, become one */ 486 dev->mm = get_task_mm(current); 487 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid); 488 if (IS_ERR(worker)) { 489 err = PTR_ERR(worker); 490 goto err_worker; 491 } 492 493 dev->worker = worker; 494 wake_up_process(worker); /* avoid contributing to loadavg */ 495 496 err = vhost_attach_cgroups(dev); 497 if (err) 498 goto err_cgroup; 499 500 err = vhost_dev_alloc_iovecs(dev); 501 if (err) 502 goto err_cgroup;
Quoted 8 lines
This name doesn't make sense because it's a come-from label which is used twice. Some people do:
if (err) goto err_iovecs;503 504 return 0;
Right and the current CodingStyle text seems to discourage this.
Quoted 5 lines
Then they add two labels here:
err_iovecs: err_cgroup: kthread_stop(worker);
Definitely good points above, I'll fix them up.
Quoted 14 lines
But if you base the label name on the label location then it makes sense. goto
stop_kthread; gotoerr_mmput;.505 err_cgroup: 506 kthread_stop(worker); 507 dev->worker = NULL; 508 err_worker: 509 if (dev->mm) 510 mmput(dev->mm); 511 dev->mm = NULL; 512 err_mm: 513 return err; 514 }
Quoted 2 lines
regards,
dan carpenter
OK, I'll consider this, thanks!
Signature · 1 line
MST
Quoted 2 lines · Michael S. Tsirkin
The point is really naming label for the part of init that failed (and so needs to be skipped), rather than the part that will run.
Naming labels after what "needs to be skipped" doesn't work. How does that meaning make sense for err_cgroup in vhost_dev_set_owner()? What needs to be skipped here?
regards,
dan carpenter
Michael S. Tsirkin
maintainerQuoted 10 lines · Dan Carpenter
Quoted 2 lines · Michael S. Tsirkin
The point is really naming label for the part of init that failed (and so needs to be skipped), rather than the part that will run.
Naming labels after what "needs to be skipped" doesn't work. How does that meaning make sense for
err_cgroupinvhost_dev_set_owner()? What needs to be skipped here?regards,
dan carpenter
Nothing because we are destroying the thread, so we don't need to detach it. I guess I'm convinced it's not very consistent at this point.
Signature · 1 line
MST
Quoted 11 lines · Michael S. Tsirkin
commit commit
ea04036032edda6f771c1381d03832d2ed0f6c31("CodingStyle:
add some more error handling guidelines") suggests never naming goto labels after the goto location - that is the error that is handled.But it's actually pretty common and IMHO it's a reasonable style provided each error gets its own label, and each label comes after the matching cleanup:
foo = kmalloc(SIZE, GFP_KERNEL); if (!foo) goto err_foo;
Come-from labels are a common anti-pattern. The don't add any information if you are reading a function from start to end/top to bottom. Imagine if we named all functions after then functions which called them. This is the same concept.
What does goto err_foo tell you? Nothing... But goto err_free_bar tells you exactly what it does.
Creating a new label for each goto means you can search easily, I suppose, but jumping down to the bottom of the function and then back up disrupts the flow. We're not really using the name itself in that case, we're just treating the text as a opaque search string and not meaningful for its own sake.
I see a lot of error handling bugs where people get confused or often they just decide that error handling is too complicated and they leave it out. But error handling is really simple to write correctly if you follow a few simple rules.
1) Don't just use one out label for everything. Doing multiple things makes the code more complicated and bug prone.
2) Don't free things which haven't been allocated.
3) Unwind in the reverse order that you allocated things.
4) Use meaningful names which tell what the goto does.
5) If there is an if statement in allocation code, then put an mirror if statement in the unwind code.
regards,
dan carpenter
Quoted 9 lines · Michael S. Tsirkin
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...Quoted 7 lines
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;I believe the CodingStyle already contain far too much personal style to be useful as real style guide. FWIW, I prefer a single error label, at the "cost" of additional tests in the error path:
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err;
...
if (ret)
goto err;
return 0;
err:
if (foo)
kfree(foo->bar);
kfree(foo);
return ret;The advantage is that I don't have to manage X different labels, ensuring that they have the order is correct if some part of the function is refactored etc. That tends to get too complicated for my simple brain. And since the error path is rarely tested, complicated equals buggy.
My sample will of course trigger all those nice "optimizing the error path" patches, but I ignore those anyway so that's not a big deal.
Bjørn
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 9 lines · Michael S. Tsirkin
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err_foo;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err_bar;
...Quoted 7 lines
kfree(foo->bar);
err_bar:
kfree(foo);
err_foo:
return ret;I believe the CodingStyle already contain far too much personal style to be useful as real style guide. FWIW, I prefer a single error label, at the "cost" of additional tests in the error path:
foo = kmalloc(SIZE, GFP_KERNEL);
if (!foo)
goto err;
foo->bar = kmalloc(SIZE, GFP_KERNEL);
if (!foo->bar)
goto err;
...
if (ret)
goto err;
return 0;
err:
if (foo)
kfree(foo->bar);
kfree(foo);
return ret;The advantage is that I don't have to manage X different labels, ensuring that they have the order is correct if some part of the function is refactored etc. That tends to get too complicated for my simple brain. And since the error path is rarely tested, complicated equals buggy.
My sample will of course trigger all those nice "optimizing the error path" patches, but I ignore those anyway so that's not a big deal.
Bjørn
Quoted 42 lines · Bjørn Mork
Quoted 9 lines · Michael S. Tsirkin
foo = kmalloc(SIZE, GFP_KERNEL); if (!foo) goto err_foo; foo->bar = kmalloc(SIZE, GFP_KERNEL); if (!foo->bar) goto err_bar; ...Quoted 7 lines
kfree(foo->bar); err_bar: kfree(foo); err_foo: return ret;I believe the CodingStyle already contain far too much personal style to be useful as real style guide. FWIW, I prefer a single error label, at the "cost" of additional tests in the error path:
foo = kmalloc(SIZE, GFP_KERNEL); if (!foo) goto err; foo->bar = kmalloc(SIZE, GFP_KERNEL); if (!foo->bar) goto err; ... if (ret) goto err; return 0; err: if (foo) kfree(foo->bar); kfree(foo); return ret;
Quoted 5 lines
The advantage is that I don't have to manage X different labels, ensuring that they have the order is correct if some part of the function is refactored etc. That tends to get too complicated for my simple brain. And since the error path is rarely tested, complicated equals buggy.
Empirically, that style is *way* more bug prone. I call these bugs "One Err Bugs". It's one of the most common types of bugs I deal with from static analysis.
The order is not hard. It's just the reverse order from how it was allocated. Hike up the mountain, then if you get stuck hike back down using the exact same path. Then at the end, you basically have written your ->remove() function so it's a bonus.
Quoted 3 lines
My sample will of course trigger all those nice "optimizing the error path" patches, but I ignore those anyway so that's not a big deal.
That's not my fault. :/ I have tried over and over and over to tell that guy to stop sending patches but everyone else encourages him. I feel like it should be a rule that if you introduce bugs, you should be told to stop sending cleanup patches until you have fixed enough bugs to redeem yourself.
regards,
dan carpenter
Quoted 2 lines · Dan Carpenter
Hike up the mountain, then if you get stuck hike back down using the exact same path.
OK, I understand what you say. I just can't resist objecting to that example ;)
In my experience, finding the exact same path back after hiking up a mountain is really hard. Especially if you are in enough trouble already to get stuck. Up and down tend to look completely different. Looking back all the time while you go up might help, but finding your way back by simply taking the reverse path is definitely not easy.
Bjørn
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 2 lines · Dan Carpenter
Hike up the mountain, then if you get stuck hike back down using the exact same path.
OK, I understand what you say. I just can't resist objecting to that example ;)
In my experience, finding the exact same path back after hiking up a mountain is really hard. Especially if you are in enough trouble already to get stuck. Up and down tend to look completely different. Looking back all the time while you go up might help, but finding your way back by simply taking the reverse path is definitely not easy.
Bjørn
Lol. The mossy side of a boulder is the alloc, the non-mossy side is the free!
:P
regards,
dan carpenter