File locking changes for v5.3

-----BEGIN PGP SIGNATURE-----
 
 iQJHBAABCAAxFiEES8DXskRxsqGE6vXTAA5oQRlWghUFAl0jN3oTHGpsYXl0b25A
 a2VybmVsLm9yZwAKCRAADmhBGVaCFZtjEADOMpZKgmUzMX4CwGd2QjGe6VEvVenV
 8rwvFgbgmkkPWD/p3n4bpWBJpwhtSLj2OGn9gsXRM52lmPuzX9XQOBp8n5/Dd7qv
 mCAe2yMFWi/imL+neq/xVQLvgi+pBC5dCLhxSX8B+uIokDX7aVWrhnP7csRT5j92
 cZWheeMSu7QWw5l8Rne5STwC6jxHhXb2p63zr6tGjlUT/xtum3bb9ZqOIk4b0Vkn
 2qTkCZVJpGEIWSNCPvW6oKgAXDQqhtQ2sVIQsfoafe1kSbCHhB6WaUfQHwKqB3Nj
 r5R2GFIni877nBqiuZYDUZKyhpkiKIo+cfq2JIQBUBcJBQJ7L7On9wN+NfaWPWXP
 pVTLIXO9ClrWc9HUBTpkHSqvd5w2QlkwdXs500Ar1QD6alvxs5WwggirSHKGubpX
 8zZsgsrvGZRjb5t/JLCRxPTrXqMvrODKh44JRLDt1Twwizw5SG+Alig7P9SvEVda
 7iboRapCJ7ca46AgeIIy2QsUmVjtCg6lFNt3OZsmOJuMSOkANXw9nnQerbprQr7G
 g4BfwkKY8IWfXXE3/TOgLHTZhyRgcbN4vuO6Ej+DdaG3NRrMio1h0+AeoXz38CKm
 7BB0Aw0NtEC1Bn9tn8SZ9cJ120FCC65EZKYzKnhoR0/XVLtXU/rlcxhID30N7185
 j8cy6iZtLoD/Iw==
 =e9Bd
 -----END PGP SIGNATURE-----

Merge tag 'locks-v5.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton/linux

Pull file locking updates from Jeff Layton:
 "Just a couple of small lease-related patches this cycle.

  One from Ira to add a new tracepoint that fires during lease conflict
  checks, and another patch from Amir to reduce false positives when
  checking for lease conflicts"

* tag 'locks-v5.3-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton/linux:
  locks: eliminate false positive conflicts for write lease
  locks: Add trace_leases_conflict
This commit is contained in:
Linus Torvalds 2019-07-10 19:21:38 -07:00
commit 988052f47a
3 changed files with 79 additions and 22 deletions

View File

