Re: [PATCH 10/79] block: rust: add `command` getter to `Request`

From: Gary Guo

Date: Wed Jun 03 2026 - 10:44:24 EST


On Wed Jun 3, 2026 at 12:50 PM BST, Andreas Hindborg wrote:
> Alice Ryhl <aliceryhl@xxxxxxxxxx> writes:
>
>> On Mon, Feb 16, 2026 at 12:34:57AM +0100, Andreas Hindborg wrote:
>>> Add a method to extract the command operation code from a request. The
>>> command is obtained by masking the lower bits of `cmd_flags` as defined by
>>> `REQ_OP_BITS`. This allows Rust block drivers to determine the type of
>>> operation being requested.
>>>
>>> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
>>
>> With the nit below fixed:
>> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>>
>>> rust/kernel/block/mq/request.rs | 6 ++++++
>>> 1 file changed, 6 insertions(+)
>>>
>>> diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request.rs
>>> index b49197a0c66d7..0dd329ae93dfc 100644
>>> --- a/rust/kernel/block/mq/request.rs
>>> +++ b/rust/kernel/block/mq/request.rs
>>> @@ -78,6 +78,12 @@ pub(crate) unsafe fn aref_from_raw(ptr: *mut bindings::request) -> ARef<Self> {
>>> unsafe { ARef::from_raw(NonNull::new_unchecked(ptr.cast())) }
>>> }
>>>
>>> + /// Get the command identifier for the request
>>> + pub fn command(&self) -> u32 {
>>> + // SAFETY: By C API contract and type invariant, `cmd_flags` is valid for read
>>> + unsafe { (*self.0.get()).cmd_flags & ((1 << bindings::REQ_OP_BITS) - 1) }
>>
>> Nit: scope of unsafe
>>
>> unsafe { (*self.0.get()).cmd_flags } & ((1 << bindings::REQ_OP_BITS) - 1)
>
> The `&` is parsed as reference operator with this change. But we can do
> this:
>
> use core::ops::BitAnd;
> // SAFETY: By C API contract and type invariant, `cmd_flags` is valid for read
> unsafe { (*self.0.get()).cmd_flags }.bitand((1u32 << bindings::REQ_OP_BITS) - 1)

Wrapping the unsafe block in a parenthesis should do the trick.

Best,
Gary