nfs_localio: change nfsd_file_put_local() to take a pointer to __rcu pointer

Instead of calling xchg() and unrcu_pointer() before
nfsd_file_put_local(), we now pass pointer to the __rcu pointer and call
xchg() and unrcu_pointer() inside that function.

Where unrcu_pointer() is currently called the internals of "struct
nfsd_file" are not known and that causes older compilers such as gcc-8
to complain.

In some cases we have a __kernel (aka normal) pointer not an __rcu
pointer so we need to cast it to __rcu first.  This is strictly a
weakening so no information is lost.  Somewhat surprisingly, this cast
is accepted by gcc-8.

This has the pleasing result that the cmpxchg() which sets ro_file and
rw_file, and also the xchg() which clears them, are both now in the nfsd
code.

Reported-by: Pali Rohár <pali@kernel.org>
Reported-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Fixes: 86e0041225 ("nfs: cache all open LOCALIO nfsd_file(s) in client")
Signed-off-by: NeilBrown <neil@brown.name>
Signed-off-by: Anna Schumaker <anna.schumaker@oracle.com>
This commit is contained in:
NeilBrown 2025-05-09 10:46:43 +10:00 committed by Anna Schumaker
parent 21fb440346
commit c25a89770d
5 changed files with 36 additions and 35 deletions

View file

@ -209,9 +209,16 @@ void nfs_local_probe_async(struct nfs_client *clp)
} }
EXPORT_SYMBOL_GPL(nfs_local_probe_async); EXPORT_SYMBOL_GPL(nfs_local_probe_async);
static inline void nfs_local_file_put(struct nfsd_file *nf) static inline void nfs_local_file_put(struct nfsd_file *localio)
{ {
nfs_to_nfsd_file_put_local(nf); /* nfs_to_nfsd_file_put_local() expects an __rcu pointer
* but we have a __kernel pointer. It is always safe
* to cast a __kernel pointer to an __rcu pointer
* because the cast only weakens what is known about the pointer.
*/
struct nfsd_file __rcu *nf = (struct nfsd_file __rcu*) localio;
nfs_to_nfsd_file_put_local(&nf);
} }
/* /*

View file

@ -170,9 +170,6 @@ static bool nfs_uuid_put(nfs_uuid_t *nfs_uuid)
while ((nfl = list_first_entry_or_null(&nfs_uuid->files, while ((nfl = list_first_entry_or_null(&nfs_uuid->files,
struct nfs_file_localio, struct nfs_file_localio,
list)) != NULL) { list)) != NULL) {
struct nfsd_file *ro_nf;
struct nfsd_file *rw_nf;
/* If nfs_uuid is already NULL, nfs_close_local_fh is /* If nfs_uuid is already NULL, nfs_close_local_fh is
* closing and we must wait, else we unlink and close. * closing and we must wait, else we unlink and close.
*/ */
@ -189,17 +186,14 @@ static bool nfs_uuid_put(nfs_uuid_t *nfs_uuid)
continue; continue;
} }
ro_nf = unrcu_pointer(xchg(&nfl->ro_file, NULL));
rw_nf = unrcu_pointer(xchg(&nfl->rw_file, NULL));
/* Remove nfl from nfs_uuid->files list */ /* Remove nfl from nfs_uuid->files list */
list_del_init(&nfl->list); list_del_init(&nfl->list);
spin_unlock(&nfs_uuid->lock); spin_unlock(&nfs_uuid->lock);
if (ro_nf)
nfs_to_nfsd_file_put_local(ro_nf); nfs_to_nfsd_file_put_local(&nfl->ro_file);
if (rw_nf) nfs_to_nfsd_file_put_local(&nfl->rw_file);
nfs_to_nfsd_file_put_local(rw_nf);
cond_resched(); cond_resched();
spin_lock(&nfs_uuid->lock); spin_lock(&nfs_uuid->lock);
/* Now we can allow racing nfs_close_local_fh() to /* Now we can allow racing nfs_close_local_fh() to
* skip the locking. * skip the locking.
@ -303,8 +297,6 @@ EXPORT_SYMBOL_GPL(nfs_open_local_fh);
void nfs_close_local_fh(struct nfs_file_localio *nfl) void nfs_close_local_fh(struct nfs_file_localio *nfl)
{ {
struct nfsd_file *ro_nf;
struct nfsd_file *rw_nf;
nfs_uuid_t *nfs_uuid; nfs_uuid_t *nfs_uuid;
rcu_read_lock(); rcu_read_lock();
@ -337,12 +329,8 @@ void nfs_close_local_fh(struct nfs_file_localio *nfl)
spin_unlock(&nfs_uuid->lock); spin_unlock(&nfs_uuid->lock);
rcu_read_unlock(); rcu_read_unlock();
ro_nf = unrcu_pointer(xchg(&nfl->ro_file, NULL)); nfs_to_nfsd_file_put_local(&nfl->ro_file);
rw_nf = unrcu_pointer(xchg(&nfl->rw_file, NULL)); nfs_to_nfsd_file_put_local(&nfl->rw_file);
if (ro_nf)
nfs_to_nfsd_file_put_local(ro_nf);
if (rw_nf)
nfs_to_nfsd_file_put_local(rw_nf);
/* Remove nfl from nfs_uuid->files list and signal nfs_uuid_put() /* Remove nfl from nfs_uuid->files list and signal nfs_uuid_put()
* that we are done. The moment we drop the spinlock the * that we are done. The moment we drop the spinlock the

View file

@ -378,11 +378,16 @@ nfsd_file_put(struct nfsd_file *nf)
* the reference of the nfsd_file. * the reference of the nfsd_file.
*/ */
struct net * struct net *
nfsd_file_put_local(struct nfsd_file *nf) nfsd_file_put_local(struct nfsd_file __rcu **pnf)
{ {
struct net *net = nf->nf_net; struct nfsd_file *nf;
struct net *net = NULL;
nf = unrcu_pointer(xchg(pnf, NULL));
if (nf) {
net = nf->nf_net;
nfsd_file_put(nf); nfsd_file_put(nf);
}
return net; return net;
} }

