From: Josh Triplett <josh@joshtriplett.org>
To: ecashin@coraid.com
Cc: linux-sparse@vger.kernel.org
Subject: Re: "unexpected unlock" when unlocking, conditional, lock in loop
Date: Sat, 6 Oct 2012 13:21:02 -0700 [thread overview]
Message-ID: <20121006202102.GA28179@leaf> (raw)
In-Reply-To: <1349552876.20963@cat.he.net>
On Sat, Oct 06, 2012 at 12:47:56PM -0700, ecashin@coraid.com wrote:
> Hi. I have a function that enters with a lock held and does
> an unlock inside a loop.
>
> Sparse 0.4.4 is fine with this function until I introduce a conditional
> between the unlock and the next lock. In the minimal example below,
> changing the "#if 1" to "#if 0" makes sparse generate the warning
> below:
>
> cd ~/git/linux && PATH=/opt/bin:$PATH make drivers/block/aoe/aoe.ko C=1
> make[1]: Nothing to be done for `all'.
> make[1]: Nothing to be done for `relocs'.
> CHK include/linux/version.h
> CHK include/generated/utsrelease.h
> CALL scripts/checksyscalls.sh
> CHECK drivers/block/aoe/demo.c
> /build/ecashin/git/linux/arch/x86/include/asm/spinlock.h:81:9: warning: context imbalance in 'demofn' - unexpected unlock
> CC [M] drivers/block/aoe/demo.o
> LD [M] drivers/block/aoe/aoe.o
> MODPOST 1 modules
> CC drivers/block/aoe/aoe.mod.o
> LD [M] drivers/block/aoe/aoe.ko
>
> I'm using 3.6.0-rc7 kernel sources.
>
> Granted, I'm unusually tired today, but I can't think of a way that
> conditionally printing a warning has changed the locking, so I
> could use some help in determining whether this is a sparse bug
> that might be fixed, one that I have to work around, or some
> confusion of mine.
>
> /* demo.c */
> #include <linux/netdevice.h>
>
> static spinlock_t lk;
> static struct sk_buff_head q;
> int demofn(void);
>
> /* enters and returns with lk held */
> int demofn(void)
> {
> struct sk_buff *skb;
>
> while ((skb = skb_dequeue(&q))) {
> spin_unlock_irq(&lk);
> #if 1
> dev_queue_xmit(skb);
> #else
> if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit())
> pr_warn("informative warning\n");
> #endif
> spin_lock_irq(&lk);
> }
> return 0;
> }
Sparse should *always* generate a context warning here; odd that it does
not in both cases.
The right fix: annotate the function to explicitly say it starts and
stops with that lock held. That should make the warning go away in
both cases.
- Josh Triplett
next prev parent reply other threads:[~2012-10-06 20:21 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-06 19:47 "unexpected unlock" when unlocking, conditional, lock in loop ecashin
2012-10-06 20:21 ` Josh Triplett [this message]
2012-10-07 1:56 ` Ed Cashin
2012-10-07 2:39 ` Josh Triplett
2012-10-07 12:49 ` Ed Cashin
2012-10-07 19:45 ` Josh Triplett
2012-10-07 21:28 ` Ed Cashin
2012-10-07 23:30 ` Josh Triplett
2012-10-08 0:35 ` Ed Cashin
2012-10-08 2:01 ` Josh Triplett
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20121006202102.GA28179@leaf \
--to=josh@joshtriplett.org \
--cc=ecashin@coraid.com \
--cc=linux-sparse@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.