Skip to content

Commit 8c4890d

Browse files
Peter Zijlstrasuryasaimadhu
authored andcommitted
smp, irq_work: Continue smp_call_function*() and irq_work*() integration
Instead of relying on BUG_ON() to ensure the various data structures line up, use a bunch of horrible unions to make it all automatic. Much of the union magic is to ensure irq_work and smp_call_function do not (yet) see the members of their respective data structures change name. Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Frederic Weisbecker <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 739f70b commit 8c4890d

File tree

6 files changed

+86
-58
lines changed

6 files changed

+86
-58
lines changed

include/linux/irq_work.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef _LINUX_IRQ_WORK_H
33
#define _LINUX_IRQ_WORK_H
44

5-
#include <linux/llist.h>
5+
#include <linux/smp_types.h>
66

77
/*
88
* An entry can be in one of four states:
@@ -13,24 +13,14 @@
1313
* busy NULL, 2 -> {free, claimed} : callback in progress, can be claimed
1414
*/
1515

16-
/* flags share CSD_FLAG_ space */
17-
18-
#define IRQ_WORK_PENDING BIT(0)
19-
#define IRQ_WORK_BUSY BIT(1)
20-
21-
/* Doesn't want IPI, wait for tick: */
22-
#define IRQ_WORK_LAZY BIT(2)
23-
/* Run hard IRQ context, even on RT */
24-
#define IRQ_WORK_HARD_IRQ BIT(3)
25-
26-
#define IRQ_WORK_CLAIMED (IRQ_WORK_PENDING | IRQ_WORK_BUSY)
27-
28-
/*
29-
* structure shares layout with single_call_data_t.
30-
*/
3116
struct irq_work {
32-
struct llist_node llnode;
33-
atomic_t flags;
17+
union {
18+
struct __call_single_node node;
19+
struct {
20+
struct llist_node llnode;
21+
atomic_t flags;
22+
};
23+
};
3424
void (*func)(struct irq_work *);
3525
};
3626

