From nobody Fri Dec 19 12:46:40 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 B26B6221D8F; Wed, 21 May 2025 01:53:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747792431; cv=none; b=TjMK6iSCdM/pA6ckzTWEZ3tUm8hb+sZTx0cBLiUIpKreCOwcJUq6z6AdqTqVSZy3hgnBtpaOqHtgjngQRb3aL8Y8iKL8n/3Da/PsWgP32uQm58xYOBGOT3cykLTOH+DFDcv3J8FGNTb2ce7E2hdSVKyuHs4F+aQT3r5N5SDUUTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747792431; c=relaxed/simple; bh=KHZo+nKyA1DftStJ0w50kOGGKXrjuV/CpUMxUHVEBbU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eep9VMz0MhJL4CsefLdAkYCv8lXk2q4DiRTCZ30QAVQWPhHKsnTE5tnYunQaABk73S41dKUhM/QgEa8WeUnVzOHUUdKbstN1CyOBOsvebQmSmYVKYiPpT62D2GRUFPdDPO8vSdNz1vFd5IyoVfleb6epYa4c1Y0iIAnYIoycO/I= 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=P9l6M3Ao; arc=none smtp.client-ip=209.85.214.173 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="P9l6M3Ao" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-232059c0b50so34099145ad.2; Tue, 20 May 2025 18:53:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747792429; x=1748397229; 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=6O56L2h87cUpxUqzozyRkGCbMF8U2ItQajiiuHkXOQ8=; b=P9l6M3Ao8iSjqNAKOO2sxENEw+sGEFDv1B/8wEg0GWasA4KXSeVFz4L1zhg/2M5dhp V25tB7pePblAdul2g37sy+70j9WifCXikdQaaCbKcj7pK5Z9b7uB0dvGm5syXkWxvTwt cWkgk4UhIcjj9rgGURwh+U5wBX5ST5W+xdDW2+s5cVI7o0824b6LgwKdl2Xxb3BPmGFA W6cXV5ygvOlqfkScl5w48arbLcF2JurQx1lSUWWiTlsbOHf0H/WEEX/8S7Evfb3X4Heg lYRL23DmjJmknHnYNuEQnpgMXeYkUzk/0zIxOiKEtshWg2FcF7OvqvGoRfOte2KvfqxZ f8Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747792429; x=1748397229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6O56L2h87cUpxUqzozyRkGCbMF8U2ItQajiiuHkXOQ8=; b=M4dnNjchseC2ff/GQnWPv+5MwV4pt6DHuorGD386Ydd7iozsrhGteKInc/IrcgXEHv acJfZLlTRn36ln3zLaQpsKDMAjuAiwA0L8ApwVDTmURVXjWADXh70Lt+nYEphLBQUWiI 9vnI0vJQJfc4UeuF4zkOsVw+fmoMaPOXpfebcSjxFA+nSV/1koMSeHX/RmPP6sA44wD9 bPEafjfGgMGjb+vg6UX6+skVqfktVO8K0BW9M+4bOfUS7iAPBkIJLefmzzFKn8D3+kI8 n6gsygxrpHl1MPXUGhk4zsuriip9iOo/WjBeY33TJcMge3gOI+yU2U6kdYdweH/rXesQ qShw== X-Forwarded-Encrypted: i=1; AJvYcCUnn6xp1Tio0o/7v7lZ1GybM8CGimNN/9WHie3hB2w+ZFqd/+ve3Dei35jny+0vx5RdGcDmFFIz6scWF8I=@vger.kernel.org X-Gm-Message-State: AOJu0YzIjZW69305qShnoGGmLblwmDjJjTAreQxsHCGU0GAFHrDerlzT gMKNSR0jcXJ7ukdfapzLK3yTYJet7+eHhm+ddOcZuGrz9xDA74GLgki2p2YXetkUjXQ= X-Gm-Gg: ASbGncvOszWvXT6y/lSpI/GI61tyAk3wB/7qza7kx5QjI0DE5zBVyYDFwjQG4gifHZj kNf28W8DFK6LR784rdLv839fKsezophreMTc06PtR9vobVV4VnAh/z7hFMwcIgv9V2D5I/yjcXA F/Z02gBSMXvkDyiU6dmlzbqg1lrFpzrq7io7d1c+3TDbADt55fT0ZN4pNhfOc09qXq4MjacuHFV TD46y6mgnfPuxLGJr566XU+dtya2wzCT2thefXP0Hh5qEDYb6TanTSa9YjWB1mqQHQAF91Q2q1z ll5gvTC5CYYqW+QpnWzX3M6WNcOXJR6coupHu12QwgHcIw6nGijkVMk= X-Google-Smtp-Source: AGHT+IEgfz7eKcp8qtyxk9ON+9kLhUBrzGZDseBty88lNVsoYpHYxcZYuwqP0BlNrrpl64ULD5MmZQ== X-Received: by 2002:a17:903:2391:b0:22f:a932:5374 with SMTP id d9443c01a7336-231de37ec19mr246366725ad.48.1747792428580; Tue, 20 May 2025 18:53:48 -0700 (PDT) Received: from gmail.com ([116.237.135.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-231d4eedb59sm82796665ad.257.2025.05.20.18.53.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 May 2025 18:53:48 -0700 (PDT) From: Qingfang Deng To: stable@vger.kernel.org, Greg Kroah-Hartman , Tejun Heo , linux-kernel@vger.kernel.org Cc: Ian Kent , Miklos Szeredi Subject: [PATCH 5.10 3/5] kernfs: switch kernfs to use an rwsem Date: Wed, 21 May 2025 09:53:33 +0800 Message-ID: <20250521015336.3450911-4-dqfext@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250521015336.3450911-1-dqfext@gmail.com> References: <20250521015336.3450911-1-dqfext@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" From: Ian Kent Commit 7ba0273b2f34a55efe967d3c7381fb1da2ca195f upstream. The kernfs global lock restricts the ability to perform kernfs node lookup operations in parallel during path walks. Change the kernfs mutex to an rwsem so that, when opportunity arises, node searches can be done in parallel with path walk lookups. Reviewed-by: Miklos Szeredi Signed-off-by: Ian Kent Link: https://lore.kernel.org/r/162642770946.63632.2218304587223241374.stgi= t@web.messagingengine.com Signed-off-by: Greg Kroah-Hartman --- fs/kernfs/dir.c | 100 ++++++++++++++++++------------------ fs/kernfs/file.c | 4 +- fs/kernfs/inode.c | 16 +++--- fs/kernfs/kernfs-internal.h | 5 +- fs/kernfs/mount.c | 12 ++--- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 2 +- 7 files changed, 72 insertions(+), 71 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 1756f7e43aac..0443a9cd72a3 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -17,7 +17,7 @@ =20 #include "kernfs-internal.h" =20 -DEFINE_MUTEX(kernfs_mutex); +DECLARE_RWSEM(kernfs_rwsem); static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */ /* * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to @@ -34,7 +34,7 @@ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr= */ =20 static bool kernfs_active(struct kernfs_node *kn) { - lockdep_assert_held(&kernfs_mutex); + lockdep_assert_held(&kernfs_rwsem); return atomic_read(&kn->active) >=3D 0; } =20 @@ -348,7 +348,7 @@ static int kernfs_sd_compare(const struct kernfs_node *= left, * @kn->parent->dir.children. * * Locking: - * mutex_lock(kernfs_mutex) + * kernfs_rwsem held exclusive * * RETURNS: * 0 on susccess -EEXIST on failure. @@ -394,7 +394,7 @@ static int kernfs_link_sibling(struct kernfs_node *kn) * removed, %false if @kn wasn't on the rbtree. * * Locking: - * mutex_lock(kernfs_mutex) + * kernfs_rwsem held exclusive */ static bool kernfs_unlink_sibling(struct kernfs_node *kn) { @@ -465,14 +465,14 @@ void kernfs_put_active(struct kernfs_node *kn) * return after draining is complete. */ static void kernfs_drain(struct kernfs_node *kn) - __releases(&kernfs_mutex) __acquires(&kernfs_mutex) + __releases(&kernfs_rwsem) __acquires(&kernfs_rwsem) { struct kernfs_root *root =3D kernfs_root(kn); =20 - lockdep_assert_held(&kernfs_mutex); + lockdep_assert_held_write(&kernfs_rwsem); WARN_ON_ONCE(kernfs_active(kn)); =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); =20 if (kernfs_lockdep(kn)) { rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); @@ -491,7 +491,7 @@ static void kernfs_drain(struct kernfs_node *kn) =20 kernfs_drain_open_files(kn); =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); } =20 /** @@ -572,18 +572,18 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) /* If the kernfs parent node has changed discard and * proceed to ->lookup. */ - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); spin_lock(&dentry->d_lock); parent =3D kernfs_dentry_node(dentry->d_parent); if (parent) { if (kernfs_dir_changed(parent, dentry)) { spin_unlock(&dentry->d_lock); - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); return 0; } } spin_unlock(&dentry->d_lock); - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); =20 /* The kernfs parent node hasn't changed, leave the * dentry negative and return success. @@ -592,7 +592,7 @@ static int kernfs_dop_revalidate(struct dentry *dentry,= unsigned int flags) } =20 kn =3D kernfs_dentry_node(dentry); - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); =20 /* The kernfs node has been deactivated */ if (!kernfs_active(kn)) @@ -611,10 +611,10 @@ static int kernfs_dop_revalidate(struct dentry *dentr= y, unsigned int flags) kernfs_info(dentry->d_sb)->ns !=3D kn->ns) goto out_bad; =20 - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); return 1; out_bad: - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); return 0; } =20 @@ -809,7 +809,7 @@ int kernfs_add_one(struct kernfs_node *kn) bool has_ns; int ret; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); =20 ret =3D -EINVAL; has_ns =3D kernfs_ns_enabled(parent); @@ -840,7 +840,7 @@ int kernfs_add_one(struct kernfs_node *kn) ps_iattr->ia_mtime =3D ps_iattr->ia_ctime; } =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); =20 /* * Activate the new node unless CREATE_DEACTIVATED is requested. @@ -854,7 +854,7 @@ int kernfs_add_one(struct kernfs_node *kn) return 0; =20 out_unlock: - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); return ret; } =20 @@ -875,7 +875,7 @@ static struct kernfs_node *kernfs_find_ns(struct kernfs= _node *parent, bool has_ns =3D kernfs_ns_enabled(parent); unsigned int hash; =20 - lockdep_assert_held(&kernfs_mutex); + lockdep_assert_held(&kernfs_rwsem); =20 if (has_ns !=3D (bool)ns) { WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", @@ -907,7 +907,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs= _node *parent, size_t len; char *p, *name; =20 - lockdep_assert_held(&kernfs_mutex); + lockdep_assert_held_read(&kernfs_rwsem); =20 spin_lock_irq(&kernfs_pr_cont_lock); =20 @@ -946,10 +946,10 @@ struct kernfs_node *kernfs_find_and_get_ns(struct ker= nfs_node *parent, { struct kernfs_node *kn; =20 - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); kn =3D kernfs_find_ns(parent, name, ns); kernfs_get(kn); - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); =20 return kn; } @@ -970,10 +970,10 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct ker= nfs_node *parent, { struct kernfs_node *kn; =20 - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); kn =3D kernfs_walk_ns(parent, path, ns); kernfs_get(kn); - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); =20 return kn; } @@ -1128,7 +1128,7 @@ static struct dentry *kernfs_iop_lookup(struct inode = *dir, struct inode *inode =3D NULL; const void *ns =3D NULL; =20 - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); if (kernfs_ns_enabled(parent)) ns =3D kernfs_info(dir->i_sb)->ns; =20 @@ -1144,7 +1144,7 @@ static struct dentry *kernfs_iop_lookup(struct inode = *dir, kernfs_set_rev(parent, dentry); /* instantiate and hash (possibly negative) dentry */ ret =3D d_splice_alias(inode, dentry); - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); =20 return ret; } @@ -1264,7 +1264,7 @@ static struct kernfs_node *kernfs_next_descendant_pos= t(struct kernfs_node *pos, { struct rb_node *rbn; =20 - lockdep_assert_held(&kernfs_mutex); + lockdep_assert_held_write(&kernfs_rwsem); =20 /* if first iteration, visit leftmost descendant which may be root */ if (!pos) @@ -1300,7 +1300,7 @@ void kernfs_activate(struct kernfs_node *kn) { struct kernfs_node *pos; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); =20 pos =3D NULL; while ((pos =3D kernfs_next_descendant_post(pos, kn))) { @@ -1314,14 +1314,14 @@ void kernfs_activate(struct kernfs_node *kn) pos->flags |=3D KERNFS_ACTIVATED; } =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); } =20 static void __kernfs_remove(struct kernfs_node *kn) { struct kernfs_node *pos; =20 - lockdep_assert_held(&kernfs_mutex); + lockdep_assert_held_write(&kernfs_rwsem); =20 /* * Short-circuit if non-root @kn has already finished removal. @@ -1344,7 +1344,7 @@ static void __kernfs_remove(struct kernfs_node *kn) pos =3D kernfs_leftmost_descendant(kn); =20 /* - * kernfs_drain() drops kernfs_mutex temporarily and @pos's + * kernfs_drain() drops kernfs_rwsem temporarily and @pos's * base ref could have been put by someone else by the time * the function returns. Make sure it doesn't go away * underneath us. @@ -1391,9 +1391,9 @@ static void __kernfs_remove(struct kernfs_node *kn) */ void kernfs_remove(struct kernfs_node *kn) { - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); __kernfs_remove(kn); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); } =20 /** @@ -1480,17 +1480,17 @@ bool kernfs_remove_self(struct kernfs_node *kn) { bool ret; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); kernfs_break_active_protection(kn); =20 /* * SUICIDAL is used to arbitrate among competing invocations. Only * the first one will actually perform removal. When the removal * is complete, SUICIDED is set and the active ref is restored - * while holding kernfs_mutex. The ones which lost arbitration - * waits for SUICDED && drained which can happen only after the - * enclosing kernfs operation which executed the winning instance - * of kernfs_remove_self() finished. + * while kernfs_rwsem for held exclusive. The ones which lost + * arbitration waits for SUICIDED && drained which can happen only + * after the enclosing kernfs operation which executed the winning + * instance of kernfs_remove_self() finished. */ if (!(kn->flags & KERNFS_SUICIDAL)) { kn->flags |=3D KERNFS_SUICIDAL; @@ -1508,9 +1508,9 @@ bool kernfs_remove_self(struct kernfs_node *kn) atomic_read(&kn->active) =3D=3D KN_DEACTIVATED_BIAS) break; =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); schedule(); - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); } finish_wait(waitq, &wait); WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); @@ -1518,12 +1518,12 @@ bool kernfs_remove_self(struct kernfs_node *kn) } =20 /* - * This must be done while holding kernfs_mutex; otherwise, waiting - * for SUICIDED && deactivated could finish prematurely. + * This must be done while kernfs_rwsem held exclusive; otherwise, + * waiting for SUICIDED && deactivated could finish prematurely. */ kernfs_unbreak_active_protection(kn); =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); return ret; } =20 @@ -1547,7 +1547,7 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pare= nt, const char *name, return -ENOENT; } =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); =20 kn =3D kernfs_find_ns(parent, name, ns); if (kn) { @@ -1556,7 +1556,7 @@ int kernfs_remove_by_name_ns(struct kernfs_node *pare= nt, const char *name, kernfs_put(kn); } =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); =20 if (kn) return 0; @@ -1582,7 +1582,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, if (!kn->parent) return -EINVAL; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); =20 error =3D -ENOENT; if (!kernfs_active(kn) || !kernfs_active(new_parent) || @@ -1636,7 +1636,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct k= ernfs_node *new_parent, =20 error =3D 0; out: - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); return error; } =20 @@ -1711,7 +1711,7 @@ static int kernfs_fop_readdir(struct file *file, stru= ct dir_context *ctx) =20 if (!dir_emit_dots(file, ctx)) return 0; - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); =20 if (kernfs_ns_enabled(parent)) ns =3D kernfs_info(dentry->d_sb)->ns; @@ -1728,12 +1728,12 @@ static int kernfs_fop_readdir(struct file *file, st= ruct dir_context *ctx) file->private_data =3D pos; kernfs_get(pos); =20 - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); if (!dir_emit(ctx, name, len, ino, type)) return 0; - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); } - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); file->private_data =3D NULL; ctx->pos =3D INT_MAX; return 0; diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index c75719312147..60e2a86c535e 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -860,7 +860,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) spin_unlock_irq(&kernfs_notify_lock); =20 /* kick fsnotify */ - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); =20 list_for_each_entry(info, &kernfs_root(kn)->supers, node) { struct kernfs_node *parent; @@ -898,7 +898,7 @@ static void kernfs_notify_workfn(struct work_struct *wo= rk) iput(inode); } =20 - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); kernfs_put(kn); goto repeat; } diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c index fc2469a20fed..ddaf18198935 100644 --- a/fs/kernfs/inode.c +++ b/fs/kernfs/inode.c @@ -106,9 +106,9 @@ int kernfs_setattr(struct kernfs_node *kn, const struct= iattr *iattr) { int ret; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); ret =3D __kernfs_setattr(kn, iattr); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); return ret; } =20 @@ -121,7 +121,7 @@ int kernfs_iop_setattr(struct dentry *dentry, struct ia= ttr *iattr) if (!kn) return -EINVAL; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); error =3D setattr_prepare(dentry, iattr); if (error) goto out; @@ -134,7 +134,7 @@ int kernfs_iop_setattr(struct dentry *dentry, struct ia= ttr *iattr) setattr_copy(inode, iattr); =20 out: - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); return error; } =20 @@ -189,9 +189,9 @@ int kernfs_iop_getattr(const struct path *path, struct = kstat *stat, struct inode *inode =3D d_inode(path->dentry); struct kernfs_node *kn =3D inode->i_private; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); kernfs_refresh_inode(kn, inode); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); =20 generic_fillattr(inode, stat); return 0; @@ -281,9 +281,9 @@ int kernfs_iop_permission(struct inode *inode, int mask) =20 kn =3D inode->i_private; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); kernfs_refresh_inode(kn, inode); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); =20 return generic_permission(inode, mask); } diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h index 6a8d0ca26d03..c933d9bd8a78 100644 --- a/fs/kernfs/kernfs-internal.h +++ b/fs/kernfs/kernfs-internal.h @@ -13,6 +13,7 @@ #include #include #include +#include #include =20 #include @@ -69,7 +70,7 @@ struct kernfs_super_info { */ const void *ns; =20 - /* anchored at kernfs_root->supers, protected by kernfs_mutex */ + /* anchored at kernfs_root->supers, protected by kernfs_rwsem */ struct list_head node; }; #define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info)) @@ -118,7 +119,7 @@ int __kernfs_setattr(struct kernfs_node *kn, const stru= ct iattr *iattr); /* * dir.c */ -extern struct mutex kernfs_mutex; +extern struct rw_semaphore kernfs_rwsem; extern const struct dentry_operations kernfs_dops; extern const struct file_operations kernfs_dir_fops; extern const struct inode_operations kernfs_dir_iops; diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c index 9dc7e7a64e10..baa4155ba2ed 100644 --- a/fs/kernfs/mount.c +++ b/fs/kernfs/mount.c @@ -255,9 +255,9 @@ static int kernfs_fill_super(struct super_block *sb, st= ruct kernfs_fs_context *k sb->s_shrink.seeks =3D 0; =20 /* get root inode, initialize and unlock it */ - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); inode =3D kernfs_get_inode(sb, info->root->kn); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); if (!inode) { pr_debug("kernfs: could not get root inode\n"); return -ENOMEM; @@ -344,9 +344,9 @@ int kernfs_get_tree(struct fs_context *fc) } sb->s_flags |=3D SB_ACTIVE; =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); list_add(&info->node, &info->root->supers); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); } =20 fc->root =3D dget(sb->s_root); @@ -372,9 +372,9 @@ void kernfs_kill_sb(struct super_block *sb) { struct kernfs_super_info *info =3D kernfs_info(sb); =20 - mutex_lock(&kernfs_mutex); + down_write(&kernfs_rwsem); list_del(&info->node); - mutex_unlock(&kernfs_mutex); + up_write(&kernfs_rwsem); =20 /* * Remove the superblock from fs_supers/s_instances diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c index 5432883d819f..c8f8e41b8411 100644 --- a/fs/kernfs/symlink.c +++ b/fs/kernfs/symlink.c @@ -116,9 +116,9 @@ static int kernfs_getlink(struct inode *inode, char *pa= th) struct kernfs_node *target =3D kn->symlink.target_kn; int error; =20 - mutex_lock(&kernfs_mutex); + down_read(&kernfs_rwsem); error =3D kernfs_get_target_path(parent, target, path); - mutex_unlock(&kernfs_mutex); + up_read(&kernfs_rwsem); =20 return error; } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 195afa63ab1c..95e1948379d0 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -193,7 +193,7 @@ struct kernfs_root { u32 id_highbits; struct kernfs_syscall_ops *syscall_ops; =20 - /* list of kernfs_super_info of this root, protected by kernfs_mutex */ + /* list of kernfs_super_info of this root, protected by kernfs_rwsem */ struct list_head supers; =20 wait_queue_head_t deactivate_waitq; --=20 2.43.0