Skip to content

Commit 99fd6e8

Browse files
Vincent Chenpalmer-dabbelt
authored andcommitted
RISC-V: Add _TIF_NEED_RESCHED check for kernel thread when CONFIG_PREEMPT=y
The cond_resched() can be used to yield the CPU resource if CONFIG_PREEMPT is not defined. Otherwise, cond_resched() is a dummy function. In order to avoid kernel thread occupying entire CPU, when CONFIG_PREEMPT=y, the kernel thread needs to follow the rescheduling mechanism like a user thread. Signed-off-by: Vincent Chen <vincentc@andestech.com> Tested-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
1 parent 49a5785 commit 99fd6e8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

arch/riscv/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void asm_offsets(void)
3939
OFFSET(TASK_STACK, task_struct, stack);
4040
OFFSET(TASK_TI, task_struct, thread_info);
4141
OFFSET(TASK_TI_FLAGS, task_struct, thread_info.flags);
42+
OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count);
4243
OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp);
4344
OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp);
4445
OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);

arch/riscv/kernel/entry.S

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ _save_context:
144144
REG_L x2, PT_SP(sp)
145145
.endm
146146

147+
#if !IS_ENABLED(CONFIG_PREEMPT)
148+
.set resume_kernel, restore_all
149+
#endif
150+
147151
ENTRY(handle_exception)
148152
SAVE_ALL
149153

@@ -228,7 +232,7 @@ ret_from_exception:
228232
REG_L s0, PT_SSTATUS(sp)
229233
csrc sstatus, SR_SIE
230234
andi s0, s0, SR_SPP
231-
bnez s0, restore_all
235+
bnez s0, resume_kernel
232236

233237
resume_userspace:
234238
/* Interrupts must be disabled here so flags are checked atomically */
@@ -250,6 +254,18 @@ restore_all:
250254
RESTORE_ALL
251255
sret
252256

257+
#if IS_ENABLED(CONFIG_PREEMPT)
258+
resume_kernel:
259+
REG_L s0, TASK_TI_PREEMPT_COUNT(tp)
260+
bnez s0, restore_all
261+
need_resched:
262+
REG_L s0, TASK_TI_FLAGS(tp)
263+
andi s0, s0, _TIF_NEED_RESCHED
264+
beqz s0, restore_all
265+
call preempt_schedule_irq
266+
j need_resched
267+
#endif
268+
253269
work_pending:
254270
/* Enter slow path for supplementary processing */
255271
la ra, ret_from_exception

0 commit comments

Comments
 (0)