]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
hrtimers: Make callback function pointer private
authorNam Cao <namcao@linutronix.de>
Wed, 5 Feb 2025 10:55:16 +0000 (11:55 +0100)
committerIngo Molnar <mingo@kernel.org>
Sat, 5 Apr 2025 08:30:17 +0000 (10:30 +0200)
Make the struct hrtimer::function field private, to prevent users from
changing this field in an unsafe way. hrtimer_update_function() should be
used if the callback function needs to be changed.

Signed-off-by: Nam Cao <namcao@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/all/7d0e6e0c5c59a64a9bea940051aac05d750bc0c2.1738746927.git.namcao@linutronix.de
include/linux/hrtimer_types.h
include/trace/events/timer.h
kernel/time/hrtimer.c
kernel/time/timer_list.c

index 7c5b27daa89d0a94470afc084bc95c8fb6556818..8fbbb6bdf7a187b410acf8769dee2a90b0f9415f 100644 (file)
@@ -39,7 +39,7 @@ enum hrtimer_restart {
 struct hrtimer {
        struct timerqueue_node          node;
        ktime_t                         _softexpires;
-       enum hrtimer_restart            (*function)(struct hrtimer *);
+       enum hrtimer_restart            (*__private function)(struct hrtimer *);
        struct hrtimer_clock_base       *base;
        u8                              state;
        u8                              is_rel;
index 1ef58a04fc5798011fd664e115cceb86d91a10c8..f8c906be4cd0d8f868f91f70cedf608f3048f7a4 100644 (file)
@@ -235,7 +235,7 @@ TRACE_EVENT(hrtimer_start,
 
        TP_fast_assign(
                __entry->hrtimer        = hrtimer;
-               __entry->function       = hrtimer->function;
+               __entry->function       = ACCESS_PRIVATE(hrtimer, function);
                __entry->expires        = hrtimer_get_expires(hrtimer);
                __entry->softexpires    = hrtimer_get_softexpires(hrtimer);
                __entry->mode           = mode;
@@ -271,7 +271,7 @@ TRACE_EVENT(hrtimer_expire_entry,
        TP_fast_assign(
                __entry->hrtimer        = hrtimer;
                __entry->now            = *now;
-               __entry->function       = hrtimer->function;
+               __entry->function       = ACCESS_PRIVATE(hrtimer, function);
        ),
 
        TP_printk("hrtimer=%p function=%ps now=%llu",
index 163cde35f0c36f548095450b54a6e4a7d63b4189..88ea4bbea9c1ee0e3e07e68b9c0b8987860a14c8 100644 (file)
@@ -1316,7 +1316,7 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
        struct hrtimer_clock_base *base;
        unsigned long flags;
 
-       if (WARN_ON_ONCE(!timer->function))
+       if (WARN_ON_ONCE(!ACCESS_PRIVATE(timer, function)))
                return;
        /*
         * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
@@ -1629,9 +1629,9 @@ static void __hrtimer_setup(struct hrtimer *timer,
        timerqueue_init(&timer->node);
 
        if (WARN_ON_ONCE(!function))
-               timer->function = hrtimer_dummy_timeout;
+               ACCESS_PRIVATE(timer, function) = hrtimer_dummy_timeout;
        else
-               timer->function = function;
+               ACCESS_PRIVATE(timer, function) = function;
 }
 
 /**
@@ -1743,7 +1743,7 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
        raw_write_seqcount_barrier(&base->seq);
 
        __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
-       fn = timer->function;
+       fn = ACCESS_PRIVATE(timer, function);
 
        /*
         * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
index cfbb46cc4e7613a3d7255b67a941dd05387cf68e..b03d0ada64695058d4c57f3e29d546ba18bff591 100644 (file)
@@ -46,7 +46,7 @@ static void
 print_timer(struct seq_file *m, struct hrtimer *taddr, struct hrtimer *timer,
            int idx, u64 now)
 {
-       SEQ_printf(m, " #%d: <%p>, %ps", idx, taddr, timer->function);
+       SEQ_printf(m, " #%d: <%p>, %ps", idx, taddr, ACCESS_PRIVATE(timer, function));
        SEQ_printf(m, ", S:%02x", timer->state);
        SEQ_printf(m, "\n");
        SEQ_printf(m, " # expires at %Lu-%Lu nsecs [in %Ld to %Ld nsecs]\n",