linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Joel Fernandes (Google)" <joel@joelfernandes.org>
To: linux-kernel@vger.kernel.org
Cc: "Joel Fernandes (Google)" <joel@joelfernandes.org>,
	paulmck@kernel.org, Aaron Lu <aaron.lwe@gmail.com>,
	Aubrey Li <aubrey.li@linux.intel.com>,
	Julien Desfossez <jdesfossez@digitalocean.com>,
	Kees Cook <keescook@chromium.org>, Paul Turner <pjt@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Tim Chen <tim.c.chen@intel.com>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Vineeth Pillai <viremana@linux.microsoft.com>,
	x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)),
	fweisbec@gmail.com, kerrnel@google.com,
	Phil Auld <pauld@redhat.com>,
	Valentin Schneider <valentin.schneider@arm.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Chen Yu <yu.c.chen@intel.com>,
	Christian Brauner <christian.brauner@ubuntu.com>
Subject: [PATCH RFC 01/12] irq_work: Add support to detect if work is pending
Date: Fri, 14 Aug 2020 23:18:57 -0400	[thread overview]
Message-ID: <20200815031908.1015049-2-joel@joelfernandes.org> (raw)
In-Reply-To: <20200815031908.1015049-1-joel@joelfernandes.org>

When an unsafe region is entered on an HT, an IPI needs to be sent to
siblings to ensure they enter the kernel.

Following are the reasons why we would like to use irq_work to implement
forcing of sibling into kernel mode:

1. Existing smp_call infrastructure cannot be used easily since we could
   end up waiting on CSD lock if previously an smp_call was not yet
   serviced.

2. I'd like to use generic code, such that there is no need to add an
   arch-specific IPI.

3. IRQ work already has support to detect that previous work was not yet
   executed through the IRQ_WORK_PENDING bit.

4. We need to know if the destination of the IPI is not sending more
   IPIs due to that IPI itself causing an entry into unsafe region.

Support for 4. requires us to be able to detect that irq_work is
pending.

This commit therefore adds a way for irq_work users to know if a
previous per-HT irq_work is pending. If it is, we need not send new
IPIs.

Memory ordering:

I was trying to handle the MP-pattern below. Consider the flag to be the
pending bit. P0() is the IRQ work handler. P1() is the code calling
irq_work_pending(). P0() already implicitly adds a memory barrier as a
part of the atomic_fetch_andnot() before calling work->func(). For P1(),
this patch adds the memory barrier as the atomic_read() in this patch's
irq_work_pending() is not sufficient.

        P0()
        {
                WRITE_ONCE(buf, 1);
                WRITE_ONCE(flag, 1);
        }

        P1()
        {
                int r1;
                int r2 = 0;

                r1 = READ_ONCE(flag);
                if (r1)
                        r2 = READ_ONCE(buf);
        }

Cc: paulmck@kernel.org
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 include/linux/irq_work.h |  1 +
 kernel/irq_work.c        | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/include/linux/irq_work.h b/include/linux/irq_work.h
index 30823780c192..b26466f95d04 100644
--- a/include/linux/irq_work.h
+++ b/include/linux/irq_work.h
@@ -42,6 +42,7 @@ bool irq_work_queue_on(struct irq_work *work, int cpu);
 
 void irq_work_tick(void);
 void irq_work_sync(struct irq_work *work);
+bool irq_work_pending(struct irq_work *work);
 
 #ifdef CONFIG_IRQ_WORK
 #include <asm/irq_work.h>
diff --git a/kernel/irq_work.c b/kernel/irq_work.c
index eca83965b631..2d206d511aa0 100644
--- a/kernel/irq_work.c
+++ b/kernel/irq_work.c
@@ -24,6 +24,17 @@
 static DEFINE_PER_CPU(struct llist_head, raised_list);
 static DEFINE_PER_CPU(struct llist_head, lazy_list);
 
+bool irq_work_pending(struct irq_work *work)
+{
+	/*
+	 * Provide ordering to callers who may read other stuff
+	 * after the atomic read (MP-pattern).
+	 */
+	bool ret = atomic_read_acquire(&work->flags) & IRQ_WORK_PENDING;
+
+	return ret;
+}
+
 /*
  * Claim the entry so that no one else will poke at it.
  */
-- 
2.28.0.220.ged08abb693-goog


  reply	other threads:[~2020-08-15 21:57 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-15  3:18 [PATCH RFC 00/12] Core-sched v6+: kernel protection and hotplug fixes Joel Fernandes (Google)
2020-08-15  3:18 ` Joel Fernandes (Google) [this message]
2020-08-15  8:13   ` [PATCH RFC 01/12] irq_work: Add support to detect if work is pending peterz
2020-08-17  2:04     ` Joel Fernandes
2020-08-15  3:18 ` [PATCH RFC 02/12] entry/idle: Add a common function for activites during idle entry/exit Joel Fernandes (Google)
2020-08-15  8:14   ` peterz
2020-08-17  2:17     ` Joel Fernandes
2020-08-15  3:18 ` [PATCH RFC 03/12] arch/x86: Add a new TIF flag for untrusted tasks Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 04/12] kernel/entry: Add support for core-wide protection of kernel-mode Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 05/12] entry/idle: Enter and exit kernel protection during idle entry and exit Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 06/12] entry/kvm: Protect the kernel when entering from guest Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 07/12] bitops: Introduce find_next_or_bit Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 08/12] cpumask: Introduce a new iterator for_each_cpu_wrap_or Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 09/12] sched/coresched: Use for_each_cpu(_wrap)_or for pick_next_task Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 10/12] sched/coresched: Make core_pick_seq per run-queue Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 11/12] sched/coresched: Check for dynamic changes in smt_mask Joel Fernandes (Google)
2020-08-15  3:19 ` [PATCH RFC 12/12] sched/coresched: rq->core should be set only if not previously set Joel Fernandes (Google)
2020-08-19 18:26 ` [PATCH RFC 00/12] Core-sched v6+: kernel protection and hotplug fixes Kees Cook
2020-08-20  1:44   ` Joel Fernandes
2020-08-22 20:22 ` Joel Fernandes

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=20200815031908.1015049-2-joel@joelfernandes.org \
    --to=joel@joelfernandes.org \
    --cc=aaron.lwe@gmail.com \
    --cc=aubrey.li@linux.intel.com \
    --cc=christian.brauner@ubuntu.com \
    --cc=fweisbec@gmail.com \
    --cc=jdesfossez@digitalocean.com \
    --cc=keescook@chromium.org \
    --cc=kerrnel@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pauld@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pjt@google.com \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@intel.com \
    --cc=tim.c.chen@linux.intel.com \
    --cc=valentin.schneider@arm.com \
    --cc=vincent.guittot@linaro.org \
    --cc=viremana@linux.microsoft.com \
    --cc=x86@kernel.org \
    --cc=yu.c.chen@intel.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;
as well as URLs for NNTP newsgroup(s).