Skip to content

Commit 55ca3cc

Browse files
Suresh SiddhaIngo Molnar
authored andcommitted
x86_64, ftrace: Make ftrace use kernel identity mapping to modify code
On x86_64, kernel text mappings are mapped read-only with CONFIG_DEBUG_RODATA. So use the kernel identity mapping instead of the kernel text mapping to modify the kernel text. Signed-off-by: Suresh Siddha <[email protected]> Acked-by: Steven Rostedt <[email protected]> Tested-by: Steven Rostedt <[email protected]> LKML-Reference: <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent 502f660 commit 55ca3cc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

arch/x86/kernel/ftrace.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,26 @@ static void wait_for_nmi(void)
187187
nmi_wait_count++;
188188
}
189189

190+
static inline int
191+
within(unsigned long addr, unsigned long start, unsigned long end)
192+
{
193+
return addr >= start && addr < end;
194+
}
195+
190196
static int
191197
do_ftrace_mod_code(unsigned long ip, void *new_code)
192198
{
199+
/*
200+
* On x86_64, kernel text mappings are mapped read-only with
201+
* CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
202+
* of the kernel text mapping to modify the kernel text.
203+
*
204+
* For 32bit kernels, these mappings are same and we can use
205+
* kernel identity mapping to modify code.
206+
*/
207+
if (within(ip, (unsigned long)_text, (unsigned long)_etext))
208+
ip = (unsigned long)__va(__pa(ip));
209+
193210
mod_code_ip = (void *)ip;
194211
mod_code_newcode = new_code;
195212

arch/x86/mm/pageattr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
279279
__pa((unsigned long)__end_rodata) >> PAGE_SHIFT))
280280
pgprot_val(forbidden) |= _PAGE_RW;
281281

282-
#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA) && \
283-
!defined(CONFIG_DYNAMIC_FTRACE)
282+
#if defined(CONFIG_X86_64) && defined(CONFIG_DEBUG_RODATA)
284283
/*
285284
* Once the kernel maps the text as RO (kernel_set_to_readonly is set),
286285
* kernel text mappings for the large page aligned text, rodata sections

0 commit comments

Comments
 (0)