use a mutex for percpu rwsemaphores

bcachefs is using a percpu rwsem to protect percpu data structures, and
in userspace we don't have real percpu data structures - so we need to
guard all access to them with a mutex.
This commit is contained in:
Kent Overstreet 2019-03-08 14:41:57 -05:00
parent 166e32606e
commit 0aaa849412

View File

@ -7,63 +7,44 @@
#include <linux/preempt.h> #include <linux/preempt.h>
struct percpu_rw_semaphore { struct percpu_rw_semaphore {
pthread_rwlock_t lock; pthread_mutex_t lock;
}; };
#define DEFINE_STATIC_PERCPU_RWSEM(name) \
static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
static struct percpu_rw_semaphore name = { \
.rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \
.read_count = &__percpu_rwsem_rc_##name, \
.rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \
.writer = __RCUWAIT_INITIALIZER(name.writer), \
}
extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
extern void __percpu_up_read(struct percpu_rw_semaphore *);
static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem) static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
{ {
pthread_rwlock_rdlock(&sem->lock); pthread_mutex_lock(&sem->lock);
preempt_disable();
} }
static inline void percpu_down_read(struct percpu_rw_semaphore *sem) static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
{ {
pthread_rwlock_rdlock(&sem->lock); pthread_mutex_lock(&sem->lock);
}
static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
{
return !pthread_rwlock_tryrdlock(&sem->lock);
} }
static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem) static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
{ {
preempt_enable(); pthread_mutex_unlock(&sem->lock);
pthread_rwlock_unlock(&sem->lock);
} }
static inline void percpu_up_read(struct percpu_rw_semaphore *sem) static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
{ {
pthread_rwlock_unlock(&sem->lock); pthread_mutex_unlock(&sem->lock);
} }
static inline void percpu_down_write(struct percpu_rw_semaphore *sem) static inline void percpu_down_write(struct percpu_rw_semaphore *sem)
{ {
pthread_rwlock_wrlock(&sem->lock); pthread_mutex_lock(&sem->lock);
} }
static inline void percpu_up_write(struct percpu_rw_semaphore *sem) static inline void percpu_up_write(struct percpu_rw_semaphore *sem)
{ {
pthread_rwlock_unlock(&sem->lock); pthread_mutex_unlock(&sem->lock);
} }
static inline void percpu_free_rwsem(struct percpu_rw_semaphore *sem) {} static inline void percpu_free_rwsem(struct percpu_rw_semaphore *sem) {}
static inline int percpu_init_rwsem(struct percpu_rw_semaphore *sem) static inline int percpu_init_rwsem(struct percpu_rw_semaphore *sem)
{ {
pthread_rwlock_init(&sem->lock, NULL); pthread_mutex_init(&sem->lock, NULL);
return 0; return 0;
} }