include/linux/sched.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,8 @@ struct task_struct {
654654
unsigned int ptrace;
655655

656656
#ifdef CONFIG_SMP
657-
struct {
658-
struct llist_node wake_entry;
659-
unsigned int wake_entry_type;
660-
};
661657
int on_cpu;
658+
struct __call_single_node wake_entry;
662659
#ifdef CONFIG_THREAD_INFO_IN_TASK
663660
/* Current CPU: */
664661
unsigned int cpu;

include/linux/smp.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,22 @@
1212
#include <linux/list.h>
1313
#include <linux/cpumask.h>
1414
#include <linux/init.h>
15-
#include <linux/llist.h>
15+
#include <linux/smp_types.h>
1616

1717
typedef void (*smp_call_func_t)(void *info);
1818
typedef bool (*smp_cond_func_t)(int cpu, void *info);
1919

20-
enum {
21-
CSD_FLAG_LOCK = 0x01,
22-
23-
/* IRQ_WORK_flags */
24-
25-
CSD_TYPE_ASYNC = 0x00,
26-
CSD_TYPE_SYNC = 0x10,
27-
CSD_TYPE_IRQ_WORK = 0x20,
28-
CSD_TYPE_TTWU = 0x30,
29-
CSD_FLAG_TYPE_MASK = 0xF0,
30-
};
31-
3220
/*
3321
* structure shares (partial) layout with struct irq_work
3422
*/
3523
struct __call_single_data {
36-
struct llist_node llist;
37-
unsigned int flags;
24+
union {
25+
struct __call_single_node node;
26+
struct {
27+
struct llist_node llist;
28+
unsigned int flags;
29+
};
30+
};
3831
smp_call_func_t func;
3932
void *info;
4033
};

include/linux/smp_types.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __LINUX_SMP_TYPES_H
3+
#define __LINUX_SMP_TYPES_H
4+
5+
#include <linux/llist.h>
6+
7+
enum {
8+
CSD_FLAG_LOCK = 0x01,
9+
10+
IRQ_WORK_PENDING = 0x01,
11+
IRQ_WORK_BUSY = 0x02,
12+
IRQ_WORK_LAZY = 0x04, /* No IPI, wait for tick */
13+
IRQ_WORK_HARD_IRQ = 0x08, /* IRQ context on PREEMPT_RT */
14+
15+
IRQ_WORK_CLAIMED = (IRQ_WORK_PENDING | IRQ_WORK_BUSY),
16+
17+
CSD_TYPE_ASYNC = 0x00,
18+
CSD_TYPE_SYNC = 0x10,
19+
CSD_TYPE_IRQ_WORK = 0x20,
20+
CSD_TYPE_TTWU = 0x30,
21+
22+
CSD_FLAG_TYPE_MASK = 0xF0,
23+
};
24+
25+
/*
26+
* struct __call_single_node is the primary type on
27+
* smp.c:call_single_queue.
28+
*
29+
* flush_smp_call_function_queue() only reads the type from
30+
* __call_single_node::u_flags as a regular load, the above
31+
* (anonymous) enum defines all the bits of this word.
32+
*
33+
* Other bits are not modified until the type is known.
34+
*
35+
* CSD_TYPE_SYNC/ASYNC:
36+
* struct {
37+
* struct llist_node node;
38+
* unsigned int flags;
39+
* smp_call_func_t func;
40+
* void *info;
41+
* };
42+
*
43+
* CSD_TYPE_IRQ_WORK:
44+
* struct {
45+
* struct llist_node node;
46+
* atomic_t flags;
47+
* void (*func)(struct irq_work *);
48+
* };
49+
*
50+
* CSD_TYPE_TTWU:
51+
* struct {
52+
* struct llist_node node;
53+
* unsigned int flags;
54+
* };
55+
*
56+
*/
57+
58+
struct __call_single_node {
59+
struct llist_node llist;
60+
union {
61+
unsigned int u_flags;
62+
atomic_t a_flags;
63+
};
64+
};
65+
66+
#endif /* __LINUX_SMP_TYPES_H */

kernel/sched/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ void sched_ttwu_pending(void *arg)
22932293
rq_lock_irqsave(rq, &rf);
22942294
update_rq_clock(rq);
22952295

2296-
llist_for_each_entry_safe(p, t, llist, wake_entry) {
2296+
llist_for_each_entry_safe(p, t, llist, wake_entry.llist) {
22972297
if (WARN_ON_ONCE(p->on_cpu))
22982298
smp_cond_load_acquire(&p->on_cpu, !VAL);
22992299

@@ -2329,7 +2329,7 @@ static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags
23292329
p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
23302330

23312331
WRITE_ONCE(rq->ttwu_pending, 1);
2332-
__smp_call_single_queue(cpu, &p->wake_entry);
2332+
__smp_call_single_queue(cpu, &p->wake_entry.llist);
23332333
}
23342334

23352335
void wake_up_if_idle(int cpu)
@@ -2786,7 +2786,7 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
27862786
#endif
27872787
init_numa_balancing(clone_flags, p);
27882788
#ifdef CONFIG_SMP
2789-
p->wake_entry_type = CSD_TYPE_TTWU;
2789+
p->wake_entry.u_flags = CSD_TYPE_TTWU;
27902790
#endif
27912791
}
27922792

kernel/smp.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -669,24 +669,6 @@ void __init smp_init(void)
669669
{
670670
int num_nodes, num_cpus;
671671

672-
/*
673-
* Ensure struct irq_work layout matches so that
674-
* flush_smp_call_function_queue() can do horrible things.
675-
*/
676-
BUILD_BUG_ON(offsetof(struct irq_work, llnode) !=
677-
offsetof(struct __call_single_data, llist));
678-
BUILD_BUG_ON(offsetof(struct irq_work, func) !=
679-
offsetof(struct __call_single_data, func));
680-
BUILD_BUG_ON(offsetof(struct irq_work, flags) !=
681-
offsetof(struct __call_single_data, flags));
682-
683-
/*
684-
* Assert the CSD_TYPE_TTWU layout is similar enough
685-
* for task_struct to be on the @call_single_queue.
686-
*/
687-
BUILD_BUG_ON(offsetof(struct task_struct, wake_entry_type) - offsetof(struct task_struct, wake_entry) !=
688-
offsetof(struct __call_single_data, flags) - offsetof(struct __call_single_data, llist));
689-
690672
idle_threads_init();
691673
cpuhp_threads_init();
692674

0 commit comments

Comments
 (0)