From: William Lee Irwin III <wli@holomorphy.com>
To: linux-kernel@vger.kernel.org
Cc: Martin.Bligh@us.ibm.com, pdorwin@us.ibm.com, gone@us.ibm.com,
rml@tech9.net, mingo@elte.hu
Subject: [PATCH] migration_init() synchronization fixes
Date: Mon, 15 Apr 2002 17:03:23 -0700 [thread overview]
Message-ID: <20020416000323.GQ23767@holomorphy.com> (raw)
This patch has helped me and some others having migration_init() troubles.
The migration_mask's semantics are altered for use as a lock word, and
some of its other functionality is deferred to a new counter and struct
completion to provide protection against pathological cases encountered
in practice.
Cheers,
Bill
diff -Nru a/kernel/sched.c b/kernel/sched.c
--- a/kernel/sched.c Fri Apr 12 04:16:07 2002
+++ b/kernel/sched.c Fri Apr 12 04:16:07 2002
@@ -1669,7 +1669,16 @@
down(&req.sem);
}
+/*
+ * Treat the bits of migration_mask as lock bits.
+ * If the bit corresponding to the cpu a migration_thread is
+ * running on then we have failed to claim our cpu and must
+ * yield in order to find another.
+ */
static volatile unsigned long migration_mask;
+static atomic_t migration_threads_seeking_cpu;
+static struct completion migration_complete
+ = COMPLETION_INITIALIZER(migration_complete);
static int migration_thread(void * unused)
{
@@ -1693,26 +1702,54 @@
* task binds itself to the current CPU.
*/
- /* wait for all migration threads to start up. */
- while (!migration_mask)
- yield();
+ preempt_disable();
- for (;;) {
- preempt_disable();
- if (test_and_clear_bit(smp_processor_id(), &migration_mask))
- current->cpus_allowed = 1 << smp_processor_id();
- if (test_thread_flag(TIF_NEED_RESCHED))
- schedule();
- if (!migration_mask)
- break;
+ /*
+ * Enter the loop with preemption disabled so that
+ * smp_processor_id() remains valid through the check. The
+ * interior of the wait loop re-enables preemption in an
+ * attempt to get scheduled off the current cpu. When the
+ * loop is exited the lock bit in migration_mask is acquired
+ * and preemption is disabled on the way out. This way the
+ * cpu acquired remains valid when ->cpus_allowed is set.
+ */
+ while (test_and_set_bit(smp_processor_id(), &migration_mask)) {
preempt_enable();
+ yield();
+ preempt_disable();
}
+
+ current->cpus_allowed = 1 << smp_processor_id();
rq = this_rq();
rq->migration_thread = current;
+
+ /*
+ * Now that we've bound ourselves to a cpu, post to
+ * migration_threads_seeking_cpu and wait for everyone else.
+ * Preemption should remain disabled and the cpu should remain
+ * in busywait. Yielding the cpu will allow the livelock
+ * where where a timing pattern causes an idle task seeking a
+ * migration_thread to always find the unbound migration_thread
+ * running on the cpu's it tries to steal tasks from.
+ */
+ atomic_dec(&migration_threads_seeking_cpu);
+ while (atomic_read(&migration_threads_seeking_cpu))
+ cpu_relax();
+
preempt_enable();
sprintf(current->comm, "migration_CPU%d", smp_processor_id());
+ /*
+ * Everyone's found their cpu, so now wake migration_init().
+ * Multiple wakeups are harmless; removal from the waitqueue
+ * has locking built-in, and waking an empty queue is valid.
+ */
+ complete(&migration_complete);
+
+ /*
+ * Initiate the event loop.
+ */
for (;;) {
runqueue_t *rq_src, *rq_dest;
struct list_head *head;
@@ -1760,33 +1797,31 @@
void __init migration_init(void)
{
- unsigned long tmp, orig_cache_decay_ticks;
+ unsigned long orig_cache_decay_ticks;
int cpu;
- tmp = 0;
- for (cpu = 0; cpu < smp_num_cpus; cpu++) {
- if (kernel_thread(migration_thread, NULL,
- CLONE_FS | CLONE_FILES | CLONE_SIGNAL) < 0)
- BUG();
- tmp |= (1UL << cpu_logical_map(cpu));
- }
+ atomic_set(&migration_threads_seeking_cpu, smp_num_cpus);
- migration_mask = tmp;
+ preempt_disable();
orig_cache_decay_ticks = cache_decay_ticks;
cache_decay_ticks = 0;
- for (cpu = 0; cpu < smp_num_cpus; cpu++) {
- int logical = cpu_logical_map(cpu);
+ for (cpu = 0; cpu < smp_num_cpus; cpu++)
+ if (kernel_thread(migration_thread, NULL,
+ CLONE_FS | CLONE_FILES | CLONE_SIGNAL) < 0)
+ BUG();
- while (!cpu_rq(logical)->migration_thread) {
- set_current_state(TASK_INTERRUPTIBLE);
- schedule_timeout(2);
- }
- }
- if (migration_mask)
- BUG();
+ /*
+ * We cannot have missed the wakeup for the migration_thread
+ * bound for the cpu migration_init() is running on cannot
+ * acquire this cpu until migration_init() has yielded it by
+ * means of wait_for_completion().
+ */
+ wait_for_completion(&migration_complete);
cache_decay_ticks = orig_cache_decay_ticks;
+
+ preempt_enable();
}
#endif
next reply other threads:[~2002-04-16 0:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-04-16 0:03 William Lee Irwin III [this message]
2002-04-18 23:15 ` [PATCH] migration_init() synchronization fixes Patricia Gaughen
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=20020416000323.GQ23767@holomorphy.com \
--to=wli@holomorphy.com \
--cc=Martin.Bligh@us.ibm.com \
--cc=gone@us.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=pdorwin@us.ibm.com \
--cc=rml@tech9.net \
/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