@ -1534,11 +1534,21 @@ static void time_out_leases(struct inode *inode, struct list_head *dispose)
static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
{ {
if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) bool rc;
return false;
if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) {
return false; rc = false;
return locks_conflict(breaker, lease); goto trace;
}
if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) {
rc = false;
goto trace;
}
rc = locks_conflict(breaker, lease);
trace:
trace_leases_conflict(rc, lease, breaker);
return rc;
} }
static bool static bool
@ -1753,10 +1763,10 @@ int fcntl_getlease(struct file *filp)
} }
/** /**
* check_conflicting_open - see if the given dentry points to a file that has * check_conflicting_open - see if the given file points to an inode that has
* an existing open that would conflict with the * an existing open that would conflict with the
* desired lease. * desired lease.
* @dentry: dentry to check * @filp: file to check
* @arg: type of lease that we're trying to acquire * @arg: type of lease that we're trying to acquire
* @flags: current lock flags * @flags: current lock flags
* *
@ -1764,30 +1774,42 @@ int fcntl_getlease(struct file *filp)
* conflict with the lease we're trying to set. * conflict with the lease we're trying to set.
*/ */
static int static int
check_conflicting_open(const struct dentry *dentry, const long arg, int flags) check_conflicting_open(struct file *filp, const long arg, int flags)
{ {
int ret = 0; struct inode *inode = locks_inode(filp);
struct inode *inode = dentry->d_inode; int self_wcount = 0, self_rcount = 0;
if (flags & FL_LAYOUT) if (flags & FL_LAYOUT)
return 0; return 0;
if ((arg == F_RDLCK) && inode_is_open_for_write(inode)) if (arg == F_RDLCK)
return inode_is_open_for_write(inode) ? -EAGAIN : 0;
else if (arg != F_WRLCK)
return 0;
/*
* Make sure that only read/write count is from lease requestor.
* Note that this will result in denying write leases when i_writecount
* is negative, which is what we want. (We shouldn't grant write leases
* on files open for execution.)
*/
if (filp->f_mode & FMODE_WRITE)
self_wcount = 1;
else if (filp->f_mode & FMODE_READ)
self_rcount = 1;
if (atomic_read(&inode->i_writecount) != self_wcount ||
atomic_read(&inode->i_readcount) != self_rcount)
return -EAGAIN; return -EAGAIN;
if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || return 0;
(atomic_read(&inode->i_count) > 1)))
ret = -EAGAIN;
return ret;
} }
static int static int
generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv) generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
{ {
struct file_lock *fl, *my_fl = NULL, *lease; struct file_lock *fl, *my_fl = NULL, *lease;
struct dentry *dentry = filp->f_path.dentry; struct inode *inode = locks_inode(filp);
struct inode *inode = dentry->d_inode;
struct file_lock_context *ctx; struct file_lock_context *ctx;
bool is_deleg = (*flp)->fl_flags & FL_DELEG; bool is_deleg = (*flp)->fl_flags & FL_DELEG;
int error; int error;
@ -1822,7 +1844,7 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
percpu_down_read(&file_rwsem); percpu_down_read(&file_rwsem);
spin_lock(&ctx->flc_lock); spin_lock(&ctx->flc_lock);
time_out_leases(inode, &dispose); time_out_leases(inode, &dispose);
error = check_conflicting_open(dentry, arg, lease->fl_flags); error = check_conflicting_open(filp, arg, lease->fl_flags);
if (error) if (error)
goto out; goto out;
@ -1879,7 +1901,7 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
* precedes these checks. * precedes these checks.
*/ */
smp_mb(); smp_mb();
error = check_conflicting_open(dentry, arg, lease->fl_flags); error = check_conflicting_open(filp, arg, lease->fl_flags);
if (error) { if (error) {
locks_unlink_lock_ctx(lease); locks_unlink_lock_ctx(lease);
goto out; goto out;

View File

@ -694,7 +694,7 @@ struct inode {
atomic_t i_count; atomic_t i_count;
atomic_t i_dio_count; atomic_t i_dio_count;
atomic_t i_writecount; atomic_t i_writecount;
#ifdef CONFIG_IMA #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
atomic_t i_readcount; /* struct files open RO */ atomic_t i_readcount; /* struct files open RO */
#endif #endif
union { union {
@ -2890,7 +2890,7 @@ static inline bool inode_is_open_for_write(const struct inode *inode)
return atomic_read(&inode->i_writecount) > 0; return atomic_read(&inode->i_writecount) > 0;
} }
#ifdef CONFIG_IMA #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
static inline void i_readcount_dec(struct inode *inode) static inline void i_readcount_dec(struct inode *inode)
{ {
BUG_ON(!atomic_read(&inode->i_readcount)); BUG_ON(!atomic_read(&inode->i_readcount));

View File

@ -203,6 +203,41 @@ TRACE_EVENT(generic_add_lease,
show_fl_type(__entry->fl_type)) show_fl_type(__entry->fl_type))
); );
TRACE_EVENT(leases_conflict,
TP_PROTO(bool conflict, struct file_lock *lease, struct file_lock *breaker),
TP_ARGS(conflict, lease, breaker),
TP_STRUCT__entry(
__field(void *, lease)
__field(void *, breaker)
__field(unsigned int, l_fl_flags)
__field(unsigned int, b_fl_flags)
__field(unsigned char, l_fl_type)
__field(unsigned char, b_fl_type)
__field(bool, conflict)
),
TP_fast_assign(
__entry->lease = lease;
__entry->l_fl_flags = lease->fl_flags;
__entry->l_fl_type = lease->fl_type;
__entry->breaker = breaker;
__entry->b_fl_flags = breaker->fl_flags;
__entry->b_fl_type = breaker->fl_type;
__entry->conflict = conflict;
),
TP_printk("conflict %d: lease=0x%p fl_flags=%s fl_type=%s; breaker=0x%p fl_flags=%s fl_type=%s",
__entry->conflict,
__entry->lease,
show_fl_flags(__entry->l_fl_flags),
show_fl_type(__entry->l_fl_type),
__entry->breaker,
show_fl_flags(__entry->b_fl_flags),
show_fl_type(__entry->b_fl_type))
);
#endif /* _TRACE_FILELOCK_H */ #endif /* _TRACE_FILELOCK_H */
/* This part must be outside protection */ /* This part must be outside protection */