From: Pete Swain <swine@google.com>
To: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org
Cc: "H. Peter Anvin" <hpa@zytor.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
John Stultz <john.stultz@linaro.org>,
Stephen Boyd <sboyd@kernel.org>,
"Maciej W. Rozycki" <macro@orcam.me.uk>,
Johan Hovold <johan@kernel.org>, Feng Tang <feng.tang@intel.com>,
"Paul E. McKenney" <paulmck@kernel.org>,
Juergen Gross <jgross@suse.com>,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
Pete Swain <swine@google.com>
Subject: [PATCH 2/2] timers: retpoline mitigation for time funcs
Date: Fri, 18 Feb 2022 14:18:20 -0800 [thread overview]
Message-ID: <20220218221820.950118-2-swine@google.com> (raw)
In-Reply-To: <20220218221820.950118-1-swine@google.com>
Adds indirect call exports for clock reads from tsc, apic,
hrtimer, via clock-event, timekeeper & posix interfaces.
Signed-off-by: Pete Swain <swine@google.com>
---
arch/x86/kernel/apic/apic.c | 8 +++++---
arch/x86/kernel/tsc.c | 3 ++-
include/linux/hrtimer.h | 19 ++++++++++++++++---
kernel/time/clockevents.c | 9 ++++++---
kernel/time/hrtimer.c | 3 ++-
kernel/time/posix-cpu-timers.c | 4 ++--
kernel/time/posix-timers.c | 3 ++-
kernel/time/timekeeping.c | 2 +-
8 files changed, 36 insertions(+), 15 deletions(-)
diff --git a/arch/x86/kernel/apic/apic.c b/arch/x86/kernel/apic/apic.c
index b70344bf6600..523a569dd35e 100644
--- a/arch/x86/kernel/apic/apic.c
+++ b/arch/x86/kernel/apic/apic.c
@@ -463,15 +463,17 @@ EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
/*
* Program the next event, relative to now
*/
-static int lapic_next_event(unsigned long delta,
+INDIRECT_CALLABLE_SCOPE
+int lapic_next_event(unsigned long delta,
struct clock_event_device *evt)
{
apic_write(APIC_TMICT, delta);
return 0;
}
-static int lapic_next_deadline(unsigned long delta,
- struct clock_event_device *evt)
+INDIRECT_CALLABLE_SCOPE
+int lapic_next_deadline(unsigned long delta,
+ struct clock_event_device *evt)
{
u64 tsc;
diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index a698196377be..ff2868d5ddea 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -1090,7 +1090,8 @@ static void tsc_resume(struct clocksource *cs)
* checking the result of read_tsc() - cycle_last for being negative.
* That works because CLOCKSOURCE_MASK(64) does not mask out any bit.
*/
-static u64 read_tsc(struct clocksource *cs)
+INDIRECT_CALLABLE_SCOPE
+u64 read_tsc(struct clocksource *cs)
{
return (u64)rdtsc_ordered();
}
diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index 0ee140176f10..9d2d110f0b8c 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -20,6 +20,7 @@
#include <linux/seqlock.h>
#include <linux/timer.h>
#include <linux/timerqueue.h>
+#include <linux/indirect_call_wrapper.h>
struct hrtimer_clock_base;
struct hrtimer_cpu_base;
@@ -297,14 +298,17 @@ static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
return ktime_to_ns(timer->node.expires);
}
+INDIRECT_CALLABLE_DECLARE(extern ktime_t ktime_get(void));
+
static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
{
- return ktime_sub(timer->node.expires, timer->base->get_time());
+ return ktime_sub(timer->node.expires,
+ INDIRECT_CALL_1(timer->base->get_time, ktime_get));
}
static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
{
- return timer->base->get_time();
+ return INDIRECT_CALL_1(timer->base->get_time, ktime_get);
}
static inline int hrtimer_is_hres_active(struct hrtimer *timer)
@@ -503,7 +507,9 @@ hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
static inline u64 hrtimer_forward_now(struct hrtimer *timer,
ktime_t interval)
{
- return hrtimer_forward(timer, timer->base->get_time(), interval);
+ return hrtimer_forward(timer,
+ INDIRECT_CALL_1(timer->base->get_time, ktime_get),
+ interval);
}
/* Precise sleep: */
@@ -536,4 +542,11 @@ int hrtimers_dead_cpu(unsigned int cpu);
#define hrtimers_dead_cpu NULL
#endif
+struct clock_event_device;
+INDIRECT_CALLABLE_DECLARE(extern __weak u64 read_tsc(struct clocksource *cs));
+INDIRECT_CALLABLE_DECLARE(extern int thread_cpu_clock_get(
+ const clockid_t which_clock, struct timespec64 *tp));
+INDIRECT_CALLABLE_DECLARE(extern __weak int lapic_next_deadline(
+ unsigned long delta, struct clock_event_device *evt));
+
#endif
diff --git a/kernel/time/clockevents.c b/kernel/time/clockevents.c
index 003ccf338d20..ac15412e87c4 100644
--- a/kernel/time/clockevents.c
+++ b/kernel/time/clockevents.c
@@ -245,7 +245,8 @@ static int clockevents_program_min_delta(struct clock_event_device *dev)
dev->retries++;
clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
- if (dev->set_next_event((unsigned long) clc, dev) == 0)
+ if (INDIRECT_CALL_1(dev->set_next_event, lapic_next_deadline,
+ (unsigned long) clc, dev) == 0)
return 0;
if (++i > 2) {
@@ -284,7 +285,8 @@ static int clockevents_program_min_delta(struct clock_event_device *dev)
dev->retries++;
clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
- if (dev->set_next_event((unsigned long) clc, dev) == 0)
+ if (INDIRECT_CALL_1(dev->set_next_event, lapic_next_deadline,
+ (unsigned long) clc, dev) == 0)
return 0;
}
return -ETIME;
@@ -331,7 +333,8 @@ int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
delta = max(delta, (int64_t) dev->min_delta_ns);
clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
- rc = dev->set_next_event((unsigned long) clc, dev);
+ rc = INDIRECT_CALL_1(dev->set_next_event, lapic_next_deadline,
+ (unsigned long) clc, dev);
return (rc && force) ? clockevents_program_min_delta(dev) : rc;
}
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 0ea8702eb516..e1e17fdfcd18 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1241,7 +1241,8 @@ static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
remove_hrtimer(timer, base, true, force_local);
if (mode & HRTIMER_MODE_REL)
- tim = ktime_add_safe(tim, base->get_time());
+ tim = ktime_add_safe(tim,
+ INDIRECT_CALL_1(base->get_time, ktime_get));
tim = hrtimer_update_lowres(timer, tim, mode);
diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index 96b4e7810426..d8bf325fa84e 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -1596,8 +1596,8 @@ static int thread_cpu_clock_getres(const clockid_t which_clock,
{
return posix_cpu_clock_getres(THREAD_CLOCK, tp);
}
-static int thread_cpu_clock_get(const clockid_t which_clock,
- struct timespec64 *tp)
+INDIRECT_CALLABLE_SCOPE
+int thread_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp)
{
return posix_cpu_clock_get(THREAD_CLOCK, tp);
}
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 1cd10b102c51..35eac10ee796 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -1089,7 +1089,8 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
if (!kc)
return -EINVAL;
- error = kc->clock_get_timespec(which_clock, &kernel_tp);
+ error = INDIRECT_CALL_1(kc->clock_get_timespec, thread_cpu_clock_get,
+ which_clock, &kernel_tp);
if (!error && put_timespec64(&kernel_tp, tp))
error = -EFAULT;
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index dcdcb85121e4..2b1a3b146614 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -190,7 +190,7 @@ static inline u64 tk_clock_read(const struct tk_read_base *tkr)
{
struct clocksource *clock = READ_ONCE(tkr->clock);
- return clock->read(clock);
+ return INDIRECT_CALL_1(clock->read, read_tsc, clock);
}
#ifdef CONFIG_DEBUG_TIMEKEEPING
--
2.35.1.473.g83b2b277ed-goog
next prev parent reply other threads:[~2022-02-18 22:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-18 22:18 [PATCH 1/2] kvm/x86: rename kvm's read_tsc() as kvm_read_host_tsc() Pete Swain
2022-02-18 22:18 ` Pete Swain [this message]
2022-04-10 12:14 ` [PATCH 2/2] timers: retpoline mitigation for time funcs Thomas Gleixner
2022-04-11 9:21 ` Thomas Gleixner
2022-04-11 11:07 ` Thomas Gleixner
2022-02-18 22:49 ` [PATCH 1/2] kvm/x86: rename kvm's read_tsc() as kvm_read_host_tsc() Jim Mattson
2022-02-19 8:40 ` Paolo Bonzini
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=20220218221820.950118-2-swine@google.com \
--to=swine@google.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=feng.tang@intel.com \
--cc=hpa@zytor.com \
--cc=jgross@suse.com \
--cc=jmattson@google.com \
--cc=johan@kernel.org \
--cc=john.stultz@linaro.org \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=macro@orcam.me.uk \
--cc=mingo@redhat.com \
--cc=paulmck@kernel.org \
--cc=pbonzini@redhat.com \
--cc=sboyd@kernel.org \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=x86@kernel.org \
/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