Skip to content

Commit 48dfb5d

Browse files
Gokul krishna KrishnakumarPeter Zijlstra
authored andcommitted
locking/rwsem: Disable preemption while trying for rwsem lock
Make the region inside the rwsem_write_trylock non preemptible. We observe RT task is hogging CPU when trying to acquire rwsem lock which was acquired by a kworker task but before the rwsem owner was set. Here is the scenario: 1. CFS task (affined to a particular CPU) takes rwsem lock. 2. CFS task gets preempted by a RT task before setting owner. 3. RT task (FIFO) is trying to acquire the lock, but spinning until RT throttling happens for the lock as the lock was taken by CFS task. This patch attempts to fix the above issue by disabling preemption until owner is set for the lock. While at it also fix the issues at the places where rwsem_{set,clear}_owner() are called. This also adds lockdep annotation of preemption disable in rwsem_{set,clear}_owner() on Peter Z. suggestion. Signed-off-by: Gokul krishna Krishnakumar <quic_gokukris@quicinc.com> Signed-off-by: Mukesh Ojha <quic_mojha@quicinc.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Waiman Long <longman@redhat.com> Link: https://lore.kernel.org/r/1662661467-24203-1-git-send-email-quic_mojha@quicinc.com
1 parent 2747b93 commit 48dfb5d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

kernel/locking/rwsem.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,19 @@
133133
* the owner value concurrently without lock. Read from owner, however,
134134
* may not need READ_ONCE() as long as the pointer value is only used
135135
* for comparison and isn't being dereferenced.
136+
*
137+
* Both rwsem_{set,clear}_owner() functions should be in the same
138+
* preempt disable section as the atomic op that changes sem->count.
136139
*/
137140
static inline void rwsem_set_owner(struct rw_semaphore *sem)
138141
{
142+
lockdep_assert_preemption_disabled();
139143
atomic_long_set(&sem->owner, (long)current);
140144
}
141145

142146
static inline void rwsem_clear_owner(struct rw_semaphore *sem)
143147
{
148+
lockdep_assert_preemption_disabled();
144149
atomic_long_set(&sem->owner, 0);
145150
}
146151

@@ -251,13 +256,16 @@ static inline bool rwsem_read_trylock(struct rw_semaphore *sem, long *cntp)
251256
static inline bool rwsem_write_trylock(struct rw_semaphore *sem)
252257
{
253258
long tmp = RWSEM_UNLOCKED_VALUE;
259+
bool ret = false;
254260

261+
preempt_disable();
255262
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp, RWSEM_WRITER_LOCKED)) {
256263
rwsem_set_owner(sem);
257-
return true;
264+
ret = true;
258265
}
259266

260-
return false;
267+
preempt_enable();
268+
return ret;
261269
}
262270

263271
/*
@@ -1352,8 +1360,10 @@ static inline void __up_write(struct rw_semaphore *sem)
13521360
DEBUG_RWSEMS_WARN_ON((rwsem_owner(sem) != current) &&
13531361
!rwsem_test_oflags(sem, RWSEM_NONSPINNABLE), sem);
13541362

1363+
preempt_disable();
13551364
rwsem_clear_owner(sem);
13561365
tmp = atomic_long_fetch_add_release(-RWSEM_WRITER_LOCKED, &sem->count);
1366+
preempt_enable();
13571367
if (unlikely(tmp & RWSEM_FLAG_WAITERS))
13581368
rwsem_wake(sem);
13591369
}

0 commit comments

Comments
 (0)