Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3390834pxb; Mon, 17 Jan 2022 19:25:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+E3NaEyWJL3WJX9yiD2VaHkS9FLX0scHAHiqDB05nbNtjEDx/u0Z+dFveLc+OofMcbxo+ X-Received: by 2002:a17:903:187:b0:14a:6ee7:682c with SMTP id z7-20020a170903018700b0014a6ee7682cmr25484645plg.105.1642476247980; Mon, 17 Jan 2022 19:24:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642476247; cv=none; d=google.com; s=arc-20160816; b=sdb2PKmX3vF/yUaJgrswNoKVGV9b0V4aIJ7CuchTWqWAj5e8YGVMkhzCX6ZzxY/dH4 tWIjfqcMpP0Ndoen9mZcqvp/TLoFARv6fQFVonSatO3kZ+4yGEuPEdxsd9hHO3ofPz66 iMETzkpeONaBVtDtAtxSMn51NCbcB/iCPQsDrugsjYhdsbNhzDjXMzpDyIv3+1cp6YIq vmtNuuoLwkiLOABaFhWWokW4al7GwIeVbA7RKdZvEsM5yDV9SnSKx/IyyNI4KvZA+7qb ip8tqW+iaHcc7wLWtiNMa1r2lwEPxJi2o6pcClWaGHjk8yK+vfhTC6DmBXkIqGnluuXY MpdQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rO4vmtSgkDEceUMBXRXD+v0I93GR2D4c9C3M+5PgjNA=; b=B5KHvWDVPrhV4h2fcKBfgaLULLz2/EaXZ6GyFCQsGRkOiC+IRAHLZteZ+SW1cMkye9 3oAX6vC/EFoMe9PujXvRxuZ+SXCfa2sL855yRayUoYlkw0axZDRCqjP4VWVKMbMbzSVM bTc0pVs8GbB8DUlyFHirOxGMzraWG1AYdEr4DlqRowFl0Fd3bQQPVHfC2DwTDyyKXCic AfhbX8nVTKNQvZiHpp2KuNX2lZGULJHIYF9CDz8uqZNwtuWnTva/Oc4MtV1rUS9v6laI KAjiENykTZephUuyTn/iZIlxf3V1BeRUDVM006Cd2VjyvZO7CwGwB3aBO1tFJO25ndLs 2oGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=mCotV6+u; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n26si3554027pgl.625.2022.01.17.19.23.56; Mon, 17 Jan 2022 19:24:07 -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=k20201202 header.b=mCotV6+u; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245239AbiARCdo (ORCPT + 99 others); Mon, 17 Jan 2022 21:33:44 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:42152 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344233AbiARC3X (ORCPT ); Mon, 17 Jan 2022 21:29:23 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id ED7FAB8122C; Tue, 18 Jan 2022 02:29:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3274C36AEF; Tue, 18 Jan 2022 02:29:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642472960; bh=9zqniQmnmRj2EP7QeluyIKIoG9IT21ZtY1hW7QzQH5Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mCotV6+uCILJleuXXSHPescsMYa39PMvTrB4ctC41cGn9ZFgwbJR9PUaQY8GII+mD 4FfJ7dp0P0Ndsjhd5ReTzzOrE2km+mJ7MbLSX3+IO/WbQ74lcKqiYmsTpFxzYYy07c L1XATqam8Jtc4leQHGd+QvtWoOHAU8VX/LldnOAQtuKN46Zj/rWESWhkhDTxYYpDAP Lwrxm1UuUTllUyzdfCgA6DlSFmJAtRWl5/8GDuNBGYa0pT81VCJMRUS5eQ/r0u3rLE mG3+wVylpXLvU5oCROjJbRBsJ2JQSEIoRypXH6DdWWQ5Ywmiz5/dz/fL0DqwfPHoYT rEIdNCBQ/hmHA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kyeong Yoo , Richard Weinberger , Sasha Levin , dwmw2@infradead.org, joel@jms.id.au, linux-mtd@lists.infradead.org Subject: [PATCH AUTOSEL 5.16 178/217] jffs2: GC deadlock reading a page that is used in jffs2_write_begin() Date: Mon, 17 Jan 2022 21:19:01 -0500 Message-Id: <20220118021940.1942199-178-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220118021940.1942199-1-sashal@kernel.org> References: <20220118021940.1942199-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kyeong Yoo [ Upstream commit aa39cc675799bc92da153af9a13d6f969c348e82 ] GC task can deadlock in read_cache_page() because it may attempt to release a page that is actually allocated by another task in jffs2_write_begin(). The reason is that in jffs2_write_begin() there is a small window a cache page is allocated for use but not set Uptodate yet. This ends up with a deadlock between two tasks: 1) A task (e.g. file copy) - jffs2_write_begin() locks a cache page - jffs2_write_end() tries to lock "alloc_sem" from jffs2_reserve_space() <-- STUCK 2) GC task (jffs2_gcd_mtd3) - jffs2_garbage_collect_pass() locks "alloc_sem" - try to lock the same cache page in read_cache_page() <-- STUCK So to avoid this deadlock, hold "alloc_sem" in jffs2_write_begin() while reading data in a cache page. Signed-off-by: Kyeong Yoo Signed-off-by: Richard Weinberger Signed-off-by: Sasha Levin --- fs/jffs2/file.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/fs/jffs2/file.c b/fs/jffs2/file.c index 4fc8cd698d1a4..bd7d58d27bfc6 100644 --- a/fs/jffs2/file.c +++ b/fs/jffs2/file.c @@ -136,20 +136,15 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping, struct page *pg; struct inode *inode = mapping->host; struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); + struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); pgoff_t index = pos >> PAGE_SHIFT; uint32_t pageofs = index << PAGE_SHIFT; int ret = 0; - pg = grab_cache_page_write_begin(mapping, index, flags); - if (!pg) - return -ENOMEM; - *pagep = pg; - jffs2_dbg(1, "%s()\n", __func__); if (pageofs > inode->i_size) { /* Make new hole frag from old EOF to new page */ - struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); struct jffs2_raw_inode ri; struct jffs2_full_dnode *fn; uint32_t alloc_len; @@ -160,7 +155,7 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping, ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); if (ret) - goto out_page; + goto out_err; mutex_lock(&f->sem); memset(&ri, 0, sizeof(ri)); @@ -190,7 +185,7 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping, ret = PTR_ERR(fn); jffs2_complete_reservation(c); mutex_unlock(&f->sem); - goto out_page; + goto out_err; } ret = jffs2_add_full_dnode_to_inode(c, f, fn); if (f->metadata) { @@ -205,13 +200,26 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping, jffs2_free_full_dnode(fn); jffs2_complete_reservation(c); mutex_unlock(&f->sem); - goto out_page; + goto out_err; } jffs2_complete_reservation(c); inode->i_size = pageofs; mutex_unlock(&f->sem); } + /* + * While getting a page and reading data in, lock c->alloc_sem until + * the page is Uptodate. Otherwise GC task may attempt to read the same + * page in read_cache_page(), which causes a deadlock. + */ + mutex_lock(&c->alloc_sem); + pg = grab_cache_page_write_begin(mapping, index, flags); + if (!pg) { + ret = -ENOMEM; + goto release_sem; + } + *pagep = pg; + /* * Read in the page if it wasn't already present. Cannot optimize away * the whole page write case until jffs2_write_end can handle the @@ -221,15 +229,17 @@ static int jffs2_write_begin(struct file *filp, struct address_space *mapping, mutex_lock(&f->sem); ret = jffs2_do_readpage_nolock(inode, pg); mutex_unlock(&f->sem); - if (ret) - goto out_page; + if (ret) { + unlock_page(pg); + put_page(pg); + goto release_sem; + } } jffs2_dbg(1, "end write_begin(). pg->flags %lx\n", pg->flags); - return ret; -out_page: - unlock_page(pg); - put_page(pg); +release_sem: + mutex_unlock(&c->alloc_sem); +out_err: return ret; } -- 2.34.1