Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3188830pxb; Mon, 9 Nov 2020 05:11:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjSSDkOJ5fTh6G+4+YMMTETz8iXBL9uRB9heG2AFcYGtSWm4zeFESWy2frGmgwtYDL0Mhz X-Received: by 2002:a17:906:5017:: with SMTP id s23mr15425339ejj.359.1604927474991; Mon, 09 Nov 2020 05:11:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604927474; cv=none; d=google.com; s=arc-20160816; b=SlL05o6ivgV6Ksnn7r1nZxc9gMg1+OiXM/3bYNXLVjPefyzwQ7AHsPSDUNc7/A4G0P udbo695DBDzKAw6KiSIR3s9ZEPYi7THZPn4i/18yKbfnxIurenR0wcWnZnFip5SWMX+N w15vHqYIwwDIiv134nSJar//WS4lwlzKUrj7XY+bipjiiP8ZwCv0fWJakYp2+LrCLKdp 5mlyOLEHFzF+b6pPDVrv4DwZ6ixRWJfNLxB3KkzfNpi5fzrtbVfzi5C+uUjXy78y2URm ZezqNUxfQ+f03V3qCb9VGaVpapFP6keWjjluN0h+OI3wfxNYMif1xkX0OCKckMoOdjuZ JhTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=64RjFInlm1wwspNRtd7P6IJAYIx12OKKmWddiyin/ag=; b=jeQz0LQAcvjNv3f8JMh93UhlBPzJTk7+2Y9gnRsD/uDOtixeoMpebY9hZD8cf4A+BK l8qicjoaHMVe6RWVdpDDMKBl6dsBEPn8Ht8KrFMApnPtCJMFguYB17S1EzvfPsZwkiih kgZuWWsmYD7hbi0lStpV5jdYLd1BQUj/nYanXtss1yhg/Y2SnzgJom3HDx12fjPn5xdR o5bV4yVnutO2C9mYmwuJ33pTntv5Mv6sA+740UTZHfXeUmhXfFf8AyN7TytjjeyTZ/qD ur6p7L1a6L4hfbJ5GXgrDXYiy6LgysBLP8oeRz950TH3MbOTGfoF54MwVlU1MstswiBC hSqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Y0/0JXGh"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c13si7114446ejs.117.2020.11.09.05.10.52; Mon, 09 Nov 2020 05:11:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Y0/0JXGh"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731704AbgKINIk (ORCPT + 99 others); Mon, 9 Nov 2020 08:08:40 -0500 Received: from mail.kernel.org ([198.145.29.99]:33442 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731060AbgKINIi (ORCPT ); Mon, 9 Nov 2020 08:08:38 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6A44320663; Mon, 9 Nov 2020 13:08:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604927317; bh=2u4uFiuVxotEO64+laPzB8xN6NojkVe8Zht9x6elIRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y0/0JXGhPNvMiYitc0aieH/Q+kDSWnrxenfonQjU55XtM60HuDw3K5TvblBiQAILT V8HtMcp9JrpcPdQCW9SOuiQQSwsWmgRBL3l+9XT36rCSSEbUnGxlkmoaHK90u2L0cK Fog65LviGvJpz3zAzCQT78Znv+9OzjdnhK2WeCAw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qu Wenruo , David Sterba , Ben Hutchings Subject: [PATCH 4.19 19/71] btrfs: extent_io: add proper error handling to lock_extent_buffer_for_io() Date: Mon, 9 Nov 2020 13:55:13 +0100 Message-Id: <20201109125020.811120362@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125019.906191744@linuxfoundation.org> References: <20201109125019.906191744@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo commit 2e3c25136adfb293d517e17f761d3b8a43a8fc22 upstream. This function needs some extra checks on locked pages and eb. For error handling we need to unlock locked pages and the eb. There is a rare >0 return value branch, where all pages get locked while write bio is not flushed. Thankfully it's handled by the only caller, btree_write_cache_pages(), as later write_one_eb() call will trigger submit_one_bio(). So there shouldn't be any problem. Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent_io.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -3554,19 +3554,27 @@ void wait_on_extent_buffer_writeback(str TASK_UNINTERRUPTIBLE); } +/* + * Lock eb pages and flush the bio if we can't the locks + * + * Return 0 if nothing went wrong + * Return >0 is same as 0, except bio is not submitted + * Return <0 if something went wrong, no page is locked + */ static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb, struct btrfs_fs_info *fs_info, struct extent_page_data *epd) { - int i, num_pages; + int i, num_pages, failed_page_nr; int flush = 0; int ret = 0; if (!btrfs_try_tree_write_lock(eb)) { - flush = 1; ret = flush_write_bio(epd); - BUG_ON(ret < 0); + if (ret < 0) + return ret; + flush = 1; btrfs_tree_lock(eb); } @@ -3576,7 +3584,8 @@ lock_extent_buffer_for_io(struct extent_ return 0; if (!flush) { ret = flush_write_bio(epd); - BUG_ON(ret < 0); + if (ret < 0) + return ret; flush = 1; } while (1) { @@ -3618,7 +3627,10 @@ lock_extent_buffer_for_io(struct extent_ if (!trylock_page(p)) { if (!flush) { ret = flush_write_bio(epd); - BUG_ON(ret < 0); + if (ret < 0) { + failed_page_nr = i; + goto err_unlock; + } flush = 1; } lock_page(p); @@ -3626,6 +3638,11 @@ lock_extent_buffer_for_io(struct extent_ } return ret; +err_unlock: + /* Unlock already locked pages */ + for (i = 0; i < failed_page_nr; i++) + unlock_page(eb->pages[i]); + return ret; } static void end_extent_buffer_writeback(struct extent_buffer *eb)