On Mon, Oct 28, 2024 at 3:35 AM Nam Cao <namcao@linutronix.de> wrote:
>
> This is the forth part of a 5-part series (split for convenience). All 5
> parts are:
>
> Part 1: https://lore.kernel.org/lkml/cover.1729864615.git.namcao@linutronix.de
> Part 2: https://lore.kernel.org/lkml/cover.1729864823.git.namcao@linutronix.de
> Part 3: https://lore.kernel.org/lkml/cover.1729865232.git.namcao@linutronix.de
> Part 4: https://lore.kernel.org/lkml/cover.1729865485.git.namcao@linutronix.de
> Part 5: https://lore.kernel.org/lkml/cover.1729865740.git.namcao@linutronix.de
>
> To use hrtimer, hrtimer_init() (or one of its variant) must be called, and
> also the timer's callfack function must be setup separately.
>
> That can cause misuse of hrtimer. For example, because:
> - The callback function is not setup
> - The callback function is setup while it is not safe to do so
>
> To prevent misuse of hrtimer, this series:
> - Introduce new functions hrtimer_setup*(). These new functions are
> similar to hrtimer_init*(), except that they also sanity-check and
> initialize the callback function.
> - Introduce hrtimer_update_function() which checks that it is safe to
> change the callback function. The 'function' field of hrtimer is then
> made private.
> - Convert all users to use the new functions.
> - Some minor cleanups on the way.
>
> Most conversion patches were created using Coccinelle with the sematic
> patch below; except for tricky cases that Coccinelle cannot handle, or for
> some cases where a Coccinelle's bug regarding 100 column limit is
> triggered. Any patches not mentioning Coccinelle were done manually.
>
> virtual patch
> @@ expression timer, clock, mode, func; @@
> - hrtimer_init(timer, clock, mode);
> ...
> - timer->function = func;
> + hrtimer_setup(timer, func, clock, mode);
>
> @@ expression timer, clock, mode, func; @@
> - hrtimer_init(&timer, clock, mode);
> ...
> - timer.function = func;
> + hrtimer_setup(&timer, func, clock, mode);
>
> @@ expression timer, clock, mode, func; @@
> - hrtimer_init_on_stack(&timer, clock, mode);
> ...
> - timer.function = func;
> + hrtimer_setup_on_stack(&timer, func, clock, mode);
>
> @@ expression timer, clock, mode; @@
> - hrtimer_init_sleeper_on_stack(timer, clock, mode);
> + hrtimer_setup_sleeper_on_stack(timer, clock, mode);
>
> Signed-off-by: Nam Cao <namcao@linutronix.de>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
> Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Cc: Linus Walleij <linus.walleij@linaro.org>
> Cc: Sebastian Reichel <sre@kernel.org>
> Cc: Will Deacon <will@kernel.org>
> Cc: Jon Mason <jdmason@kudzu.us>
> Cc: Jaehoon Chung <jh80.chung@samsung.com>
> Cc: Hans Verkuil <hverkuil-cisco@xs4all.nl>
> Cc: Jassi Brar <jassisinghbrar@gmail.com>
> Cc: Pavel Machek <pavel@ucw.cz>
> Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> Cc: Jonathan Cameron <jic23@kernel.org>
> Cc: Andi Shyti <andi.shyti@kernel.org>
> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> Cc: Alex Deucher <alexander.deucher@amd.com>
> Cc: Jani Nikula <jani.nikula@linux.intel.com>
> Cc: Rob Clark <robdclark@gmail.com>
> Cc: Lucas De Marchi <lucas.demarchi@intel.com>
> Cc: Zack Rusin <zack.rusin@broadcom.com>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: "Uwe Kleine-König" <ukleinek@kernel.org>
> Cc: Takashi Iwai <tiwai@suse.com>
>
> Nam Cao (44):
> USB: chipidea: Switch to use hrtimer_setup()
> usb: dwc2: Switch to use hrtimer_setup()
> usb: fotg210-hcd: Switch to use hrtimer_setup()
> usb: gadget: Switch to use hrtimer_setup()
> usb: ehci: Switch to use hrtimer_setup()
> usb: musb: cppi41: Switch to use hrtimer_setup()
> usb: typec: tcpm: Switch to use hrtimer_setup()
> serial: 8250: Switch to use hrtimer_setup()
> serial: imx: Switch to use hrtimer_setup()
> serial: sh-sci: Switch to use hrtimer_setup()
> serial: xilinx_uartps: Switch to use hrtimer_setup()
> scsi: Switch to use hrtimer_setup()
> rtc: class: Switch to use hrtimer_setup()
> pps: generators: pps_gen_parport: Switch to use hrtimer_setup()
> powercap: Switch to use hrtimer_setup()
> power: supply: ab8500_chargalg: Switch to use hrtimer_setup()
> power: reset: ltc2952-poweroff: Switch to use hrtimer_setup()
> drivers: perf: Switch to use hrtimer_setup()
> ntb: ntb_pingpong: Switch to use hrtimer_setup()
> mmc: dw_mmc: Switch to use hrtimer_setup()
> misc: vcpu_stall_detector: Switch to use hrtimer_setup()
> media: Switch to use hrtimer_setup()
> mailbox: Switch to use hrtimer_setup()
> leds: trigger: pattern: Switch to use hrtimer_setup()
> Input: Switch to use hrtimer_setup()
> iio: Switch to use hrtimer_setup()
> i2c: Switch to use hrtimer_setup()
> stm class: heartbeat: Switch to use hrtimer_setup()
> drm/amdgpu: Switch to use hrtimer_setup()
> drm/i915/huc: Switch to use hrtimer_setup()
> drm/i915/gvt: Switch to use hrtimer_setup()
> drm/i915/perf: Switch to use hrtimer_setup()
> drm/i915/pmu: Switch to use hrtimer_setup()
> drm/i915/uncore: Switch to use hrtimer_setup()
> drm/i915/request: Switch to use hrtimer_setup()
> drm/msm: Switch to use hrtimer_setup()
> drm/vkms: Switch to use hrtimer_setup()
> drm/xe/oa: Switch to use hrtimer_setup()
> drm/vmwgfx: Switch to use hrtimer_setup()
Acked-by: Zack Rusin <zack.rusin@broadcom.com>
FWIW, even if there's no current errors in those paths, that's a nice
change. I remember looking at the hrtimer initialization code the
first time and thinking that setting the function member of the struct
after initialization must be a bug or some hack... Now it will
instinctively make sense to anyone reading it for the first time.
z