Skip to contentSkip to conversation

kfifo must_check warning

DISCUSSION Randy Dunlap 5 participants 7 messages Oct 19, 2010 – Oct 20, 2010

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

In 2.6.36-rc8, I see this build warning:

drivers/char/n_gsm.c: In function 'gsm_dlci_alloc':
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

The helper seems to be getting in the way (?). The driver code does this:

	if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
		kfree(dlci);
		return NULL;
	}

Should the driver code be doing something else?
or should the kfifo_alloc() macro be checking the result of the helper?

thanks,
---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#1 lore raw
Quoted 7 lines · On Tue, 19 Oct 2010 15:10:48 -0700 Randy Dunlap <[email protected]> wrote:

In 2.6.36-rc8, I see this build warning:

drivers/char/n_gsm.c: In function 'gsm_dlci_alloc':
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

The helper seems to be getting in the way (?). The driver code does this:

Quoted 8 lines
	if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
		kfree(dlci);
		return NULL;
	}

Should the driver code be doing something else?
or should the kfifo_alloc() macro be checking the result of the helper?

A gcc bug, I'd say. The code looks OK and my gcc doesn't warn.

Perhaps see if you can find some code transformation in n_gsm.c which makes it go away? Add a new local variable or something.

I did see a probably unrelated bug in there though. __kfifo_must_check_helper() coerces its arg into an `unsigned int' and returns an unsigned int. Consequently if kfifo_alloc() tries to return -EINVAL, the above-quoted code won't detect the error: it will see -EINVAL as a large, positive unsigned value.

I don't see a simple fix for that apart from creating several flavours of __kfifo_must_check_helper() and carefully going through each instance and using the one which takes (and returns) the correct type.

A suitable temporary 2.6.37 patch would bee to just disable __kfifo_must_check_helper():

include/linux/kfifo.h renamed from include/linux/kfifo.h~a +1 -5
@@ -171,11 +171,7 @@ struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PT
 	}
 
 
-static inline unsigned int __must_check
-__kfifo_must_check_helper(unsigned int val)
-{
-	return val;
-}
+#define __kfifo_must_check_helper(x) (x)
 
 /**
  * kfifo_initialized - Check if the fifo is initialized

_

#2 lore raw

Re: kfifo must_check warning (+ patch)

Quoted 14 lines · On Tue, 19 Oct 2010 22:12:42 -0700 Andrew Morton wrote:
Quoted 12 lines · On Tue, 19 Oct 2010 15:10:48 -0700 Randy Dunlap <[email protected]> wrote:

In 2.6.36-rc8, I see this build warning:

drivers/char/n_gsm.c: In function 'gsm_dlci_alloc':
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

The helper seems to be getting in the way (?). The driver code does this:

	if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
		kfree(dlci);
		return NULL;
	}
Quoted 5 lines
Quoted 3 lines

Should the driver code be doing something else?
or should the kfifo_alloc() macro be checking the result of the helper?

A gcc bug, I'd say. The code looks OK and my gcc doesn't warn.

OK.

Quoted 2 lines

Perhaps see if you can find some code transformation in n_gsm.c which makes it go away? Add a new local variable or something.

Yes, that works for me. Patch is below.

Quoted 30 lines

I did see a probably unrelated bug in there though. __kfifo_must_check_helper() coerces its arg into an `unsigned int' and returns an unsigned int. Consequently if kfifo_alloc() tries to return

(quoted diff) +0 -2
-EINVAL, the above-quoted code won't detect the error: it will see
-EINVAL as a large, positive unsigned value.

I don't see a simple fix for that apart from creating several flavours of __kfifo_must_check_helper() and carefully going through each instance and using the one which takes (and returns) the correct type.

A suitable temporary 2.6.37 patch would bee to just disable __kfifo_must_check_helper():

include/linux/kfifo.h renamed from include/linux/kfifo.h~a +1 -5
@@ -171,11 +171,7 @@ struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PT
 	}
 
 
