Skip to content

Commit 5b06fd3

Browse files
Peter Zijlstraingomolnar
authored andcommitted
static_call: Handle tail-calls
GCC can turn our static_call(name)(args...) into a tail call, in which case we get a JMP.d32 into the trampoline (which then does a further tail-call). Teach objtool to recognise and mark these in .static_call_sites and adjust the code patching to deal with this. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: https://lore.kernel.org/r/20200818135805.101186767@infradead.org
1 parent 452cddb commit 5b06fd3

6 files changed

Lines changed: 60 additions & 18 deletions

File tree

arch/x86/kernel/static_call.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,30 @@ static void __static_call_transform(void *insn, enum insn_type type, void *func)
4141
text_poke_bp(insn, code, size, NULL);
4242
}
4343

44-
void arch_static_call_transform(void *site, void *tramp, void *func)
44+
static inline enum insn_type __sc_insn(bool null, bool tail)
45+
{
46+
/*
47+
* Encode the following table without branches:
48+
*
49+
* tail null insn
50+
* -----+-------+------
51+
* 0 | 0 | CALL
52+
* 0 | 1 | NOP
53+
* 1 | 0 | JMP
54+
* 1 | 1 | RET
55+
*/
56+
return 2*tail + null;
57+
}
58+
59+
void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
4560
{
4661
mutex_lock(&text_mutex);
4762

4863
if (tramp)
49-
__static_call_transform(tramp, func ? JMP : RET, func);
64+
__static_call_transform(tramp, __sc_insn(!func, true), func);
5065

5166
if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site)
52-
__static_call_transform(site, func ? CALL : NOP, func);
67+
__static_call_transform(site, __sc_insn(!func, tail), func);
5368

5469
mutex_unlock(&text_mutex);
5570
}

include/linux/static_call.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
/*
104104
* Either @site or @tramp can be NULL.
105105
*/
106-
extern void arch_static_call_transform(void *site, void *tramp, void *func);
106+
extern void arch_static_call_transform(void *site, void *tramp, void *func, bool tail);
107107

108108
#define STATIC_CALL_TRAMP_ADDR(name) &STATIC_CALL_TRAMP(name)
109109

@@ -206,7 +206,7 @@ void __static_call_update(struct static_call_key *key, void *tramp, void *func)
206206
{
207207
cpus_read_lock();
208208
WRITE_ONCE(key->func, func);
209-
arch_static_call_transform(NULL, tramp, func);
209+
arch_static_call_transform(NULL, tramp, func, false);
210210
cpus_read_unlock();
211211
}
212212

include/linux/static_call_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
#define STATIC_CALL_TRAMP(name) __PASTE(STATIC_CALL_TRAMP_PREFIX, name)
1717
#define STATIC_CALL_TRAMP_STR(name) __stringify(STATIC_CALL_TRAMP(name))
1818

19+
/*
20+
* Flags in the low bits of static_call_site::key.
21+
*/
22+
#define STATIC_CALL_SITE_TAIL 1UL /* tail call */
23+
#define STATIC_CALL_SITE_INIT 2UL /* init section */
24+
#define STATIC_CALL_SITE_FLAGS 3UL
25+
1926
/*
2027
* The static call site table needs to be created by external tooling (objtool
2128
* or a compiler plugin).

kernel/static_call.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ extern struct static_call_site __start_static_call_sites[],
1515

1616
static bool static_call_initialized;
1717

18-
#define STATIC_CALL_INIT 1UL
19-
2018
/* mutex to protect key modules/sites */
2119
static DEFINE_MUTEX(static_call_mutex);
2220

@@ -39,18 +37,23 @@ static inline void *static_call_addr(struct static_call_site *site)
3937
static inline struct static_call_key *static_call_key(const struct static_call_site *site)
4038
{
4139
return (struct static_call_key *)
42-
(((long)site->key + (long)&site->key) & ~STATIC_CALL_INIT);
40+
(((long)site->key + (long)&site->key) & ~STATIC_CALL_SITE_FLAGS);
4341
}
4442

4543
/* These assume the key is word-aligned. */
4644
static inline bool static_call_is_init(struct static_call_site *site)
4745
{
48-
return ((long)site->key + (long)&site->key) & STATIC_CALL_INIT;
46+
return ((long)site->key + (long)&site->key) & STATIC_CALL_SITE_INIT;
47+
}
48+
49+
static inline bool static_call_is_tail(struct static_call_site *site)
50+
{
51+
return ((long)site->key + (long)&site->key) & STATIC_CALL_SITE_TAIL;
4952
}
5053

