From 40762ffb951f82261a56edd7325b4ff9fa215ea0 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Mon, 1 May 2023 08:26:41 +0100 Subject: [PATCH] kernel: add function to query the current preempt count --- include/socks/cpu.h | 3 ++- kernel/cpu.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/socks/cpu.h b/include/socks/cpu.h index 9e1a404..4b25595 100644 --- a/include/socks/cpu.h +++ b/include/socks/cpu.h @@ -17,7 +17,7 @@ enum cpu_flags { struct cpu_data { enum cpu_flags c_flags; unsigned int c_id; - unsigned int c_preempt_count; + int c_preempt_count; struct runqueue c_rq; struct queue c_timers; @@ -46,6 +46,7 @@ static inline cycles_t cycles_diff(cycles_t then, cycles_t now) extern void preempt_disable(void); extern void preempt_enable(void); +extern int preempt_count(void); extern unsigned int cpu_get_highest_available(void); diff --git a/kernel/cpu.c b/kernel/cpu.c index e239fec..001432a 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -43,7 +43,7 @@ void preempt_disable(void) return; } - unsigned int preempt = READ_ONCE(cpu_data->c_preempt_count); + int preempt = READ_ONCE(cpu_data->c_preempt_count); preempt++; WRITE_ONCE(cpu_data->c_preempt_count, preempt); } @@ -56,7 +56,7 @@ void preempt_enable(void) return; } - unsigned int preempt = READ_ONCE(cpu_data->c_preempt_count); + int preempt = READ_ONCE(cpu_data->c_preempt_count); if (preempt > 0) { preempt--; @@ -65,6 +65,17 @@ void preempt_enable(void) WRITE_ONCE(cpu_data->c_preempt_count, preempt); } +int preempt_count(void) +{ + ml_cpu_block *ml_cpu = ml_this_cpu(); + struct cpu_data *cpu_data = ml_cpu_block_get_data(ml_cpu); + if (!cpu_data) { + return 0; + } + + return READ_ONCE(cpu_data->c_preempt_count); +} + unsigned int cpu_get_highest_available(void) { return bitmap_highest_set(cpu_available, CPU_MAX);