Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3556756pxb; Mon, 24 Jan 2022 12:09:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJw650briyGwtjfMJvtXPq75xyYxAkooJFi/FR4eeqj8QMf52Gi/eAdIM5ACnIWQ7PvqOTFc X-Received: by 2002:a17:90a:8548:: with SMTP id a8mr3563250pjw.114.1643054942562; Mon, 24 Jan 2022 12:09:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643054942; cv=none; d=google.com; s=arc-20160816; b=UaevwTDZD/A86ZZywn1cZfCs7omArDNLgb7egSY+La4M4b0PDlsnZnmAegwDja05jQ 8tFizEzw96o3aGmEIEDNJiJmW2LmAXrDUyGeGsZZiKiiv+EWVLoBtR2K/+KpWb+Qn6BT IRw5mADG7da1vII3wSGuOMTuREtfhfP3ZAlt2Mj+G+jM+jReXR+i5YkUQ15ythB9VeoP 9pNo4/MEL85JrgE3kXUhxfZAGZWdYgNw5Zizv3hKgPe0oHE38phn7rgl6EOYwRu82F90 eDNTZLbnSWjqhJT2G4ah7GPTdwvGmSiDbVWM/BgkrWZSS3YqOM6g8AZD20/RMK3HDzn8 n+HA== 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=0UzutdVwbJ54D1ewkdwTHgYDwYOAwhoxK84MBamijAM=; b=z+4grHNUvOTJeARXB1gu+laoJ2om7MM6XAnDMfdKUvKDrDVIeOfZzu/Tb7xB92O+Gs n/P2wncrqfWsXAYDg8G7D0O1KFzBv4XvqE4UuN7q1Zx4CYxHBDHQieFiDCi4NukvzaEX WQWLv/7G8yDl8/ORulRQ0lqoZSAYuezbIAlGlKz21yrhViIjRPYKk8Vm1EfjArCB/Q4Z 08X+X5myZYaivQpV1gkkcrDXxAQPDcFqrdsM2t30Mev3B/qQNFi0fKyeMnfX0VECR447 411n/MYjZr6jLnF+Fr5aaDcnnRb0WRJ5r0aG2TmUZt0gczBU0ef7X4PLgPVipvbMR5qP fPaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qQXLW2WF; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bj8si5625186pgb.745.2022.01.24.12.08.43; Mon, 24 Jan 2022 12:09:02 -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=@linuxfoundation.org header.s=korg header.b=qQXLW2WF; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347026AbiAXTJg (ORCPT + 99 others); Mon, 24 Jan 2022 14:09:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345233AbiAXTDG (ORCPT ); Mon, 24 Jan 2022 14:03:06 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31D51C061777; Mon, 24 Jan 2022 10:58:48 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id C70526154A; Mon, 24 Jan 2022 18:58:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD559C340E7; Mon, 24 Jan 2022 18:58:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643050727; bh=6+/Oo9XO52MP/CUVThTtWd/19ICfGPzvfT5bMIBO7hI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qQXLW2WF2vGdgzBnrfuEZUvUHM5Sr8Kwta/4Cc8Grr16DKLR94CTZo0egmPH6AnbQ OFWu9AHkExApbG7RmSdBGkYI5xa1iFAH3uz3jKk0LApjw5bDQuN0eyHN5tOLb9jrWK N3WS6u7y4UEuOVYP+yLhPRUaHE34ysRmRzxe7nJA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kyeong Yoo , Richard Weinberger , Sasha Levin Subject: [PATCH 4.9 094/157] jffs2: GC deadlock reading a page that is used in jffs2_write_begin() Date: Mon, 24 Jan 2022 19:43:04 +0100 Message-Id: <20220124183935.760570016@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183932.787526760@linuxfoundation.org> References: <20220124183932.787526760@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: 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 c12476e309c67..eb4e4d784d26e 100644 --- a/fs/jffs2/file.c +++ b/fs/jffs2/file.c @@ -135,20 +135,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; @@ -159,7 +154,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)); @@ -189,7 +184,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) { @@ -204,13 +199,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 @@ -220,15 +228,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