From nobody Thu Apr 2 04:15:42 2026 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 79F263CCA1A for ; Mon, 30 Mar 2026 12:26:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774873577; cv=none; b=HVgGgHSvOUDURIYToYuEbOkefi2rER3KfM7dpiZ07KPXYfU+p3abH24GBRFPwUAh8D6BMqj9gDc9MaTl2n9PJZ7WUpFC3qkOLNEDdk+90si+EH0+ftXclKtd9aKl/I+5WkN5ogcawduXTxZ6x3Qrx+Z3yROnQVZe9C4p05w5ms8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774873577; c=relaxed/simple; bh=HHQdlPXWfQ0bdc5Tr17fd+OYag4Slbu5vp5M9ZuZwb8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eJEidowZWU7CG9VwbyuTpyLaE95qohAcEBndMMWSl3/x85NRJr9RvbnUXpCAv8QYWK6mgmuefnAtGpRS57bCzcsll4PhhnnMpsCarYdE63y4UiP2KkDeE6lNqd+MtyAlQH3h5Ov+HWbAWQWTB9ZXzerqSny3hji/pxlRMwvW0EQ= 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=FvxNZGG/; arc=none smtp.client-ip=209.85.128.44 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="FvxNZGG/" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4852b81c73aso37587115e9.3 for ; Mon, 30 Mar 2026 05:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774873574; x=1775478374; 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=FScv3i6uAosiDBVQ3BCLq5X7rIHAG+u+NDcPUFmbj9Q=; b=FvxNZGG/yIbXKzYYDidIcHzHENx5nmzfMD3FdZ6OBA3GW1x2VIKx+XDmA0SP8dJGRA u4iXClCkqpOkFeo5gqHC/jrehzFUZ32XLsUpSm0OM47yghO16qMiSGqQoMUnZ03Wj9oq jdPOJ3s2fPUO1AAhGjTBQAMISxy+WjQ6I24oxK/cst6B8SSSaOCrlxI4vIj9OjrMbHpa jlVNSBL4w+jvpf2wK8gWHdHaH2q5kz4q0eOGp92XGN6JJoSPgxl14T1+gjrFqq3T5eCJ 1UD0/k6WXtEmmhi6TiBhOIwEnt8hLkXIITwiPQUF/VKSVFIN6liojrAHRkVd59Ee6/t9 ZmMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774873574; x=1775478374; 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=FScv3i6uAosiDBVQ3BCLq5X7rIHAG+u+NDcPUFmbj9Q=; b=kNYjsJuPFLWM36aZ1+NDDQo4B5GuAcsB/OhmGk7HSpXsQRuVKGgc/bdoxTUrYP/iGR JCmdyFU0rNKtPKS2U/UfXLWZtJtXLuHQADPLodiHg0Sx8kwnWeE53lejD6u1Ls3gHeUn 6GEPeDwuYikJke1FF3IBl9eIt40zrK3QZ2w1VluBepUYNQVXIadBwGB63XJ+Tyod7EXy owJtJRcHafWBfwRlH5fAfsBqb5T/p4vqs0n4t5wvFXPFD8TLL4Qcr6WKht9CzyuXxOwJ N/CabcdQ/GbSa5fPcVZUtEB3cnLaXDOJqhIA6Sl8Qi0vLn+VDXxhZftmRqZuHgSBJG2M EMkg== X-Forwarded-Encrypted: i=1; AJvYcCVqZCQPDfZhEEoMV+C9udXR5KKE/msCZ2SLngfVuSYeoZjZd1aQ3DTa1iGNgx+A6NGbcOiFIc4MaEj5Epc=@vger.kernel.org X-Gm-Message-State: AOJu0YyRmUc8+y6hKjFexq1/JTr6oa12V00prQrG+gjsyDWDgM55Xaq8 vpMqY7tvHQF5e+hq1VYg4qKZ5+1wuzlpjLXy+3GMzyPPxiqgD6/H/mc1 X-Gm-Gg: ATEYQzwZWHUO9btM/bYNfYPtlgxzoZBIgz8hbtXc2emNk8bnhmV+GWFoMPJWxwnsgfH eFAIuw4xz28rAMlQ6NCs3CAlmjz+Kc/osj+q0qlHLl6Xxl1BiAdSqbPkECuWro3eXkT8OpYjxTR lq7VfyxqNokeGCEDpBGD75dBZoIsb8KMEorrHMfiPCX94G++uZciysaht/w5QJY/v4kWdSAk1Pq I/6K078xjaTjiJBZvJnJNgciIZX32JFMwsohB7BdnOBluXynKCVcDe/DF6J9CH67JeQv/Hh9oJD 4K/mJtdnO3z4uDz3kMA0u3OhK+l48pwY075YoiQYKt/h/FMM+pNNPJhebmKSVzEBD9f4qTA4hIY 7VNbBjf+xm4+ARrPWt2BaKmhq2ce6NZm79DUg2TdpvtQ9782GMX3QX0i+PEKQIROIZ9tvYR3j1a oDdwP5FRd/k7Hi3sOXCvn/fIVgBLWlYAHMi/LHmjmL0hvDKpkFzj2RkqbztKHzx3Hoozw4BY9Xd g== X-Received: by 2002:a05:600c:1d15:b0:485:ae14:8191 with SMTP id 5b1f17b1804b1-48727ee99f6mr200832315e9.5.1774873573523; Mon, 30 Mar 2026 05:26:13 -0700 (PDT) Received: from f.. (cst-prg-89-171.cust.vodafone.cz. [46.135.89.171]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48725da0333sm113541525e9.2.2026.03.30.05.26.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2026 05:26:12 -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 v4 3/4] fs: handle potential filesystems which use I_DONTCACHE and drop the lock in ->drop_inode Date: Mon, 30 Mar 2026 14:26:01 +0200 Message-ID: <20260330122602.3659417-4-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260330122602.3659417-1-mjguzik@gmail.com> References: <20260330122602.3659417-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..a06edd557e84 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)) { + /* + * XXXCRAP: there are ->drop_inode hooks playing nasty games releasing the + * spinlock and temporarily grabbing refs. This opens a possibility someo= ne + * else will sneak in and grab a ref while it happens. + * + * If such a hook returns 0 (=3D=3D don't drop) this happens to be harmle= ss 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 (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