public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* rust nvme driver: potential sleep-in-atomic-context
@ 2022-11-03  6:12 Dennis Dai
  2022-11-03  9:37 ` Miguel Ojeda
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dennis Dai @ 2022-11-03  6:12 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, rust-for-linux, linux-kernel

The rust nvme driver [1] (which is still pending to be merged into
mainline [2]) has a potential sleep-in-atomic-context bug.

The potential buggy code is below

    // drivers/block/nvme.rs:192
    dev.queues.lock().io.try_reserve(nr_io_queues as _)?;
    // drivers/block/nvme.rs:227
    dev.queues.lock().io.try_push(io_queue.clone())?;

The queues field is wrapped in SpinLock, which means that we cannot
sleep (or indirectly call any function that may sleep) when the lock
is held.
However try_reserve function may indirectly call krealloc with a
sleepable flag GFP_KERNEL (that's default behaviour of the global rust
allocator).
The the case is similar for try_push.

I wonder if the bug could be confirmed.


[1] https://github.com/metaspace/rust-linux/commit/d88c3744d6cbdf11767e08bad56cbfb67c4c96d0
[2] https://lore.kernel.org/lkml/202210010816.1317F2C@keescook/

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

* Re: rust nvme driver: potential sleep-in-atomic-context
  2022-11-03  6:12 rust nvme driver: potential sleep-in-atomic-context Dennis Dai
@ 2022-11-03  9:37 ` Miguel Ojeda
  2022-11-03  9:38 ` Björn Roy Baron
  2022-11-03  9:57 ` Andreas Hindborg
  2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2022-11-03  9:37 UTC (permalink / raw)
  To: Dennis Dai
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, rust-for-linux, linux-kernel,
	Wedson Almeida Filho, Andreas Hindborg

On Thu, Nov 3, 2022 at 7:12 AM Dennis Dai <dzy.0424thu@gmail.com> wrote:
>
> The rust nvme driver [1] (which is still pending to be merged into
> mainline [2]) has a potential sleep-in-atomic-context bug.
>
>     dev.queues.lock().io.try_reserve(nr_io_queues as _)?;

Cc'ing Andreas and fixing Wedson's email. Note that this was written
when it wasn't decided how the `try_*` methods would know about the
flags.

Cheers,
Miguel

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

* Re: rust nvme driver: potential sleep-in-atomic-context
  2022-11-03  6:12 rust nvme driver: potential sleep-in-atomic-context Dennis Dai
  2022-11-03  9:37 ` Miguel Ojeda
@ 2022-11-03  9:38 ` Björn Roy Baron
  2022-11-03 10:38   ` Andreas Hindborg
  2022-11-03  9:57 ` Andreas Hindborg
  2 siblings, 1 reply; 5+ messages in thread
From: Björn Roy Baron @ 2022-11-03  9:38 UTC (permalink / raw)
  To: Dennis Dai
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, rust-for-linux, linux-kernel

On Thursday, November 3rd, 2022 at 07:12, Dennis Dai <dzy.0424thu@gmail.com> wrote:


> The rust nvme driver [1] (which is still pending to be merged into
> mainline [2]) has a potential sleep-in-atomic-context bug.
> 
> The potential buggy code is below
> 
> // drivers/block/nvme.rs:192
> dev.queues.lock().io.try_reserve(nr_io_queues as _)?;
> // drivers/block/nvme.rs:227
> dev.queues.lock().io.try_push(io_queue.clone())?;
> 
> The queues field is wrapped in SpinLock, which means that we cannot
> sleep (or indirectly call any function that may sleep) when the lock
> is held.
> However try_reserve function may indirectly call krealloc with a
> sleepable flag GFP_KERNEL (that's default behaviour of the global rust
> allocator).
> The the case is similar for try_push.
> 
> I wonder if the bug could be confirmed.
> 
> 
> [1] https://github.com/metaspace/rust-linux/commit/d88c3744d6cbdf11767e08bad56cbfb67c4c96d0
> [2] https://lore.kernel.org/lkml/202210010816.1317F2C@keescook/

setup_io_queues is only called by dev_add which in turn is only called NvmeDevice::probe. This last function is responsible for creating the &Ref<DeviceData> that ends up being passed to setup_io_queues. It doesn't seem like any reference is passed to another thread between &Ref<DeviceData>. As such no other thread can block on the current thread due to holding the lock. As far as I understand this means that sleeping while the lock is held is harmless. I think it would be possible to replace the &Ref<DeviceData> argument with an Pin<&mut DeviceData> argument by moving the add_dev call to before Ref::<DeviceData>::from(data). This would make it clear that only the current thread holds a reference and would also allow using a method like get_mut [1] to get a reference to the protected data without actually locking the spinlock as it is statically enforced that nobody can else can hold the lock. It seems that get_mut is missing from all of the locks offered in the kernel crate. I opened an issue for this. [2]

[1]: https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html#method.get_mut
[2]: https://github.com/Rust-for-Linux/linux/issues/924

Cheers,
Björn

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

* Re: rust nvme driver: potential sleep-in-atomic-context
  2022-11-03  6:12 rust nvme driver: potential sleep-in-atomic-context Dennis Dai
  2022-11-03  9:37 ` Miguel Ojeda
  2022-11-03  9:38 ` Björn Roy Baron
