From nobody Thu Apr 2 10:44:29 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 3C1E22874E3 for ; Sun, 29 Mar 2026 17:20:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804817; cv=none; b=RWnRhelr9dZAez1uXgrXkSQjmTp1adLvHje2s4LtbnfwznZh5ouKBg3DaAk3pJVWQOEq5UpvT1gsDwCoxcLngm3JFm5xucygnkzp6JIzj6LxMdYzucr8TMaktRDp8S+rCl/Qz1jE9vTJtqdDtGtN/+GsrCKFK0ATADDPIicSlf0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804817; c=relaxed/simple; bh=q6/M4OdZtc+alS1DwnvRyoUObRGRNii9NRUn255hTcA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KF5zoVVNA0/2A7a2gCKuAWr14gpSt3H/nOdQioPPdC5XmwHfr58ASCKz7s8MDS+D+v2rZNyJhLqz73/E1TfN8eoBiNfCiapGeR3XgsocWU1Rvjwk2asEPAsWTEI9pS/zSR6W1IQd73NBkrEOyyNFeuXd8HisL45rRVevi0mQJTc= 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=KlsVpaM9; arc=none smtp.client-ip=209.85.128.47 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="KlsVpaM9" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4852e9ca034so35841745e9.2 for ; Sun, 29 Mar 2026 10:20:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804815; x=1775409615; 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=ALuBtMTHfXISMz1y0xRBvnd5xru82Lmp3xXwNF/dfxA=; b=KlsVpaM9Yhvb1+q2rQjP/m0pJ6Q9uu02dunJiXVzr9AHBLjmaRqRVJ7nqL/qxmyE2y aMRAaptKmRzCAXhY/9cz+hCL9Ks+2+4gonZJTJdthR9SX4cliUOHlk4ElT6FqY6de0vI o51E2RxyPYa9G+NG79EucxAe8Bb1O0hWvTd9/G3xbXNTVrtmGyuSuJb6NBhaSBEDfvoD 2MLsHQCdWMdB6Lm56faQ/6E3jx6yNKgeTGNjc6R0Wd6jNxM1D9Aeu4Ojh3WSo+MPI9D1 4MBAL4dJb6xvELuV0rDvDHdqvm+YFBZ+Pe4xetg6jKOAoCRs41PcTYzW3Jym0U8eVZsy dEtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804815; x=1775409615; 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=ALuBtMTHfXISMz1y0xRBvnd5xru82Lmp3xXwNF/dfxA=; b=kS0HilKZ+RUgBXBWoJm3wcTGAwqJe3bbVHq6m5Z60UZlyljOwfOo6V7mGCFDeesFSo Vm3PTXZ7tob4uCjzIMDtmwwKNWWrIcY69uetfvR8ha55HA437LK0QR5Rq0k1iqtvo7xD JfmrOPocqCQPu4OalbUuZoxgstjYlpVrm1eOq2Aj060oOK92QqRAkJFxpWaI3yp2BfnH gY6Z+FrQxqKxM75HQuekwkgHmz22eh8aPtA6BEFEViIIJlI368OPqVtAfI+FjUMdB6WT PjD7L2jnAUn36/F1bISiQXzcd9PEnwvo7ISsgGmbSNA7c7pKTnb40OQ6ux7QsmXsy5jW H/9w== X-Forwarded-Encrypted: i=1; AJvYcCWCU8jZsPghGHzIc7FihFYwO+2o9aHYwEUAsRTV5cUyhGNrO9nuLI2gNYsxm9Jjt0EYEtKFY13gYUOgL90=@vger.kernel.org X-Gm-Message-State: AOJu0YwFXQGCvGo24fS8hU/7vNpOSdBFXKS4YLljmE9+K2TRDPWTWPs8 79ls3/JwIj8kBZPf5h6XquxBpk8oG7sF9b9dskxnNAlCzYq44F+5a48A X-Gm-Gg: ATEYQzzRydt5/d4LPy3nBtEBNVeyaGQqFZfZDxoQ0dFkyxeLwW4CRdJuDl+y96MEHN5 68EgvE0h7bCsxRZjqKrac+QAXz7dm74RIe4H7P3GOUfW0f9fo7HQqCDMiu5yXNQNJCL50mdgqu4 amYZ2Dff2wbwFZXqh7yI/YxHOYQWyOo1BUGm6Gzru0G7+sc74TYL1vinaYmf1tBdDfcd9dCd/rJ YolTlklLNNvHEVWAZgcnKsAEl5MfVgxNfBj18Z9tCwAFkWep6DNLWEYlbHsUGdumfi0TGHgh1BD KsFRXBJh6qwqNJ8c7IfqN+hgQIE6ceYzEKceuVbq41ESurgQHc1gElG9K2UTppptmjFtY853fw9 v1r/8WFjbUGzlrJ7pnvA/0/P4YsfDKiza48Wu9RXGAFYIqFifxLbWRtNAA1IKHVYOiM6OYfNpCf thiTsfaCniaN5zQNgRrYe1uCOwP1oVGyOMoiNB9hXVer+q/fzLfIcsjpSPEkDPW9pqIH9GL6+mc Mk5+HWNJh9m X-Received: by 2002:a05:600d:7:b0:485:439b:683f with SMTP id 5b1f17b1804b1-48727f5fe73mr129079015e9.20.1774804814537; Sun, 29 Mar 2026 10:20:14 -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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:13 -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 1/7] fs: add icount_read_once() Date: Sun, 29 Mar 2026 19:19:56 +0200 Message-ID: <20260329172002.3557801-2-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" It will be used to denote the caller acknowledges how the count can change from under them. Signed-off-by: Mateusz Guzik --- include/linux/fs.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 8afbe2ef2686..0cf27085a579 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2225,6 +2225,11 @@ static inline void mark_inode_dirty_sync(struct inod= e *inode) __mark_inode_dirty(inode, I_DIRTY_SYNC); } =20 +static inline int icount_read_once(const struct inode *inode) +{ + return atomic_read(&inode->i_count); +} + static inline int icount_read(const struct inode *inode) { return atomic_read(&inode->i_count); --=20 2.48.1 From nobody Thu Apr 2 10:44:29 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 C9C86385523 for ; Sun, 29 Mar 2026 17:20:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804819; cv=none; b=ucMK8KxV0c6adfvbC/s7MusUx8fCPIMHV+nFr2H4nZiEta170BZwnfh0n4+lNab56hKUHVAhli6Laxyi7zHozDmVzdNAEY5FL+Fpok8Xo3iw6+7bJX1EYvTgLIpiM89h9teLIsnRG2M9lN0cutqukmdrBP1ucIhZgbU33y4hkKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804819; c=relaxed/simple; bh=8xxPYCYi3jBF56gf5sck7mIQAohhuUh0PXeHCMgHG/g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HuamYfgTxT7lmFkye3HzXcPgArRXT+i4Poq/1hQm8al5t2yY/DBwSdHSbtGCeYDxacirnpHEUoBNf+vx/qioLf4hNCzdimypVQKTa4BcbxuqxFhalZSP8MAqhOQIGbhc4AjO2HHnWAIKgeq7gpy1rS6mMjHivuvEh4R3cas3lLY= 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=Ttxi0UNh; arc=none smtp.client-ip=209.85.128.51 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="Ttxi0UNh" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-48700b1ba53so33169075e9.1 for ; Sun, 29 Mar 2026 10:20:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804816; x=1775409616; 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=a1fu/byEUJ6qDFBkZYB9aFVVTkqU/ACSQVsqGJXvhx0=; b=Ttxi0UNhA30K2/D9a5MlnIwp04Ti+6LOyt/vP/oQSlzm2kNS8fC73vOQ8W1os4kAL2 vOSuW1ZFR6ag0wpiDgLQ4fLQAWEOOUg0FT0FWqJY1jsnPpmbHQ7yjqPhJjZXdM2Zwild wRlw8tSB1Mp89qoFydhzpjmbSE0kAr8Cfrj0EvvjEn8Z7tiuB4NjU26s962A3uWuaoUe a1t5FMcXPdOSOfEc1LB+C0aLPTi3K1rKfR/4kGthDA9NfQUbq8HVjnjUtG72HcMEPR6Y eTBs8+iXHF+flZjbU7rpcxdMw6MgNllct0Lk9PQ/oTkxIqvAKBGFIEiDsgZNYwhgsB6m 31LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804816; x=1775409616; 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=a1fu/byEUJ6qDFBkZYB9aFVVTkqU/ACSQVsqGJXvhx0=; b=OmR2W0F6gWahzPRUDhS822oFOfBjczm1KZhLZL/5WAQbonY0aIAvlA0KqeC3RLQsQs 4T5Y0NKlQO+h73CAE02YSjHbqx9FghQzAxdVnwXoW9dhFyy5X8spMkvNCg2Ny+gV1K4I i1sVP65yqMwZtAvoY+xBzM+XPk2FoF9SMkmQ4eTjooB4pfazcXEDyrtZdZgD7q8VBlSC /CjiENMFYhFW+IZT0L4TIRSgWTLZQfm8XxedopaFZaVZ5UbiatlpG9+zG1gv33B9VVdd uRvoTiA2NRSRVw0VziCTpXa/ZXoo0tzOBrTuGd/Qi4U0V+XRBz2jlBFKONCphvTSUDEy beqw== X-Forwarded-Encrypted: i=1; AJvYcCUgwomR0l/L8YOnlAteEcT6R18sA2IKX/rijPqs6aXQ1ywAIiRzh4QWxs8c1k/2ktG/SIg2nErcO1+W0Cc=@vger.kernel.org X-Gm-Message-State: AOJu0Yw12XHOf4l2GxWfwSRw7oAZX4Ti2Pywwue7jtbT6qd/BByUysAq tLpuAhFEW25uHtgBA6NcjOpZin6HLDJauu8XzZD7oQweVGProXes2xWi X-Gm-Gg: ATEYQzxBZFuCYvdMwE8LqhscQoSCcZYXgrzUSy2aRo14QpTKc/QkSjRJn0Y5hmr0yJL aWamk5tsUGG30bbR8DRX8/gQXoBJJtMJ84gO99SWohoFD8Cg8I2poW/w+4xPx1mPOv0mA0PsIRI Z+G445uyX1DX17+LkfH9xTrmM9mfwf5mHnevZ41XdG9q6XogFz7vT7DQ7kj0938cwgKOuYXA9xs /v+M8KoefADeNkwx1639wSG28uk8nPPSUf/nLXW8m45nQB6eqZDX/BR+gGFxEUS740LcQH1t+UP DqVePQ+Md4BGWkPT8xfZuubbTkoFh8rqz4VadXzf+O+wNO67QKvV4GZHi4ihd9M5Kk3kGYdRDRk 2uYT4XsbwVg0e+O8R8jfaL39bNgC8orcW5NTyMWEsmyfCS/ShkezIEHcNEdTZEzlN2sDTtU3EWM /CEmeFtBElEyJDwm7Y5npqz2YU5qej8JUTRqZmaF9WMFOr7e8apmoqLzl3lvk/kJ/BPRtVGjjqY Q== X-Received: by 2002:a05:600c:8b03:b0:483:8062:b2f with SMTP id 5b1f17b1804b1-48727ef16aemr196607465e9.6.1774804816012; Sun, 29 Mar 2026 10:20:16 -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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:15 -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 2/7] Use icount_read() and icount_read_once() as appropriate. Date: Sun, 29 Mar 2026 19:19:57 +0200 Message-ID: <20260329172002.3557801-3-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" Don't open-code ->i_count access. This is expected to be a nop. Signed-off-by: Mateusz Guzik --- arch/powerpc/platforms/cell/spufs/file.c | 2 +- fs/btrfs/inode.c | 2 +- fs/ceph/mds_client.c | 2 +- fs/ext4/ialloc.c | 4 ++-- fs/hpfs/inode.c | 2 +- fs/inode.c | 12 ++++++------ fs/nfs/inode.c | 4 ++-- fs/smb/client/inode.c | 2 +- fs/ubifs/super.c | 2 +- fs/xfs/xfs_inode.c | 2 +- fs/xfs/xfs_trace.h | 2 +- include/trace/events/filelock.h | 2 +- security/landlock/fs.c | 2 +- 13 files changed, 20 insertions(+), 20 deletions(-) diff --git a/arch/powerpc/platforms/cell/spufs/file.c b/arch/powerpc/platfo= rms/cell/spufs/file.c index 10fa9b844fcc..f6de8c1169d5 100644 --- a/arch/powerpc/platforms/cell/spufs/file.c +++ b/arch/powerpc/platforms/cell/spufs/file.c @@ -1430,7 +1430,7 @@ static int spufs_mfc_open(struct inode *inode, struct= file *file) if (ctx->owner !=3D current->mm) return -EINVAL; =20 - if (icount_read(inode) !=3D 1) + if (icount_read_once(inode) !=3D 1) return -EBUSY; =20 mutex_lock(&ctx->mapping_lock); diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 8d97a8ad3858..f36c49e83c04 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -4741,7 +4741,7 @@ static void btrfs_prune_dentries(struct btrfs_root *r= oot) =20 inode =3D btrfs_find_first_inode(root, min_ino); while (inode) { - if (icount_read(&inode->vfs_inode) > 1) + if (icount_read_once(&inode->vfs_inode) > 1) d_prune_aliases(&inode->vfs_inode); =20 min_ino =3D btrfs_ino(inode) + 1; diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index b1746273f186..2cb3c919d40d 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -2223,7 +2223,7 @@ static int trim_caps_cb(struct inode *inode, int mds,= void *arg) int count; dput(dentry); d_prune_aliases(inode); - count =3D icount_read(inode); + count =3D icount_read_once(inode); if (count =3D=3D 1) (*remaining)--; doutc(cl, "%p %llx.%llx cap %p pruned, count now %d\n", diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c index 3fd8f0099852..8c80d5087516 100644 --- a/fs/ext4/ialloc.c +++ b/fs/ext4/ialloc.c @@ -252,10 +252,10 @@ void ext4_free_inode(handle_t *handle, struct inode *= inode) "nonexistent device\n", __func__, __LINE__); return; } - if (icount_read(inode) > 1) { + if (icount_read_once(inode) > 1) { ext4_msg(sb, KERN_ERR, "%s:%d: inode #%llu: count=3D%d", __func__, __LINE__, inode->i_ino, - icount_read(inode)); + icount_read_once(inode)); return; } if (inode->i_nlink) { diff --git a/fs/hpfs/inode.c b/fs/hpfs/inode.c index 0e932cc8be1b..1b4fcf760aad 100644 --- a/fs/hpfs/inode.c +++ b/fs/hpfs/inode.c @@ -184,7 +184,7 @@ void hpfs_write_inode(struct inode *i) struct hpfs_inode_info *hpfs_inode =3D hpfs_i(i); struct inode *parent; if (i->i_ino =3D=3D hpfs_sb(i->i_sb)->sb_root) return; - if (hpfs_inode->i_rddir_off && !icount_read(i)) { + if (hpfs_inode->i_rddir_off && !icount_read_once(i)) { if (*hpfs_inode->i_rddir_off) pr_err("write_inode: some position still there\n"); kfree(hpfs_inode->i_rddir_off); diff --git a/fs/inode.c b/fs/inode.c index 5ad169d51728..1f5a383ccf27 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -907,7 +907,7 @@ void evict_inodes(struct super_block *sb) again: spin_lock(&sb->s_inode_list_lock); list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { - if (icount_read(inode)) + if (icount_read_once(inode)) continue; =20 spin_lock(&inode->i_lock); @@ -1926,7 +1926,7 @@ static void iput_final(struct inode *inode) int drop; =20 WARN_ON(inode_state_read(inode) & I_NEW); - VFS_BUG_ON_INODE(atomic_read(&inode->i_count) !=3D 0, inode); + VFS_BUG_ON_INODE(icount_read(inode) !=3D 0, inode); =20 if (op->drop_inode) drop =3D op->drop_inode(inode); @@ -1945,7 +1945,7 @@ static void iput_final(struct inode *inode) * 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(atomic_read(&inode->i_count) !=3D 0, inode); + VFS_BUG_ON_INODE(icount_read(inode) !=3D 0, inode); =20 if (drop) { inode_state_set(inode, I_FREEING); @@ -1989,7 +1989,7 @@ void iput(struct inode *inode) * equal to one, then two CPUs racing to further drop it can both * conclude it's fine. */ - VFS_BUG_ON_INODE(atomic_read(&inode->i_count) < 1, inode); + VFS_BUG_ON_INODE(icount_read_once(inode) < 1, inode); =20 if (atomic_add_unless(&inode->i_count, -1, 1)) return; @@ -2023,7 +2023,7 @@ EXPORT_SYMBOL(iput); void iput_not_last(struct inode *inode) { VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), in= ode); - VFS_BUG_ON_INODE(atomic_read(&inode->i_count) < 2, inode); + VFS_BUG_ON_INODE(icount_read_once(inode) < 2, inode); =20 WARN_ON(atomic_sub_return(1, &inode->i_count) =3D=3D 0); } @@ -3046,7 +3046,7 @@ void dump_inode(struct inode *inode, const char *reas= on) } =20 state =3D inode_state_read_once(inode); - count =3D atomic_read(&inode->i_count); + count =3D icount_read_once(inode); =20 if (!sb || get_kernel_nofault(s_type, &sb->s_type) || !s_type || diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c index 98a8f0de1199..22834eddd5b1 100644 --- a/fs/nfs/inode.c +++ b/fs/nfs/inode.c @@ -608,7 +608,7 @@ nfs_fhget(struct super_block *sb, struct nfs_fh *fh, st= ruct nfs_fattr *fattr) inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode), nfs_display_fhandle_hash(fh), - icount_read(inode)); + icount_read_once(inode)); =20 out: return inode; @@ -2261,7 +2261,7 @@ static int nfs_update_inode(struct inode *inode, stru= ct nfs_fattr *fattr) dfprintk(VFS, "NFS: %s(%s/%llu fh_crc=3D0x%08x ct=3D%d info=3D0x%llx)\n", __func__, inode->i_sb->s_id, inode->i_ino, nfs_display_fhandle_hash(NFS_FH(inode)), - icount_read(inode), fattr->valid); + icount_read_once(inode), fattr->valid); =20 if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { /* Only a mounted-on-fileid? Just exit */ diff --git a/fs/smb/client/inode.c b/fs/smb/client/inode.c index 888f9e35f14b..ab35e35b16d7 100644 --- a/fs/smb/client/inode.c +++ b/fs/smb/client/inode.c @@ -2842,7 +2842,7 @@ int cifs_revalidate_dentry_attr(struct dentry *dentry) } =20 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_t= ime %ld jiffies %ld\n", - full_path, inode, icount_read(inode), + full_path, inode, icount_read_once(inode), dentry, cifs_get_time(dentry), jiffies); =20 again: diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c index 9a77d8b64ffa..38972786817e 100644 --- a/fs/ubifs/super.c +++ b/fs/ubifs/super.c @@ -358,7 +358,7 @@ static void ubifs_evict_inode(struct inode *inode) goto out; =20 dbg_gen("inode %llu, mode %#x", inode->i_ino, (int)inode->i_mode); - ubifs_assert(c, !icount_read(inode)); + ubifs_assert(c, !icount_read_once(inode)); =20 truncate_inode_pages_final(&inode->i_data); =20 diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index beaa26ec62da..4f659eba6ae5 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c @@ -1046,7 +1046,7 @@ xfs_itruncate_extents_flags( int error =3D 0; =20 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); - if (icount_read(VFS_I(ip))) + if (icount_read_once(VFS_I(ip))) xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL); if (whichfork =3D=3D XFS_DATA_FORK) ASSERT(new_size <=3D XFS_ISIZE(ip)); diff --git a/fs/xfs/xfs_trace.h b/fs/xfs/xfs_trace.h index 5e8190fe2be9..cbdec40826b3 100644 --- a/fs/xfs/xfs_trace.h +++ b/fs/xfs/xfs_trace.h @@ -1156,7 +1156,7 @@ DECLARE_EVENT_CLASS(xfs_iref_class, TP_fast_assign( __entry->dev =3D VFS_I(ip)->i_sb->s_dev; __entry->ino =3D ip->i_ino; - __entry->count =3D icount_read(VFS_I(ip)); + __entry->count =3D icount_read_once(VFS_I(ip)); __entry->pincount =3D atomic_read(&ip->i_pincount); __entry->iflags =3D ip->i_flags; __entry->caller_ip =3D caller_ip; diff --git a/include/trace/events/filelock.h b/include/trace/events/fileloc= k.h index 116774886244..c8c8847bb6f6 100644 --- a/include/trace/events/filelock.h +++ b/include/trace/events/filelock.h @@ -190,7 +190,7 @@ TRACE_EVENT(generic_add_lease, __entry->i_ino =3D inode->i_ino; __entry->wcount =3D atomic_read(&inode->i_writecount); __entry->rcount =3D atomic_read(&inode->i_readcount); - __entry->icount =3D icount_read(inode); + __entry->icount =3D icount_read_once(inode); __entry->owner =3D fl->c.flc_owner; __entry->flags =3D fl->c.flc_flags; __entry->type =3D fl->c.flc_type; diff --git a/security/landlock/fs.c b/security/landlock/fs.c index c1ecfe239032..32d560f12dbd 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -1278,7 +1278,7 @@ static void hook_sb_delete(struct super_block *const = sb) struct landlock_object *object; =20 /* Only handles referenced inodes. */ - if (!icount_read(inode)) + if (!icount_read_once(inode)) continue; =20 /* --=20 2.48.1 From nobody Thu Apr 2 10:44:29 2026 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 D366E3859F0 for ; Sun, 29 Mar 2026 17:20:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804820; cv=none; b=rPL5/vTMQ4h8jERpzrDZoi5Tdsgb1pyndSQ6Hulm0p4IxNNamyQojPs5/aJ8kquXZ0N2hErEtIX/TQQ2stXwy1CX+L2/4oVCBfTAxtNYrY3+I5RqBkxLMqptvMwau37EvZH7OiXGCJl1jM7N0Dg+UjXP1Xg3xDdfI/bJ/tKwvCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804820; c=relaxed/simple; bh=HZYHztId16pgGQ1AA2UfRM6DJRt8s7EcWewCjcgDwxM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EuzrUQyCCMinyLS6dsIAKkGC9bZE7gfR7uVWpBHlBfio20t53K7fWAPDg5q1KMPjJZut2NDv9yEa24dIsfBMclvbFWVl4N9w6wtXba5VayFBoFLrSL3kibM1j6s/wJTtfQMYF3g+DMcZuECiS0WhG0fz8NnFEzuks1zCEcST5Y8= 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=niFPxUXC; arc=none smtp.client-ip=209.85.128.51 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="niFPxUXC" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4873ce69ba9so1095165e9.2 for ; Sun, 29 Mar 2026 10:20:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804817; x=1775409617; 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=Wqq43BIgVMA1uE3zJr5f2QtdxqeDEFGIZU0biEFlpEg=; b=niFPxUXC/63chZUq+K34EG3QtUOVNXVlgdYPHjrBa8ngLmDm6bQak9thHc5kGYqhgh gCgsQwhOu40Wb3SmYD/qxSgcMlmidwlLD96+rO55faYzrpFBe6uoei+/pK5rzUa+OJGI V9faHJ678PgPTWy6SPazkJdL1XPygDKbS/D3u9Stjcsy/35J7IYkAWaIXR26fcdfSIdG OyzBIYt/IYROOGfg2+tWrM5EodpHYa5qzsfBGJI2Tx643b4A5LUbv5Ee5VJRnilRArCo DmHSPSXr4Cfx82Une3NckzzqieQVqYGJNjZG3iPPb15QLzjweH2sAUQTZF+TWFqxmJzA LX9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804817; x=1775409617; 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=Wqq43BIgVMA1uE3zJr5f2QtdxqeDEFGIZU0biEFlpEg=; b=ZwaUx6ggm1OLAI25boI8dMVGZNNHJ0gkj0/p/Nb38iqd4VllSgBVwMULLLJ8Rom6Su 496OHy9oUROou6yTXuQNAFLBpW0ebnGr5KIUoU2smPJsqxlhAzXduvKUcLDL91Gi7p4z IZQO+fAK2UATN89PzxnECWWksBJrOeGnUWP2d0VfO1I4ocQEbmWUm6UxLmXVNOVENG0S JdyUFGtj+jLYSnp6cF3s4sPBmdY5kG2nblJBSFsBe6LkUULBZVCQeywyix+RHugKnM4J hTJAniJ8RLL29bh1h8xUx2b9CEYXEO+fciK81bz/7kjodooM5RmJBkCznicTLeJiLP4L OoqA== X-Forwarded-Encrypted: i=1; AJvYcCXLcnBAK1h1q/+JVYtKtj8YvbDAYWncVOA+dYOygL3D8uUOmuhEhy4OiaLZmPC6tcmdE39M3vfErV6CNyE=@vger.kernel.org X-Gm-Message-State: AOJu0YzzZMWkyFKh0/po5cfBlFx581OjYALXBPYah8it9DV8L50OOObX Lhr41Cckjj4ZxB7951u3HPscqkdaF0fw39tDvIl/lInLR6DvCyHSFVeE X-Gm-Gg: ATEYQzxEzXRk9yFtWoe1yMm+U3iqKUWZ1342f7sMHuSzGB7bfn5lOsIhNHiSDr6lhm+ ILcUuJksLrANAg0DvrFKG2th++OqWw7gzbU8R+XEL/11sFlmZVYrRxlAdgLPB3f21+MKMpQhbGj u/T/rypTLxvRFNjTTFihwFh2OGnV6aA6/Rw9DeZJQD5L667C0Zgj2PYAXXmER1PWfRE5BpsYkXq tR1g+hMq+OsvGFI+tZkNfS/lGXLV1HJJKhZAjsrRft/jVYdRdj9TbCLvBEKD7M5Z3LmCVvx/zma iFdTd52U7AQI2soVeur7V4s0LVA122ih7AAf5ehvxuoqnsQOc/rm2rf2yvay5KgMgZ9habHy6Pn jpMLpVWCkT9OS6JpsLGoMLUfbwz1E8G/Is8udsc6a46gwFkvZoZJTdCBRppZn5Wnn1wpFd7Ekzi N6x5po6GU2NFKvK0xqQ3BEDJfIrJIKNm7vrcl3AuYJyun8xboMpTxaiuSD2twVqe4W/+SvUXPDK A== X-Received: by 2002:a05:600c:45c4:b0:486:fe45:483 with SMTP id 5b1f17b1804b1-48727efbed2mr164015635e9.22.1774804817218; Sun, 29 Mar 2026 10:20:17 -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.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:16 -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 3/7] fs: enforce locking in icount_read(), add some commentary Date: Sun, 29 Mar 2026 19:19:58 +0200 Message-ID: <20260329172002.3557801-4-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" Signed-off-by: Mateusz Guzik --- include/linux/fs.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 0cf27085a579..07363fce4406 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2225,13 +2225,21 @@ static inline void mark_inode_dirty_sync(struct ino= de *inode) __mark_inode_dirty(inode, I_DIRTY_SYNC); } =20 +/* + * returns the refcount on the inode. it can change arbitrarily. + */ static inline int icount_read_once(const struct inode *inode) { return atomic_read(&inode->i_count); } =20 +/* + * returns the refcount on the inode. The lock guarantees no new references + * are added, but references can be dropped as long as the result is > 0. + */ static inline int icount_read(const struct inode *inode) { + lockdep_assert_held(&inode->i_lock); return atomic_read(&inode->i_count); } =20 --=20 2.48.1 From nobody Thu Apr 2 10:44:29 2026 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 6778E3859FE for ; Sun, 29 Mar 2026 17:20:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804822; cv=none; b=hGUWLgRWBbfSkMkdrt/6w5Z9QWbBoOGKmPfDiM2ZYUJNI4+n1ZyRYs26k5/3tYVuqN8DMS47FRlP8iWEtzKea4TZ1mjWbfSsV+xxsQwH9OWcag5ayIwZ4b8rNT0CdBYq+wA7Frxy3T1ocm2eqEIk8VS4ZXgybkQVguELmtsR8VM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804822; c=relaxed/simple; bh=hYUpnp+Ac15RiBQCN2Hkk4rhsctI+fBGp2S/cJmDxI4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JKadtOq8sU+VF8sPTQUjhlz84yfhOfFV0rFPqVsck24sZtDoMi2tuhBFX9Oqvt7YwyKvumgHdqy5IR4lJRqXQ2Zc5FVolV7/Xcnh9l1nPXCbSAM+3PFknkE1DP0LwzGUnSwH6L6da2sgV7YswAItPmkRbXs1wNVHZPCyiUAIaDg= 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=raaOPlLd; arc=none smtp.client-ip=209.85.128.46 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="raaOPlLd" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-486ff3a0fc1so32765215e9.2 for ; Sun, 29 Mar 2026 10:20:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804819; x=1775409619; 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=eeU9c3QhXSSwwCUTOdTttCtE5NyIfRbHvJK3GzoFu3E=; b=raaOPlLd7vgz+OjLQW6oZ0jdtNEMbgCwYLnrI7OfKCCphjK9CwMGbYz3Eq4ICqaIoK yDaQ7xpj1aF7NFaX+lURMG3wtu8rL0zLpyINXljp3uPfzeUSViA95Mz3xvNX8JKQ7wJJ WKttx3OE7rGuVXz6PD76uNqa8N0OOjHdepELVHsFuzb/p3W/qIvfecdPOqinOGSa4jyO D/O7ervLQBLdFtYZinKbw+11V2w0ROUGBqtzBRjVNf9R096VYAOWhQPfm8uatZBOssCu j5aUpOGgH+YAniM/cblICixkeiRx/9vboORBZNmMH7HEITFKdaDzi4xsy0sBHwoW1d47 WUYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804819; x=1775409619; 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=eeU9c3QhXSSwwCUTOdTttCtE5NyIfRbHvJK3GzoFu3E=; b=Xw655uUFmtMt9CSMxgjpkTlYE51+l1N3hzeZ/6CvHMqKyKU0PnJeKdIPlYTW5ipe0v 8H34XAr0WQdqt5RQC6orB6xOw0y4QFp6xNkvy2k6s9IvblPkMIFAcIShMNn+BPOpRh/f 0LxxUUSp7zRIVfwpqR/QUC6BxOzMlIOyb6cLqUBg2syi6TKAgRdKZnZaYlfsro7ZGY+6 kSClpPJYI8/KLPASIVavS7TUO/lKKH/KFRKEjU9BCbkw5M23HEDKx/DSRu0wiCDJFeBz c8YyrX48sI6obcuL3GZvLc2t/aonqSQTeXTupi2PpbatPYBIWd8WijrinvYEf26BK9qd bF/g== X-Forwarded-Encrypted: i=1; AJvYcCUidrgxVy1wxN/KQMznYma4U+YpmQneRYgAzuWSItsO6A0xL6MCJXVQHlsjrHpGZGNDkYnU2adkcZ3nYqo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1S7CvwQ5OWZTB+a7zZ8O7iA1/QhFiW+Eae4udf5zTj67q0zF7 WRdQ2hpQyAS3i+ISlIL7wcGM4HMqxhTJdzXVVTW0nkC8MMrA/pe1S7zo X-Gm-Gg: ATEYQzyGL7I+5Ee06zT2Z+iG6QVhXESIloAc7MRIJzGTcw2cl9CPTxYKUMs18T7OqHA +eJSMZeHUTcHo+xF3tRBeicGVqeovJKTh026+1bDZpwSPDO2o8JLbyjIOtITchtjsZb5P81WWpa aJ3dj/vNWdx/Aa2dZjcc8ayUfUdZ6woGUZYj1aOT/IqSnigoxQJM1wsdNrk9qbVc2WSBzZPO5Qh BRcoQtna58RGPUgVeNd/SuWmHycxKM0usRoSBGAfpqXjeQDfQCtxgpWaWlIhJDVwQjSoIkQwQCp mVWAP2kxzlHJwZZ19veI4idxlBeSCC8rYi99TDtc9oE62RMAXJ/2M1ku9YWYGySAzTkgymWpODG 1EUmFQtmAnYFPcjd2pkHYdt53ePbHMZ2TsnnLZ2Pbx3lZAHWAZ+mvfEYW6Y8c3zyN3KXe2amg0U VFJJUidZw2UOrKmZEM8Uyp/d6kO07MR6TVxcVNRsVdGpmviUC6PoDkX476Uol1XegL1IxSuXN/H w== X-Received: by 2002:a05:600c:6287:b0:485:469f:5320 with SMTP id 5b1f17b1804b1-48727ef6865mr171642215e9.30.1774804818460; Sun, 29 Mar 2026 10:20:18 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:17 -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 4/7] fs: relocate and tidy up ihold() Date: Sun, 29 Mar 2026 19:19:59 +0200 Message-ID: <20260329172002.3557801-5-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" The placement was illogical, move it next to igrab(). Take this opportunity to add docs and an assert on the refcount. While its modification remains gated with a WARN_ON, the new assert will also dump the inode state which might aid debugging. No functional changes. Signed-off-by: Mateusz Guzik --- fs/inode.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index 1f5a383ccf27..0050eca6d83f 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -522,15 +522,6 @@ static void init_once(void *foo) inode_init_once(inode); } =20 -/* - * get additional reference to inode; caller must already hold one. - */ -void ihold(struct inode *inode) -{ - WARN_ON(atomic_inc_return(&inode->i_count) < 2); -} -EXPORT_SYMBOL(ihold); - struct wait_queue_head *inode_bit_waitqueue(struct wait_bit_queue_entry *w= qe, struct inode *inode, u32 bit) { @@ -1578,6 +1569,17 @@ ino_t iunique(struct super_block *sb, ino_t max_rese= rved) } EXPORT_SYMBOL(iunique); =20 +/** + * ihold - get a reference on the inode provided you already have one + * @inode: inode to operate on + */ +void ihold(struct inode *inode) +{ + VFS_BUG_ON_INODE(icount_read(inode) < 1, inode); + WARN_ON(atomic_inc_return(&inode->i_count) < 2); +} +EXPORT_SYMBOL(ihold); + struct inode *igrab(struct inode *inode) { spin_lock(&inode->i_lock); --=20 2.48.1 From nobody Thu Apr 2 10:44:29 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 From nobody Thu Apr 2 10:44:29 2026 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 A17ED387357 for ; Sun, 29 Mar 2026 17:20:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804827; cv=none; b=moJxH/7J3rEIoNiNsTIZfV9UQc4sy7UNgI9UI3+3HMGOirFXw0buwmIDxxwLIqbU8Z3PACUQJNJ5VHJ3zqx8LFLKncbhJwhvhTPleK9+JOn04mPRihfryncDT0IiftxiDuVDv0Z/vFOM2PjYBZelkvHwNgCiJmg7emyJjMdE1F4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804827; c=relaxed/simple; bh=aDru4qjl/iYVBfN5B5xn9JTcaAG/lsaOLojHF6nk8UQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LN1527ljBOW3pNsrZ8OZnrAL6wp/ZqIUHHZa+6o2lNI+Rbb6qd5EsGXWfQtk+k58a9sO/CXk4x3ZVy80Ki85RF6rRU+Q5hv4cXM+9HAPcCT3OPEPE8H1zVtKxNzwmzBLK+rNuMrszcw/pwtZHP5ao5YrFyKzmx3aHDlmhEMXenM= 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=s2bYDMpU; arc=none smtp.client-ip=209.85.128.46 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="s2bYDMpU" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-48557c8ad47so28253545e9.0 for ; Sun, 29 Mar 2026 10:20:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804821; x=1775409621; 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=tEImE9v8UsSlxEU0A2Tuvpp3kv2Is3KLwcc3BUa1x8Y=; b=s2bYDMpUKTbn49VtJb3RwtATDdzh9MsfJewiHhG9t9nD5vFxp2KlbIQDX5X9TouXa/ 99tcpKJhrUZBY7im241ZlAFzOuWOf+QVYJZjRrI3VTK2hutlVb8qGKFOKIfdXy4GQPyr wR5HRH/WloKyfzTiUPJfDAvwI8RpNU5EDtxifvuQY938TPNdxrU510kTXu3yLyVBPLM1 MIPsVBR8cowJyHuMhyl4tTQXV3aJrYowjgn22gwmey4UHHrOLw2JC94RPWZxx9Tbjy31 SfmWsVRIJpHeA5iv72qWmX41OmoxiKcx6fc18h38W3XVZx4hCtg7cUmo7WP9is5ETQM+ Xj4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804821; x=1775409621; 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=tEImE9v8UsSlxEU0A2Tuvpp3kv2Is3KLwcc3BUa1x8Y=; b=JE79b2eZ/04OUXJDZVz95y002B8fzuE1kbrjL5imzNHMWwmf90ggq7GcGx+z9WNgRt xFNWx0ihFj2QjT3ZwuRxn5M4miBGVkC4Cfmh4Q/4dTIR3Qdl1pdsSyl8637p4PfW2O3k nk9G68r6qbS8j0+H+QJnh8lS7gk1j9Lhq10y9cpzFfoA5jcS81NF/q1PNC3IlBVTPYe/ jyhjogrDDIopEdg8XzpjJdX03RAJ3ed+VBn1681flLXgV4+I1rkaSnu+3niIRM2dvzEC QrhdrU6R7HZQ+k7oCr/7XpzpbtjHUOdw4umPK7hr3xRf9Mw/11CF/WBrp5J8WmqopXSx IRZw== X-Forwarded-Encrypted: i=1; AJvYcCV2O+TAjFkNVCGIhR8qGBU0BGIYIb2R6/fUEWYvqk461lOh7ZgTDM2ThojxWk8/x6f0t2bfJVK1fzKCb4U=@vger.kernel.org X-Gm-Message-State: AOJu0YxbRcmxsCF38DA93yD+rXeYY/M4d3UU2GShhLEw6jy59B8U5Iwy CKN0kIudbzTjHnF3beF0ETpCeOLYf8mtgbKa1i71k9l/EY5S997urgmR X-Gm-Gg: ATEYQzxbSjzGG8ldlqc3K63LgkVzIrQMQlmmFssNRwBXD6huPXfJZqMHGx8u0+Lenup S2lEIOfBXbg3lUCC9Pqaz4J67wU+3hh+Ylrxu7DlXlcoDpLBLH3ybT/s1E3iiy3BhmFoA65cakp 1N396WtO3My/JqC1NieLmrrCpuqqWhoqAYjPny58127ZcQXMYxBvAVjRflEicWMfj0UczjCQkq1 pD1RoTAqEtkInr1D/DPnJRtprYppoz7FhXnY3Vt9URdJoCEssxvj4brq8GbRP97/dEYrjgg8C82 zdyNbD48/psnnBG0aHuqhJOqu8wOwIQaPIx4uucjrnvM7W5rn/4e7httymW3AyTUHkHI7eUYEn9 TQdcyU2SFEPzLe1F4ChELZD/TVVttP2S+eB3fwbt6ZoR3ZFMfeS2hp+TB+YJjxceR53s0qItmUm NH3UwTuU7ORXQQuIHN8c5Ka6vMpB9+QMxuy+VGKjdcZpjMylH61RXWFyKg18eCZbnTj6dvA0dXV w== X-Received: by 2002:a05:600c:64c3:b0:477:6d96:b3e5 with SMTP id 5b1f17b1804b1-48727d59bb9mr164125175e9.7.1774804820679; Sun, 29 Mar 2026 10:20:20 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:20 -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 6/7] fs: locklessly bump refs in igrab as long as it does not transition 0->1 Date: Sun, 29 Mar 2026 19:20:01 +0200 Message-ID: <20260329172002.3557801-7-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" The bump is gated by I_FREEING | I_WILL_FREE, but these flags can only legally show up if the count is 0. Consequently if the value is at least 1 and it succesfully CAS'ed to something higher, the flags must not be there. I verified all places which look at the refcount either only care about it staying 0 (and have the lock enforce it) or don't hold the inode lock to begin with. Thus the patch retains the invariant for correct consumers and does not make things worse for the rest. Signed-off-by: Mateusz Guzik --- fs/inode.c | 5 +++++ include/linux/fs.h | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index a417abc64822..c7585924d5c8 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -1582,6 +1582,11 @@ EXPORT_SYMBOL(ihold); =20 struct inode *igrab(struct inode *inode) { + if (atomic_add_unless(&inode->i_count, 1, 0)) { + VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE= ), inode); + return inode; + } + spin_lock(&inode->i_lock); if (!(inode_state_read(inode) & (I_FREEING | I_WILL_FREE))) { __iget(inode); diff --git a/include/linux/fs.h b/include/linux/fs.h index 07363fce4406..119e0a3d2f42 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2234,8 +2234,8 @@ static inline int icount_read_once(const struct inode= *inode) } =20 /* - * returns the refcount on the inode. The lock guarantees no new references - * are added, but references can be dropped as long as the result is > 0. + * returns the refcount on the inode. The lock guarantees no 0->1 or 1->0 = transitions + * of the count are going to take place, otherwise it changes arbitrarily. */ static inline int icount_read(const struct inode *inode) { --=20 2.48.1 From nobody Thu Apr 2 10:44:29 2026 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 71ABE38736F for ; Sun, 29 Mar 2026 17:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804840; cv=none; b=mM7nEdHFJNaLqnZMcFFY+A44t0F8bsvg8NRr3R8AcS70HGkbKb62HHaTIdStTS4PXfaxEa7qvYPZWz6g9wrmquyONw/yB2oaJzEPA6emOsBxXLdz4BgFvlzadlAYDW1uaCPnUqERu9xf/wCENEoQPvpSiOOUqx8Fx+l49+IjiMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774804840; c=relaxed/simple; bh=pSGIt1gpj97rMHRo/uSF1TFQzt+/bTerdPhiTqudf/w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sqKm+37rTyoQAnz6zoK7sSvSfCNiR3oivrwKdVN5FgoIf6CvERneBWYX5hmOeCHsuqZB09esyBv9+vEtPXTfkkpT7gmN2mbuxNjaVG7Vk5qEgnqmeMBL8M8gB59WngPHBPzy1lAkAarxbnn9gXcKrrxVQKzoTumBocy/WfJYtiI= 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=tEPc1U26; arc=none smtp.client-ip=209.85.128.48 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="tEPc1U26" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-48540d21f7dso43656105e9.0 for ; Sun, 29 Mar 2026 10:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774804822; x=1775409622; 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=/g2CowQdixIfq8ensM5Khpl9kYXMah3qefhvCGYiZ7M=; b=tEPc1U26Zm3mAiPYX7qSNo05GlyVeepw1Sqg8+lR9bKGa+kfP7AQ5zYWapk+J1E3eJ j5RFsQ5tc0WGWIG9wB7+qN3gUccHuHkTmb9cC15EFSB5nQtS/kfEHPE8tzzPnCa6oMlK zZ9TAAhEnIUdCo/zNfEidVivMjB3Lt+KjYNuKaFLcCuA0teRnPWAXDOtVWybbDYX6Csb XdLBEo7eONpYbHxk+qIrFlRnghdPmug5s6/YMPiKuMRHMAPqf4D1CYcdXDxDOx2gwtH7 /lPofDF81Cw9/SIfYcvunOXj8rX3izhQUDWCultOETDRafVje7x7gFTTJRXH4vlyHoIH HYFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774804822; x=1775409622; 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=/g2CowQdixIfq8ensM5Khpl9kYXMah3qefhvCGYiZ7M=; b=erhs63F+0uod8ph/uwyaYMRKM7SqcN0FAGxmqGvp7x3vTqIY5EayUhGyRuz0BBZEze XLrdgu6EH05DI0cPlv7o7VQcRcTgMVjXi7pecao2idvyKE4+PoBwk/mQsYG61zVkJ3qx YEX/CyP1l1nDvBxYOLDhO0qHmyLGtpjRI9SkUQUBGW4F4sfZQIQUfixG4RgOLIj2aJvK trBs4ZGKZ97HIR9wixhxd8DvxCLKWnaD6Jdq3RSRSwVm49/eVQ1YahWekrGfIvRJub3e vzGKjAddjFbCoyIsAOVpVYmjtvUUwF62Ck+MqTn0WqkInEHsH+H1fralJNY9g/KGXHl8 0Umw== X-Forwarded-Encrypted: i=1; AJvYcCVtVNmW/Pj54oW02exjg1FDkuSYow0FwwAEbV3uvPGxDDdAQsQiA42hNIZPMWwPF4QPtGlRFucEqpnULUI=@vger.kernel.org X-Gm-Message-State: AOJu0YyIgnsEvJhmXB6BdF+z2yMwKGJAVjxA+Y2V7AweStH6L306I5Me I+bpWELu7Q2qwpo0XTilHfnKZEYNkgBpQTiUDX06Il+DNz2VvJqO8DhZ X-Gm-Gg: ATEYQzwiDVllMW20bzyDeli6TOu2u2P/VuoIZmicdT4SAZMOvYWlF4XLiXecBQXQCd1 eat6Itx0VUvkBdw0Ds5KWEnpr/T+L+nxBt6dXUvtN7Ex0BE/M77lZBjTvO7BXaPDCYHlftrAjWR F8u8vDL+rFaqMGVfz/rNd2N/eLI9vqSAoQ7v3ssV78UJzUX1l+RSMK+eidOwmSsuZuCmSppjREN E5979OfzEhLxDwwRmjC+f+QTpkrT4VQTUsawaUfBRVBOHAPi6UNKdLYO2cfQei/+KPeDP/oAsE/ q5FwYGh1iirtKPU+Z1/WCw28sYGIzdEfvZx2TJa7w6Q/GsrSEtXvbXJTSP7qKDq3AVqwUglP1xi 2oKY311AvFdQ3o5jSL1+yVYj+W4RY6CyY9BhZljjkZvMP+9HVXcQdJDIr1NArY3KaNc0+ZLfCmD Vb9WW0N40e/8xvU2xP6gMngqs1b/ePOxHhcByafKgCeKxHmsbnre0NbwpdnEXWDru92HKoIJp7U w== X-Received: by 2002:a05:600c:5304:b0:485:f1d1:8f3d with SMTP id 5b1f17b1804b1-48727d67a43mr164826365e9.6.1774804821809; Sun, 29 Mar 2026 10:20:21 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2026 10:20:21 -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 7/7] fs: locklessly bump refs in the inode hash when possible Date: Sun, 29 Mar 2026 19:20:02 +0200 Message-ID: <20260329172002.3557801-8-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" Signed-off-by: Mateusz Guzik --- fs/dcache.c | 4 ++++ fs/inode.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/fs/dcache.c b/fs/dcache.c index 9ceab142896f..b63450ebb85c 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2033,6 +2033,10 @@ void d_instantiate_new(struct dentry *entry, struct = inode *inode) __d_instantiate(entry, inode); spin_unlock(&entry->d_lock); WARN_ON(!(inode_state_read(inode) & I_NEW)); + /* + * Paired with igrab_try_lockless() + */ + smp_wmb(); inode_state_clear(inode, I_NEW | I_CREATING); inode_wake_up_bit(inode, __I_NEW); spin_unlock(&inode->i_lock); diff --git a/fs/inode.c b/fs/inode.c index c7585924d5c8..c6e53ec90057 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -1029,6 +1029,7 @@ long prune_icache_sb(struct super_block *sb, struct s= hrink_control *sc) } =20 static void __wait_on_freeing_inode(struct inode *inode, bool hash_locked,= bool rcu_locked); +static bool igrab_try_lockless(struct inode *inode); =20 /* * Called with the inode lock held. @@ -1053,6 +1054,11 @@ static struct inode *find_inode(struct super_block *= sb, continue; if (!test(inode, data)) continue; + if (igrab_try_lockless(inode)) { + rcu_read_unlock(); + *isnew =3D false; + return inode; + } spin_lock(&inode->i_lock); if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE)) { __wait_on_freeing_inode(inode, hash_locked, true); @@ -1095,6 +1101,11 @@ static struct inode *find_inode_fast(struct super_bl= ock *sb, continue; if (inode->i_sb !=3D sb) continue; + if (igrab_try_lockless(inode)) { + rcu_read_unlock(); + *isnew =3D false; + return inode; + } spin_lock(&inode->i_lock); if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE)) { __wait_on_freeing_inode(inode, hash_locked, true); @@ -1212,6 +1223,10 @@ void unlock_new_inode(struct inode *inode) lockdep_annotate_inode_mutex_key(inode); spin_lock(&inode->i_lock); WARN_ON(!(inode_state_read(inode) & I_NEW)); + /* + * Paired with igrab_try_lockless() + */ + smp_wmb(); inode_state_clear(inode, I_NEW | I_CREATING); inode_wake_up_bit(inode, __I_NEW); spin_unlock(&inode->i_lock); @@ -1223,6 +1238,10 @@ void discard_new_inode(struct inode *inode) lockdep_annotate_inode_mutex_key(inode); spin_lock(&inode->i_lock); WARN_ON(!(inode_state_read(inode) & I_NEW)); + /* + * Paired with igrab_try_lockless() + */ + smp_wmb(); inode_state_clear(inode, I_NEW); inode_wake_up_bit(inode, __I_NEW); spin_unlock(&inode->i_lock); @@ -1604,6 +1623,39 @@ struct inode *igrab(struct inode *inode) } EXPORT_SYMBOL(igrab); =20 +/* + * Special routine for the inode hash. Don't use elsewhere. + * + * It provides lockless refcount acquire in the common case of no problema= tic + * flags being set. + * + * Any of I_NEW, I_CREATING, I_FREEING and I_WILL_FREE require dedicated t= reatment + * during lookup and bumping inodes with these is intentionally avoided. A= dditionally + * it is illegal to add refs if eiter I_FREEING or I_WILL_FREE is set in t= he first place. + * + * Correctness is achieved as follows: + * 1. both I_NEW and I_CREATING can only legally get set *before* the inod= e is visible + * in the hash, meaning the upfront read takes care of them. + * 2. unsetting of I_NEW is preceded with a store fence, paired with full = fence in + * atomic_add_unless + * 3. both I_FREEING and I_WILL_FREE can only legally get set if ->i_count= =3D=3D 0, thus if + * cmpxchg managed to replace any non-0 value, we have an invariant the= se flags are + * not present + */ +static bool igrab_try_lockless(struct inode *inode) +{ + if (inode_state_read_once(inode) & (I_NEW | I_CREATING | I_FREEING | I_WI= LL_FREE)) + return false; + /* + * Paired with routines clearing I_NEW + */ + if (atomic_add_unless(&inode->i_count, 1, 0)) { + VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE= ), inode); + return true; + } + return false; +} + /** * ilookup5_nowait - search for an inode in the inode cache * @sb: super block of file system to search --=20 2.48.1