Skip to content

Commit 8a30aeb

Browse files
committed
Merge tag 'nfsd-7.0-2' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux
Pull nfsd fixes from Chuck Lever: - Fix cache_request leak in cache_release() - Fix heap overflow in the NFSv4.0 LOCK replay cache - Hold net reference for the lifetime of /proc/fs/nfs/exports fd - Defer sub-object cleanup in export "put" callbacks * tag 'nfsd-7.0-2' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux: nfsd: fix heap overflow in NFSv4.0 LOCK replay cache sunrpc: fix cache_request leak in cache_release NFSD: Hold net reference for the lifetime of /proc/fs/nfs/exports fd NFSD: Defer sub-object cleanup in export put callbacks
2 parents 04a9f17 + 5133b61 commit 8a30aeb

6 files changed

Lines changed: 118 additions & 26 deletions

File tree

fs/nfsd/export.c

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,30 @@
3636
* second map contains a reference to the entry in the first map.
3737
*/
3838

39+
static struct workqueue_struct *nfsd_export_wq;
40+
3941
#define EXPKEY_HASHBITS 8
4042
#define EXPKEY_HASHMAX (1 << EXPKEY_HASHBITS)
4143
#define EXPKEY_HASHMASK (EXPKEY_HASHMAX -1)
4244

43-
static void expkey_put(struct kref *ref)
45+
static void expkey_release(struct work_struct *work)
4446
{
45-
struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
47+
struct svc_expkey *key = container_of(to_rcu_work(work),
48+
struct svc_expkey, ek_rwork);
4649

4750
if (test_bit(CACHE_VALID, &key->h.flags) &&
4851
!test_bit(CACHE_NEGATIVE, &key->h.flags))
4952
path_put(&key->ek_path);
5053
auth_domain_put(key->ek_client);
51-
kfree_rcu(key, ek_rcu);
54+
kfree(key);
55+
}
56+
57+
static void expkey_put(struct kref *ref)
58+
{
59+
struct svc_expkey *key = container_of(ref, struct svc_expkey, h.ref);
60+
61+
INIT_RCU_WORK(&key->ek_rwork, expkey_release);
62+
queue_rcu_work(nfsd_export_wq, &key->ek_rwork);
5263
}
5364

5465
static int expkey_upcall(struct cache_detail *cd, struct cache_head *h)
@@ -353,11 +364,13 @@ static void export_stats_destroy(struct export_stats *stats)
353364
EXP_STATS_COUNTERS_NUM);
354365
}
355366

