An option to make fcntl & flock locks fair
Subsystems (2)
PATCH 1/1 An option to make fcntl & flock locks fair
Shlomi Fish asked about including first-come, first-served style locking for posix and flock locks. After some back-and-forth, we came up with the following patch which seems unintrusive enough to bother including. Personally, I doubt the utility of this, but someone might have an application for it, and the code's already written.
fs/locks.c +21 -15
@@ -126,6 +126,14 @@#include <asm/semaphore.h> #include <asm/uaccess.h> +/* Defining FAIR_LOCKS prevents readers from starving writers. I'm not +* sure anyone has a usage pattern where this is actually a problem, but +* it's a trivial change to allow you to enable this, so I added it. +* Note that we record the owner and check it first, so you can't deadlock +* on an attempt to modify your own lock. +*/ +#undef FAIR_LOCKS +#define IS_POSIX (fl) (fl->fl_flags & FL_POSIX)#define IS_FLOCK (fl) (fl->fl_flags & FL_FLOCK)#define IS_LEASE (fl) (fl->fl_flags & FL_LEASE)
@@ -503,24 +508,22 @@ static void locks_delete_lock(struct fillocks_free_lock (fl); } -/* Determine if lock sys_fl blocks lock caller_fl. Common functionality -* checks for shared/exclusive status of overlapping locks. +/* +* Determine if lock sys_fl blocks lock caller_fl. Write locks require +* exclusive access, read locks can share. If you define FAIR_LOCKS, +* readers can only share until a writer comes along and blocks. */ static int locks_conflict (struct file_lock *caller_fl,struct file_lock *sys_fl) { -switch (caller_fl->fl_type) { -case F_RDLCK : -return (sys_fl->fl_type ==F_WRLCK ); - -case F_WRLCK : -return (1 ); - -default : -printk (KERN_ERR "locks_conflict(): impossible lock type - %d\n" , - caller_fl->fl_type); -break ; - } -return (0 );/* This should never happen */ +if (caller_fl->fl_type ==F_WRLCK ) +return 1 ; +if (sys_fl->fl_type ==F_WRLCK ) +return 1 ; +#ifdef FAIR_LOCKS +if (!list_empty (&sys_fl->fl_block)) +return 1 ; +#endif +return 0 ; }/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
Signature · 1 line
Revolutions do not require corporate support.