From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=174.142.148.226 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796365643; b=RjVvU4dylyRxddQyeccVQ1w309s+FsneTxDtx+QnY4P4XXctOZ9378F39e+VhUSyaqur qAOPRgfrY4x8iRPYLeFZliN58BIbmIU2EeFwVccw4p4lucVBe0g7ttKxbQkzLS5DoFVjK WLLKrqtfWhzxTcs7BfaTux1sEUSHsHQ7i3YCnXzIug4p7MOTRBAwn+ggiJwhlvflX0M5N zqI8lcgTjcRK6UUWLk26KToEnvbZ2lG9mbcPLGHf60//KjmMBk9gF5LWoHbsxxVOpgP5J Q8vk1UIYN0vQjRdFPl7h1PjSkAVmfL4jntCljPDpkbQPg6WIaLmIVlhqXmyurZ8ZHKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796365643; h=DMARC-Filter:Received:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=KU4MwbFt9NBF+BWuZKGyz052l+j87OcWKw4gtyNhWPc=; b=bvK2Z1HhbhsIej4292lijLgUOzZMzrucRnE0QdECMQy2e0Y9EsJitqyhS69N6ZW7Nps7 Q+QiIdBnNacKvRB0EfENWxYP3Tt9/XJg5zmeCZRFEDm4l7rYzA/KGvGDDNxV6UEcX6N2q p3Qx7colUE46bhL3OSeYCgzLuwARJOQDVFj+fJ4doDUQUYjnj/gsrnFRnDWe146SlUaUO wG1B871JfmYjXi/+fGA532z7KVxBpViP0zwXbCPT0k7uplEF4gAikDxgB0kWGxEHanXFl k3l4Dcy7tVDYZyxsh1B/HMZLmiXaATRmamaXl5LRgBAAjoLxjhqGBshG2bwtWDJEMHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=umail.furryterror.org; spf=pass smtp.mailfrom=drax.hungrycats.org; arc=none smtp.remote-ip=174.142.148.226 Received: from drax.kayaks.hungrycats.org (drax.kayaks.hungrycats.org [174.142.148.226]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6225A606A322 for ; Fri, 4 Dec 2026 06:27:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6225A606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=umail.furryterror.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=drax.hungrycats.org Received: by drax.kayaks.hungrycats.org (Postfix, from userid 1002) id EF5FC1D36C3A; Fri, 04 Dec 2026 01:27:20 -0500 (EST) Date: Fri, 4 Dec 2026 01:27:20 -0500 From: Zygo Blaxell To: Boris Burkov Cc: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH v2 0/4] btrfs: scrub: support low bwlimits Message-ID: X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Mon, Nov 30, 2026 at 05:14:32PM -0800, Boris Burkov wrote: > Improve the scrub throttling semantics by implementing it as a cancel > aware sleep wrapped in a pause. TL;DR there is a pre-existing scrub/commit/discard race that this patch series makes worse, resulting in device replace failures and spurious scrub errors. My LLM agent found it when checking this patch against fixes for one of the existing bugs, then discovered the other issues while testing this patch. This patch puts the throttle sleep in a pause, so a commit can run while stripes are queued, and the throttle holds the remaining reads back long enough for the commit to discard their blocks. Scrub cancel doesn't interrupt the pause, so under steady commits a cancel can be held off for minutes. Making the pause return on cancel fixes that, but the scrub then submits the rest of its queued reads while a commit may still be running, which can hit the same read-after-discard failure. Test failures hit 60-80% with a 32 MiB/s limit--not even particularly slow. discard=async lowers the failure rate, but it's still not zero. [curated LLM analysis follows] The pause lets a transaction commit run while the scrub still holds stripes it filled from the old commit root that it has not read yet. scrub_throttle_dev_io() is called from submit_initial_group_read(), which is reached from queue_scrub_stripe() and flush_scrub_stripes(). With patch 4/4 the sleep happens between the stripes of a group: the stripes before it are submitted, and the ones after it were filled by scrub_find_fill_first_stripe() from the commit root but are not submitted yet. 1/4 drops the paths at the pause, but not those stripes. During the sleep, a commit can switch the commit roots and run btrfs_finish_extent_commit(), which unpins the extents freed in that transaction. The block group is read-only, so the space is not reused, but it is discarded. With discard=sync, that discard happens before btrfs_scrub_continue(), so the scrub reads zeros for the stripes of the group that come after the sleep. On for-next 4e4ea25ed954 vs 1382d88402c0 (patch 1/4 only), discard=sync, 32MiB/s limit, files deleted ahead of the scrub: scrub failed 6/10 and replace 8/10 test runs on the whole series, 0/10 with patch 1/4 only; 0/10 with no limit or nodiscard. The failures are what you'd expect from reading zeros: "has bad bytenr, has 0" on tree blocks, data csum errors, uncorrectable errors on a healthy filesystem, and dev-replace canceled with -EIO on the first bad metadata sector. A bpftrace probe on the discard and the reads put every logged bad block inside a range discarded while the reading thread was paused in scrub_throttle_sleep(), and read 0.4-23.5ms after the discard. None of them fell in a commit at the existing pause point in scrub_simple_mirror(). With discard=async it is rarer, but it does not go away. Async discard only puts partly used block groups on the discard list if they hold only data, so the exposure is data blocks, not metadata. do_trimming() discards free space in read-only block groups too, and a block group on the list is trimmed when its discard_eligible_time comes up. That is now + BTRFS_DISCARD_DELAY (120s) when the group joins the list, and again when a discard pass over it finishes. So whether 120s protects the scrub depends on when the group last joined the list, not on when this commit freed the extents. If the deadline is already partly used up, or a pass is running on that group, the extents freed by this commit can be trimmed well under 120s later, or within about a second. The throttle is what makes the scrub slow. The stripes after the sleep are read one throttle interval apart, and each interval is another pause and another commit. The last stripe of a group is read up to 7 * 64KiB / limit after it was filled, counted from the commit root it was filled from. Patch 3/4 allows limits below 512KiB/s with no floor. So at about 3.7KiB/s that is past a fresh 120s deadline, and with a deadline that is already partly used up it takes much less. Anything else that holds the scrub thread up adds to that. In a test with discard=async, a block group queued before the scrub reached it, and the limit dropped to 64KiB/s on data, the whole series failed 1 of 8 runs (data csum errors, uncorrectable). The bad sectors were trimmed at the group's first eligible pass, about 120s after it was queued, while the scrub was in the throttle sleep, and read 8s later. The async tests also showed a second problem. When commits run back to back (file deletion plus a sync every 50ms, about 6 commits per second), both scrub threads can stop reading for minutes: 2 of 8 runs in the first test, and all 5 runs in a test that cancels a scrub making no progress for 20s, with stalls of 60-205s. For the whole time they were in __scrub_blocked_if_needed() called from scrub_throttle_sleep(), i.e. the scrub_pause_off() at the end of the sleep, waiting for scrub_pause_req to drop to zero. None of the commits had to wait for the scrub, because it was already counted as paused. __scrub_blocked_if_needed() does not check the cancel flags, so a cancel issued during a stall took effect only when the stall ended, 42-188s late. On patch 1/4 only, every cancel returned within a second. So under steady commit load, the paused sleep can starve the scrub and delay the cancel this patch is meant to speed up. The same stale-stripe window exists without this series at the existing pause point in scrub_simple_mirror(), where stripes are queued but not read. I reproduced that on v7.3.3 with discard=sync and fixed it in our tree by finishing the queued stripes before pausing. That fix does not carry over here, because this sleep is reached from inside the submission and the flush. A possible solution is to move the sleep out of the submission path, to the top of the scrub_simple_mirror() loop: - charge each stripe against the limit when it is submitted, but never sleep there; - at the top of the loop, sleep without pausing, and wake on a cancel or on scrub_pause_req. btrfs_scrub_pause() would need a wake_up() on scrub_pause_wait for that; - when woken by a pause request, finish the queued stripes, drop the paths, and then pause. A commit then waits only for the reads already queued (which are submitted at device speed because nothing sleeps in the submission), and not for the throttle sleep. That keeps the goal of this patch, and the scrub is not counted as paused while it sleeps, so commits cannot pass it indefinitely. To keep patch 4/4's per-stripe pacing, submit each stripe as soon as it is queued when the device has a limit. Separately, __scrub_blocked_if_needed() should return on a cancel. The agent tested that on top of the whole series: it wakes on scrub_cancel_req or the sctx's cancel_req and returns. In 19 stalls like the ones above, both scrub threads returned 10-26ms after the cancel, against 42-188s on the series alone. But the scrub then leaves the pause while the commit that kept it there may still be running, and it went on to submit the rest of the group, 4-16 stripes per run, all filled before that commit. Those reads can report errors that are not on the disk. So the canceled scrub also has to drop the stripes it has not submitted: complete them without reading, reporting, writing them to a replace target, or advancing last_physical. With both changes, no read was submitted after the cancel in 10 runs.