From: Stefan Richter <stefanr@s5r6.in-berlin.de>
To: "Leonidas ." <leonidas137@gmail.com>
Cc: Gleb Natapov <gleb@redhat.com>,
linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: How to check whether executing in atomic context?
Date: Wed, 14 Oct 2009 19:39:29 +0200 [thread overview]
Message-ID: <4AD60CD1.4000804@s5r6.in-berlin.de> (raw)
In-Reply-To: <f22d86810910140324l7b78ed57v7e7c774d5f8a1bd3@mail.gmail.com>
On 10/14/2009 12:24 PM, Leonidas . wrote:
> On Wed, Oct 14, 2009 at 3:13 AM, Gleb Natapov <gleb@redhat.com> wrote:
>> On Wed, Oct 14, 2009 at 02:21:22AM -0700, Leonidas . wrote:
>>> On Tue, Oct 13, 2009 at 11:36 PM, Leonidas . <leonidas137@gmail.com> wrote:
>>> > Hi List,
>>> >
>>> > I am working on a profiler kind of module, the exported apis of my module can be
>>> > called from process context and interrupt context as well. Depending on the
>>> > context I am called in, I need to call sleepable/nonsleepable variants
>>> > of my internal bookkeeping functions.
>>> >
>>> > I am aware of in_interrupt() call which can be used to check current
>>> > context and take action accordingly.
>>> >
>>> > Is there any api which can help figure out whether we are executing while hold a spinlock? I.e
>>> > an api which can help figure out sleepable/nonsleepable context? If it is not there, what can
>>> > be done for writing the same? Any pointers will be helpful.
[...]
>>> While searching through the sources, I found this,
>>>
>>> 97/*
>>> 98 * Are we running in atomic context? WARNING: this macro cannot
>>> 99 * always detect atomic context; in particular, it cannot know about
>>> 100 * held spinlocks in non-preemptible kernels. Thus it should not be
>>> 101 * used in the general case to determine whether sleeping is possible.
>>> 102 * Do not use in_atomic() in driver code.
>>> 103 */
>>> 104#define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_INATOMIC_BASE)
>>> 105
>>>
>>> this just complicates the matter, right? This does not work in general case but I think this
>>> will always work if the kernel is preemptible.
>>>
>>> Is there no way to write a generic macro?
[...]
>> Attached patch make in_atomic() to work for non-preemptable kernels too.
>> Doesn't look to big or scary.
>>
>> Disclaimer: tested only inside kvm guest 64bit, haven't measured overhead.
[...]
> Unbelievable! I was just thinking about the logic to achieve the same, and
> someone has already done this. Thanks for the patch.
I don't know whether Gleb's patch works or doesn't work as you require
it. But my opinion is that the recommendation "do not use in_atomic()
in driver code" is valid nevertheless.
Very often the better course of action is to change your API from
void my_routine()
{
if (in_atomic())
this;
else
that;
}
to either
void my_routine(bool can_sleep)
{
if (!can_sleep)
this;
else
that;
}
or to
void my_routine_atomic()
{
this;
}
void my_routine()
{
that;
}
In other words, let the caller of your routine tell it whether it's
atomic context or not.
Instead of a "bool can_sleep" argument, a "gfp_t flags" argument is
often used.
Or provide only the my_routine_atomic() variant if the difference to the
sleeping variant isn't huge.
--
Stefan Richter
-=====-==--= =-=- -===-
http://arcgraph.de/sr/
next prev parent reply other threads:[~2009-10-14 17:41 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-14 6:36 Leonidas .
2009-10-14 9:21 ` Leonidas .
2009-10-14 9:34 ` Leonidas .
2009-10-14 10:13 ` Gleb Natapov
2009-10-14 10:24 ` Leonidas .
2009-10-14 17:39 ` Stefan Richter [this message]
2009-10-14 19:11 ` Leonidas .
2009-10-14 19:15 ` Stefan Richter
2009-10-14 19:22 ` Samuel Thibault
2009-10-14 19:24 ` Leonidas .
2009-10-15 8:31 ` Leonidas .
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=4AD60CD1.4000804@s5r6.in-berlin.de \
--to=stefanr@s5r6.in-berlin.de \
--cc=gleb@redhat.com \
--cc=leonidas137@gmail.com \
--cc=linux-kernel@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®