Skip to content

Commit c5b1f0d

Browse files
committed
locks/nfsd: allocate file lock outside of spinlock
As suggested by Christoph Hellwig, this moves allocation of new file locks out of generic_setlease into the callers, nfs4_open_delegation and fcntl_setlease in order to allow GFP_KERNEL allocations when lock_flocks has become a spinlock. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Acked-by: J. Bruce Fields <bfields@redhat.com>
1 parent a282a1f commit c5b1f0d

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

fs/locks.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ EXPORT_SYMBOL_GPL(unlock_flocks);
162162
static struct kmem_cache *filelock_cache __read_mostly;
163163

164164
/* Allocate an empty lock structure. */
165-
static struct file_lock *locks_alloc_lock(void)
165+
struct file_lock *locks_alloc_lock(void)
166166
{
167167
return kmem_cache_alloc(filelock_cache, GFP_KERNEL);
168168
}
169+
EXPORT_SYMBOL_GPL(locks_alloc_lock);
169170

170171
void locks_release_private(struct file_lock *fl)
171172
{
@@ -1365,7 +1366,6 @@ int fcntl_getlease(struct file *filp)
13651366
int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
13661367
{
13671368
struct file_lock *fl, **before, **my_before = NULL, *lease;
1368-
struct file_lock *new_fl = NULL;
13691369
struct dentry *dentry = filp->f_path.dentry;
13701370
struct inode *inode = dentry->d_inode;
13711371
int error, rdlease_count = 0, wrlease_count = 0;
@@ -1385,11 +1385,6 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
13851385
lease = *flp;
13861386

13871387
if (arg != F_UNLCK) {
1388-
error = -ENOMEM;
1389-
new_fl = locks_alloc_lock();
1390-
if (new_fl == NULL)
1391-
goto out;
1392-
13931388
error = -EAGAIN;
13941389
if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
13951390
goto out;
@@ -1434,23 +1429,18 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
14341429
goto out;
14351430
}
14361431

1437-
error = 0;
14381432
if (arg == F_UNLCK)
14391433
goto out;
14401434

14411435
error = -EINVAL;
14421436
if (!leases_enable)
14431437
goto out;
14441438

1445-
locks_copy_lock(new_fl, lease);
1446-
locks_insert_lock(before, new_fl);
1447-
1448-
*flp = new_fl;
1439+
locks_insert_lock(before, lease);
14491440
return 0;
14501441

14511442
out:
1452-
if (new_fl != NULL)
1453-
locks_free_lock(new_fl);
1443+
locks_free_lock(lease);
14541444
return error;
14551445
}
14561446
EXPORT_SYMBOL(generic_setlease);
@@ -1514,26 +1504,24 @@ EXPORT_SYMBOL_GPL(vfs_setlease);
15141504
*/
15151505
int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
15161506
{
1517-
struct file_lock fl, *flp = &fl;
1507+
struct file_lock *fl;
15181508
struct inode *inode = filp->f_path.dentry->d_inode;
15191509
int error;
15201510

1521-
locks_init_lock(&fl);
1522-
error = lease_init(filp, arg, &fl);
1523-
if (error)
1524-
return error;
1511+
fl = lease_alloc(filp, arg);
1512+
if (IS_ERR(fl))
1513+
return PTR_ERR(fl);
15251514

15261515
lock_flocks();
1527-
1528-
error = __vfs_setlease(filp, arg, &flp);
1516+
error = __vfs_setlease(filp, arg, &fl);
15291517
if (error || arg == F_UNLCK)
15301518
goto out_unlock;
15311519

1532-
error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
1520+
error = fasync_helper(fd, filp, 1, &fl->fl_fasync);
15331521
if (error < 0) {
15341522
/* remove lease just inserted by setlease */
1535-
flp->fl_type = F_UNLCK | F_INPROGRESS;
1536-
flp->fl_break_time = jiffies - 10;
1523+
fl->fl_type = F_UNLCK | F_INPROGRESS;
1524+
fl->fl_break_time = jiffies - 10;
15371525
time_out_leases(inode);
15381526
goto out_unlock;
15391527
}

fs/nfsd/nfs4state.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_sta
26142614
struct nfs4_delegation *dp;
26152615
struct nfs4_stateowner *sop = stp->st_stateowner;
26162616
int cb_up = atomic_read(&sop->so_client->cl_cb_set);
2617-
struct file_lock fl, *flp = &fl;
2617+
struct file_lock *fl;
26182618
int status, flag = 0;
26192619

26202620
flag = NFS4_OPEN_DELEGATE_NONE;
@@ -2648,20 +2648,24 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_sta
26482648
flag = NFS4_OPEN_DELEGATE_NONE;
26492649
goto out;
26502650
}
2651-
locks_init_lock(&fl);
2652-
fl.fl_lmops = &nfsd_lease_mng_ops;
2653-
fl.fl_flags = FL_LEASE;
2654-
fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2655-
fl.fl_end = OFFSET_MAX;
2656-
fl.fl_owner = (fl_owner_t)dp;
2657-
fl.fl_file = find_readable_file(stp->st_file);
2658-
BUG_ON(!fl.fl_file);
2659-
fl.fl_pid = current->tgid;
2651+
status = -ENOMEM;
2652+
fl = locks_alloc_lock();
2653+
if (!fl)
2654+
goto out;
2655+
locks_init_lock(fl);
2656+
fl->fl_lmops = &nfsd_lease_mng_ops;
2657+
fl->fl_flags = FL_LEASE;
2658+
fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2659+
fl->fl_end = OFFSET_MAX;
2660+
fl->fl_owner = (fl_owner_t)dp;
2661+
fl->fl_file = find_readable_file(stp->st_file);
2662+
BUG_ON(!fl->fl_file);
2663+
fl->fl_pid = current->tgid;
26602664

26612665
/* vfs_setlease checks to see if delegation should be handed out.
26622666
* the lock_manager callbacks fl_mylease and fl_change are used
26632667
*/
2664-
if ((status = vfs_setlease(fl.fl_file, fl.fl_type, &flp))) {
2668+
if ((status = vfs_setlease(fl->fl_file, fl->fl_type, &fl))) {
26652669
dprintk("NFSD: setlease failed [%d], no delegation\n", status);
26662670
unhash_delegation(dp);
26672671
flag = NFS4_OPEN_DELEGATE_NONE;

include/linux/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ extern int fcntl_getlease(struct file *filp);
11131113

11141114
/* fs/locks.c */
11151115
extern void locks_init_lock(struct file_lock *);
1116+
extern struct file_lock * locks_alloc_lock(void);
11161117
extern void locks_copy_lock(struct file_lock *, struct file_lock *);
11171118
extern void __locks_copy_lock(struct file_lock *, const struct file_lock *);
11181119
extern void locks_remove_posix(struct file *, fl_owner_t);

0 commit comments

Comments
 (0)