Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2707727pxv; Sun, 11 Jul 2021 23:22:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPVW8NxWMCfv7715IpJhywFOmeXPpU9x4wXW+/uk9jrAXw/91jwn01pCgT1BRvPE51jlvn X-Received: by 2002:a05:6638:2382:: with SMTP id q2mr35075736jat.35.1626070949314; Sun, 11 Jul 2021 23:22:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626070949; cv=none; d=google.com; s=arc-20160816; b=UZwaHM9heMVxwoSaN6h3KrRtE29k1iOCpKmB1sGHNIlfMuixeUn51KR8qtqJ3un1mO eZe+UqyU86E4gOHr6mZKU4B5fYAvxJ0BnK+kkWsOPvUpvb1G0e8W2qMFyilFYzjU9DUl eAKZZTdrnDuHJDvNwWVRRmKuew4Ps+oCWiGF+mkvoUEKriwyHH12Fs+Qlf3kmY6cmoPj Yw2/9j9xBWoSqEWK+Eb5h+B0cLqwc4NjMeuf/Bl8vifcxUpdsYXG0bm26XGeGB2Qvww2 UkU8r9DOAz873z/NfuN9ThBcp3mXsflSzIBYZOWzcFwvXG7jUVpEf3jHoaUnvmxPUQIV TAzg== 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=QjDlqr3NsmrXZnfraVjFLC6RmGXL3q2ZSj06p9WIHsU=; b=SFmJd11a1e7e8yZVL4RF6lFodxVR/KYxGqmrfBYDjf6WoDW/MTLsce9CjSwnw1E4/K XT1mVx+uKHpM5lVEHF42G+X8W6toKbi67qEDwOoRvf8kpLFHor2Jn1wRJ4TgV1R2tWsu JS5jkB1Re7UYsxowlj/2DXZF+6QcuTKJZv+KlSbnsjnh+KrDfirppGtDcMXpaU/QgtBS e4lm34p+Eh8DxNsgta1r/TjhSA30DMA6J7s0e4G2FPgKDg07efpdxdUawxiqFv78s6Pf 8st+sI0YbrU1gRRcvIdYqkYsD6gFdnaa6XXKtrCD40wvn8ONISiV1HSPlYBPmR4uDNuC 2bXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Wr9ByQRi; 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 f20si10689197iol.42.2021.07.11.23.22.17; Sun, 11 Jul 2021 23:22:29 -0700 (PDT) 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=Wr9ByQRi; 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 S233869AbhGLGYJ (ORCPT + 99 others); Mon, 12 Jul 2021 02:24:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:40462 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234252AbhGLGXA (ORCPT ); Mon, 12 Jul 2021 02:23:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E1777610CB; Mon, 12 Jul 2021 06:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626070780; bh=VfZMHmrMkR5FKZSV0EAtpTsIsFOSua5Z84CgBXwadr0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wr9ByQRi/fa/vZFjYxgnm47zY9bN0QJAsz+tv6BPT5toV/ldK53MnCLdffQa+2JGP DoIztliPRX+6hkM+bNnTDoKLYZfZ8qLkO2K+dvaw/LT6gEfKohbrty2AAuZX8gL6XT yY873hEXyeHMwWVBE5/esDglQfXyn7tyRoMUHhlc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Roman Gushchin , Jan Kara , Jan Kara , Alexander Viro , Dave Chinner , Dennis Zhou , Tejun Heo , Jens Axboe , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.4 137/348] writeback, cgroup: increment isw_nr_in_flight before grabbing an inode Date: Mon, 12 Jul 2021 08:08:41 +0200 Message-Id: <20210712060719.406279545@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060659.886176320@linuxfoundation.org> References: <20210712060659.886176320@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: Roman Gushchin [ Upstream commit 8826ee4fe75051f8cbfa5d4a9aa70565938e724c ] isw_nr_in_flight is used to determine whether the inode switch queue should be flushed from the umount path. Currently it's increased after grabbing an inode and even scheduling the switch work. It means the umount path can walk past cleanup_offline_cgwb() with active inode references, which can result in a "Busy inodes after unmount." message and use-after-free issues (with inode->i_sb which gets freed). Fix it by incrementing isw_nr_in_flight before doing anything with the inode and decrementing in the case when switching wasn't scheduled. The problem hasn't yet been seen in the real life and was discovered by Jan Kara by looking into the code. Link: https://lkml.kernel.org/r/20210608230225.2078447-4-guro@fb.com Signed-off-by: Roman Gushchin Suggested-by: Jan Kara Reviewed-by: Jan Kara Cc: Alexander Viro Cc: Dave Chinner Cc: Dennis Zhou Cc: Tejun Heo Cc: Jens Axboe Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/fs-writeback.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index fd6b50582c87..3a0d7b8af141 100644 --- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -505,6 +505,8 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id) if (!isw) return; + atomic_inc(&isw_nr_in_flight); + /* find and pin the new wb */ rcu_read_lock(); memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); @@ -535,11 +537,10 @@ static void inode_switch_wbs(struct inode *inode, int new_wb_id) * Let's continue after I_WB_SWITCH is guaranteed to be visible. */ call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn); - - atomic_inc(&isw_nr_in_flight); return; out_free: + atomic_dec(&isw_nr_in_flight); if (isw->new_wb) wb_put(isw->new_wb); kfree(isw); -- 2.30.2