From nobody Wed Apr 1 11:13:39 2026 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 402AE423A66 for ; Tue, 31 Mar 2026 16:09:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774973350; cv=none; b=s6btNoIHbqXCdF9KYERbpEkBTPNVqsI24dpkIrVgZpIYXRaoxHlLGsBgyGLHdcPR0U4vwo4jW83A17EbhfS+k5oy7pkYh+yMzqjXkkaTsLCwjkZxutm8fP7WGFCHqOYxPWN8y5Q9wcDr5AdbxtzdXoUYHSvWNceMIu1APqS0YiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774973350; c=relaxed/simple; bh=Kx0hCzlQLvB2ZwWtgXSqQFrn1UOzjSBANutOmgJEla8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VBvzIwPtC6qDv39ji/gDOdC47aiZGIlhzPtRAvxA/iu/mCTnC+okFS2Dw+K/OqAJyCVkxYtMt7m2F3MRyqTM25uFxILYZTZsbkO/4M1CuTU5oWD7R5v1CCeGXBIojAebXsdzhtQABfpleyqStNkjs4mSYnyT82tJhbEQS3qt0fI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=g8FSt3T0; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="g8FSt3T0" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-43cf73bbfbdso1815075f8f.1 for ; Tue, 31 Mar 2026 09:09:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774973347; x=1775578147; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IAjANhRSSjFG14Tt4RrpUuJ/1k05o3tXfxT9ZI8DWb4=; b=g8FSt3T02fI/YQZMCiL5sHjiSYS2HMRjztBx5W9JznBNx/qYnBk0AATWFKwcc7kVoQ Rxl7Hf3I9MEWXJESRL8MhAXh57z+yulRFzlaRvHrk1ZHewV3XwtMxZYaXAzp/6WIwaR/ riGP2hMDgOKPHhaVA/c2ytOSiwQAlyM5UHC/dvCkSNM9nEU21U4G60PehDoKYtY8LMtq QCOtQcuAuUC9+IB47OUMJg9+/MpAGhRnm49aiaZyy3sseYbumsGTfInqKV13N2zzxU8z CHzH6YXHeBtmsDG1ChFMO4IbJSLKoyvhZgt1hWhq7zOTX8XmJQTlw0Bq2s5IwnebK/lF R+fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774973347; x=1775578147; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=IAjANhRSSjFG14Tt4RrpUuJ/1k05o3tXfxT9ZI8DWb4=; b=JVJc04KsuW9iSAxdD68GqW98w8FTIQGvwoGebxc6CDvii968NChUJEHRx6Ucxs55lw ZpWVOOFEJ1MQRlj8mom2yAtYV24wAXo40PtrTgTcJjv7a4+Cz9rgrkLk7oLj79Hky33m hgjc8xRXCJPqJoSqi9txpL26GJNPx/tu0KkfqUTT+uSaQykun6Lvj/aZhrzdrdUYf742 AmeRNXtgsOalr6sNSbPM99BVh8EnSWJTEACfLBjZ8v2oQFapHqLqyanRoTzxw5dfANzc 9Zme7cTE125FHW3XuTYho+Tcd+xhqcFvSQpe97KeIJahMGeYSqVAa51MCCu/k0vIIwp/ GYUw== X-Forwarded-Encrypted: i=1; AJvYcCVc6vbTrJDGfli+zRyCkldv09sOu6NNHDrKe5QuGf9MgFi7nmcxQ2ONRkBnU0SGQCOTdt9oPEl9aujGP0c=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5hb8VVH2RgRYXF4fq3uu3Vz8l/8XvTKSLlYP0mlDfJIgL38CT k7bp9/RqULGUI1SJZb1JQwgR6fnxxirVZDkYV+zrJNy1+e/uXYuM3N5XZ7/ZiQ== X-Gm-Gg: ATEYQzwvuUvpq37ZyZOfHX0qXJ106uASBr+E21Xv+qZ0osdUiBcYe4zsKRCQhV7XEFL ocm0PMFQeMQLnJxhDD6SY9BjRY0h+TV2Q6MhMt5Wh9YKd0BctTIPJ6drP9w6sKEbreruF+ynTJ4 keDZUpiBgy2AHRDe3iCxzkO8LsbuKYUJD+lZoYJD3m31h2x8w1nG88cypr6gZz/WkGwpJ57Ogo5 9NRO2vEVSajYoo/A8ibF4cRe3IX1znWtbQXyE9X9trB0Llkx39LetD2p+ATnGUv/ddDzxxOt7m/ fROfoe3FrG0Grl3B7E+1jM7p7iqgqa9kb5d9BZ8/E6ui94RIEwLtmousf5Q6RrQzMOUQ6uHq/9s EVEsC945epcpzXfwP7nfB29sFjSEhIEVeIXkkZW2yMQMoXJ9YILrf/RaWLX3EOyrI4SM3q6+HDP g6n/X8Eto5ODw5Qkgmzo6QKAg34SbZIJberpTv3tNEa7rWpZAPtZHOJf3EEfdJELhRw3kOpIwjp w== X-Received: by 2002:a05:6000:3106:b0:43b:45f5:eec with SMTP id ffacd0b85a97d-43d1504d2c0mr401055f8f.4.1774973347360; Tue, 31 Mar 2026 09:09:07 -0700 (PDT) Received: from f.. (cst-prg-89-171.cust.vodafone.cz. [46.135.89.171]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43cf21e3602sm28792632f8f.4.2026.03.31.09.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 09:09:06 -0700 (PDT) From: Mateusz Guzik To: brauner@kernel.org Cc: viro@zeniv.linux.org.uk, jack@suse.cz, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Mateusz Guzik Subject: [PATCH v5 3/4] fs: handle potential filesystems which use I_DONTCACHE and drop the lock in ->drop_inode Date: Tue, 31 Mar 2026 18:08:50 +0200 Message-ID: <20260331160851.3854954-4-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260331160851.3854954-1-mjguzik@gmail.com> References: <20260331160851.3854954-1-mjguzik@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" f2fs and ntfs play games where they transitioning the refcount 0->1 and rel= ease the inode spinlock, allowing other threads to grab a ref of their own. They also return 0 in that case, making this problem harmless. However, should they start using the I_DONTCACHE machinery down the road while retaining the above, iput_final() will get a race where it can proceed to teardown an inode with references. The easiest way out for the time being is to future-proof it by predicating caching on the count. Developing better ->drop_inode semantics and sanitizing all users is left as en exercise for the reader. Signed-off-by: Mateusz Guzik --- fs/inode.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index e397a4b56671..013470e6d144 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -1935,20 +1935,29 @@ static void iput_final(struct inode *inode) else drop =3D inode_generic_drop(inode); =20 - if (!drop && - !(inode_state_read(inode) & I_DONTCACHE) && - (sb->s_flags & SB_ACTIVE)) { + /* + * FIXME: there are ->drop_inode hooks playing nasty games releasing the + * spinlock and temporarily grabbing refs, in turn opening a possibility + * someone else will sneak in and grab a ref while it happens. + * + * If such a hook returns 0 (=3D=3D don't drop) it ends being up harmless= as long + * as the inode is not marked with I_DONTCACHE. Otherwise we are proceedi= ng + * with teardown despite references being present. + * + * Damage-control the problem by including the count in the decision. How= ever, + * assert no refs showed up if the hook decided to drop the inode. + */ + if (drop) + VFS_BUG_ON_INODE(icount_read(inode) !=3D 0, inode); + + if (unlikely(icount_read(inode) > 0) || + (!drop && !(inode_state_read(inode) & I_DONTCACHE) && + (sb->s_flags & SB_ACTIVE))) { __inode_lru_list_add(inode, true); spin_unlock(&inode->i_lock); return; } =20 - /* - * Re-check ->i_count in case the ->drop_inode() hooks played games. - * Note we only execute this if the verdict was to drop the inode. - */ - VFS_BUG_ON_INODE(icount_read(inode) !=3D 0, inode); - if (drop) { inode_state_set(inode, I_FREEING); } else { --=20 2.48.1