From patchwork Wed Mar 5 18:17:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14003123 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68CA8251798 for ; Wed, 5 Mar 2025 18:17:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198675; cv=none; b=c2EOnLc6c1S8QCITjS6rnQMCtuqg++UreBApVtBvZK9weVQJPSin5oMhYF/E0lGqNdo1PBe9xQGJCnKSCAjvf4zXMBXRP8lQQXO0xxDOoDJL7WyOvsXDN7xa8Kj1BaXtoqa+pVSIh50EbCS698lcmY+BHS1Vt7ugDVwWWShL2X0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198675; c=relaxed/simple; bh=GnwABZja+u7FKNqckJXjm8zIcmtAQUN5PrFZUsArmo4=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V2Zaf36Ge/2a84NcM08oOx8/JTX7BaNtb+GL4aRTJyG+wr3/Mo76csHnS1ekOkK6b4vAO4eTFjDCt7BzW7LiywkMgqMszsVd+/wwWp5lXGeFAVlet2NsXs/5BzfSDHxLfynKNM8tFvI9VBZxJJZjeoYaBE02YKGUwBavXrG4q4A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gk0U0+Zf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gk0U0+Zf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FA77C4CEE2 for ; Wed, 5 Mar 2025 18:17:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741198675; bh=GnwABZja+u7FKNqckJXjm8zIcmtAQUN5PrFZUsArmo4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=gk0U0+ZfyHckx59lLG825GyNZV3nYsZh3UTpb3C7S+veUcsk6I0OIipARyI2JBhvE EYwK37rAcsO/yYrroCqmjc3KGnIywhxRjFJwplt9Kvubic6Mi5ePNDBNl1dIWpu5g3 JP0/hHUGw8/AkKWK5foPZcxnjywCbVOIyzNZNCub9WR1+lrE3/0o+LESj8/rtHLY++ S0I/+J+yLctFZjeS9OvgGDLVm2f/QTtSoWG8TpgUApkvj1t0XOMLPV2l4ZyFSoEvWJ dSFrms93/MHe11BTXhsb252LADVOhg+izr0GsV9KLbhyyPKTlq+DLKCocFzQlSPVf9 GCjAMo6mIjy1Q== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 1/4] btrfs: fix non-empty delayed iputs list on unmount due to endio workers Date: Wed, 5 Mar 2025 18:17:47 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana At close_ctree() after we have ran delayed iputs either through explicitly calling btrfs_run_delayed_iputs() or later during the call to btrfs_commit_super() or btrfs_error_commit_super(), we assert that the delayed iputs list is empty. Sometimes this assertion may fail because delayed iputs may have been added to the list after we last ran delayed iputs, and this happens due to workers in the endio_workers workqueue still running. These workers can do a final put on an ordered extent attached to a data bio, which results in adding a delayed iput. This is done at btrfs_bio_end_io() and its helper __btrfs_bio_end_io(). Fix this by flushing the endio_workers workqueue before running delayed iputs at close_ctree(). David reported this when running generic/648. Reported-by: David Sterba Fixes: ec63b84d4611 ("btrfs: add an ordered_extent pointer to struct btrfs_bio") Signed-off-by: Filipe Manana --- fs/btrfs/disk-io.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index d96ea974ef73..b6194ae97361 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -4340,6 +4340,15 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) */ btrfs_flush_workqueue(fs_info->delalloc_workers); + /* + * We can also have ordered extents getting their last reference dropped + * from the endio_workers workqueue because for data bios we keep a + * reference on an ordered extent which gets dropped when running + * btrfs_bio_end_io() in that workqueue, and that final drop results in + * adding a delayed iput for the inode. + */ + flush_workqueue(fs_info->endio_workers); + /* * After we parked the cleaner kthread, ordered extents may have * completed and created new delayed iputs. If one of the async reclaim From patchwork Wed Mar 5 18:17:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14003124 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4694225485B for ; Wed, 5 Mar 2025 18:17:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198676; cv=none; b=op/i+hkkYhPW/OR1zceODhNh3jqSWCeaZDf6QDkLL1NW5lOKtPYDb27F0KOijfBof8kC3Awzd+QPGC705Tc35cuzo9xOlT0w3hGAD8AUVDCojuariCSY1t8Q6qjfZ2aq3xh6Su6rsT2oER06F0PV4FUVx4/zPkR3VY3iKdx6bxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198676; c=relaxed/simple; bh=XmChc0syTGzI4PfyPGOYDisNWqzuEgQxc3+1lnLelag=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JKAJJAQMDNcAzJTjXIRh7hzhIUOwiWoOh47DKtKn1CX0iTp/0mDe9OzmSBTF3UdvOEs2hBKDJIh6mPghBZCNFw4VarBumnqKSPHfBJN/nnRrJMxzN8p6lDyqxiivo/6G54YsK/xtr4e6RZ538EhZx/x1YFhz6k3TH/YNY4SGqqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B2JScHwr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="B2JScHwr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9877AC4CEE0 for ; Wed, 5 Mar 2025 18:17:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741198676; bh=XmChc0syTGzI4PfyPGOYDisNWqzuEgQxc3+1lnLelag=; h=From:To:Subject:Date:In-Reply-To:References:From; b=B2JScHwrhYYVfMvVP7qC9JOJoZvuXyZ8QTV6EJAV2pffb+S1c3FvICy2bvVjiSFnN j9zNipIy4SM9ThkYlufV3c2J3ocMTyvX3+/+UARRqoD+WmYp4IY1i/ArVAOpR26n6y QGPokbM5U37iV0ss0ezYaN+LG0jmriCkcgtEfYbIuGuYjWtSoUiRZ4jUDZR5WpqKaM Yp7UiJcCYKwWf2Hv8jR0Ut0cA3uuNBZVAtxeUChpoBYfEf8l/SD/efq7ZgFTews+a0 SWOpOFpkz/zIvI6l8xsN4o7fIN0LViA1tEhvcx7MgDQYYSfLUrYxaFrTs8ndgMXS8f zExn5CtHTJJDA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 2/4] btrfs: fix non-empty delayed iputs list on unmount due to compressed write workers Date: Wed, 5 Mar 2025 18:17:48 +0000 Message-Id: <617abbacfa82b497460a8c2b3319fbca010037b4.1741198394.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana At close_ctree() after we have ran delayed iputs either through explicitly calling btrfs_run_delayed_iputs() or later during the call to btrfs_commit_super() or btrfs_error_commit_super(), we assert that the delayed iputs list is empty. When we have compressed writes this assertion may fail because delayed iputs may have been added to the list after we last ran delayed iputs. This happens like this: 1) We have a compressed write bio executing; 2) We enter close_ctree() and flush the fs_info->endio_write_workers queue which is the queue used for running ordered extent completion; 3) The compressed write bio finishes and enters btrfs_finish_compressed_write_work(), where it calls btrfs_finish_ordered_extent() which in turn calls btrfs_queue_ordered_fn(), which queues a work item in the fs_info->endio_write_workers queue that we have flushed before; 4) At close_ctree() we proceed, run all existing delayed iputs and call btrfs_commit_super() (which also runs delayed iputs), but before we run the following assertion below: ASSERT(list_empty(&fs_info->delayed_iputs)) A delayed iput is added by the step below... 5) The ordered extent completion job queued in step 3 runs and results in creating a delayed iput when dropping the last reference of the ordered extent (a call to btrfs_put_ordered_extent() made from btrfs_finish_one_ordered()); 6) At this point the delayed iputs list is not empty, so the assertion at close_ctree() fails. Fix this by flushing the fs_info->compressed_write_workers queue at close_ctree() before flushing the fs_info->endio_write_workers queue, respecting the queue dependency as the later is responsible for the execution of ordered extent completion. Signed-off-by: Filipe Manana --- fs/btrfs/disk-io.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index b6194ae97361..cae5113b91da 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -4349,6 +4349,18 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) */ flush_workqueue(fs_info->endio_workers); + /* + * When finishing a compressed write bio we schedule a work queue item + * to finish an ordered extent - btrfs_finish_compressed_write_work() + * calls btrfs_finish_ordered_extent() which in turns does a call to + * btrfs_queue_ordered_fn(), and that queues the ordered extent + * completion either in the endio_write_workers work queue or in the + * fs_info->endio_freespace_worker work queue. We flush those queues + * below, so before we flush them we must flush this queue for the + * workers of compressed writes. + */ + flush_workqueue(fs_info->compressed_write_workers); + /* * After we parked the cleaner kthread, ordered extents may have * completed and created new delayed iputs. If one of the async reclaim From patchwork Wed Mar 5 18:17:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14003125 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D04425487E for ; Wed, 5 Mar 2025 18:17:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198677; cv=none; b=BkfI7fpoQe6n+pPzqQOTUXF/3yk543m53YBIQQLJkJAiNjS3ytD232eFdUS9jhlpLJcBcLPKcvIVkKbAuPMkP737tatXL6u9+uLBtkXRDfx3hLyEnX3kp5PSv9EbkEW+s4oZsNWMYg9saVqZ1tWOyrV1KjRnSVYs6mEjMrYSCJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198677; c=relaxed/simple; bh=MgWOsgZqI5T8K169gZV9TDzeg88d/7NeM/BpQfmy4nw=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PtjC7F45UxBACdPGIWX2kJ0hgnE78hduEGGycRzVo6nV4MffP+/ZvZyXjSe5aFLRSiOzkHCNfckMnCjDJaxIi4DhXN8oVfcI+MdhrnW0pDFlRkH0d3AT7rm8lbIBF6CTZpLeqzQ/Mw4APFALXGW2060+jVePEW5hPwM3JW/xRI0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KJ2Ii9CX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KJ2Ii9CX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0098C4CED1 for ; Wed, 5 Mar 2025 18:17:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741198677; bh=MgWOsgZqI5T8K169gZV9TDzeg88d/7NeM/BpQfmy4nw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=KJ2Ii9CXvZJS12EFKKsdHjyc5vyCn47nepdXefTsfNRgsEKf5MAqu4AFn4ABKDXFS 6VjkIba//7LQUkOnotJGvBUSf5760IDy/AV35Kj/FHU7V4lPO0tdrTo96eHmubPNHW IYpBjfpv64SJukJWPH5irK/m3ZmEpJSpUwR8iHq5Gh82dFLJBhJDeGx+YTfPUrlBE5 s9E+XsLMQdSc6UwHSKNwaKxpPhViRYvrqbPYItRKlUqGuuThus6VTfsuNcnVX47j+V 77e2/PSmK9MKB8wQ41xUM2QTkwBQjUQxHluU52StqHWHiQVRwRp1jZ8TZMCn/rQmGX 4QdHADNhpC2gg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 3/4] btrfs: move __btrfs_bio_end_io() code into its single caller Date: Wed, 5 Mar 2025 18:17:49 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana The __btrfs_bio_end_io() helper is trivial and has a single caller, so there's no point in having a dedicated helper function. Further the double underscore prefix in the name is discouraged. So get rid of it and move its code into the caller (btrfs_bio_end_io()). Signed-off-by: Filipe Manana --- fs/btrfs/bio.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c index 2f32ee215c3f..07bbb0da2812 100644 --- a/fs/btrfs/bio.c +++ b/fs/btrfs/bio.c @@ -105,18 +105,6 @@ static void btrfs_cleanup_bio(struct btrfs_bio *bbio) bio_put(&bbio->bio); } -static void __btrfs_bio_end_io(struct btrfs_bio *bbio) -{ - if (bbio_has_ordered_extent(bbio)) { - struct btrfs_ordered_extent *ordered = bbio->ordered; - - bbio->end_io(bbio); - btrfs_put_ordered_extent(ordered); - } else { - bbio->end_io(bbio); - } -} - void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status) { bbio->bio.bi_status = status; @@ -138,7 +126,15 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status) /* Load split bio's error which might be set above. */ if (status == BLK_STS_OK) bbio->bio.bi_status = READ_ONCE(bbio->status); - __btrfs_bio_end_io(bbio); + + if (bbio_has_ordered_extent(bbio)) { + struct btrfs_ordered_extent *ordered = bbio->ordered; + + bbio->end_io(bbio); + btrfs_put_ordered_extent(ordered); + } else { + bbio->end_io(bbio); + } } } From patchwork Wed Mar 5 18:17:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14003126 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7EA25254AFF for ; Wed, 5 Mar 2025 18:17:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198678; cv=none; b=ESgi9u+35vs+MCCxLoVE3qBdNSCyrEsxb4IbpE4x01mMfrq5kfy7QRgbUKHGj/BD4UTvyik97iDbxO2Ke/RAGYgaCPYdeF1Q3QmxzNw86F5zvRIay2d90A7o4Nr1rchzba2Ogj5jcszyqKjU6LVMHRGYoeSc90cuyj5U2hX7+pM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741198678; c=relaxed/simple; bh=UWVLiD4f7nvJaf4OdEkWZ/tDZYdLBqAGx4ZsEmp01co=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RGKZZEDLppJoxATgdZ2qc2QXRUsREiHN9NcWJvor8psfyRP0Ii9PEuXFLoKUQx8jOH018u8GtQ/twdpO+2W6gZGnoAE+hcgtCViKlkjwwlzl5DN4pFrdd9KQwgjXDfFg4+pKvWlCTglySCHpDnLW3aothAaPaQhP2wFeCV6H9ko= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c/49ksyr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="c/49ksyr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A91EFC4CED1 for ; Wed, 5 Mar 2025 18:17:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741198678; bh=UWVLiD4f7nvJaf4OdEkWZ/tDZYdLBqAGx4ZsEmp01co=; h=From:To:Subject:Date:In-Reply-To:References:From; b=c/49ksyrOxrFpvSh/8xN20CfBSK4j6gw4w9TExoMxD7dPIQHU+XGwLQSvjh7B+26G 1LWbpHVVqXkhmrwqIaWSPlj2v6qCdMzh5Zx0AfisKi072b7jabwod27MCeig8fJ2WQ Xb6rnNLXYz4ozuEe30yJP7b1ZM++CAQkfQ4mm2CyXb53WsZZPNXzLumX0AXMOZY7Rz j/UjxeSz9fEaGteWY6r0M3ZKkBBAf6BJyJjHAwk1av2niSbF3t/gpVwmY3e94pMc1e 8PhZ5dx9pzVzMoDSpXRgJymOtQe6mh3p/XFcN25xYi1JCSY4UtgbDGedY/RFXVU5fx TWI/k/3zy0Fzg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 4/4] btrfs: move btrfs_cleanup_bio() code into its single caller Date: Wed, 5 Mar 2025 18:17:50 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana The btrfs_cleanup_bio() helper is trivial and has a single caller, there's no point in having a dedicated helper function. So get rid of it and move its code into the caller (btrfs_bio_end_io()). Signed-off-by: Filipe Manana --- fs/btrfs/bio.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c index 07bbb0da2812..e9840954bf4a 100644 --- a/fs/btrfs/bio.c +++ b/fs/btrfs/bio.c @@ -97,21 +97,17 @@ static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info, return bbio; } -/* Free a bio that was never submitted to the underlying device. */ -static void btrfs_cleanup_bio(struct btrfs_bio *bbio) -{ - if (bbio_has_ordered_extent(bbio)) - btrfs_put_ordered_extent(bbio->ordered); - bio_put(&bbio->bio); -} - void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status) { bbio->bio.bi_status = status; if (bbio->bio.bi_pool == &btrfs_clone_bioset) { struct btrfs_bio *orig_bbio = bbio->private; - btrfs_cleanup_bio(bbio); + /* Free bio that was never submitted to the underlying device. */ + if (bbio_has_ordered_extent(bbio)) + btrfs_put_ordered_extent(bbio->ordered); + bio_put(&bbio->bio); + bbio = orig_bbio; }