Skip to content

Commit 4149be7

Browse files
q2venjtlayton
authored andcommitted
fs/lock: Don't allocate file_lock in flock_make_lock().
Two functions, flock syscall and locks_remove_flock(), call flock_make_lock(). It allocates struct file_lock from slab cache if its argument fl is NULL. When we call flock syscall, we pass NULL to allocate memory for struct file_lock. However, we always free it at the end by locks_free_lock(). We need not allocate it and instead should use a local variable as locks_remove_flock() does. Also, the validation for flock_translate_cmd() is not necessary for locks_remove_flock(). So we move the part to flock syscall and make flock_make_lock() return nothing. Signed-off-by: Kuniyuki Iwashima <[email protected]> Reviewed-by: Chuck Lever <[email protected]> Signed-off-by: Jeff Layton <[email protected]>
1 parent ff69927 commit 4149be7

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

fs/locks.c

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -425,30 +425,16 @@ static inline int flock_translate_cmd(int cmd) {
425425
}
426426

427427
/* Fill in a file_lock structure with an appropriate FLOCK lock. */
428-
static struct file_lock *
429-
flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
428+
static void flock_make_lock(struct file *filp, struct file_lock *fl, int type)
430429
{
431-
int type = flock_translate_cmd(cmd);
432-
433-
if (type < 0)
434-
return ERR_PTR(type);
435-
436-
if (fl == NULL) {
437-
fl = locks_alloc_lock();
438-
if (fl == NULL)
439-
return ERR_PTR(-ENOMEM);
440-
} else {
441-
locks_init_lock(fl);
442-
}
430+
locks_init_lock(fl);
443431

444432
fl->fl_file = filp;
445433
fl->fl_owner = filp;
446434
fl->fl_pid = current->tgid;
447435
fl->fl_flags = FL_FLOCK;
448436
fl->fl_type = type;
449437
fl->fl_end = OFFSET_MAX;
450-
451-
return fl;
452438
}
453439

454440
static int assign_type(struct file_lock *fl, long type)
@@ -2097,10 +2083,9 @@ EXPORT_SYMBOL(locks_lock_inode_wait);
20972083
*/
20982084
SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
20992085
{
2086+
int can_sleep, error, unlock, type;
21002087
struct fd f = fdget(fd);
2101-
struct file_lock *lock;
2102-
int can_sleep, unlock;
2103-
int error;
2088+
struct file_lock fl;
21042089

21052090
error = -EBADF;
21062091
if (!f.file)
@@ -2127,28 +2112,27 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21272112
goto out_putf;
21282113
}
21292114

2130-
lock = flock_make_lock(f.file, cmd, NULL);
2131-
if (IS_ERR(lock)) {
2132-
error = PTR_ERR(lock);
2115+
type = flock_translate_cmd(cmd);
2116+
if (type < 0) {
2117+
error = type;
21332118
goto out_putf;
21342119
}
21352120

2121+
flock_make_lock(f.file, &fl, type);
2122+
21362123
if (can_sleep)
2137-
lock->fl_flags |= FL_SLEEP;
2124+
fl.fl_flags |= FL_SLEEP;
21382125

2139-
error = security_file_lock(f.file, lock->fl_type);
2126+
error = security_file_lock(f.file, fl.fl_type);
21402127
if (error)
2141-
goto out_free;
2128+
goto out_putf;
21422129

21432130
if (f.file->f_op->flock)
21442131
error = f.file->f_op->flock(f.file,
21452132
(can_sleep) ? F_SETLKW : F_SETLK,
2146-
lock);
2133+
&fl);
21472134
else
2148-
error = locks_lock_file_wait(f.file, lock);
2149-
2150-
out_free:
2151-
locks_free_lock(lock);
2135+
error = locks_lock_file_wait(f.file, &fl);
21522136

21532137
out_putf:
21542138
fdput(f);
@@ -2614,7 +2598,7 @@ locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
26142598
if (list_empty(&flctx->flc_flock))
26152599
return;
26162600

2617-
flock_make_lock(filp, LOCK_UN, &fl);
2601+
flock_make_lock(filp, &fl, F_UNLCK);
26182602
fl.fl_flags |= FL_CLOSE;
26192603

26202604
if (filp->f_op->flock)

0 commit comments

Comments
 (0)