Re: [PATCH] lock assertion macros for 2.5.31

Andrew Morton (akpm@zip.com.au)
Wed, 21 Aug 2002 11:40:10 -0700


Jesse Barnes wrote:
>
> On Mon, Aug 12, 2002 at 04:12:41PM -0700, Andrew Morton wrote:
> > ...
> > #define might_sleep() BUG_ON(preempt_count())
> >
> > _this_ would catch numerous bugs, including code which is not buggy
> > in 2.4, but became buggy when wild-eyed loonies changed core kernel
> > rules without even looking at what drivers were doing (rant).
> >
> > I expect something like this will fall out of the wash soon, at
> > least for preemptible kernels.
>
> Is it really that simple?

It sure is:

/**
* in_atomic_region() - determine whether it is legal to perform a context
* switch
*
* The in_atomic_region() predicate returns true if the current task is
* executing atomically, and may not perform a context switch.
*
* If preemption is enabled, in_atomic_region() is most accurate, because it
* returns true if this task has taken any spinlocks.
*
* If preemption is disabled then there is no spinlocking record available, and
* we can only look at the interrupt state.
*
* If the task has taken a lock_kernel() then it is still legal to perform a
* context switch.
*/
#ifdef CONFIG_PREEMPT
#define in_atomic_region() (preempt_count() - !!(current->lock_depth + 1))
#else
#define in_atomic_region() in_interrupt()
#endif

/**
* may_sleep() - debugging check for possible illegal scheduling.
*
* may_sleep() is to be used in code paths which _may_ perform a context switch.
* It will force a BUG if the caller is executing in an atomic region.
*/
extern void __in_atomic_region(char *file, int line);
#define may_sleep() \
do { \
if (in_atomic_region()) \
__in_atomic_region(__FILE__, __LINE__); \
} while (0)

> Maybe it should go into sched.h sometime
> soon? I guess the real work is sprinkling it in all the places where
> it needs to go.

Well I added checks just to kmalloc, kmem_cache_alloc, __alloc_pages
and saw a shower of bloopers during bootup. Such as drivers/ide/probe.c:init_irq()
calling request_irq() inside ide_lock.

> Anyway, here's an updated version of the lock assertion patch.

Well I like it. It's unintrusive, imparts useful info to the reader
and checks stuff at runtime.

> Should
> it be split into two patches, one that implements the macros and
> another that puts checks everywhere?

I don't think it needs splitting. You have the core infrastructure plus
a couple of example applications.

> Should I add a small doc to
> Documentation/ (maybe the might_sleep() could be documented there
> too)?

These things are self-evident and even self-checking. They don't need
supporting documentation. I'll put out a test tree RSN, include this
in it.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/