5154
static inline void static_call_set_init(struct static_call_site *site)
5255
{
53-
site->key = ((long)static_call_key(site) | STATIC_CALL_INIT) -
56+
site->key = ((long)static_call_key(site) | STATIC_CALL_SITE_INIT) -
5457
(long)&site->key;
5558
}
5659

@@ -104,7 +107,7 @@ void __static_call_update(struct static_call_key *key, void *tramp, void *func)
104107

105108
key->func = func;
106109

107-
arch_static_call_transform(NULL, tramp, func);
110+
arch_static_call_transform(NULL, tramp, func, false);
108111

109112
/*
110113
* If uninitialized, we'll not update the callsites, but they still
@@ -154,7 +157,8 @@ void __static_call_update(struct static_call_key *key, void *tramp, void *func)
154157
continue;
155158
}
156159

157-
arch_static_call_transform(site_addr, NULL, func);
160+
arch_static_call_transform(site_addr, NULL, func,
161+
static_call_is_tail(site));
158162
}
159163
}
160164

@@ -198,7 +202,8 @@ static int __static_call_init(struct module *mod,
198202
key->mods = site_mod;
199203
}
200204

201-
arch_static_call_transform(site_addr, NULL, key->func);
205+
arch_static_call_transform(site_addr, NULL, key->func,
206+
static_call_is_tail(site));
202207
}
203208

204209
return 0;

tools/include/linux/static_call_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
#define STATIC_CALL_TRAMP(name) __PASTE(STATIC_CALL_TRAMP_PREFIX, name)
1717
#define STATIC_CALL_TRAMP_STR(name) __stringify(STATIC_CALL_TRAMP(name))
1818

19+
/*
20+
* Flags in the low bits of static_call_site::key.
21+
*/
22+
#define STATIC_CALL_SITE_TAIL 1UL /* tail call */
23+
#define STATIC_CALL_SITE_INIT 2UL /* init section */
24+
#define STATIC_CALL_SITE_FLAGS 3UL
25+
1926
/*
2027
* The static call site table needs to be created by external tooling (objtool
2128
* or a compiler plugin).

tools/objtool/check.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ static int create_static_call_sections(struct objtool_file *file)
516516
}
517517
memset(reloc, 0, sizeof(*reloc));
518518
reloc->sym = key_sym;
519-
reloc->addend = 0;
519+
reloc->addend = is_sibling_call(insn) ? STATIC_CALL_SITE_TAIL : 0;
520520
reloc->type = R_X86_64_PC32;
521521
reloc->offset = idx * sizeof(struct static_call_site) + 4;
522522
reloc->sec = reloc_sec;
@@ -747,6 +747,10 @@ static int add_jump_destinations(struct objtool_file *file)
747747
} else {
748748
/* external sibling call */
749749
insn->call_dest = reloc->sym;
750+
if (insn->call_dest->static_call_tramp) {
751+
list_add_tail(&insn->static_call_node,
752+
&file->static_call_list);
753+
}
750754
continue;
751755
}
752756

@@ -798,6 +802,10 @@ static int add_jump_destinations(struct objtool_file *file)
798802

799803
/* internal sibling call */
800804
insn->call_dest = insn->jump_dest->func;
805+
if (insn->call_dest->static_call_tramp) {
806+
list_add_tail(&insn->static_call_node,
807+
&file->static_call_list);
808+
}
801809
}
802810
}
803811
}
@@ -1684,6 +1692,10 @@ static int decode_sections(struct objtool_file *file)
16841692
if (ret)
16851693
return ret;
16861694

1695+
ret = read_static_call_tramps(file);
1696+
if (ret)
1697+
return ret;
1698+
16871699
ret = add_jump_destinations(file);
16881700
if (ret)
16891701
return ret;
@@ -1716,10 +1728,6 @@ static int decode_sections(struct objtool_file *file)
17161728
if (ret)
17171729
return ret;
17181730

1719-
ret = read_static_call_tramps(file);
1720-
if (ret)
1721-
return ret;
1722-
17231731
return 0;
17241732
}
17251733

0 commit comments

Comments
 (0)