@ 2022-11-03  9:57 ` Andreas Hindborg
  2 siblings, 0 replies; 5+ messages in thread
From: Andreas Hindborg @ 2022-11-03  9:57 UTC (permalink / raw)
  To: Dennis Dai
  Cc: Andreas Hindborg, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Boqun Feng, Gary Guo, Björn Roy Baron, rust-for-linux,
	linux-kernel


Hi Dennis,

Dennis Dai <dzy.0424thu@gmail.com> writes:

> The rust nvme driver [1] (which is still pending to be merged into
> mainline [2]) has a potential sleep-in-atomic-context bug.
>
> The potential buggy code is below
>
>     // drivers/block/nvme.rs:192
>     dev.queues.lock().io.try_reserve(nr_io_queues as _)?;
>     // drivers/block/nvme.rs:227
>     dev.queues.lock().io.try_push(io_queue.clone())?;
>
> The queues field is wrapped in SpinLock, which means that we cannot
> sleep (or indirectly call any function that may sleep) when the lock
> is held.
> However try_reserve function may indirectly call krealloc with a
> sleepable flag GFP_KERNEL (that's default behaviour of the global rust
> allocator).
> The the case is similar for try_push.
>
> I wonder if the bug could be confirmed.

Nice catch, I was not aware of that one. I will add a TODO. Did you
manage to trigger this bug or did you find it by review?

I am not sure if it has been decided how to pass flags to allocations
yet. There is a discussion about the interface for Box here [1] and
there is also some discussion on the list [2]. For reference, I use an
atomic box allocation here [3].

The NVMe driver is very much a prototype and I expect there are many
bugs like this still in it. So while I am not surprised, really I
appreciate the report :)

[1] https://github.com/Rust-for-Linux/linux/pull/815
[2] https://lore.kernel.org/rust-for-linux/Yyr5pKpjib%2Fyqk5e@kroah.com/T/#mb55cf54067002d503ca63c5ad0688d55c6184cca
[3] https://github.com/metaspace/rust-linux/blob/nvme/drivers/block/nvme_mq.rs#L261

Best regards,
Andreas Hindborg

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

* Re: rust nvme driver: potential sleep-in-atomic-context
  2022-11-03  9:38 ` Björn Roy Baron
@ 2022-11-03 10:38   ` Andreas Hindborg
  0 siblings, 0 replies; 5+ messages in thread
From: Andreas Hindborg @ 2022-11-03 10:38 UTC (permalink / raw)
  To: Björn Roy Baron
  Cc: Dennis Dai, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho,
	Boqun Feng, Gary Guo, rust-for-linux, linux-kernel


Björn Roy Baron <bjorn3_gh@protonmail.com> writes:

> On Thursday, November 3rd, 2022 at 07:12, Dennis Dai <dzy.0424thu@gmail.com> wrote:
>
>
>> The rust nvme driver [1] (which is still pending to be merged into
>> mainline [2]) has a potential sleep-in-atomic-context bug.
>>
>> The potential buggy code is below
>>
>> // drivers/block/nvme.rs:192
>> dev.queues.lock().io.try_reserve(nr_io_queues as _)?;
>> // drivers/block/nvme.rs:227
>> dev.queues.lock().io.try_push(io_queue.clone())?;
>>
>> The queues field is wrapped in SpinLock, which means that we cannot
>> sleep (or indirectly call any function that may sleep) when the lock
>> is held.
>> However try_reserve function may indirectly call krealloc with a
>> sleepable flag GFP_KERNEL (that's default behaviour of the global rust
>> allocator).
>> The the case is similar for try_push.
>>
>> I wonder if the bug could be confirmed.
>>
>>
>> [1] https://github.com/metaspace/rust-linux/commit/d88c3744d6cbdf11767e08bad56cbfb67c4c96d0
>> [2] https://lore.kernel.org/lkml/202210010816.1317F2C@keescook/
>
> setup_io_queues is only called by dev_add which in turn is only called
> NvmeDevice::probe. This last function is responsible for creating the
> &Ref<DeviceData> that ends up being passed to setup_io_queues. It doesn't seem
> like any reference is passed to another thread between &Ref<DeviceData>. As such
> no other thread can block on the current thread due to holding the lock. As far
> as I understand this means that sleeping while the lock is held is harmless. I
> think it would be possible to replace the &Ref<DeviceData> argument with an
> Pin<&mut DeviceData> argument by moving the add_dev call to before
> Ref::<DeviceData>::from(data). This would make it clear that only the current
> thread holds a reference and would also allow using a method like get_mut [1] to
> get a reference to the protected data without actually locking the spinlock as
> it is statically enforced that nobody can else can hold the lock. 

I think you are right. The lock is just there to allow interior
mutability of the queue arrays. I could try to shuffle stuff around and
move queue setup before converting `data` to a Ref. That should be fine
as far as I can tell.

BR Andreas

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

end of thread, other threads:[~2022-11-03 10:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-03  6:12 rust nvme driver: potential sleep-in-atomic-context Dennis Dai
2022-11-03  9:37 ` Miguel Ojeda
2022-11-03  9:38 ` Björn Roy Baron
2022-11-03 10:38   ` Andreas Hindborg
2022-11-03  9:57 ` Andreas Hindborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox