On 4/15/25 5:24 PM, Ming Lei wrote: >>> >>> Why is updating nr_requests related with removing hctx attributes? >>> >>> Can you explain the side effect in details? >> Thread 1: >> writing-to-blk-mq-sysfs-attribute-nr_requests >> -> queue_requests_store ==> freezes queue and acquires ->elevator_lock >> -> blk_mq_update_nr_requests >> -> blk_mq_tag_update_depth >> -> blk_mq_alloc_map_and_rqs >> -> blk_mq_alloc_rq_map >> -> blk_mq_init_tags ==> updates ->nr_tags and ->nr_reserved_tags >> >> Thread2: >> blk_mq_update_nr_hw_queues >> -> __blk_mq_update_nr_hw_queues >> -> blk_mq_realloc_tag_set_tags >> -> __blk_mq_alloc_map_and_rqs >> -> blk_mq_alloc_map_and_rqs >> -> blk_mq_alloc_rq_map >> -> blk_mq_init_tags ==> updates ->nr_tags and ->nr_reserved_tags >> >> Thread 3: >> reading-hctx-sysfs-attribute-nr_tags >> -> blk_mq_hw_sysfs_show ==> acquires ->elevaor_lock >> -> blk_mq_hw_sysfs_nr_tags_show ==> access nr_tags >> >> Thread 4: >> reading-hctx-sysfs-attribute-nr_reserved_tags >> -> blk_mq_hw_sysfs_show ==> acquires ->elevaor_lock >> -> blk_mq_hw_sysfs_nr_reserved_tags_show ==> access nr_reserved_tags > > `hctx->tags` is guaranteed to be live if above ->show() method, and the > elevator lock is actually not needed, which isn't supposed to protect > hctx->tags too. > I think, the ->elavtor_lock would still be needed for protecting updates to hctx->tags from thread # 1 above and simultaneously reading the hctx->tags from thread #3 and #4 above. >> >> As we can see above, ->nr_tags and ->nr_reserved_tags are also exported >> to userspace using hctx sysfs attributes (nr_tags and nr_reserved_tags). >> >> So my point was, >> #1 For alleviating race between nr_hw_queues and nr_requests update, >> we need protection (probably using srcu lock) so that ->nr_tags >> and ->nr_reserved_tags are not updated simultaneously. >> >> #2 How could we protect race between thread 3 and thread 2 above or >> race between thread 4 and thread 2 above? > > blk_mq_update_nr_hw_queues() calls blk_mq_sysfs_unregister_hctxs() first, > then user can not see the above attributes before calling blk_mq_sysfs_register_hctxs(). > > So there isn't the race. > >> >>> >>>> Maybe we also want to protect blk_mq_update_nr_requests >>>> with srcu read lock (set->update_nr_hwq_srcu) so that it couldn't run while >>>> blk_mq_update_nr_hw_queues is in progress? >>> >>> Yeah, agree, and it can be one new patch for covering race between >>> blk_mq_update_nr_requests and blk_mq_update_nr_hw_queues, the point is just >>> that nr_hw_queues is being changed, and not related with removing hctx >>> attributes, IMO. >>> >> Please note that blk_mq_update_nr_requests also updates q->nr_requests, > > blk_mq_update_nr_requests() uses nr_hw_queues, so there is race between > blk_mq_update_nr_requests() and blk_mq_update_nr_hw_queues(). > Okay so I believe, this could be protected using srcu lock. >> however looking at all code paths which updates this value is already >> protected with ->elevator_lock. So the only thing which worries me >> about updates of ->nr_tags and ->nr_reserved tags as shown above. > > As I mentioned, there isn't such race. Yes agreed, there's no race between thread 3 and thread 2 or thread 4 and thread 2. Thanks, --Nilay