From patchwork Sat May 14 00:07:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Bo X-Patchwork-Id: 9094281 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 1A2A99F372 for ; Sat, 14 May 2016 00:05:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 479F22017E for ; Sat, 14 May 2016 00:05:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4549520220 for ; Sat, 14 May 2016 00:05:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932427AbcENAFa (ORCPT ); Fri, 13 May 2016 20:05:30 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:19192 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932341AbcENAF0 (ORCPT ); Fri, 13 May 2016 20:05:26 -0400 Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u4E05OsE028129 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Sat, 14 May 2016 00:05:25 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.13.8) with ESMTP id u4E05Ogi020387 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Sat, 14 May 2016 00:05:24 GMT Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id u4E05OgT008379 for ; Sat, 14 May 2016 00:05:24 GMT Received: from localhost.us.oracle.com (/10.211.47.181) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 13 May 2016 17:05:24 -0700 From: Liu Bo To: linux-btrfs@vger.kernel.org Subject: [PATCH 6/7] Btrfs: fix eb memory leak due to readpage failure Date: Fri, 13 May 2016 17:07:01 -0700 Message-Id: <1463184422-13584-6-git-send-email-bo.li.liu@oracle.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1463184422-13584-1-git-send-email-bo.li.liu@oracle.com> References: <1463184422-13584-1-git-send-email-bo.li.liu@oracle.com> X-Source-IP: userv0022.oracle.com [156.151.31.74] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP eb->io_pages is set in read_extent_buffer_pages(). In case of readpage failure, for pages that have been added to bio, it calls bio_endio and later readpage_io_failed_hook() does the work. When this eb's page (couldn't be the 1st page) fails to add itself to bio due to failure in merge_bio(), it cannot decrease eb->io_pages via bio_endio, and ends up with a memory leak eventually. This adds the 'atomic_dec(&eb->io_pages)' to the readpage error handling. Signed-off-by: Liu Bo --- fs/btrfs/extent_io.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 99286d1..2327200 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c @@ -3069,6 +3069,30 @@ static int __do_readpage(struct extent_io_tree *tree, *bio_flags = this_bio_flag; } else { SetPageError(page); + /* + * Only metadata io request has this issue, for data it + * just unlocks extent and releases page's lock. + * + * eb->io_pages is set in read_extent_buffer_pages(). + * + * When this eb's page fails to add itself to bio, + * it cannot decrease eb->io_pages via bio_endio, and + * ends up with extent_buffer_under_io() always being + * true, because of that, eb won't be freed and we have + * a memory leak eventually. + * + * Here we still hold this page's lock, and other tasks + * who're also reading this eb are blocked. + */ + if (rw & REQ_META) { + struct extent_buffer *eb; + + WARN_ON_ONCE(!PagePrivate(page)); + eb = (struct extent_buffer *)page->private; + + WARN_ON_ONCE(atomic_read(&eb->io_pages) < 1); + atomic_dec(&eb->io_pages); + } unlock_extent(tree, cur, cur + iosize - 1); } cur = cur + iosize;