Skip to content

Commit b965f1d

Browse files
Peter Zijlstra (Intel)ingomolnar
authored andcommitted
preempt/dynamic: Provide cond_resched() and might_resched() static calls
Provide static calls to control cond_resched() (called in !CONFIG_PREEMPT) and might_resched() (called in CONFIG_PREEMPT_VOLUNTARY) to that we can override their behaviour when preempt= is overriden. Since the default behaviour is full preemption, both their calls are ignored when preempt= isn't passed. [fweisbec: branch might_resched() directly to __cond_resched(), only define static calls when PREEMPT_DYNAMIC] Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Frederic Weisbecker <frederic@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lkml.kernel.org/r/20210118141223.123667-6-frederic@kernel.org
1 parent 6ef869e commit b965f1d

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎include/linux/kernel.h‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <linux/typecheck.h>
1616
#include <linux/printk.h>
1717
#include <linux/build_bug.h>
18-
18+
#include <linux/static_call_types.h>
1919
#include <asm/byteorder.h>
2020

2121
#include <uapi/linux/kernel.h>
@@ -81,11 +81,26 @@ struct pt_regs;
8181
struct user;
8282

8383
#ifdef CONFIG_PREEMPT_VOLUNTARY
84-
extern int _cond_resched(void);
85-
# define might_resched() _cond_resched()
84+
85+
extern int __cond_resched(void);
86+
# define might_resched() __cond_resched()
87+
88+
#elif defined(CONFIG_PREEMPT_DYNAMIC)
89+
90+
extern int __cond_resched(void);
91+
92+
DECLARE_STATIC_CALL(might_resched, __cond_resched);
93+
94+
static __always_inline void might_resched(void)
95+
{
96+
static_call(might_resched)();
97+
}
98+
8699
#else
100+
87101
# define might_resched() do { } while (0)
88-
#endif
102+
103+
#endif /* CONFIG_PREEMPT_* */
89104

90105
#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
91106
extern void ___might_sleep(const char *file, int line, int preempt_offset);

‎include/linux/sched.h‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,11 +1871,32 @@ static inline int test_tsk_need_resched(struct task_struct *tsk)
18711871
* value indicates whether a reschedule was done in fact.
18721872
* cond_resched_lock() will drop the spinlock before scheduling,
18731873
*/
1874-
#ifndef CONFIG_PREEMPTION
1875-
extern int _cond_resched(void);
1874+
#if !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC)
1875+
extern int __cond_resched(void);
1876+
1877+
#ifdef CONFIG_PREEMPT_DYNAMIC
1878+
1879+
DECLARE_STATIC_CALL(cond_resched, __cond_resched);
1880+
1881+
static __always_inline int _cond_resched(void)
1882+
{
1883+
return static_call(cond_resched)();
1884+
}
1885+
18761886
#else
1887+
1888+
static inline int _cond_resched(void)
1889+
{
1890+
return __cond_resched();
1891+
}
1892+
1893+
#endif /* CONFIG_PREEMPT_DYNAMIC */
1894+
1895+
#else
1896+
18771897
static inline int _cond_resched(void) { return 0; }
1878-
#endif
1898+
1899+
#endif /* !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC) */
18791900

18801901
#define cond_resched() ({ \
18811902
___might_sleep(__FILE__, __LINE__, 0); \

‎kernel/sched/core.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6785,17 +6785,27 @@ SYSCALL_DEFINE0(sched_yield)
67856785
return 0;
67866786
}
67876787

6788-
#ifndef CONFIG_PREEMPTION
6789-
int __sched _cond_resched(void)
6788+
#if !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC)
6789+
int __sched __cond_resched(void)
67906790
{
67916791
if (should_resched(0)) {
67926792
preempt_schedule_common();
67936793
return 1;
67946794
}
6795+
#ifndef CONFIG_PREEMPT_RCU
67956796
rcu_all_qs();
6797+
#endif
67966798
return 0;
67976799
}
6798-
EXPORT_SYMBOL(_cond_resched);
6800+
EXPORT_SYMBOL(__cond_resched);
6801+
#endif
6802+
6803+
#ifdef CONFIG_PREEMPT_DYNAMIC
6804+
DEFINE_STATIC_CALL_RET0(cond_resched, __cond_resched);
6805+
EXPORT_STATIC_CALL(cond_resched);
6806+
6807+
DEFINE_STATIC_CALL_RET0(might_resched, __cond_resched);
6808+
EXPORT_STATIC_CALL(might_resched);
67996809
#endif
68006810

68016811
/*

0 commit comments

Comments
 (0)