linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Cc: Song Liu <songliubraving@fb.com>,
	"linux-sparse@vger.kernel.org" <linux-sparse@vger.kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@kernel.org>
Subject: Re: "warning: context imbalance" in kernel/bpf/hashtab.c
Date: Sat, 7 Nov 2020 16:52:04 -0800	[thread overview]
Message-ID: <CAHk-=wh4bx8A8dHnX612MsDO13st6uzAz1mJ1PaHHVevJx_ZCw@mail.gmail.com> (raw)
In-Reply-To: <20201108004159.gwbwirczpltkm7c3@ltop.local>

On Sat, Nov 7, 2020 at 4:42 PM Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
>
> But isn't a lot of code written to explicitly support this, with an
> argument or some other condition selecting between the locked mode
> or the unlocked mode?

A lot? No. I've actively discouraged it.

It does exist - don't get me wrong - but it shouldn't be the normal pattern.

> This one is fine for Sparse
>         static inline int cond_lock1(void)
>         {
>                 if (complex_cond()) {
>                         take_lock();
>                         minor();
>                         return 1;
>                 }
>                 return 0;
>         }
>         static void ok1(void)
>         {
>                 int cond = cond_lock1();
>                 if (cond) {
>                         major();
>                         drop_lock();
>                 }
>         }

That _is_ a somewhat common case, and it's basically the "first helper
tells the caller that it took the lock".

The _really_ basic version of that is the various "trylock()" things,
which has that "__cond_lock()" macro in the kernel for it, so that
sparse can catch that very special case.

That said, it's still not the _common_ case, which is just that the
code is straightforward

    take_lock();
    do something under the lock
    drop_lock();

> The next one corresponds to the situation Song Liu reported
>         static inline int cond_lock2(void)
>         {
>                 if (!complex_cond())
>                         return -1;
>
>                 take_lock();
>                 minor();
>                 return 0;
>         }
>         static int okish(void)
>         {
>                 int cond = cond_lock2();
>                 if (cond)
>                         return 0;
>                 major();
>                 drop_lock();
>                 return 1;
>         }

Yeah, this is a more complex version of the same.

Clearly sparse doesn't grok it today, but the fact that your patches
make sparse able to track it is a good improvement.

> The one that really annoys me is this one. It's very simple and, as
> far as I know, quite common in the kernel but kinda hopeless.
>         static void ko(int cond)
>         {
>                 if (cond)
>                         take_lock();
>                 major();
>                 if (cond)
>                         drop_lock();
>         }

This really is invalid code. "major()" is done in two different lock contexts.

Sparse _should_ complain.

             Linus

      reply	other threads:[~2020-11-08  0:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-05 19:34 "warning: context imbalance" in kernel/bpf/hashtab.c Song Liu
2020-11-05 21:13 ` Luc Van Oostenryck
2020-11-05 21:18   ` Linus Torvalds
2020-11-05 21:50     ` Luc Van Oostenryck
2020-11-06 17:30       ` Song Liu
2020-11-06 17:45         ` Linus Torvalds
2020-11-06 19:04           ` Song Liu
2020-11-06 19:44             ` Linus Torvalds
2020-11-06 22:19               ` Luc Van Oostenryck
2020-11-06 22:46                 ` Linus Torvalds
2020-11-07  1:32                   ` Luc Van Oostenryck
2020-11-07 19:39                     ` Linus Torvalds
2020-11-07 20:09                       ` Luc Van Oostenryck
2020-11-07 20:33                         ` Linus Torvalds
2020-11-07 21:23                           ` Luc Van Oostenryck
2020-11-07 22:20                             ` Linus Torvalds
2020-11-08  0:41                               ` Luc Van Oostenryck
2020-11-08  0:52                                 ` Linus Torvalds [this message]

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='CAHk-=wh4bx8A8dHnX612MsDO13st6uzAz1mJ1PaHHVevJx_ZCw@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-sparse@vger.kernel.org \
    --cc=luc.vanoostenryck@gmail.com \
    --cc=songliubraving@fb.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).