From nobody Thu Apr 2 12:13:09 2026 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 2C4432FD7D3 for ; Sun, 29 Mar 2026 17:20:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804822; cv=none; b=t6qrDP+U/AJcmUkGo4tjjRz+LEwUMXlrguekYfYvcr+dimb6v78EjQ93BY9KLnBtoM7baWGarELJ44H/d3PX4WYKJ3v0a1TeD10+T5oaKYe15THpH3VE/o+0Fc7OnVi2fzTffp5aP8s5MxlB27C5jabrjLYZmEHyHS5AXbZ1VFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804822; c=relaxed/simple; bh=3wWUg5LSl4md60Gl4sfBqptcrZXuVuHRnorf8MypEC8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BHFNDAxq62Ve+CAdqEEs8oGNFQ8VfLrkmkgJWXd7vGyN6pZeo/omzjEEXIPrKPbgsOB0r7NC8IAW1nMuJ/ynz0Xt49wyKWCoPqJEmhk0UNQgJstPMku5HaZa2s0f5Eukw/ZBx1Q30ws55f9ItkHF8c6Bc7yUq8QNYGBKRnE0iv0= 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=PAK+FuEw; arc=none smtp.client-ip=209.85.128.41 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="PAK+FuEw" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-48334ee0aeaso31228745e9.1 for ; Sun, 29 Mar 2026 10:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804820; x=1775409620; 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=tE4V2dhR2jT7NrbkOWfwwUAC12jyCa2VsCL+/A8CWNg=; b=PAK+FuEwo/FnUbA43J7PQTFpjD49fEADnsWzoNXP89Wzi+9d17xRcJSmTE2U+w7JbJ CM44nYK+RDKJXs6YIV0utHwXFipKwrrcsA3scUJkYDJ4u4yvAnw766M0/xgNWq/wBOzj oq0eC7aybl7+TNoOEzkVhFEarq9tvG/609S/73Rn414ffX39/A9PD0hVCAJDic+MRMVc Lix/rkYrEkDtGYChQUMFmid6TxGisngmR4gjjs0IR/Ny2dxGpvfuZcclqHSL7K4JIbZp Wen7ZAplzGGOgyu7n4DmCNJ5QpV7AvU8ExpcWRQKW0/ZE7N2xAtbdS5ZxfsHPB1jzDdv 8ksA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804820; x=1775409620; 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=tE4V2dhR2jT7NrbkOWfwwUAC12jyCa2VsCL+/A8CWNg=; b=hBj95o9gMmA5t7elyw7Lat7TZrZQuGQWWAsULsbZKU7hJVipKgNURHAhPuWNra7mgi 1DQ1sj8bY0OFj62eW++2Z3HnAzdB2jpghsutTe+xqu7jeCOBqgwGMlJIsXvVkxDUNnpl y2P6RwlmUthKWt8t66Z7Uung8hBrLutAPUhH7wrgAxc3mq0tktusdnJjZgpEEqh0TpHR pPyt2o8vmDm2NB82OiBRLStZUXce3jE+HRsJx0CwLTyzA89xGqrM+LEt3kTucj3dKhHO ISheaF9/GmFWYGZ2nIm7pGuL9tIc+xEXuLm5MnxrISXn/CgmhWmcWYp3Vl+fZx8lFe5d DXzg== X-Forwarded-Encrypted: i=1; AJvYcCXp/sA4W6UsawToHCm6C6lnpbhkOg/A/PYzRqv5ku3kGnzYPuexK7lml4i5uEAspo+wo+/SbysKb67PbPY=@vger.kernel.org X-Gm-Message-State: AOJu0YydnrT0d6JmO5t9WI/XoZ/nOrYsBkjl42cbznkbwRVrT3NVlVzC hkLftS0UMrIFJFR/62iyZBVmam/wiYFukhLrgR0uadbkQALHqVXCVOA+ X-Gm-Gg: ATEYQzxuHiRNNdk0pfimpkkddvF5ndrBLFuWxVCNmImGaObMBCZIW9kRfxplTM1mXzF uhnE0hkK7GK0c1mlGUrUbAVVwE++W8NI5tbCb0bO3o4L5zav/IJ2iUmIdf2VEyRT1LK6koz1C52 WXc70QT3160GE9TJoWeH857xAkUGTxOJxAZJOUy/a4EovtPKDNuBdt8hFFDVXmoYr1BqYj3D8Gu lK5J9wzaFUiqOpSDMMYfWgrF99FhczwcOrPfIykN3xzIsov6wMbpeRf68sCs+uX/tQeTJ/9mKKu je2LoK4cjtFka4ckb5dF4xuI0lB4zf7G2h+GOB6xb/mSQecljwcNP6Q5nvVmrDI/UMepeubCMVw BltMOl9AOmopT4h6OILddoMF8LXK9ghH6ntLIDjC/fZtYWbn/RPyLTe8Goo9HFQkOROjr3IrjQh DDKdjUhhBTpXLrhylnyXBWX5D0aAVKBX5JKxgR+/GjPjjZmFEdJvw1SIc1gnZns2HTIDBDc7TB6 w== X-Received: by 2002:a05:600c:34c3:b0:485:3f65:94a1 with SMTP id 5b1f17b1804b1-48727eda72fmr171565335e9.18.1774804819534; Sun, 29 Mar 2026 10:20:19 -0700 (PDT) Received: from f.. (cst-prg-89-171.cust.vodafone.cz. [46.135.89.171]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4873cd7d039sm9776805e9.15.2026.03.29.10.20.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:19 -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 v3 5/7] fs: handle hypothetical filesystems which use I_DONTCACHE and drop the lock in ->drop_inode Date: Sun, 29 Mar 2026 19:20:00 +0200 Message-ID: <20260329172002.3557801-6-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260329172002.3557801-1-mjguzik@gmail.com> References: <20260329172002.3557801-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 transitiong the refcount 0->1 and relea= se the inode spinlock, allowing other threads to grab a ref of their own. They also return 0 in that case, making this problem harmless. 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. Future-proof it. Developing better ->drop_inode 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 0050eca6d83f..a417abc64822 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