From: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
To: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Cc: kosaki.motohiro@jp.fujitsu.com, linux-kernel@vger.kernel.org,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Oleg Nesterov <oleg@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@elte.hu>,
akpm@linux-foundation.org, josh@joshtriplett.org,
tglx@linutronix.de, Valdis.Kletnieks@vt.edu, dhowells@redhat.com,
laijs@cn.fujitsu.com, dipankar@in.ibm.com
Subject: Re: [RFC PATCH] introduce sys_membarrier(): process-wide memory barrier (v5)
Date: Wed, 13 Jan 2010 12:23:43 +0900 (JST) [thread overview]
Message-ID: <20100113110455.B3D3.A69D9226@jp.fujitsu.com> (raw)
In-Reply-To: <20100113013757.GA29314@Krystal>
Hi
Interesting patch :)
I have few comments.
> Index: linux-2.6-lttng/kernel/sched.c
> ===================================================================
> --- linux-2.6-lttng.orig/kernel/sched.c 2010-01-12 10:25:47.000000000 -0500
> +++ linux-2.6-lttng/kernel/sched.c 2010-01-12 14:33:20.000000000 -0500
> @@ -10822,6 +10822,117 @@ struct cgroup_subsys cpuacct_subsys = {
> };
> #endif /* CONFIG_CGROUP_CPUACCT */
>
> +#ifdef CONFIG_SMP
> +
> +/*
> + * Execute a memory barrier on all active threads from the current process
> + * on SMP systems. Do not rely on implicit barriers in IPI handler execution,
> + * because batched IPI lists are synchronized with spinlocks rather than full
> + * memory barriers. This is not the bulk of the overhead anyway, so let's stay
> + * on the safe side.
> + */
> +static void membarrier_ipi(void *unused)
> +{
> + smp_mb();
> +}
> +
> +/*
> + * Handle out-of-mem by sending per-cpu IPIs instead.
> + */
> +static void membarrier_retry(void)
> +{
> + struct mm_struct *mm;
> + int cpu;
> +
> + for_each_cpu(cpu, mm_cpumask(current->mm)) {
> + spin_lock_irq(&cpu_rq(cpu)->lock);
> + mm = cpu_curr(cpu)->mm;
> + spin_unlock_irq(&cpu_rq(cpu)->lock);
> + if (current->mm == mm)
> + smp_call_function_single(cpu, membarrier_ipi, NULL, 1);
> + }
> +}
> +
> +#endif /* #ifdef CONFIG_SMP */
> +
> +/*
> + * sys_membarrier - issue memory barrier on current process running threads
> + * @expedited: (0) Lowest overhead. Few milliseconds latency.
> + * (1) Few microseconds latency.
Why do we need both expedited and non-expedited mode? at least, this documentation
is bad. it suggest "you have to use non-expedited mode always!".
> + *
> + * Execute a memory barrier on all running threads of the current process.
> + * Upon completion, the caller thread is ensured that all process threads
> + * have passed through a state where memory accesses match program order.
> + * (non-running threads are de facto in such a state)
> + *
> + * mm_cpumask is used as an approximation. It is a superset of the cpumask to
> + * which we must send IPIs, mainly due to lazy TLB shootdown. Therefore,
> + * we check each runqueue to make sure our ->mm is indeed running on them. This
> + * reduces the risk of disturbing a RT task by sending unnecessary IPIs. There
> + * is still a slight chance to disturb an unrelated task, because we do not lock
> + * the runqueues while sending IPIs, but the real-time effect of this heavy
> + * locking would be worse than the comparatively small disruption of an IPI.
> + *
> + * RED PEN: before assinging a system call number for sys_membarrier() to an
> + * architecture, we must ensure that switch_mm issues full memory barriers (or a
> + * synchronizing instruction having the same effect) between:
> + * - user-space code execution and clear mm_cpumask.
> + * - set mm_cpumask and user-space code execution.
> + * In some case adding a comment to this effect will suffice, in others we will
> + * need to add smp_mb__before_clear_bit()/smp_mb__after_clear_bit() or simply
> + * smp_mb(). These barriers are required to ensure we do not _miss_ a CPU that
> + * need to receive an IPI, which would be a bug.
> + *
> + * On uniprocessor systems, this system call simply returns 0 without doing
> + * anything, so user-space knows it is implemented.
> + */
> +SYSCALL_DEFINE1(membarrier, int, expedited)
> +{
> +#ifdef CONFIG_SMP
> + cpumask_var_t tmpmask;
> + struct mm_struct *mm;
> + int cpu;
> +
> + if (unlikely(thread_group_empty(current) || (num_online_cpus() == 1)))
> + return 0;
> + if (!unlikely(expedited)) {
unlikely(!expedited)?
> + synchronize_sched();
> + return 0;
> + }
> + /*
> + * Memory barrier on the caller thread _before_ sending first
> + * IPI. Matches memory barriers around mm_cpumask modification in
> + * switch_mm().
> + */
> + smp_mb();
> + if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) {
> + membarrier_retry();
> + goto unlock;
> + }
if CONFIG_CPUMASK_OFFSTACK=1, alloc_cpumask_var call kmalloc. FWIW,
kmalloc calling seems destory the worth of this patch.
#ifdef CONFIG_CPUMASK_OFFSTACK
membarrier_retry();
goto unlock;
#endif
is better? I'm not sure.
> + cpumask_copy(tmpmask, mm_cpumask(current->mm));
> + preempt_disable();
> + cpumask_clear_cpu(smp_processor_id(), tmpmask);
> + for_each_cpu(cpu, tmpmask) {
> + spin_lock_irq(&cpu_rq(cpu)->lock);
> + mm = cpu_curr(cpu)->mm;
> + spin_unlock_irq(&cpu_rq(cpu)->lock);
> + if (current->mm != mm)
> + cpumask_clear_cpu(cpu, tmpmask);
> + }
> + smp_call_function_many(tmpmask, membarrier_ipi, NULL, 1);
> + preempt_enable();
> + free_cpumask_var(tmpmask);
> +unlock:
> + /*
> + * Memory barrier on the caller thread _after_ we finished
> + * waiting for the last IPI. Matches memory barriers around mm_cpumask
> + * modification in switch_mm().
> + */
> + smp_mb();
> +#endif /* #ifdef CONFIG_SMP */
> + return 0;
> +}
next prev parent reply other threads:[~2010-01-13 3:23 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-13 1:37 Mathieu Desnoyers
2010-01-13 3:23 ` KOSAKI Motohiro [this message]
2010-01-13 3:58 ` Mathieu Desnoyers
2010-01-13 4:47 ` KOSAKI Motohiro
2010-01-13 5:33 ` Paul E. McKenney
2010-01-13 15:03 ` Mathieu Desnoyers
2010-01-14 0:15 ` KOSAKI Motohiro
2010-01-14 2:16 ` Mathieu Desnoyers
2010-01-14 2:25 ` KOSAKI Motohiro
2010-01-13 5:00 ` Nicholas Miell
2010-01-13 5:31 ` Paul E. McKenney
2010-01-13 5:39 ` Nicholas Miell
2010-01-13 14:38 ` Mathieu Desnoyers
2010-01-13 18:07 ` Nicholas Miell
2010-01-13 18:24 ` Mathieu Desnoyers
2010-01-13 18:41 ` Nicholas Miell
2010-01-13 19:17 ` Mathieu Desnoyers
2010-01-13 19:42 ` David Daney
2010-01-13 19:53 ` Nicholas Miell
2010-01-13 23:42 ` Mathieu Desnoyers
2010-01-13 15:58 ` Paul E. McKenney
2010-01-13 11:07 ` Heiko Carstens
2010-01-13 14:46 ` Mathieu Desnoyers
2010-01-13 16:38 ` Peter Zijlstra
2010-01-13 19:36 ` Mathieu Desnoyers
2010-01-14 9:08 ` Peter Zijlstra
2010-01-14 16:26 ` Mathieu Desnoyers
2010-01-14 17:03 ` Peter Zijlstra
2010-01-14 17:54 ` Mathieu Desnoyers
2010-01-14 18:37 ` Mathieu Desnoyers
2010-01-14 18:52 ` Steven Rostedt
2010-01-14 19:33 ` Mathieu Desnoyers
2010-01-14 21:26 ` Steven Rostedt
2010-01-19 18:37 ` Peter Zijlstra
2010-01-19 19:06 ` Peter Zijlstra
2010-01-20 3:13 ` Mathieu Desnoyers
2010-01-20 8:45 ` Peter Zijlstra
2010-01-21 11:26 ` Peter Zijlstra
2010-01-21 16:07 ` Mathieu Desnoyers
2010-01-21 16:12 ` Steven Rostedt
2010-01-21 16:22 ` Mathieu Desnoyers
2010-01-21 16:32 ` Steven Rostedt
2010-01-21 17:02 ` Mathieu Desnoyers
2010-01-21 16:17 ` Peter Zijlstra
2010-01-21 17:01 ` Mathieu Desnoyers
2010-01-19 19:43 ` Steven Rostedt
2010-01-14 18:50 ` Steven Rostedt
2010-01-19 16:47 ` Peter Zijlstra
2010-01-19 17:11 ` Mathieu Desnoyers
2010-01-19 17:30 ` Steven Rostedt
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=20100113110455.B3D3.A69D9226@jp.fujitsu.com \
--to=kosaki.motohiro@jp.fujitsu.com \
--cc=Valdis.Kletnieks@vt.edu \
--cc=akpm@linux-foundation.org \
--cc=dhowells@redhat.com \
--cc=dipankar@in.ibm.com \
--cc=josh@joshtriplett.org \
--cc=laijs@cn.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@polymtl.ca \
--cc=mingo@elte.hu \
--cc=oleg@redhat.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
/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®