From: Andreas Hindborg <andreas.hindborg@wdc.com>
To: "Björn Roy Baron" <bjorn3_gh@protonmail.com>
Cc: Dennis Dai <dzy.0424thu@gmail.com>,
Miguel Ojeda <ojeda@kernel.org>,
Alex Gaynor <alex.gaynor@gmail.com>,
Wedson Almeida Filho <wedsonaf@gmail.com>,
Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: rust nvme driver: potential sleep-in-atomic-context
Date: Thu, 03 Nov 2022 11:38:42 +0100 [thread overview]
Message-ID: <87mt98nwny.fsf@wdc.com> (raw)
In-Reply-To: <nyfz0H8A-5nGCgsAynqZyYtFlEcvPHDecY6c8MC840zNNdEWz2NSPPuZgE_VS7RnrTwFhd6aCDi2TXWh6YtqfUeUEWZvFCT7HjpO-Z_UPCk=@protonmail.com>
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
next prev parent reply other threads:[~2022-11-03 10:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2022-11-03 9:57 ` Andreas Hindborg
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=87mt98nwny.fsf@wdc.com \
--to=andreas.hindborg@wdc.com \
--cc=alex.gaynor@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dzy.0424thu@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.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