Linux block layer
 help / color / mirror / Atom feed
From: Damien Le Moal <dlemoal@kernel.org>
To: Yongpeng Yang <yangyongpeng.storage@outlook.com>,
	Yongpeng Yang <yangyongpeng.storage@gmail.com>,
	Christoph Hellwig <hch@lst.de>, Jens Axboe <axboe@kernel.dk>,
	Ming Lei <ming.lei@redhat.com>
Cc: linux-block@vger.kernel.org, Yongpeng Yang <yangyongpeng@xiaomi.com>
Subject: Re: [PATCH 1/1] loop: convert lo_state to atomic_t type to ensure atomic state checks in queue_rq path
Date: Mon, 15 Dec 2025 16:44:09 +0900	[thread overview]
Message-ID: <caa412f0-b4f4-4269-a584-288ad3fbbe3e@kernel.org> (raw)
In-Reply-To: <SEZPR02MB55202AF5E15D574D7E157A9799ADA@SEZPR02MB5520.apcprd02.prod.outlook.com>

On 12/15/25 16:34, Yongpeng Yang wrote:
> 
> On 12/15/25 15:12, Damien Le Moal wrote:
>> On 12/15/25 15:54, Yongpeng Yang wrote:
>>> From: Yongpeng Yang <yangyongpeng@xiaomi.com>
>>>
>>> lo_state is currently defined as an int, which does not guarantee
>>> atomicity for state checks. In the queue_rq path, ensuring correct state
>>> checks requires holding lo->lo_mutex, which may increase I/O submission
>>> latency. This patch converts lo_state to atomic_t type. The main changes
>>> are:
>>> 1. Updates to lo_state still require holding lo->lo_mutex, since the
>>> state must be validated before modification, and the lock ensures that
>>> no concurrent operation can change the state.
>>> 2. Read-only accesses to lo_state no longer require holding lo->lo_mutex.
>>>
>>> This allows atomic state checks in the queue_rq fast path while avoiding
>>> unnecessary locking overhead.
>>
>> Code like:
>>
>> if (loop_device_get_state(lo) != Lo_bound)
>>
>> is absolutely *not* atomic, since the state can change in between the atomic
>> read and the comparison instruction. So this is not about atomicity, it is about
>> not reading garbage from the state field if there is a load and a store
>> concurrently executed on different CPUs (that happening depends on the CPU
>> architecture though).
> 
> Yes, I hadn’t considered that before.
> 
>>
>> As Christoph suggested, using "data_race()" may be enough to silence code
>> checkers. Or use READ_ONCE() WRITE_ONCE() for the state.
> 
> Considering the earlier point that the queue_rq check of lo->lo_state is
> just an optimization, using READ_ONCE() seems more appropriate. As noted
> in the comment for data_race(), for accesses without locking, would
> data_race(READ_ONCE(lo->lo_state)) make more sense here?

Yes, I think it is OK.


-- 
Damien Le Moal
Western Digital Research

  reply	other threads:[~2025-12-15  7:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-15  6:54 [PATCH 1/1] loop: convert lo_state to atomic_t type to ensure atomic state checks in queue_rq path Yongpeng Yang
2025-12-15  7:12 ` Damien Le Moal
2025-12-15  7:34   ` Yongpeng Yang
2025-12-15  7:44     ` Damien Le Moal [this message]
2025-12-15  8:19       ` Yongpeng Yang

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=caa412f0-b4f4-4269-a584-288ad3fbbe3e@kernel.org \
    --to=dlemoal@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=ming.lei@redhat.com \
    --cc=yangyongpeng.storage@gmail.com \
    --cc=yangyongpeng.storage@outlook.com \
    --cc=yangyongpeng@xiaomi.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