sched: Introduce struct balance_callback to avoid CFI mismatches

Introduce distinct struct balance_callback instead of performing function
pointer casting which will trip CFI. Avoids warnings as found by Clang's
future -Wcast-function-type-strict option:

In file included from kernel/sched/core.c:84:
kernel/sched/sched.h:1755:15: warning: cast from 'void (*)(struct rq *)' to 'void (*)(struct callback_head *)' converts to incompatible function type [-Wcast-function-type-strict]
head->func = (void (*)(struct callback_head *))func;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

No binary differences result from this change.

This patch is a cleanup based on Brad Spengler/PaX Team's modifications
to sched code in their last public patch of grsecurity/PaX based on my
understanding of the code. Changes or omissions from the original code
are mine and don't reflect the original grsecurity/PaX code.

Reported-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Link: https://github.com/ClangBuiltLinux/linux/issues/1724
Link: https://lkml.kernel.org/r/20221008000758.2957718-1-keescook@chromium.org

authored by Kees Cook and committed by Peter Zijlstra 8e5bad7d e705968d

Changed files
+26 -20
kernel
+12 -12
kernel/sched/core.c
··· 4823 4823 4824 4824 #ifdef CONFIG_SMP 4825 4825 4826 - static void do_balance_callbacks(struct rq *rq, struct callback_head *head) 4826 + static void do_balance_callbacks(struct rq *rq, struct balance_callback *head) 4827 4827 { 4828 4828 void (*func)(struct rq *rq); 4829 - struct callback_head *next; 4829 + struct balance_callback *next; 4830 4830 4831 4831 lockdep_assert_rq_held(rq); 4832 4832 ··· 4853 4853 * This abuse is tolerated because it places all the unlikely/odd cases behind 4854 4854 * a single test, namely: rq->balance_callback == NULL. 4855 4855 */ 4856 - struct callback_head balance_push_callback = { 4856 + struct balance_callback balance_push_callback = { 4857 4857 .next = NULL, 4858 - .func = (void (*)(struct callback_head *))balance_push, 4858 + .func = balance_push, 4859 4859 }; 4860 4860 4861 - static inline struct callback_head * 4861 + static inline struct balance_callback * 4862 4862 __splice_balance_callbacks(struct rq *rq, bool split) 4863 4863 { 4864 - struct callback_head *head = rq->balance_callback; 4864 + struct balance_callback *head = rq->balance_callback; 4865 4865 4866 4866 if (likely(!head)) 4867 4867 return NULL; ··· 4883 4883 return head; 4884 4884 } 4885 4885 4886 - static inline struct callback_head *splice_balance_callbacks(struct rq *rq) 4886 + static inline struct balance_callback *splice_balance_callbacks(struct rq *rq) 4887 4887 { 4888 4888 return __splice_balance_callbacks(rq, true); 4889 4889 } ··· 4893 4893 do_balance_callbacks(rq, __splice_balance_callbacks(rq, false)); 4894 4894 } 4895 4895 4896 - static inline void balance_callbacks(struct rq *rq, struct callback_head *head) 4896 + static inline void balance_callbacks(struct rq *rq, struct balance_callback *head) 4897 4897 { 4898 4898 unsigned long flags; 4899 4899 ··· 4910 4910 { 4911 4911 } 4912 4912 4913 - static inline struct callback_head *splice_balance_callbacks(struct rq *rq) 4913 + static inline struct balance_callback *splice_balance_callbacks(struct rq *rq) 4914 4914 { 4915 4915 return NULL; 4916 4916 } 4917 4917 4918 - static inline void balance_callbacks(struct rq *rq, struct callback_head *head) 4918 + static inline void balance_callbacks(struct rq *rq, struct balance_callback *head) 4919 4919 { 4920 4920 } 4921 4921 ··· 6188 6188 preempt_enable(); 6189 6189 } 6190 6190 6191 - static DEFINE_PER_CPU(struct callback_head, core_balance_head); 6191 + static DEFINE_PER_CPU(struct balance_callback, core_balance_head); 6192 6192 6193 6193 static void queue_core_balance(struct rq *rq) 6194 6194 { ··· 7419 7419 int oldpolicy = -1, policy = attr->sched_policy; 7420 7420 int retval, oldprio, newprio, queued, running; 7421 7421 const struct sched_class *prev_class; 7422 - struct callback_head *head; 7422 + struct balance_callback *head; 7423 7423 struct rq_flags rf; 7424 7424 int reset_on_fork; 7425 7425 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
+2 -2
kernel/sched/deadline.c
··· 644 644 return rq->online && dl_task(prev); 645 645 } 646 646 647 - static DEFINE_PER_CPU(struct callback_head, dl_push_head); 648 - static DEFINE_PER_CPU(struct callback_head, dl_pull_head); 647 + static DEFINE_PER_CPU(struct balance_callback, dl_push_head); 648 + static DEFINE_PER_CPU(struct balance_callback, dl_pull_head); 649 649 650 650 static void push_dl_tasks(struct rq *); 651 651 static void pull_dl_task(struct rq *);
+2 -2
kernel/sched/rt.c
··· 410 410 return !plist_head_empty(&rq->rt.pushable_tasks); 411 411 } 412 412 413 - static DEFINE_PER_CPU(struct callback_head, rt_push_head); 414 - static DEFINE_PER_CPU(struct callback_head, rt_pull_head); 413 + static DEFINE_PER_CPU(struct balance_callback, rt_push_head); 414 + static DEFINE_PER_CPU(struct balance_callback, rt_pull_head); 415 415 416 416 static void push_rt_tasks(struct rq *); 417 417 static void pull_rt_task(struct rq *);
+10 -4
kernel/sched/sched.h
··· 938 938 DECLARE_STATIC_KEY_FALSE(sched_uclamp_used); 939 939 #endif /* CONFIG_UCLAMP_TASK */ 940 940 941 + struct rq; 942 + struct balance_callback { 943 + struct balance_callback *next; 944 + void (*func)(struct rq *rq); 945 + }; 946 + 941 947 /* 942 948 * This is the main, per-CPU runqueue data structure. 943 949 * ··· 1042 1036 unsigned long cpu_capacity; 1043 1037 unsigned long cpu_capacity_orig; 1044 1038 1045 - struct callback_head *balance_callback; 1039 + struct balance_callback *balance_callback; 1046 1040 1047 1041 unsigned char nohz_idle_balance; 1048 1042 unsigned char idle_balance; ··· 1550 1544 #endif 1551 1545 }; 1552 1546 1553 - extern struct callback_head balance_push_callback; 1547 + extern struct balance_callback balance_push_callback; 1554 1548 1555 1549 /* 1556 1550 * Lockdep annotation that avoids accidental unlocks; it's like a ··· 1730 1724 1731 1725 static inline void 1732 1726 queue_balance_callback(struct rq *rq, 1733 - struct callback_head *head, 1727 + struct balance_callback *head, 1734 1728 void (*func)(struct rq *rq)) 1735 1729 { 1736 1730 lockdep_assert_rq_held(rq); ··· 1743 1737 if (unlikely(head->next || rq->balance_callback == &balance_push_callback)) 1744 1738 return; 1745 1739 1746 - head->func = (void (*)(struct callback_head *))func; 1740 + head->func = func; 1747 1741 head->next = rq->balance_callback; 1748 1742 rq->balance_callback = head; 1749 1743 }