356-
static void svc_export_release(struct rcu_head *rcu_head)
367+
static void svc_export_release(struct work_struct *work)
357368
{
358-
struct svc_export *exp = container_of(rcu_head, struct svc_export,
359-
ex_rcu);
369+
struct svc_export *exp = container_of(to_rcu_work(work),
370+
struct svc_export, ex_rwork);
360371

372+
path_put(&exp->ex_path);
373+
auth_domain_put(exp->ex_client);
361374
nfsd4_fslocs_free(&exp->ex_fslocs);
362375
export_stats_destroy(exp->ex_stats);
363376
kfree(exp->ex_stats);
@@ -369,9 +382,8 @@ static void svc_export_put(struct kref *ref)
369382
{
370383
struct svc_export *exp = container_of(ref, struct svc_export, h.ref);
371384

372-
path_put(&exp->ex_path);
373-
auth_domain_put(exp->ex_client);
374-
call_rcu(&exp->ex_rcu, svc_export_release);
385+
INIT_RCU_WORK(&exp->ex_rwork, svc_export_release);
386+
queue_rcu_work(nfsd_export_wq, &exp->ex_rwork);
375387
}
376388

377389
static int svc_export_upcall(struct cache_detail *cd, struct cache_head *h)
@@ -1479,6 +1491,36 @@ const struct seq_operations nfs_exports_op = {
14791491
.show = e_show,
14801492
};
14811493

1494+
/**
1495+
* nfsd_export_wq_init - allocate the export release workqueue
1496+
*
1497+
* Called once at module load. The workqueue runs deferred svc_export and
1498+
* svc_expkey release work scheduled by queue_rcu_work() in the cache put
1499+
* callbacks.
1500+
*
1501+
* Return values:
1502+
* %0: workqueue allocated
1503+
* %-ENOMEM: allocation failed
1504+
*/
1505+
int nfsd_export_wq_init(void)
1506+
{
1507+
nfsd_export_wq = alloc_workqueue("nfsd_export", WQ_UNBOUND, 0);
1508+
if (!nfsd_export_wq)
1509+
return -ENOMEM;
1510+
return 0;
1511+
}
1512+
1513+
/**
1514+
* nfsd_export_wq_shutdown - drain and free the export release workqueue
1515+
*
1516+
* Called once at module unload. Per-namespace teardown in
1517+
* nfsd_export_shutdown() has already drained all deferred work.
1518+
*/
1519+
void nfsd_export_wq_shutdown(void)
1520+
{
1521+
destroy_workqueue(nfsd_export_wq);
1522+
}
1523+
14821524
/*
14831525
* Initialize the exports module.
14841526
*/
@@ -1540,6 +1582,9 @@ nfsd_export_shutdown(struct net *net)
15401582

15411583
cache_unregister_net(nn->svc_expkey_cache, net);
15421584
cache_unregister_net(nn->svc_export_cache, net);
1585+
/* Drain deferred export and expkey release work. */
1586+
rcu_barrier();
1587+
flush_workqueue(nfsd_export_wq);
15431588
cache_destroy_net(nn->svc_expkey_cache, net);
15441589
cache_destroy_net(nn->svc_export_cache, net);
15451590
svcauth_unix_purge(net);

fs/nfsd/export.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <linux/sunrpc/cache.h>
99
#include <linux/percpu_counter.h>
10+
#include <linux/workqueue.h>
1011
#include <uapi/linux/nfsd/export.h>
1112
#include <linux/nfs4.h>
1213

@@ -75,7 +76,7 @@ struct svc_export {
7576
u32 ex_layout_types;
7677
struct nfsd4_deviceid_map *ex_devid_map;
7778
struct cache_detail *cd;
78-
struct rcu_head ex_rcu;
79+
struct rcu_work ex_rwork;
7980
unsigned long ex_xprtsec_modes;
8081
struct export_stats *ex_stats;
8182
};
@@ -92,7 +93,7 @@ struct svc_expkey {
9293
u32 ek_fsid[6];
9394

9495
struct path ek_path;
95-
struct rcu_head ek_rcu;
96+
struct rcu_work ek_rwork;
9697
};
9798

9899
#define EX_ISSYNC(exp) (!((exp)->ex_flags & NFSEXP_ASYNC))
@@ -110,6 +111,8 @@ __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
110111
/*
111112
* Function declarations
112113
*/
114+
int nfsd_export_wq_init(void);
115+
void nfsd_export_wq_shutdown(void);
113116
int nfsd_export_init(struct net *);
114117
void nfsd_export_shutdown(struct net *);
115118
void nfsd_export_flush(struct net *);

fs/nfsd/nfs4xdr.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6281,9 +6281,14 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
62816281
int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
62826282

62836283
so->so_replay.rp_status = op->status;
6284-
so->so_replay.rp_buflen = len;
6285-
read_bytes_from_xdr_buf(xdr->buf, op_status_offset + XDR_UNIT,
6284+
if (len <= NFSD4_REPLAY_ISIZE) {
6285+
so->so_replay.rp_buflen = len;
6286+
read_bytes_from_xdr_buf(xdr->buf,
6287+
op_status_offset + XDR_UNIT,
62866288
so->so_replay.rp_buf, len);
6289+
} else {
6290+
so->so_replay.rp_buflen = 0;
6291+
}
62876292
}
62886293
status:
62896294
op->status = nfsd4_map_status(op->status,

fs/nfsd/nfsctl.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,19 @@ static int exports_net_open(struct net *net, struct file *file)
149149

150150
seq = file->private_data;
151151
seq->private = nn->svc_export_cache;
152+
get_net(net);
152153
return 0;
153154
}
154155

156+
static int exports_release(struct inode *inode, struct file *file)
157+
{
158+
struct seq_file *seq = file->private_data;
159+
struct cache_detail *cd = seq->private;
160+
161+
put_net(cd->net);
162+
return seq_release(inode, file);
163+
}
164+
155165
static int exports_nfsd_open(struct inode *inode, struct file *file)
156166
{
157167
return exports_net_open(inode->i_sb->s_fs_info, file);
@@ -161,7 +171,7 @@ static const struct file_operations exports_nfsd_operations = {
161171
.open = exports_nfsd_open,
162172
.read = seq_read,
163173
.llseek = seq_lseek,
164-
.release = seq_release,
174+
.release = exports_release,
165175
};
166176

167177
static int export_features_show(struct seq_file *m, void *v)
@@ -1376,7 +1386,7 @@ static const struct proc_ops exports_proc_ops = {
13761386
.proc_open = exports_proc_open,
13771387
.proc_read = seq_read,
13781388
.proc_lseek = seq_lseek,
1379-
.proc_release = seq_release,
1389+
.proc_release = exports_release,
13801390
};
13811391

13821392
static int create_proc_exports_entry(void)
@@ -2259,9 +2269,12 @@ static int __init init_nfsd(void)
22592269
if (retval)
22602270
goto out_free_pnfs;
22612271
nfsd_lockd_init(); /* lockd->nfsd callbacks */
2272+
retval = nfsd_export_wq_init();
2273+
if (retval)
2274+
goto out_free_lockd;
22622275
retval = register_pernet_subsys(&nfsd_net_ops);
22632276
if (retval < 0)
2264-
goto out_free_lockd;
2277+
goto out_free_export_wq;
22652278
retval = register_cld_notifier();
22662279
if (retval)
22672280
goto out_free_subsys;
@@ -2290,6 +2303,8 @@ static int __init init_nfsd(void)
22902303
unregister_cld_notifier();
22912304
out_free_subsys:
22922305
unregister_pernet_subsys(&nfsd_net_ops);
2306+
out_free_export_wq:
2307+
nfsd_export_wq_shutdown();
22932308
out_free_lockd:
22942309
nfsd_lockd_shutdown();
22952310
nfsd_drc_slab_free();
@@ -2310,6 +2325,7 @@ static void __exit exit_nfsd(void)
23102325
nfsd4_destroy_laundry_wq();
23112326
unregister_cld_notifier();
23122327
unregister_pernet_subsys(&nfsd_net_ops);
2328+
nfsd_export_wq_shutdown();
23132329
nfsd_drc_slab_free();
23142330
nfsd_lockd_shutdown();
23152331
nfsd4_free_slabs();

fs/nfsd/state.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,18 @@ struct nfs4_client_reclaim {
541541
struct xdr_netobj cr_princhash;
542542
};
543543

544-
/* A reasonable value for REPLAY_ISIZE was estimated as follows:
545-
* The OPEN response, typically the largest, requires
546-
* 4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) + 8(verifier) +
547-
* 4(deleg. type) + 8(deleg. stateid) + 4(deleg. recall flag) +
548-
* 20(deleg. space limit) + ~32(deleg. ace) = 112 bytes
544+
/*
545+
* REPLAY_ISIZE is sized for an OPEN response with delegation:
546+
* 4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) +
547+
* 8(verifier) + 4(deleg. type) + 8(deleg. stateid) +
548+
* 4(deleg. recall flag) + 20(deleg. space limit) +
549+
* ~32(deleg. ace) = 112 bytes
550+
*
551+
* Some responses can exceed this. A LOCK denial includes the conflicting
552+
* lock owner, which can be up to 1024 bytes (NFS4_OPAQUE_LIMIT). Responses
553+
* larger than REPLAY_ISIZE are not cached in rp_ibuf; only rp_status is
554+
* saved. Enlarging this constant increases the size of every
555+
* nfs4_stateowner.
549556
*/
550557

551558
#define NFSD4_REPLAY_ISIZE 112

net/sunrpc/cache.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,24 +1062,40 @@ static int cache_release(struct inode *inode, struct file *filp,
10621062
struct cache_reader *rp = filp->private_data;
10631063

10641064
if (rp) {
1065+
struct cache_request *rq = NULL;
1066+
10651067
spin_lock(&queue_lock);
10661068
if (rp->offset) {
10671069
struct cache_queue *cq;
1068-
for (cq= &rp->q; &cq->list != &cd->queue;
1069-
cq = list_entry(cq->list.next, struct cache_queue, list))
1070+
for (cq = &rp->q; &cq->list != &cd->queue;
1071+
cq = list_entry(cq->list.next,
1072+
struct cache_queue, list))
10701073
if (!cq->reader) {
1071-
container_of(cq, struct cache_request, q)
1072-
->readers--;
1074+
struct cache_request *cr =
1075+
container_of(cq,
1076+
struct cache_request, q);
1077+
cr->readers--;
1078+
if (cr->readers == 0 &&
1079+
!test_bit(CACHE_PENDING,
1080+
&cr->item->flags)) {
1081+
list_del(&cr->q.list);
1082+
rq = cr;
1083+
}
10731084
break;
10741085
}
10751086
rp->offset = 0;
10761087
}
10771088
list_del(&rp->q.list);
10781089
spin_unlock(&queue_lock);
10791090

1091+
if (rq) {
1092+
cache_put(rq->item, cd);
1093+
kfree(rq->buf);
1094+
kfree(rq);
1095+
}
1096+
10801097
filp->private_data = NULL;
10811098
kfree(rp);
1082-
10831099
}
10841100
if (filp->f_mode & FMODE_WRITE) {
10851101
atomic_dec(&cd->writers);

0 commit comments

Comments
 (0)