-static inline unsigned int __must_check
-__kfifo_must_check_helper(unsigned int val)
-{
-	return val;
-}
+#define __kfifo_must_check_helper(x) (x)
 
 /**
  * kfifo_initialized - Check if the fifo is initialized

_

Quoted 1 line

--

From: Randy Dunlap <[email protected]>

Some versions of gcc mysteriously report:
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

This warning can be eliminated by using a local variable for the returned result value, as suggested by Andrew Morton.

---
 drivers/char/n_gsm.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/char/n_gsm.c +4 -1
@@ -1573,11 +1573,14 @@ static void gsm_dlci_command(struct gsm_
 static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
 {
 	struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
+	int err;
+
 	if (dlci == NULL)
 		return NULL;
 	spin_lock_init(&dlci->lock);
 	dlci->fifo = &dlci->_fifo;
-	if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
+	err = kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL);
+	if (err < 0) {
 		kfree(dlci);
 		return NULL;
 	}
#3 lore raw

Re: kfifo must_check warning (+ patch)

Quoted 11 lines
drivers/char/n_gsm.c +2 -0
@@ -1573,11 +1573,14 @@  static void gsm_dlci_command(struct gsm_
 static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
 {
 	struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
+	int err;
+

drivers/char/n_gsm.c static void gsm_dlci_command(struct gsm_ · line 1586

Quoted 10 lines
drivers/char/n_gsm.c +2 -1
 	if (dlci == NULL)
 		return NULL;
 	spin_lock_init(&dlci->lock);
 	dlci->fifo = &dlci->_fifo;
-	if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
+	err = kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL);
+	if (err < 0) {
 		kfree(dlci);
 		return NULL;
 	}

I think this is not a gcc bug but Randy's gcc beeing smarter and showing a signed bug with the old code.

The earlier discussion had:

Quoted 14 lines
Quoted 5 lines
Quoted 3 lines

Should the driver code be doing something else?
or should the kfifo_alloc() macro be checking the result of the helper?

A gcc bug, I'd say. The code looks OK and my gcc doesn't warn.

OK.

Quoted 2 lines

Perhaps see if you can find some code transformation in n_gsm.c which makes it go away? Add a new local variable or something.

Yes, that works for me. Patch is below.

Quoted 6 lines

Review

Quoted 6 lines

I did see a probably unrelated bug in there though. __kfifo_must_check_helper() coerces its arg into an `unsigned int' and returns an unsigned int. Consequently if kfifo_alloc() tries to return

(quoted diff) +0 -2
-EINVAL, the above-quoted code won't detect the error: it will see
-EINVAL as a large, positive unsigned value.

and then gcc decides that the check of < 0 is meaningless, and then says that the result is unused.

Note: I'm a kernel hacker , not a gcc one. This is pure speculation.

So to me the helper is the one causing the error! ... and the patch is undoing the damage at the caller.

So the correct fix is to change the return type of the helper, and if someone wants the int to be unsigned then it needs to be unsigned long so we can use an is_error type test.

milton

#4 lore raw

Re: kfifo must_check warning (+ patch)

In reply to Randy Dunlap
Quoted 5 lines

Some versions of gcc mysteriously report:
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

This warning can be eliminated by using a local variable for the returned result value, as suggested by Andrew Morton.

But that just hides the bug in the helper surely - the helper has type errors so needs fixing - not this. If you apply this then the underlying bug is just going to get forgotten

Alan

#5 lore raw

Re: kfifo must_check warning (+ patch)

Quoted 10 lines · On Wed, 20 Oct 2010 19:37:02 +0100 Alan Cox <[email protected]> wrote:
Quoted 5 lines

Some versions of gcc mysteriously report:
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

This warning can be eliminated by using a local variable for the returned result value, as suggested by Andrew Morton.

But that just hides the bug in the helper surely - the helper has type errors so needs fixing - not this. If you apply this then the underlying bug is just going to get forgotten

It won't get forgotten.

I agree that the warning is probably an artifact of the signedness thing. So for 2.6.36, disabling __kfifo_must_check_helper() will suffice. I'm sure that Stefani will fix up __kfifo_must_check_helper() for real for 2.6.37, then we can see if the warning reoccurs.

maintainer
#6 lore raw

Re: kfifo must_check warning (+ patch)

The problem should be fixed. I renamed the __kfifo_must_check_helper() into __kfifo_uint_must_check_helper() and add a new __kfifo_int_must_check_helper() which is now used for kfifo_alloc().

A kernel linux 2.6.36-rc compiled with gcc 4.4.4 doesn't show the warning.

There a only two functions which return a int: kfifo_alloc() and kfifo_init(). The later one does not make use of __kfifo_int_must_check_helper(), but this could be change if required.

I will send the patch in a separate mail.

Am Mittwoch, den 20.10.2010, 11:56 -0700 schrieb Andrew Morton:

Quoted 20 lines
Quoted 10 lines · On Wed, 20 Oct 2010 19:37:02 +0100 Alan Cox <[email protected]> wrote:
Quoted 5 lines

Some versions of gcc mysteriously report:
drivers/char/n_gsm.c:1580: warning: ignoring return value of '__kfifo_must_check_helper', declared with attribute warn_unused_result

This warning can be eliminated by using a local variable for the returned result value, as suggested by Andrew Morton.

But that just hides the bug in the helper surely - the helper has type errors so needs fixing - not this. If you apply this then the underlying bug is just going to get forgotten

It won't get forgotten.

I agree that the warning is probably an artifact of the signedness thing. So for 2.6.36, disabling __kfifo_must_check_helper() will suffice. I'm sure that Stefani will fix up __kfifo_must_check_helper() for real for 2.6.37, then we can see if the warning reoccurs.