Skip to content

Commit 9721873

Browse files
puranjaymohanpalmer-dabbelt
authored andcommitted
riscv: extend patch_text_nosync() for multiple pages
The patch_insn_write() function currently doesn't work for multiple pages of instructions, therefore patch_text_nosync() will fail with a page fault if called with lengths spanning multiple pages. This commit extends the patch_insn_write() function to support multiple pages by copying at max 2 pages at a time in a loop. This implementation is similar to text_poke_copy() function of x86. Signed-off-by: Puranjay Mohan <puranjay12@gmail.com> Reviewed-by: Pu Lehui <pulehui@huawei.com> Reviewed-by: Björn Töpel <bjorn@rivosinc.com> Tested-by: Björn Töpel <bjorn@rivosinc.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20230831131229.497941-3-puranjay12@gmail.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 20e490a commit 9721873

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

arch/riscv/kernel/patch.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ static void patch_unmap(int fixmap)
5353
}
5454
NOKPROBE_SYMBOL(patch_unmap);
5555

56-
static int patch_insn_write(void *addr, const void *insn, size_t len)
56+
static int __patch_insn_write(void *addr, const void *insn, size_t len)
5757
{
5858
void *waddr = addr;
5959
bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE;
6060
int ret;
6161

62+
/*
63+
* Only two pages can be mapped at a time for writing.
64+
*/
65+
if (len + offset_in_page(addr) > 2 * PAGE_SIZE)
66+
return -EINVAL;
67+
6268
/*
6369
* Before reaching here, it was expected to lock the text_mutex
6470
* already, so we don't need to give another lock here and could
@@ -74,7 +80,7 @@ static int patch_insn_write(void *addr, const void *insn, size_t len)
7480
lockdep_assert_held(&text_mutex);
7581

7682
if (across_pages)
77-
patch_map(addr + len, FIX_TEXT_POKE1);
83+
patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1);
7884

7985
waddr = patch_map(addr, FIX_TEXT_POKE0);
8086

@@ -87,15 +93,36 @@ static int patch_insn_write(void *addr, const void *insn, size_t len)
8793

8894
return ret;
8995
}
90-
NOKPROBE_SYMBOL(patch_insn_write);
96+
NOKPROBE_SYMBOL(__patch_insn_write);
9197
#else
92-
static int patch_insn_write(void *addr, const void *insn, size_t len)
98+
static int __patch_insn_write(void *addr, const void *insn, size_t len)
9399
{
94100
return copy_to_kernel_nofault(addr, insn, len);
95101
}
96-
NOKPROBE_SYMBOL(patch_insn_write);
102+
NOKPROBE_SYMBOL(__patch_insn_write);
97103
#endif /* CONFIG_MMU */
98104

105+
static int patch_insn_write(void *addr, const void *insn, size_t len)
106+
{
107+
size_t patched = 0;
108+
size_t size;
109+
int ret = 0;
110+
111+
/*
112+
* Copy the instructions to the destination address, two pages at a time
113+
* because __patch_insn_write() can only handle len <= 2 * PAGE_SIZE.
114+
*/
115+
while (patched < len && !ret) {
116+
size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
117+
ret = __patch_insn_write(addr + patched, insn + patched, size);
118+
119+
patched += size;
120+
}
121+
122+
return ret;
123+
}
124+
NOKPROBE_SYMBOL(patch_insn_write);
125+
99126
int patch_text_nosync(void *addr, const void *insns, size_t len)
100127
{
101128
u32 *tp = addr;

0 commit comments

Comments
 (0)