Skip to content

Commit 5bb76f1

Browse files
author
Peter Zijlstra
committed
sched: Simplify: migrate_swap_stop()
Use guards to reduce gotos and simplify control flow. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Valentin Schneider <vschneid@redhat.com> Link: https://lore.kernel.org/r/20230801211811.964370836@infradead.org
1 parent 0f92cdf commit 5bb76f1

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

kernel/sched/core.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3433,41 +3433,32 @@ static int migrate_swap_stop(void *data)
34333433
{
34343434
struct migration_swap_arg *arg = data;
34353435
struct rq *src_rq, *dst_rq;
3436-
int ret = -EAGAIN;
34373436

34383437
if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
34393438
return -EAGAIN;
34403439

34413440
src_rq = cpu_rq(arg->src_cpu);
34423441
dst_rq = cpu_rq(arg->dst_cpu);
34433442

3444-
double_raw_lock(&arg->src_task->pi_lock,
3445-
&arg->dst_task->pi_lock);
3446-
double_rq_lock(src_rq, dst_rq);
3443+
guard(double_raw_spinlock)(&arg->src_task->pi_lock, &arg->dst_task->pi_lock);
3444+
guard(double_rq_lock)(src_rq, dst_rq);
34473445

34483446
if (task_cpu(arg->dst_task) != arg->dst_cpu)
3449-
goto unlock;
3447+
return -EAGAIN;
34503448

34513449
if (task_cpu(arg->src_task) != arg->src_cpu)
3452-
goto unlock;
3450+
return -EAGAIN;
34533451

34543452
if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr))
3455-
goto unlock;
3453+
return -EAGAIN;
34563454

34573455
if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr))
3458-
goto unlock;
3456+
return -EAGAIN;
34593457

34603458
__migrate_swap_task(arg->src_task, arg->dst_cpu);
34613459
__migrate_swap_task(arg->dst_task, arg->src_cpu);
34623460

3463-
ret = 0;
3464-
3465-
unlock:
3466-
double_rq_unlock(src_rq, dst_rq);
3467-
raw_spin_unlock(&arg->dst_task->pi_lock);
3468-
raw_spin_unlock(&arg->src_task->pi_lock);
3469-
3470-
return ret;
3461+
return 0;
34713462
}
34723463

34733464
/*

kernel/sched/sched.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,6 +2614,12 @@ static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2)
26142614
static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) {}
26152615
#endif
26162616

2617+
#define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...) \
2618+
__DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__) \
2619+
static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2) \
2620+
{ class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t; \
2621+
_lock; return _t; }
2622+
26172623
#ifdef CONFIG_SMP
26182624

26192625
static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
@@ -2743,6 +2749,16 @@ static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
27432749
raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
27442750
}
27452751

2752+
static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
2753+
{
2754+
raw_spin_unlock(l1);
2755+
raw_spin_unlock(l2);
2756+
}
2757+
2758+
DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
2759+
double_raw_lock(_T->lock, _T->lock2),
2760+
double_raw_unlock(_T->lock, _T->lock2))
2761+
27462762
/*
27472763
* double_rq_unlock - safely unlock two runqueues
27482764
*
@@ -2800,6 +2816,10 @@ static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
28002816

28012817
#endif
28022818

2819+
DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
2820+
double_rq_lock(_T->lock, _T->lock2),
2821+
double_rq_unlock(_T->lock, _T->lock2))
2822+
28032823
extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
28042824
extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
28052825

0 commit comments

Comments
 (0)