mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Q: semaphore and kmalloc(GFP_KERNEL)?
@ 2009-05-11  2:30 Tetsuo Handa
  2009-05-11  2:56 ` KOSAKI Motohiro
  0 siblings, 1 reply; 5+ messages in thread
From: Tetsuo Handa @ 2009-05-11  2:30 UTC (permalink / raw)
  To: linux-kernel

Hello.
I got a question.

When we use kmalloc(GFP_KERNEL), we cannot use spinlock.
We use semaphore instead.

What happens if a writer process got into sleep state at kmalloc()
without releasing a rw_semaphore for writing,

    down_write(&sem);
    list_for_each()
        ...;
    ptr = kmalloc(size, GFP_KERNEL);
    list_add();
    up_write(&sem);

and then reader processes tried to hold that rw_semaphore?

    down_read(&sem);
    list_for_each()
        ...;
    up_read(&sem);

Can the kernel detect that we need to let kmalloc() fail and
wake up the writer process so that the writer process releases
the rw_semaphore and reader processes can continue?

(If the kernel can't detect, I worry that we experience
all processes sleeping for undefined duration.)

Regards.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Q: semaphore and kmalloc(GFP_KERNEL)?
  2009-05-11  2:30 Q: semaphore and kmalloc(GFP_KERNEL)? Tetsuo Handa
@ 2009-05-11  2:56 ` KOSAKI Motohiro
  2009-05-11  3:19   ` Tetsuo Handa
  0 siblings, 1 reply; 5+ messages in thread
From: KOSAKI Motohiro @ 2009-05-11  2:56 UTC (permalink / raw)
  To: Tetsuo Handa; +Cc: kosaki.motohiro, linux-kernel

> Hello.
> I got a question.
> 
> When we use kmalloc(GFP_KERNEL), we cannot use spinlock.
> We use semaphore instead.
> 
> What happens if a writer process got into sleep state at kmalloc()
> without releasing a rw_semaphore for writing,
> 
>     down_write(&sem);
>     list_for_each()
>         ...;
>     ptr = kmalloc(size, GFP_KERNEL);
>     list_add();
>     up_write(&sem);
> 
> and then reader processes tried to hold that rw_semaphore?
> 
>     down_read(&sem);
>     list_for_each()
>         ...;
>     up_read(&sem);
> 
> Can the kernel detect that we need to let kmalloc() fail and
> wake up the writer process so that the writer process releases
> the rw_semaphore and reader processes can continue?

it can't. Plus it shouldn't.
some caller don't want kmalloc() failure.


> (If the kernel can't detect, I worry that we experience
> all processes sleeping for undefined duration.)





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Q: semaphore and kmalloc(GFP_KERNEL)?
  2009-05-11  2:56 ` KOSAKI Motohiro
@ 2009-05-11  3:19   ` Tetsuo Handa
  2009-05-11  3:21     ` KOSAKI Motohiro
  0 siblings, 1 reply; 5+ messages in thread
From: Tetsuo Handa @ 2009-05-11  3:19 UTC (permalink / raw)
  To: kosaki.motohiro; +Cc: linux-kernel

KOSAKI Motohiro wrote:
> > Can the kernel detect that we need to let kmalloc() fail and
> > wake up the writer process so that the writer process releases
> > the rw_semaphore and reader processes can continue?
> 
> it can't. Plus it shouldn't.
> some caller don't want kmalloc() failure.

Thanks.
Then, should I move kmalloc(GFP_KERNEL) to outside the lock whenever possible?
(I worry some lock held by the caller interferes kmalloc(GFP_KERNEL).)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Q: semaphore and kmalloc(GFP_KERNEL)?
  2009-05-11  3:19   ` Tetsuo Handa
@ 2009-05-11  3:21     ` KOSAKI Motohiro
  2009-05-11  3:26       ` Tetsuo Handa
  0 siblings, 1 reply; 5+ messages in thread
From: KOSAKI Motohiro @ 2009-05-11  3:21 UTC (permalink / raw)
  To: Tetsuo Handa; +Cc: kosaki.motohiro, linux-kernel

> KOSAKI Motohiro wrote:
> > > Can the kernel detect that we need to let kmalloc() fail and
> > > wake up the writer process so that the writer process releases
> > > the rw_semaphore and reader processes can continue?
> > 
> > it can't. Plus it shouldn't.
> > some caller don't want kmalloc() failure.
> 
> Thanks.
> Then, should I move kmalloc(GFP_KERNEL) to outside the lock whenever possible?

Yes. almost driver does it.
this is generic issue of nested locking.

> (I worry some lock held by the caller interferes kmalloc(GFP_KERNEL).)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Q: semaphore and kmalloc(GFP_KERNEL)?
  2009-05-11  3:21     ` KOSAKI Motohiro
@ 2009-05-11  3:26       ` Tetsuo Handa
  0 siblings, 0 replies; 5+ messages in thread
From: Tetsuo Handa @ 2009-05-11  3:26 UTC (permalink / raw)
  To: kosaki.motohiro; +Cc: linux-kernel

KOSAKI Motohiro wrote:
> > Then, should I move kmalloc(GFP_KERNEL) to outside the lock whenever possible?
> 
> Yes. almost driver does it.
> this is generic issue of nested locking.

I see. I'll try to rewrite.
Thank you.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-05-11  3:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-11  2:30 Q: semaphore and kmalloc(GFP_KERNEL)? Tetsuo Handa
2009-05-11  2:56 ` KOSAKI Motohiro
2009-05-11  3:19   ` Tetsuo Handa
2009-05-11  3:21     ` KOSAKI Motohiro
2009-05-11  3:26       ` Tetsuo Handa

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®