From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.167 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796034018; b=3YCxiRcLyMNUi7+E97DyZb2aJt57wWs+2TFU+9b955nURJL50hzZga4+Nr6UDFCAFNrk F4ctKRuaJ2jg+qFjZ76TYQLUNUG5c3TiL9/4L+KwQHEImS122n7a8iYT8DNKri4XNcewt 7Z3dQvgPVhBlgiR6HAeObSsY/VoL9HhgtKw5neK1NHQn4m2ivaR2F4F2yX31WaL6MPcMw ZfF6sxbIZ1WG//eCWrpq0XQjinqAupmcYdOxNLkO2syW6N8tzJMJCfDkz/vDg6/fQQeb5 jJfxwKyPdyJVVFAb1tf3z/+rQ8lq+dhh+CKPEXIO0LBUEsSWWqAub7rdOmBmevtxVKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796034018; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Cc:Subject:To:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=AoPps37o/XNI7lEgSfINfd58w62OFVRKhgUQArYfhgM=; b=dQvM18l9kfcYwND4neM0KjJw3rB0Y9tv8UXCCCEh5IduQ5lUHOSbWQI7aWDuHPmlQ7lD iU/tEna/DkRsmc+tq4vcTmrSUWC/Xu+pfhIJ2F1V9StRjoFA9qMJm1btwrayM2znWbMCO Ao2uJVknBaS40m3RKsrzCpXREiWgz5zTcnRW4RXGZNdurkxHCvxhNieQMMiLn5azUHiJT A/K0978iJ6qK60dYg9p+4EnSpcPxpAaAHKGwj9UsvkOVbAoM1V0jstj0ioBOLFx0XgcTc 1OLps6kuykImX5+5cmbdX/Iq9Cq+hC3JQJL5vkGqIjA28dvVMrlZZno/oYGeg5qp6xw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=wpoD05k5; arc=none smtp.remote-ip=95.215.58.167 Received: from mta1.migadu.com (out-167.mta1.migadu.com [95.215.58.167]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73A77606A32A for ; Mon, 30 Nov 2026 10:20:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 73A77606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=wpoD05k5 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=/ceRecYart57/MVFkIT0Liw3OHaj6pTiazo1MqPhIA4=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796034014; v=1; x=1796638814; b=wpoD05k5b4t+znVT9Ctfh7Q8A3K7o8T1bkSr9Z/xL9ycm9Syu02zQEApnWF8VACGeflPZ+MR dMcDf2rHRbv4X7Zx1QMNwVRoFZgQrulBIIColAiWM2UwjTg0ApZry2XE0GE/hZty5+Rj5z2gFzR xobIkuUxZyGx5vTWnSEuM1Pg= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 6e97d8d445612790; Mon, 30 Nov 2026 10:20:13 +0000 X-Mizu-Trace-ID: 6e97d8d445612790 X-Migadu-Flow: FLOW_OUT Message-ID: <0523850b-2049-41ff-8dfa-ca3a8f055a5f@linux.dev> Date: Mon, 30 Nov 2026 18:20:08 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Shin'ichiro Kawasaki , Jens Axboe , Christoph Hellwig , Tetsuo Handa , cuitao@kylinos.cn, cui.tao@linux.dev Subject: Re: [PATCH v9] loop: defer the queue limits clear to a workqueue To: Bart Van Assche References: <20261126054223.176824-1-cui.tao@linux.dev> <20261126135628.495497-1-cui.tao@linux.dev> <46cefe96-a193-40f9-bc1a-facc73cf6afa@acm.org> From: Tao Cui In-Reply-To: <46cefe96-a193-40f9-bc1a-facc73cf6afa@acm.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hello, Bart, 在 2026/11/26 23:32, Bart Van Assche 写道: > On 11/26/26 5:56 AM, Tao Cui wrote: >> loop_clear_limits() calls queue_limits_commit_update() directly from >> the loop workqueue that processes the request.  That does a >> non-atomic struct assignment to q->limits without freezing the queue, >> which races with lockless readers of q->limits on other CPUs, such >> as bio splitting reading max_hw_sectors and the discard path reading >> max_hw_discard_sectors, letting them observe torn values.  The >> trigger is a discard or write-zeroes request on a loop device whose >> backing file does not support the corresponding fallocate operation. >> >> The code already has an XXX comment saying this should move to a >> workqueue.  Do that: schedule a work item on the system workqueue, >> where it is safe to freeze the queue around the limits update.  The >> work item takes the queue limits lock before freezing, matching the >> order used by loop_set_dio() and loop_set_block_size().  The >> unsupported modes live under a new mutex, lo->clear_limits_lock, >> guarded by a backing-file check that drops records from a racing >> unbind or rebind.  The work item is scheduled while holding that >> lock, so an unbind resets the verdicts under the lock before calling >> cancel_work_sync() and cannot miss a schedule from a request that is >> still in flight.  Requests that arrive while the clear is in flight >> fail quietly, so the console report stays at the first failure. >> loop_remove() also cancels the work item before the queue becomes >> unreachable. > > This patch is more complicated and harder to verify than it should. Has > it been considered to allocate memory dynamically instead of adding new > members to struct loop_device, as is done in the untested patch below? > v10 will below switches to your suggestion. It is indeed much simpler: a dynamically allocated work item carries a reference to the backing file, so unbind and rebind are detected by comparing against that pinned file under lo->lo_mutex, and the clear_limits_seen tracking, the clear_limits_lock mutex and all three cancel_work_sync() calls are gone. I first implemented your sketch as-is. That removed all of the shared state used to track queue-limit updates. The only shared state left is a single reported bit for one separate requirement: loop/011 expects exactly one console report per binding, which is inherently a cross-request property. Without that bit, concurrent failures against the same binding can all report before the first work item updates the queue limits. The first failure per binding is left without RQF_QUIET, repeats are marked quiet, and the bit is reset on unbind. If the loop device is already unbound, no backing file reference is taken and the work item is simply discarded. I'm planning to send this as v10 unless you see a problem with the design. Tested with PROVE_LOCKING enabled: loop/011, the concurrency sweep and the mkfs/unbind stress run clean. Thanks, Tao > Thanks, > > Bart. > > > diff --git a/drivers/block/loop.c b/drivers/block/loop.c > index d122cb008811..bd9c453a9b8c 100644 > --- a/drivers/block/loop.c > +++ b/drivers/block/loop.c > @@ -222,26 +222,76 @@ static void loop_set_size(struct loop_device *lo, loff_t size) >          kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE); >  } > > -static void loop_clear_limits(struct loop_device *lo, int mode) > +/* > + * Information passed from lo_fallocate() to loop_clear_limits_workfn(). > + * @lo_backing_file holds a reference to prevent that the backing file is > + * freed and its address is reused by another backing file before > + * loop_clear_limits_workfn() has finished. > + */ > +struct loop_clear_limits_work { > +    struct work_struct    clear_limits_work; > +    struct loop_device    *lo; > +    struct file        *lo_backing_file; > +    int            mode; > +}; > + > +static void loop_clear_limits_workfn(struct work_struct *work) >  { > -    struct queue_limits lim = queue_limits_start_update(lo->lo_queue); > +    struct loop_clear_limits_work *w = container_of(work, > +                struct loop_clear_limits_work, clear_limits_work); > +    struct loop_device *lo = w->lo; > +    struct queue_limits lim; > +    unsigned int memflags; > +    bool changed = false; > > -    if (mode & FALLOC_FL_ZERO_RANGE) > -        lim.max_write_zeroes_sectors = 0; > +    mutex_lock(&lo->lo_mutex); > +    /* Skip the update if the loop device has been unbound or rebound. */ > +    if (lo->lo_state != Lo_bound || lo->lo_backing_file != w->lo_backing_file) > +        goto unlock; > > -    if (mode & FALLOC_FL_PUNCH_HOLE) { > +    lim = queue_limits_start_update(lo->lo_queue); > +    if ((w->mode & FALLOC_FL_ZERO_RANGE) && lim.max_write_zeroes_sectors) { > +        lim.max_write_zeroes_sectors = 0; > +        changed = true; > +    } > +    if ((w->mode & FALLOC_FL_PUNCH_HOLE) && lim.max_hw_discard_sectors) { >          lim.max_hw_discard_sectors = 0; >          lim.discard_granularity = 0; > +        changed = true; >      } > - > -    /* > -     * XXX: this updates the queue limits without freezing the queue, which > -     * is against the locking protocol and dangerous.  But we can't just > -     * freeze the queue as we're inside the ->queue_rq method here.  So this > -     * should move out into a workqueue unless we get the file operations to > -     * advertise if they support specific fallocate operations. > -     */ > +    if (!changed) { > +        queue_limits_cancel_update(lo->lo_queue); > +        goto unlock; > +    } > +    memflags = blk_mq_freeze_queue(lo->lo_queue); >      queue_limits_commit_update(lo->lo_queue, &lim); > +    blk_mq_unfreeze_queue(lo->lo_queue, memflags); > + > +unlock: > +    mutex_unlock(&lo->lo_mutex); > +    fput(w->lo_backing_file); > +    kfree(w); > +} > + > +/* > + * The queue limits can't be modified from inside lo_fallocate() since that > + * would require freezing the queue while a request is being processed. Hence > + * defer the queue limits update to a work item. The work item is queued on > + * lo->workqueue such that lo_free_disk() waits for it to finish. > + */ > +static void loop_clear_limits(struct loop_device *lo, struct file *file, > +                  int mode) > +{ > +    struct loop_clear_limits_work *w; > + > +    w = kmalloc_obj(*w, GFP_NOIO); > +    if (!w) > +        return; > +    INIT_WORK(&w->clear_limits_work, loop_clear_limits_workfn); > +    w->lo = lo; > +    w->lo_backing_file = get_file(file); > +    w->mode = mode; > +    queue_work(lo->workqueue, &w->clear_limits_work); >  } > >  static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, > @@ -268,7 +318,7 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, >       * supported and clear them here if that turns out not to be true. >       */ >      if (unlikely(ret == -EOPNOTSUPP)) > -        loop_clear_limits(lo, mode); > +        loop_clear_limits(lo, file, mode); > >      return ret; >  }