kernel_optimize_test/drivers/rtc
Kees Cook e99e88a9d2 treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.

Casting from unsigned long:

    void my_callback(unsigned long data)
    {
        struct something *ptr = (struct something *)data;
    ...
    }
    ...
    setup_timer(&ptr->my_timer, my_callback, ptr);

and forced object casts:

    void my_callback(struct something *ptr)
    {
    ...
    }
    ...
    setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);

become:

    void my_callback(struct timer_list *t)
    {
        struct something *ptr = from_timer(ptr, t, my_timer);
    ...
    }
    ...
    timer_setup(&ptr->my_timer, my_callback, 0);

Direct function assignments:

    void my_callback(unsigned long data)
    {
        struct something *ptr = (struct something *)data;
    ...
    }
    ...
    ptr->my_timer.function = my_callback;

have a temporary cast added, along with converting the args:

    void my_callback(struct timer_list *t)
    {
        struct something *ptr = from_timer(ptr, t, my_timer);
    ...
    }
    ...
    ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;

And finally, callbacks without a data assignment:

    void my_callback(unsigned long data)
    {
    ...
    }
    ...
    setup_timer(&ptr->my_timer, my_callback, 0);

have their argument renamed to verify they're unused during conversion:

    void my_callback(struct timer_list *unused)
    {
    ...
    }
    ...
    timer_setup(&ptr->my_timer, my_callback, 0);

The conversion is done with the following Coccinelle script:

spatch --very-quiet --all-includes --include-headers \
	-I ./arch/x86/include -I ./arch/x86/include/generated \
	-I ./include -I ./arch/x86/include/uapi \
	-I ./arch/x86/include/generated/uapi -I ./include/uapi \
	-I ./include/generated/uapi --include ./include/linux/kconfig.h \
	--dir . \
	--cocci-file ~/src/data/timer_setup.cocci

@fix_address_of@
expression e;
@@

 setup_timer(
-&(e)
+&e
 , ...)

// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@

(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)

@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@

(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
 _E->_timer@_stl.function = _callback;
|
 _E->_timer@_stl.function = &_callback;
|
 _E->_timer@_stl.function = (_cast_func)_callback;
|
 _E->_timer@_stl.function = (_cast_func)&_callback;
|
 _E._timer@_stl.function = _callback;
|
 _E._timer@_stl.function = &_callback;
|
 _E._timer@_stl.function = (_cast_func)_callback;
|
 _E._timer@_stl.function = (_cast_func)&_callback;
)

// callback(unsigned long arg)
@change_callback_handle_cast
 depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@

 void _callback(
-_origtype _origarg
+struct timer_list *t
 )
 {
(
	... when != _origarg
	_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
	... when != _origarg
|
	... when != _origarg
	_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
	... when != _origarg
|
	... when != _origarg
	_handletype *_handle;
	... when != _handle
	_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
	... when != _origarg
|
	... when != _origarg
	_handletype *_handle;
	... when != _handle
	_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
	... when != _origarg
)
 }

// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
 depends on change_timer_function_usage &&
                     !change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@

 void _callback(
-_origtype _origarg
+struct timer_list *t
 )
 {
+	_handletype *_origarg = from_timer(_origarg, t, _timer);
+
	... when != _origarg
-	(_handletype *)_origarg
+	_origarg
	... when != _origarg
 }

// Avoid already converted callbacks.
@match_callback_converted
 depends on change_timer_function_usage &&
            !change_callback_handle_cast &&
	    !change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@

 void _callback(struct timer_list *t)
 { ... }

// callback(struct something *handle)
@change_callback_handle_arg
 depends on change_timer_function_usage &&
	    !match_callback_converted &&
            !change_callback_handle_cast &&
            !change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@

 void _callback(
-_handletype *_handle
+struct timer_list *t
 )
 {
+	_handletype *_handle = from_timer(_handle, t, _timer);
	...
 }

// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
 depends on change_timer_function_usage &&
	    change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@

 void _callback(struct timer_list *t)
 {
-	_handletype *_handle = from_timer(_handle, t, _timer);
 }

// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
 depends on change_timer_function_usage &&
            !change_callback_handle_cast &&
            !change_callback_handle_cast_no_arg &&
	    !change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@

(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)

// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
 depends on change_timer_function_usage &&
            (change_callback_handle_cast ||
             change_callback_handle_cast_no_arg ||
             change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@

(
 _E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
 ;
|
 _E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
 ;
)

// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
 depends on change_timer_function_usage &&
            (change_callback_handle_cast ||
             change_callback_handle_cast_no_arg ||
             change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@

 _callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
 )

// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@

(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)

@change_callback_unused_data
 depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@

 void _callback(
-_origtype _origarg
+struct timer_list *unused
 )
 {
	... when != _origarg
 }

Signed-off-by: Kees Cook <keescook@chromium.org>
2017-11-21 15:57:07 -08:00
..
class.c rtc: Allow rtc drivers to specify the tv_nsec value for ntp 2017-10-30 15:03:24 -07:00
hctosys.c
interface.c rtc: interface: Validate alarm-time before handling rollover 2017-05-31 13:04:54 +02:00
Kconfig rtc: Add Realtek RTD1295 2017-09-05 09:55:02 +02:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
nvmem.c rtc: add generic nvmem support 2017-07-07 13:14:14 +02:00
rtc-88pm80x.c
rtc-88pm860x.c
rtc-ab-b5ze-s3.c
rtc-ab3100.c
rtc-ab8500.c
rtc-abx80x.c
rtc-ac100.c
rtc-armada38x.c rtc: armada38x: Add support for Armada 7K/8K 2017-02-21 23:21:30 +01:00
rtc-as3722.c
rtc-asm9260.c rtc: asm9260: fix module autoload 2016-10-19 08:45:48 +02:00
rtc-at32ap700x.c
rtc-at91rm9200.c rtc: at91rm9200: remove race condition 2017-07-07 13:14:12 +02:00
rtc-at91rm9200.h
rtc-at91sam9.c
rtc-au1xxx.c rtc: constify rtc_class_ops structures 2017-01-11 17:23:06 +01:00
rtc-bfin.c rtc: constify rtc_class_ops structures 2017-01-11 17:23:06 +01:00
rtc-bq32k.c rtc: bq32k: Add OF device ID table 2017-03-09 01:29:17 +01:00
rtc-bq4802.c
rtc-brcmstb-waketimer.c rtc: brcmstb-waketimer: Add Broadcom STB wake-timer 2017-07-05 23:11:59 +02:00
rtc-cmos.c Merge branches 'pm-sleep' and 'powercap' 2017-05-22 20:32:05 +02:00
rtc-coh901331.c
rtc-core.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
rtc-cpcap.c rtc: cpcap: fix improper use of IRQ_NONE for request_threaded_irq 2017-04-14 12:08:52 +02:00
rtc-da9052.c
rtc-da9055.c
rtc-da9063.c
rtc-davinci.c
rtc-dev.c treewide: setup_timer() -> timer_setup() 2017-11-21 15:57:07 -08:00
rtc-digicolor.c
rtc-dm355evm.c mfd: dm355evm_msp: Move header file out of I2C realm 2017-08-15 08:06:14 +01:00
rtc-ds1216.c
rtc-ds1286.c
rtc-ds1302.c
rtc-ds1305.c
rtc-ds1307.c RTC for 4.14 2017-09-13 10:56:00 -07:00
rtc-ds1343.c
rtc-ds1347.c
rtc-ds1374.c rtc: ds1374: wdt: Fix stop/start ioctl always returning -EINVAL 2017-05-04 14:46:25 +02:00
rtc-ds1390.c
rtc-ds1511.c
rtc-ds1553.c
rtc-ds1672.c rtc: constify i2c_device_id 2017-09-01 01:10:11 +02:00
rtc-ds1685.c
rtc-ds1742.c
rtc-ds2404.c
rtc-ds3232.c rtc: ds3232: add temperature support 2017-07-06 22:37:16 +02:00
rtc-efi-platform.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
rtc-efi.c
rtc-em3027.c rtc: constify i2c_device_id 2017-09-01 01:10:11 +02:00
rtc-ep93xx.c
rtc-fm3130.c
rtc-ftrtc010.c rtc: gemini/ftrtc010: rename driver and symbols 2017-07-06 22:37:15 +02:00
rtc-generic.c
rtc-goldfish.c rtc: goldfish: Add RTC driver for Android emulator 2017-09-01 01:10:18 +02:00
rtc-hid-sensor-time.c rtc: hid-sensor-time: remove some dead code 2017-04-22 17:56:18 +02:00
rtc-hym8563.c
rtc-imxdi.c sched/wait: Disambiguate wq_entry->task_list and wq_head->task_list naming 2017-06-20 12:19:14 +02:00
rtc-isl1208.c rtc: isl1208: Add OF device ID table 2017-03-09 01:29:27 +01:00
rtc-isl12022.c
rtc-jz4740.c rtc: jz4740: make the driver buildable as a module again 2017-01-26 23:03:21 +01:00
rtc-lib.c rtc: make rtc-lib explicitly non-modular 2016-11-04 23:31:29 +01:00
rtc-lp8788.c
rtc-lpc24xx.c
rtc-lpc32xx.c
rtc-ls1x.c rtc: constify rtc_class_ops structures 2017-01-11 17:23:06 +01:00
rtc-m41t80.c rtc: m41t80: remove debug sysfs attribute 2017-08-24 11:03:34 +02:00
rtc-m41t93.c
rtc-m41t94.c
rtc-m48t35.c
rtc-m48t59.c
rtc-m48t86.c rtc: m48t86: remove unused platform_data 2017-02-21 21:23:13 +01:00
rtc-max6900.c rtc: constify i2c_device_id 2017-09-01 01:10:11 +02:00
rtc-max6902.c
rtc-max6916.c
rtc-max8907.c
rtc-max8925.c rtc: max8925: remove redundant check on ret 2017-07-31 00:17:48 +02:00
rtc-max8997.c
rtc-max8998.c
rtc-max77686.c
rtc-mc13xxx.c
rtc-mc146818-lib.c
rtc-mcp795.c rtc: mcp795: add alarm support. 2017-01-11 17:23:04 +01:00
rtc-moxart.c
rtc-mpc5121.c
rtc-mrst.c
rtc-msm6242.c
rtc-mt6397.c
rtc-mv.c
rtc-mxc.c rtc: mxc: avoid disabling interrupts on device close 2017-08-24 16:23:16 +02:00
rtc-nuc900.c rtc: rtc-nuc900: fix loop timeout test 2017-07-06 22:37:16 +02:00
rtc-omap.c rtc: omap: mark PM methods as __maybe_unused 2017-03-09 01:32:32 +01:00
rtc-opal.c rtc: opal: Implement rtc_class_ops.alarm_irq_enable callback 2017-06-24 07:48:30 +02:00
rtc-palmas.c
rtc-pcap.c
rtc-pcf2123.c
rtc-pcf2127.c rtc: pcf2127: bulk read only date and time registers. 2017-02-23 18:38:54 +01:00
rtc-pcf8523.c
rtc-pcf8563.c rtc: pcf8563: avoid using rtc->name 2017-06-03 10:52:05 +02:00
rtc-pcf8583.c
rtc-pcf50633.c
rtc-pcf85063.c rtc: pcf85063: do not register a RTC device if chip is not present 2016-11-04 23:08:59 +01:00
rtc-pic32.c
rtc-pl030.c
rtc-pl031.c
rtc-pm8xxx.c
rtc-proc.c
rtc-ps3.c
rtc-puv3.c rtc: puv3: make alarms useful 2017-08-22 12:05:21 +02:00
rtc-pxa.c rtc: pxa: fix possible race condition 2017-08-24 11:03:34 +02:00
rtc-r7301.c rtc: add support for EPSON TOYOCOM RTC-7301SF/DG 2016-12-19 00:59:21 +01:00
rtc-r9701.c
rtc-rc5t583.c
rtc-rk808.c
rtc-rp5c01.c
rtc-rs5c313.c
rtc-rs5c348.c
rtc-rs5c372.c rtc: rs5c372: Add OF device ID table 2017-03-09 01:29:34 +01:00
rtc-rtd119x.c rtc: Add Realtek RTD1295 2017-09-05 09:55:02 +02:00
rtc-rv3029c2.c rtc: constify i2c_device_id 2017-09-01 01:10:11 +02:00
rtc-rv8803.c rtc: rv8803: remove rv8803_remove 2017-07-07 13:14:19 +02:00
rtc-rx4581.c
rtc-rx6110.c
rtc-rx8010.c rtc: rx8010: Add OF device ID table 2017-03-09 01:29:21 +01:00
rtc-rx8025.c
rtc-rx8581.c rtc: rx8581: Add OF device ID table 2017-03-09 01:29:31 +01:00
rtc-s3c.c rtc: s3c: Handle clock enable failures 2017-06-24 22:52:43 +02:00
rtc-s3c.h
rtc-s5m.c
rtc-s35390a.c rtc: s35390a: implement ioctls 2017-07-31 00:17:44 +02:00
rtc-sa1100.c rtc: sa1100: make alarms useful 2017-08-24 11:03:35 +02:00
rtc-sa1100.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
rtc-sh.c rtc: sh: mark PM functions as unused 2017-04-22 18:01:15 +02:00
rtc-sirfsoc.c
rtc-snvs.c rtc: snvs: fix an incorrect check of return value 2017-05-05 00:43:50 +02:00
rtc-spear.c
rtc-st-lpc.c rtc: st-lpc: make it robust against y2038/2106 bug 2017-07-09 22:32:18 +02:00
rtc-starfire.c rtc: sparc: make starfire explicitly non-modular 2016-11-04 23:31:31 +01:00
rtc-stk17ta8.c
rtc-stm32.c rtc: stm32: add STM32H7 RTC support 2017-07-06 22:52:54 +02:00
rtc-stmp3xxx.c
rtc-sun4v.c rtc: sparc: make sun4v explicitly non-modular 2016-11-04 23:31:33 +01:00
rtc-sun6i.c rtc: sun6i: Add support for the external oscillator gate 2017-09-01 01:10:19 +02:00
rtc-sunxi.c
rtc-sysfs.c rtc: sysfs: make name uniform 2017-06-03 10:52:04 +02:00
rtc-tegra.c rtc: tegra: Implement clock handling 2017-01-23 00:46:55 +01:00
rtc-test.c
rtc-tile.c
rtc-tps6586x.c
rtc-tps65910.c rtc: tps65910: use 'unsigned int' instead of 'unsigned' in arguments 2017-01-10 01:32:51 +01:00
rtc-tps80031.c
rtc-twl.c mfd: twl: Move header file out of I2C realm 2017-09-04 14:41:02 +01:00
rtc-tx4939.c
rtc-v3020.c
rtc-vr41xx.c rtc: vr41xx: make alarms useful 2017-08-24 11:03:36 +02:00
rtc-vt8500.c
rtc-wm831x.c
rtc-wm8350.c rtc: wm8350: Remove unused to_wm8350_from_rtc_dev 2017-03-16 22:59:54 +01:00
rtc-x1205.c
rtc-xgene.c
rtc-zynqmp.c
systohc.c rtc: Allow rtc drivers to specify the tv_nsec value for ntp 2017-10-30 15:03:24 -07:00