Skip to contentSkip to conversation

scatterlist: don't BUG when we can trivially return a proper error.

PATCH In review Nick Bowler 3 participants 5 messages Nov 1, 2012 – Nov 14, 2012 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scatterlist: don't BUG when we can trivially return a proper error.

There is absolutely no reason to crash the kernel when we have a perfectly good return value already available to use for conveying failure status.

Let's return an error code instead of crashing the kernel: that sounds like a much better plan.

---
 lib/scatterlist.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
lib/scatterlist.c +2 -1
index 3675452b23ca..11ecaf000696 100644
@@ -248,7 +248,8 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
 	unsigned int left;
 
 #ifndef ARCH_HAS_SG_CHAIN
-	BUG_ON(nents > max_ents);
+	if (WARN_ON_ONCE(nents > max_ents))
+		return -E2BIG;
 #endif
 
 	memset(table, 0, sizeof(*table));
Signature · 1 line
1.7.8.6
maintainer
#1 lore raw
Quoted 3 lines · On Thu, 1 Nov 2012 15:03:00 -0400 Nick Bowler <[email protected]> wrote:

There is absolutely no reason to crash the kernel when we have a perfectly good return value already available to use for conveying failure status.

Yes, I suppose that's true. I don't see a case for BUGging the kernel here.

Quoted 13 lines

Let's return an error code instead of crashing the kernel: that sounds like a much better plan.

---
 lib/scatterlist.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
lib/scatterlist.c +0 -0
index 3675452b23ca..11ecaf000696 100644

lib/scatterlist.c int __sg_alloc_table(struct sg_table *table, unsigned int nents, · line 253

Quoted 7 lines
lib/scatterlist.c +2 -1
@@ -248,7 +248,8 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
 	unsigned int left;
 
 #ifndef ARCH_HAS_SG_CHAIN
-	BUG_ON(nents > max_ents);
+	if (WARN_ON_ONCE(nents > max_ents))
+		return -E2BIG;
 #endif

OK, pet peeve: if this E2BIG gets returned to userspace, our poor user will look it up and see "Argument list too long; used when the arguments passed to a new program being executed with one of the exec functions occupy too much memory space". He then gets to spend half a day reviewing his code's exec() callsites!

See? Although the error's name sounds like a nice match to the internal state, it isn't really a match at all and our use of it is misleading.

Unfortunately there is no EKERNELSCREWEDUP, so we usually use EINVAL.

#2 lore raw
Quoted 9 lines · Andrew Morton
Quoted 3 lines · On Thu, 1 Nov 2012 15:03:00 -0400 Nick Bowler <[email protected]> wrote:

There is absolutely no reason to crash the kernel when we have a perfectly good return value already available to use for conveying failure status.

Yes, I suppose that's true. I don't see a case for BUGging the kernel here.

[...]

Quoted 11 lines

lib/scatterlist.c int __sg_alloc_table(struct sg_table *table, unsigned int nents, · line 253

Quoted 4 lines
lib/scatterlist.c +2 -1
-	BUG_ON(nents > max_ents);
+	if (WARN_ON_ONCE(nents > max_ents))
+		return -E2BIG;
 #endif

OK, pet peeve: if this E2BIG gets returned to userspace, our poor user will look it up and see "Argument list too long; used when the arguments passed to a new program being executed with one of the exec functions occupy too much memory space". He then gets to spend half a day reviewing his code's exec() callsites!

Quoted 5 lines

See? Although the error's name sounds like a nice match to the internal state, it isn't really a match at all and our use of it is misleading.

Unfortunately there is no EKERNELSCREWEDUP,

Well, maybe we should add it! :P

Quoted 1 line

so we usually use EINVAL.

Fair enough. I will prepare v2. But perhaps EOPNOTSUPP would be a better fit?

Thanks,

Signature · 1 line
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
#3 lore raw
Quoted 26 lines · Nick Bowler
Quoted 9 lines · Andrew Morton
Quoted 3 lines · On Thu, 1 Nov 2012 15:03:00 -0400 Nick Bowler <[email protected]> wrote:

There is absolutely no reason to crash the kernel when we have a perfectly good return value already available to use for conveying failure status.

Yes, I suppose that's true. I don't see a case for BUGging the kernel here.

[...]

Quoted 15 lines

lib/scatterlist.c int __sg_alloc_table(struct sg_table *table, unsigned int nents, · line 253

Quoted 4 lines
lib/scatterlist.c +2 -1
-   BUG_ON(nents > max_ents);
+   if (WARN_ON_ONCE(nents > max_ents))
+           return -E2BIG;
 #endif

OK, pet peeve: if this E2BIG gets returned to userspace, our poor user will look it up and see "Argument list too long; used when the arguments passed to a new program being executed with one of the exec functions occupy too much memory space". He then gets to spend half a day reviewing his code's exec() callsites!

See? Although the error's name sounds like a nice match to the internal state, it isn't really a match at all and our use of it is misleading.

Quoted 8 lines
Quoted 1 line

Unfortunately there is no EKERNELSCREWEDUP,

Well, maybe we should add it! :P

Quoted 1 line

so we usually use EINVAL.

Fair enough. I will prepare v2. But perhaps EOPNOTSUPP would be a better fit?

IMHO this would be even more confusing...

Signature · 2 lines
Thanks,
//richard
maintainer
#4 lore raw
In reply to Nick Bowler
Quoted 3 lines · On Wed, 14 Nov 2012 17:15:44 -0500 Nick Bowler <[email protected]> wrote:
Quoted 1 line

Unfortunately there is no EKERNELSCREWEDUP,

Well, maybe we should add it! :P

Well. Given how frequently it will be used, it would need to be a much shorter identifier. Perhaps "EEK". Or just "E".

Quoted 4 lines
Quoted 1 line

so we usually use EINVAL.

Fair enough. I will prepare v2. But perhaps EOPNOTSUPP would be a better fit?

I have queued a delta:

From: Andrew Morton <[email protected]>
Subject: scatterlist-dont-bug-when-we-can-trivially-return-a-proper-error-fix

s/E2BIG/EINVAL/

Cc Nick Bowler <[email protected]>
Cc Maxim Levitsky <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Jens Axboe <[email protected]>

---

 lib/scatterlist.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/scatterlist.c renamed from lib/scatterlist.c~scatterlist-dont-bug-when-we-can-trivially-return-a-proper-error-fix +1 -1
diff -puN lib/scatterlist.c~scatterlist-dont-bug-when-we-can-trivially-return-a-proper-error-fix lib/scatterlist.c
@@ -249,7 +249,7 @@ int __sg_alloc_table(struct sg_table *ta
 
 #ifndef ARCH_HAS_SG_CHAIN
 	if (WARN_ON_ONCE(nents > max_ents))
-		return -E2BIG;
+		return -EINVAL;
 #endif
 
 	memset(table, 0, sizeof(*table));

_