Message ID | 20241203-const_dfc_done-v2-0-7436a98c497f@quicinc.com |
---|---|
Headers | show |
Series | driver core: Constify API device_find_child() and adapt for various existing usages | expand |
On 2024-12-03 08:58:26-0500, James Bottomley wrote: > On Tue, 2024-12-03 at 21:02 +0800, Zijun Hu wrote: > > On 2024/12/3 20:41, Greg Kroah-Hartman wrote: > > > On Tue, Dec 03, 2024 at 08:23:45PM +0800, Zijun Hu wrote: > [...] > > > > or squash such patch series into a single patch ? > > > > > > > > various subsystem maintainers may not like squashing way. > > > > > > Agreed, so look into either doing it in a bisectable way if at all > > > possible. As I don't see a full series here, I can't suggest how > > > it needs to happen :( > > > > > > > let me send you a full series later and discuss how to solve this > > issue. > > It's only slightly more complex than what we normally do: modify all > instances and then change the API. In this case you have an additional > problem because the prototype "const void *" will cause a mismatch if a > function has "void *". The easiest way to solve this is probably to > make device_find_child a macro that coerces its function argument to > having a non const "void *" and then passes off to the real function. > If you do that in the first patch, then you can constify all the > consumers and finally remove the macro coercion in the last patch. Casting function pointers like that should be detected and trapped by control flow integrity checking (KCFI). Another possibility would be to use a macro and _Generic to dispatch to two different backing functions. See __BIN_ATTR() in include/linux/sysfs.h for an inspiration. This also enables an incremental migration. Thomas
On 2024/12/3 22:07, Thomas Weißschuh wrote: > On 2024-12-03 08:58:26-0500, James Bottomley wrote: >> On Tue, 2024-12-03 at 21:02 +0800, Zijun Hu wrote: >>> On 2024/12/3 20:41, Greg Kroah-Hartman wrote: >>>> On Tue, Dec 03, 2024 at 08:23:45PM +0800, Zijun Hu wrote: >> [...] >>>>> or squash such patch series into a single patch ? >>>>> >>>>> various subsystem maintainers may not like squashing way. >>>> >>>> Agreed, so look into either doing it in a bisectable way if at all >>>> possible. As I don't see a full series here, I can't suggest how >>>> it needs to happen :( >>>> >>> >>> let me send you a full series later and discuss how to solve this >>> issue. >> >> It's only slightly more complex than what we normally do: modify all >> instances and then change the API. In this case you have an additional >> problem because the prototype "const void *" will cause a mismatch if a >> function has "void *". The easiest way to solve this is probably to >> make device_find_child a macro that coerces its function argument to >> having a non const "void *" and then passes off to the real function. >> If you do that in the first patch, then you can constify all the >> consumers and finally remove the macro coercion in the last patch. > > Casting function pointers like that should be detected and trapped by > control flow integrity checking (KCFI). > > Another possibility would be to use a macro and _Generic to dispatch to > two different backing functions. See __BIN_ATTR() in > include/linux/sysfs.h for an inspiration. this way may fix building error issue but does not achieve our purpose. our purpose is that there are only constified device_find_child(). > This also enables an incremental migration. > > change the API prototype from: device_find_child(..., void *data_0, int (*match)(struct device *dev, void *data)); to: device_find_child(..., const void *data_0, int (*match)(struct device *dev, const void *data)); For @data_0, void * -> const void * is okay. but for @match, the problem is function pointer type incompatibility. there are two solutions base on discussions. 1) squashing likewise Greg mentioned. Do all of the "prep work" first, and then do the const change at the very end, all at once. 2) as changing platform_driver's remove() prototype. Commit: e70140ba0d2b ("Get rid of 'remove_new' relic from platform driver struct") introduce extra device_find_child_new() which is constified -> use *_new() replace ALL device_find_child() instances one by one -> remove device_find_child() -> rename *_new() to device_find_child() once. > Thomas
On 2024/12/3 23:34, James Bottomley wrote: >>> This also enables an incremental migration. >> change the API prototype from: >> device_find_child(..., void *data_0, int (*match)(struct device *dev, >> void *data)); >> >> to: >> device_find_child(..., const void *data_0, int (*match)(struct device >> *dev, const void *data)); >> >> For @data_0, void * -> const void * is okay. >> but for @match, the problem is function pointer type incompatibility. >> >> there are two solutions base on discussions. >> >> 1) squashing likewise Greg mentioned. >> Do all of the "prep work" first, and then >> do the const change at the very end, all at once. >> >> 2) as changing platform_driver's remove() prototype. >> Commit: e70140ba0d2b ("Get rid of 'remove_new' relic from platform >> driver struct") >> >> introduce extra device_find_child_new() which is constified -> use >> *_new() replace ALL device_find_child() instances one by one -> >> remove device_find_child() -> rename *_new() to device_find_child() >> once. > Why bother with the last step, which churns the entire code base again? keep the good API name device_find_child(). > Why not call the new function device_find_child_const() and simply keep > it (it's descriptive of its function). That way you can have a patch > series without merging and at the end simply remove the old function. device_find_child is a good name for the API, 'find' already means const.
On Wed, 2024-12-04 at 20:26 +0800, Zijun Hu wrote: > On 2024/12/3 23:34, James Bottomley wrote: > > > > This also enables an incremental migration. > > > change the API prototype from: > > > device_find_child(..., void *data_0, int (*match)(struct device > > > *dev, void *data)); > > > > > > to: > > > device_find_child(..., const void *data_0, int (*match)(struct > > > device *dev, const void *data)); > > > > > > For @data_0, void * -> const void * is okay. > > > but for @match, the problem is function pointer type > > > incompatibility. > > > > > > there are two solutions base on discussions. > > > > > > 1) squashing likewise Greg mentioned. > > > Do all of the "prep work" first, and then > > > do the const change at the very end, all at once. > > > > > > 2) as changing platform_driver's remove() prototype. > > > Commit: e70140ba0d2b ("Get rid of 'remove_new' relic from > > > platform driver struct") > > > > > > introduce extra device_find_child_new() which is constified -> > > > use *_new() replace ALL device_find_child() instances one by one > > > -> remove device_find_child() -> rename *_new() to > > > device_find_child() once. > > Why bother with the last step, which churns the entire code base > > again? > > keep the good API name device_find_child(). Well, I think it's a good opportunity to rename the API better, but if that's the goal, you can still do it with _Generic() without churning the code base a second time. The example is in slab.h:kmem_cache_create > > Why not call the new function device_find_child_const() and simply > > keep it (it's descriptive of its function). That way you can have > > a patch series without merging and at the end simply remove the old > > function. > > device_find_child is a good name for the API, 'find' already means > const. Not to me it doesn't, but that's actually not what I think is wrong with the API name: it actually only returns the first match, so I'd marginally prefer it to be called device_find_first_child() ... not enough to churn the code to change it, but since you're doing that anyway it might make sense as an update. Regards, James
On 2024/12/5 00:42, James Bottomley wrote: >>>> introduce extra device_find_child_new() which is constified -> >>>> use *_new() replace ALL device_find_child() instances one by one >>>> -> remove device_find_child() -> rename *_new() to >>>> device_find_child() once. >>> Why bother with the last step, which churns the entire code base >>> again? >> keep the good API name device_find_child(). > Well, I think it's a good opportunity to rename the API better, but if > that's the goal, you can still do it with _Generic() without churning > the code base a second time. The example is in > slab.h:kmem_cache_create > i understand these solutions _Generic()/_new/squashing. every solutions have its advantages and disadvantages. i decide to use squashing solution for this concrete scenario after some considerations since: 1) it has the minimal patch count to achieve target. 2) every patch is valuable, but other solutions needs to undo beginning patch finally. 3) for the squashing patch, i will only make the least and simplest changes for various match functions, that will compensate its disadvantages. >>> Why not call the new function device_find_child_const() and simply >>> keep it (it's descriptive of its function). That way you can have >>> a patch series without merging and at the end simply remove the old >>> function. >> device_find_child is a good name for the API, 'find' already means >> const. > Not to me it doesn't, but that's actually not what I think is wrong > with the API name: it actually only returns the first match, so I'd > marginally prefer it to be called device_find_first_child() ... not > enough to churn the code to change it, but since you're doing that > anyway it might make sense as an update. name device_find_child appeared 18 years ago, it is good to keep this name developers known about. the API only return one device *, it should be obvious that it is the first device which meet matching condition. Other device finding APIs (bus|class|driver)_find_device() does not have concern about 'first' So, IMO, current name is good.
This patch series is to constify the following API: struct device *device_find_child(struct device *dev, void *data, int (*match)(struct device *dev, void *data)); To : struct device *device_find_child(struct device *dev, const void *data, device_match_t match); typedef int (*device_match_t)(struct device *dev, const void *data); Why to constify the API? - Protect caller's match data @*data which is for comparison and lookup and the API does not actually need to modify @*data. - Make the API's parameters (@match)() and @data have the same type as all of other device finding APIs (bus|class|driver)_find_device(). - All kinds of existing device matching functions can be directly taken as the API's argument, they were exported by driver core. How to constify the API? - Now, no (@match)() argument of the API usages is modifying its match data @*data after previous cleanup, so it is easy and safe to make its parameter @data take const void * as type. - Simplify involved codes further if it is possbile with benefits brought by constifying the API. Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com> --- Changes in v2: - Series v1 have no code review comments and are posted a long time ago, so may ignore differences. - Link to v1: https://lore.kernel.org/r/20240811-const_dfc_done-v1-0-9d85e3f943cb@quicinc.com - Motivation link: https://lore.kernel.org/lkml/917359cc-a421-41dd-93f4-d28937fe2325@icloud.com --- Zijun Hu (32): driver core: Constify API device_find_child() driver core: Introduce device_match_type() to match device with a device type drm/mediatek: Adapt for constified device_find_child() hwmon: Adapt for constified device_find_child() media: pci: mgb4: Adapt for constified device_find_child() thunderbolt: Adapt for constified device_find_child() gpio: sim: Remove gpio_sim_dev_match_fwnode() net: dsa: Adapt for constified device_find_child() pwm: Adapt for constified device_find_child() nvdimm: Adapt for constified device_find_child() libnvdimm: Simplify nd_namespace_store() implementation firewire: core: Adapt for constified device_find_child() serial: core: Adapt for constified device_find_child() usb: typec: class: Remove both cable_match() and partner_match() usb: typec: class: Adapt for constified device_find_child() slimbus: core: Simplify of_find_slim_device() implementation slimbus: core: Constify slim_eaddr_equal() slimbus: core: Adapt for constified device_find_child() scsi: iscsi: Constify API iscsi_find_flashnode_sess() scsi: qla4xxx: Adapt for constified iscsi_find_flashnode_sess() scsi: iscsi: Adapt for constified device_find_child() cxl/region: Adapt for constified device_find_child() cxl/pmem: Remove match_nvdimm_bridge() cxl/core/pci: Adapt for constified device_find_child() cxl/test: Adapt for constified device_find_child() sparc: vio: Adapt for constified device_find_child() bus: fsl-mc: Adapt for constified device_find_child() block: sunvdc: Adapt for constified device_find_child() firmware: arm_scmi: Adapt for constified device_find_child() efi: dev-path-parser: Adapt for constified device_find_child() rpmsg: core: Adapt for constified device_find_child() driver core: Simplify API device_find_child_by_name() implementation arch/sparc/kernel/vio.c | 6 +++--- drivers/base/core.c | 30 ++++++++++-------------------- drivers/block/sunvdc.c | 6 +++--- drivers/bus/fsl-mc/dprc-driver.c | 6 +++--- drivers/cxl/core/pci.c | 4 ++-- drivers/cxl/core/pmem.c | 9 +++------ drivers/cxl/core/region.c | 21 ++++++++++++--------- drivers/firewire/core-device.c | 4 ++-- drivers/firmware/arm_scmi/bus.c | 4 ++-- drivers/firmware/efi/dev-path-parser.c | 4 ++-- drivers/gpio/gpio-sim.c | 7 +------ drivers/gpu/drm/mediatek/mtk_drm_drv.c | 2 +- drivers/hwmon/hwmon.c | 2 +- drivers/media/pci/mgb4/mgb4_core.c | 4 ++-- drivers/nvdimm/bus.c | 2 +- drivers/nvdimm/claim.c | 9 +-------- drivers/pwm/core.c | 2 +- drivers/rpmsg/rpmsg_core.c | 4 ++-- drivers/scsi/qla4xxx/ql4_os.c | 3 ++- drivers/scsi/scsi_transport_iscsi.c | 10 +++++----- drivers/slimbus/core.c | 17 +++++------------ drivers/thunderbolt/retimer.c | 2 +- drivers/thunderbolt/xdomain.c | 2 +- drivers/tty/serial/serial_core.c | 4 ++-- drivers/usb/typec/class.c | 31 ++++++++++++++----------------- include/linux/device.h | 4 ++-- include/linux/device/bus.h | 1 + include/scsi/scsi_transport_iscsi.h | 4 ++-- net/dsa/dsa.c | 2 +- tools/testing/cxl/test/cxl.c | 2 +- 30 files changed, 89 insertions(+), 119 deletions(-) --- base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37 change-id: 20241201-const_dfc_done-aaec71e3bbea Best regards,