View file

@ -62,7 +62,7 @@ void nfsd_file_cache_shutdown(void);
int nfsd_file_cache_start_net(struct net *net); int nfsd_file_cache_start_net(struct net *net);
void nfsd_file_cache_shutdown_net(struct net *net); void nfsd_file_cache_shutdown_net(struct net *net);
void nfsd_file_put(struct nfsd_file *nf); void nfsd_file_put(struct nfsd_file *nf);
struct net *nfsd_file_put_local(struct nfsd_file *nf); struct net *nfsd_file_put_local(struct nfsd_file __rcu **nf);
struct nfsd_file *nfsd_file_get_local(struct nfsd_file *nf); struct nfsd_file *nfsd_file_get_local(struct nfsd_file *nf);
struct nfsd_file *nfsd_file_get(struct nfsd_file *nf); struct nfsd_file *nfsd_file_get(struct nfsd_file *nf);
struct file *nfsd_file_file(struct nfsd_file *nf); struct file *nfsd_file_file(struct nfsd_file *nf);

View file

@ -50,10 +50,6 @@ void nfs_localio_invalidate_clients(struct list_head *nn_local_clients,
spinlock_t *nn_local_clients_lock); spinlock_t *nn_local_clients_lock);
/* localio needs to map filehandle -> struct nfsd_file */ /* localio needs to map filehandle -> struct nfsd_file */
extern struct nfsd_file *
nfsd_open_local_fh(struct net *, struct auth_domain *, struct rpc_clnt *,
const struct cred *, const struct nfs_fh *,
const fmode_t) __must_hold(rcu);
void nfs_close_local_fh(struct nfs_file_localio *); void nfs_close_local_fh(struct nfs_file_localio *);
struct nfsd_localio_operations { struct nfsd_localio_operations {
@ -64,8 +60,9 @@ struct nfsd_localio_operations {
struct rpc_clnt *, struct rpc_clnt *,
const struct cred *, const struct cred *,
const struct nfs_fh *, const struct nfs_fh *,
struct nfsd_file __rcu **pnf,
const fmode_t); const fmode_t);
struct net *(*nfsd_file_put_local)(struct nfsd_file *); struct net *(*nfsd_file_put_local)(struct nfsd_file __rcu **);
struct nfsd_file *(*nfsd_file_get_local)(struct nfsd_file *); struct nfsd_file *(*nfsd_file_get_local)(struct nfsd_file *);
struct file *(*nfsd_file_file)(struct nfsd_file *); struct file *(*nfsd_file_file)(struct nfsd_file *);
} ____cacheline_aligned; } ____cacheline_aligned;
@ -76,6 +73,7 @@ extern const struct nfsd_localio_operations *nfs_to;
struct nfsd_file *nfs_open_local_fh(nfs_uuid_t *, struct nfsd_file *nfs_open_local_fh(nfs_uuid_t *,
struct rpc_clnt *, const struct cred *, struct rpc_clnt *, const struct cred *,
const struct nfs_fh *, struct nfs_file_localio *, const struct nfs_fh *, struct nfs_file_localio *,
struct nfsd_file __rcu **pnf,
const fmode_t); const fmode_t);
static inline void nfs_to_nfsd_net_put(struct net *net) static inline void nfs_to_nfsd_net_put(struct net *net)
@ -90,15 +88,18 @@ static inline void nfs_to_nfsd_net_put(struct net *net)
rcu_read_unlock(); rcu_read_unlock();
} }
static inline void nfs_to_nfsd_file_put_local(struct nfsd_file *localio) static inline void nfs_to_nfsd_file_put_local(struct nfsd_file __rcu **localio)
{ {
/* /*
* Must not hold RCU otherwise nfsd_file_put() can easily trigger: * Either *localio must be guaranteed to be non-NULL, or caller
* "Voluntary context switch within RCU read-side critical section!" * must prevent nfsd shutdown from completing as nfs_close_local_fh()
* by scheduling deep in underlying filesystem (e.g. XFS). * does by blocking the nfs_uuid from being finally put.
*/ */
struct net *net = nfs_to->nfsd_file_put_local(localio); struct net *net;
net = nfs_to->nfsd_file_put_local(localio);
if (net)
nfs_to_nfsd_net_put(net); nfs_to_nfsd_net_put(net);
} }