Re: [PATCH] linux/compiler.h: Add __must_hold macro for functionscalled with a lock held

From: Ed Cashin
Date: Tue Oct 09 2012 - 16:35:31 EST


On Oct 9, 2012, at 4:06 PM, Andrew Morton wrote:

> On Sun, 7 Oct 2012 19:06:10 -0700
> Josh Triplett <josh@xxxxxxxxxxxxxxxx> wrote:
>
>> linux/compiler.h has macros to denote functions that acquire or release
>> locks, but not to denote functions called with a lock held that return
>> with the lock still held. Add a __must_hold macro to cover that case.
>
> hum. How does this work? Any code examples and sample sparse output?
> Does it apply to all lock types, etc?

I accidentally found a bug in sparse where sparse should have
been complaining about the locking in the aoe driver's
aoenet.c:tx function, which enters with the txlock spinlock held
and releases and reacquires it inside a loop.

When I modified the loop contents between lock release and
acquisition in a patch that I'm preparing now, sparse began
complaining about context imbalance. (Before the modifications
to the tx function, sparse was exhibiting a bug by *not*
complaining.) Here's the complaint:

CHECK drivers/block/aoe/aoenet.c
/build/ecashin/git/linux/arch/x86/include/asm/spinlock.h:81:9: warning: context imbalance in 'tx' - unexpected unlock
CC [M] drivers/block/aoe/aoenet.o

... although I used a smaller file to hone in on the issue when
discussing it on the linux-sparse mailing list:

http://thread.gmane.org/gmane.comp.parsers.sparse/3091

Josh Triplett suggested I add an annotation telling sparse that
the function enters and exits with the lock held, but after
reading the sparse man page, where the context feature is
described, it looked like that meant specifying a 1 for both
parameters: __attribute__((context(x,1,1))), but there was no
macro for that.

The new patch from Josh Triplett adds the macro that I can use to
keep sparse informed about the locking requirements for the tx
function.

> IOW, where is all this stuff documented?

It wasn't real easy to find it, but it's basically something you
can infer from reading include/linux/compiler.h and the sparse.1
man page that's included with sparse. I think a brief mention in
Documentation/sparse.txt of the relationship between the
sparse "context" feature and the locking annotations like
__acquires and __must_hold would be a nice addition.

I can take a stab at it if folks agree.

--
Ed Cashin
ecashin@xxxxxxxxxx


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/