mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: mingo@redhat.com
Cc: peterz@infradead.org, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	bristot@redhat.com, vschneid@redhat.com,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/9] sched: Simplify: migrate_swap_stop()
Date: Tue, 01 Aug 2023 22:41:24 +0200	[thread overview]
Message-ID: <20230801211811.964370836@infradead.org> (raw)
In-Reply-To: <20230801204121.929256934@infradead.org>

Use guards to reduce gotos and simplify control flow.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/sched/core.c  |   23 +++++++----------------
 kernel/sched/sched.h |   20 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 16 deletions(-)

--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3258,7 +3258,6 @@ static int migrate_swap_stop(void *data)
 {
 	struct migration_swap_arg *arg = data;
 	struct rq *src_rq, *dst_rq;
-	int ret = -EAGAIN;
 
 	if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
 		return -EAGAIN;
@@ -3266,33 +3265,25 @@ static int migrate_swap_stop(void *data)
 	src_rq = cpu_rq(arg->src_cpu);
 	dst_rq = cpu_rq(arg->dst_cpu);
 
-	double_raw_lock(&arg->src_task->pi_lock,
-			&arg->dst_task->pi_lock);
-	double_rq_lock(src_rq, dst_rq);
+	guard(double_raw_spinlock)(&arg->src_task->pi_lock, &arg->dst_task->pi_lock);
+	guard(double_rq_lock)(src_rq, dst_rq);
 
 	if (task_cpu(arg->dst_task) != arg->dst_cpu)
-		goto unlock;
+		return -EAGAIN;
 
 	if (task_cpu(arg->src_task) != arg->src_cpu)
-		goto unlock;
+		return -EAGAIN;
 
 	if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr))
-		goto unlock;
+		return -EAGAIN;
 
 	if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr))
-		goto unlock;
+		return -EAGAIN;
 
 	__migrate_swap_task(arg->src_task, arg->dst_cpu);
 	__migrate_swap_task(arg->dst_task, arg->src_cpu);
 
-	ret = 0;
-
-unlock:
-	double_rq_unlock(src_rq, dst_rq);
-	raw_spin_unlock(&arg->dst_task->pi_lock);
-	raw_spin_unlock(&arg->src_task->pi_lock);
-
-	return ret;
+	return 0;
 }
 
 /*
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2572,6 +2572,12 @@ static inline void double_rq_clock_clear
 static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2) {}
 #endif
 
+#define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...)		\
+__DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__) \
+static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2) \
+{ class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t;	\
+  _lock; return _t; }
+
 #ifdef CONFIG_SMP
 
 static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
@@ -2701,6 +2707,16 @@ static inline void double_raw_lock(raw_s
 	raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
 }
 
+static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
+{
+	raw_spin_unlock(l1);
+	raw_spin_unlock(l2);
+}
+
+DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
+		    double_raw_lock(_T->lock, _T->lock2),
+		    double_raw_unlock(_T->lock, _T->lock2))
+
 /*
  * double_rq_unlock - safely unlock two runqueues
  *
@@ -2758,6 +2774,10 @@ static inline void double_rq_unlock(stru
 
 #endif
 
+DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
+		    double_rq_lock(_T->lock, _T->lock2),
+		    double_rq_unlock(_T->lock, _T->lock2))
+
 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
 



  parent reply	other threads:[~2023-08-01 21:25 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-01 20:41 [PATCH 0/9] sched: Use lock guards, wave 1 Peter Zijlstra
2023-08-01 20:41 ` [PATCH 1/9] sched: Simplify get_nohz_timer_target() Peter Zijlstra
2023-08-06 21:39   ` Joel Fernandes
2023-08-09 19:48     ` Peter Zijlstra
2023-08-11  7:35       ` Joel Fernandes
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 2/9] sched: Simplify sysctl_sched_uclamp_handler() Peter Zijlstra
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` Peter Zijlstra [this message]
2023-08-14 15:08   ` [tip: sched/core] sched: Simplify: migrate_swap_stop() tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 4/9] sched: Simplify wake_up_if_idle() Peter Zijlstra
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 5/9] sched: Simplify ttwu() Peter Zijlstra
2023-08-09 15:21   ` Valentin Schneider
2023-08-09 19:26     ` Peter Zijlstra
2023-08-10  8:03       ` Valentin Schneider
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 6/9] sched: Simplify sched_exec() Peter Zijlstra
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 7/9] sched: Simplify sched_tick_remote() Peter Zijlstra
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 8/9] sched: Simplify try_steal_cookie() Peter Zijlstra
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-01 20:41 ` [PATCH 9/9] sched: Simplify sched_core_cpu_{starting,deactivate}() Peter Zijlstra
2023-08-14 15:08   ` [tip: sched/core] " tip-bot2 for Peter Zijlstra
2023-08-09 15:35 ` [PATCH 0/9] sched: Use lock guards, wave 1 Valentin Schneider

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230801211811.964370836@infradead.org \
    --to=peterz@infradead.org \
    --cc=bristot@redhat.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®