Remove use of i2c_match_id in PMBUS
v2 cover Remove use of i2c_match_id in PMBUS
Hello all,
Much like we already have done for HWMON top level drivers, we are removing the i2c_match_id() function from all hwmon/pmbus/ drivers now. Using i2c_get_match_data() can simplify code and has some other benefits described in the patches.
I don't have most of this hardware, so testing is very welcome :)
Thanks,
Andrew
Changes for v2:
- Moved enum value [2/11] to prevent 0 from matching valid device
- Trimmed all commit messages to only patch relevant info
Andrew Davis (11):
hwmon: (pmbus/bel-pfe) Remove use of i2c_match_id()
hwmon: (pmbus/ibm-cffps) Remove use of i2c_match_id()
hwmon: (pmbus/isl68137) Remove use of i2c_match_id()
hwmon: (pmbus/max20730) Remove use of i2c_match_id()
hwmon: (pmbus/max34440) Remove use of i2c_match_id()
hwmon: (pmbus) Remove use of i2c_match_id()
hwmon: (pmbus/q54sj108a2) Remove use of i2c_match_id()
hwmon: (pmbus/tps53679) Remove use of i2c_match_id()
hwmon: (pmbus/fsp-3y) Remove use of i2c_match_id()
hwmon: (pmbus/ltc2978) Remove use of i2c_match_id()
hwmon: (pmbus/max16601) Remove use of i2c_match_id()
drivers/hwmon/pmbus/bel-pfe.c | 5 +---- drivers/hwmon/pmbus/fsp-3y.c | 13 ++++++------- drivers/hwmon/pmbus/ibm-cffps.c | 16 +++------------- drivers/hwmon/pmbus/isl68137.c | 4 +--- drivers/hwmon/pmbus/ltc2978.c | 2 +- drivers/hwmon/pmbus/max16601.c | 2 +- drivers/hwmon/pmbus/max20730.c | 5 +---- drivers/hwmon/pmbus/max34440.c | 4 +--- drivers/hwmon/pmbus/pmbus.c | 4 +--- drivers/hwmon/pmbus/q54sj108a2.c | 5 +---- drivers/hwmon/pmbus/tps53679.c | 5 +---- 11 files changed, 18 insertions(+), 47 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/11 hwmon: (pmbus/bel-pfe) Remove use of i2c_match_id()
The function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It doesn't need the i2c_device_id passed in so we do not need to have that forward declared, allowing us to remove that.
--- drivers/hwmon/pmbus/bel-pfe.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/hwmon/pmbus/bel-pfe.c +1 -4
@@ -88,13 +88,10 @@ static struct pmbus_driver_info pfe_driver_info[] = { }, }; -static const struct i2c_device_id pfe_device_id[]; -static int pfe_pmbus_probe (struct i2c_client *client) { -int model; +int model = (uintptr_t )i2c_get_match_data (client); - model = (int )i2c_match_id (pfe_device_id, client)->driver_data; client->dev.platform_data = &pfe_plat_data;/*
Signature · 1 line
2.39.2
PATCH v2 2/11 hwmon: (pmbus/ibm-cffps) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It also checks for device match data, which allows for OF based probing. That means we do not have to manually check those first and can remove that check.
As i2c_get_match_data() return NULL/0 on failure which also matches the enum for "cffps1", switch around the enum order so cffps_unknown is index 0 and existing behavior is preserved.
--- drivers/hwmon/pmbus/ibm-cffps.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/hwmon/pmbus/ibm-cffps.c +3 -13
@@ -58,7 +58,7 @@ enum {CFFPS_DEBUGFS_NUM_ENTRIES }; -enum versions { cffps1, cffps2, cffps_unknown }; +enum versions { cffps_unknown, cffps1, cffps2 };struct ibm_cffps {enum versions version;
@@ -482,19 +482,9 @@ MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);static int ibm_cffps_probe (struct i2c_client *client) {int i, rc; -enum versions vs = cffps_unknown; +enum versions vs = (uintptr_t )i2c_get_match_data (client);struct dentry *debugfs;struct ibm_cffps *psu; -const void *md =of_device_get_match_data (&client->dev); -const struct i2c_device_id *id; - -if (md) { - vs = (uintptr_t )md; - }else { - id =i2c_match_id (ibm_cffps_id, client); -if (id) - vs = (enum versions )id->driver_data; - }if (vs == cffps_unknown) {u16 ccin_revision =0 ;
@@ -534,7 +524,7 @@ static int ibm_cffps_probe(struct i2c_client *client) }/* Set the client name to include the version number. */ -snprintf (client->name,I2C_NAME_SIZE ,"cffps%d" , vs +1 ); +snprintf (client->name,I2C_NAME_SIZE ,"cffps%d" , vs); } client->dev.platform_data = &ibm_cffps_pdata;
Signature · 1 line
2.39.2
PATCH v2 3/11 hwmon: (pmbus/isl68137) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has a couple other benefits:
* It doesn't need the i2c_device_id passed in so we do not need to have that forward declared, allowing us to remove that.
* It also checks for device match data, which allows for OF and ACPI based probing.
--- drivers/hwmon/pmbus/isl68137.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwmon/pmbus/isl68137.c +1 -3
@@ -90,8 +90,6 @@ struct isl68137_data {#define to_isl68137_data (x) container_of(x, struct isl68137_data, info) -static const struct i2c_device_id raa_dmpvr_id[]; -static ssize_t isl68137_avs_enable_show_page (struct i2c_client *client,int page,char *buf)
@@ -375,7 +373,7 @@ static int isl68137_probe(struct i2c_client *client)memcpy (&data->info, &raa_dmpvr_info,sizeof (data->info)); info = &data->info; -switch (i2c_match_id (raa_dmpvr_id, client)->driver_data) { +switch ((uintptr_t )i2c_get_match_data (client)) {case raa_dmpvr1_2rail: info->pages =2 ; info->R[PSC_VOLTAGE_IN ] =3 ;
Signature · 1 line
2.39.2
PATCH v2 4/11 hwmon: (pmbus/max20730) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It also checks for device match data. That means we do not have to manually check that first.
--- drivers/hwmon/pmbus/max20730.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/hwmon/pmbus/max20730.c +1 -4
@@ -715,10 +715,7 @@ static int max20730_probe(struct i2c_client *client)return -ENODEV ; } -if (client->dev.of_node) - chip_id = (uintptr_t )of_device_get_match_data (dev); -else - chip_id =i2c_match_id (max20730_id, client)->driver_data; + chip_id = (uintptr_t )i2c_get_match_data (client); data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL );if (!data)
Signature · 1 line
2.39.2
PATCH v2 5/11 hwmon: (pmbus/max34440) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It doesn't need the i2c_device_id passed in so we do not need to have that forward declared, allowing us to remove that.
--- drivers/hwmon/pmbus/max34440.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwmon/pmbus/max34440.c +1 -3
@@ -71,8 +71,6 @@ struct max34440_data {#define to_max34440_data (x) container_of(x, struct max34440_data, info) -static const struct i2c_device_id max34440_id[]; -static int max34440_read_word_data (struct i2c_client *client,int page,int phase,int reg) {
@@ -628,7 +626,7 @@ static int max34440_probe(struct i2c_client *client) GFP_KERNEL);if (!data)return -ENOMEM ; - data->id =i2c_match_id (max34440_id, client)->driver_data; + data->id = (uintptr_t )i2c_get_match_data (client); data->info = max34440_info[data->id]; data->iout_oc_fault_limit =MAX34440_IOUT_OC_FAULT_LIMIT ; data->iout_oc_warn_limit =MAX34440_IOUT_OC_WARN_LIMIT ;
Signature · 1 line
2.39.2
PATCH v2 6/11 hwmon: (pmbus) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It doesn't need the i2c_device_id passed in so we do not need to have that forward declared, allowing us to remove that.
--- drivers/hwmon/pmbus/pmbus.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwmon/pmbus/pmbus.c +1 -3
@@ -20,8 +20,6 @@ struct pmbus_device_info {u32 flags; }; -static const struct i2c_device_id pmbus_id[]; -/* * Find sensor groups and status registers on each page. */
@@ -174,7 +172,7 @@ static int pmbus_probe(struct i2c_client *client)if (!info)return -ENOMEM ; - device_info = (struct pmbus_device_info *)i2c_match_id (pmbus_id, client)->driver_data; + device_info = (struct pmbus_device_info *)i2c_get_match_data (client);if (device_info->flags) { pdata =devm_kzalloc (dev,sizeof (struct pmbus_platform_data ),GFP_KERNEL );
Signature · 1 line
2.39.2
PATCH v2 7/11 hwmon: (pmbus/q54sj108a2) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It also checks for device match data, which means we do not have to manually check that first.
--- drivers/hwmon/pmbus/q54sj108a2.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/hwmon/pmbus/q54sj108a2.c +1 -4
@@ -291,10 +291,7 @@ static int q54sj108a2_probe(struct i2c_client *client) I2C_FUNC_SMBUS_BLOCK_DATA))return -ENODEV ; -if (client->dev.of_node) - chip_id = (enum chips )(unsigned long )of_device_get_match_data (dev); -else - chip_id =i2c_match_id (q54sj108a2_id, client)->driver_data; + chip_id = (enum chips )(uintptr_t )i2c_get_match_data (client); ret =i2c_smbus_read_block_data (client,PMBUS_MFR_ID , buf);if (ret <0 ) {
Signature · 1 line
2.39.2
PATCH v2 8/11 hwmon: (pmbus/tps53679) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This is often used to then retrieve the matching driver_data. This can be done in one step with the helper i2c_get_match_data().
This helper has another benefit:
* It also checks for device match data, which means we do not have to manually check that first.
--- drivers/hwmon/pmbus/tps53679.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/hwmon/pmbus/tps53679.c +1 -4
@@ -253,10 +253,7 @@ static int tps53679_probe(struct i2c_client *client)struct pmbus_driver_info *info;enum chips chip_id; -if (dev->of_node) - chip_id = (uintptr_t )of_device_get_match_data (dev); -else - chip_id =i2c_match_id (tps53679_id, client)->driver_data; + chip_id = (uintptr_t )i2c_get_match_data (client); info =devm_kmemdup (dev, &tps53679_info,sizeof (*info),GFP_KERNEL );if (!info)
Signature · 1 line
2.39.2
PATCH v2 9/11 hwmon: (pmbus/fsp-3y) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This can be done instead with i2c_client_get_device_id() which doesn't need the i2c_device_id passed in so we do not need to have that forward declared, allowing us to move the i2c_device_id table down to its more natural spot with the other module info.
--- drivers/hwmon/pmbus/fsp-3y.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/hwmon/pmbus/fsp-3y.c +6 -7
@@ -222,12 +222,6 @@ static int fsp3y_detect(struct i2c_client *client)return -ENODEV ; } -static const struct i2c_device_id fsp3y_id[] = { - {"ym2151e" , ym2151e}, - {"yh5151e" , yh5151e}, - { } -}; -static int fsp3y_probe (struct i2c_client *client) {struct fsp3y_data *data;
@@ -242,7 +236,7 @@ static int fsp3y_probe(struct i2c_client *client)if (data->chip <0 )return data->chip; - id =i2c_match_id (fsp3y_id, client); + id =i2c_client_get_device_id (client);if (data->chip != id->driver_data)dev_warn (&client->dev,"Device mismatch: Configured %s (%d), detected %d\n" , id->name, (int )id->driver_data, data->chip);
@@ -276,6 +270,11 @@ static int fsp3y_probe(struct i2c_client *client)return pmbus_do_probe (client, &data->info); } +static const struct i2c_device_id fsp3y_id[] = { + {"ym2151e" , ym2151e}, + {"yh5151e" , yh5151e}, + { } +};MODULE_DEVICE_TABLE (i2c, fsp3y_id);static struct i2c_driver fsp3y_driver = {
Signature · 1 line
2.39.2
PATCH v2 10/11 hwmon: (pmbus/ltc2978) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This can instead be done with i2c_client_get_device_id(). For this driver functionality should not change. Switch over to remove the last couple users of the i2c_match_id() function from kernel.
--- drivers/hwmon/pmbus/ltc2978.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/pmbus/ltc2978.c +1 -1
@@ -733,7 +733,7 @@ static int ltc2978_probe(struct i2c_client *client)return chip_id; data->id = chip_id; - id =i2c_match_id (ltc2978_id, client); + id =i2c_client_get_device_id (client);if (data->id != id->driver_data) dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",
Signature · 1 line
2.39.2
Guenter Roeck
maintainerRe: [PATCH v2 10/11] hwmon: (pmbus/ltc2978) Remove use of i2c_match_id()
Quoted 16 lines · Andrew Davis
The function
i2c_match_id()is used to fetch the matching ID from thei2c_device_idtable. This can instead be done withi2c_client_get_device_id(). For this driver functionality should not change. Switch over to remove the last couple users of thei2c_match_id()function from kernel.Signed-off-by Andrew Davis <[email protected]>--- drivers/hwmon/pmbus/ltc2978.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwmon/pmbus/ltc2978.c +0
-0
drivers/hwmon/pmbus/ltc2978.c static int ltc2978_probe(struct i2c_client *client) · line 736
Quoted 5 lines
drivers/hwmon/pmbus/ltc2978.c +1
-1@@ -733,7 +733,7 @@ static int ltc2978_probe(struct i2c_client *client)return chip_id; data->id = chip_id; - id =i2c_match_id (ltc2978_id, client); + id =i2c_client_get_device_id (client);
AI feedback:
Is id guaranteed to be non-NULL here?
If the device is instantiated via ACPI PRP0001 or using a fallback DT compatible string where the first compatible string is not in the ltc2978_id table, i2c_client_get_device_id() will return NULL. This leads to a NULL pointer dereference when accessing id->driver_data.
While this vulnerability existed in the old code with i2c_match_id(), adding a NULL check here might be a good idea while the code is being refactored.
I never know if this is real. Any idea ?
Thanks,
Guenter
Quoted 6 lines
drivers/hwmon/pmbus/ltc2978.c +0
-0if (data->id != id->driver_data) dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",Signature · 1 line
2.39.2
Re: [PATCH v2 10/11] hwmon: (pmbus/ltc2978) Remove use of i2c_match_id()
Quoted 32 lines · Guenter Roeck
Quoted 16 lines · Andrew Davis
The function
i2c_match_id()is used to fetch the matching ID from thei2c_device_idtable. This can instead be done withi2c_client_get_device_id(). For this driver functionality should not change. Switch over to remove the last couple users of thei2c_match_id()function from kernel.Signed-off-by Andrew Davis <[email protected]>--- drivers/hwmon/pmbus/ltc2978.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwmon/pmbus/ltc2978.c +0
-0drivers/hwmon/pmbus/ltc2978.c static int ltc2978_probe(struct i2c_client *client) · line 736
Quoted 5 lines
drivers/hwmon/pmbus/ltc2978.c +1
-1@@ -733,7 +733,7 @@ static int ltc2978_probe(struct i2c_client *client)return chip_id; data->id = chip_id; - id =i2c_match_id (ltc2978_id, client); + id =i2c_client_get_device_id (client);AI feedback:
Is
idguaranteed to be non-NULL here?If the device is instantiated via ACPI
PRP0001or using a fallback DT compatible string where the first compatible string is not in theltc2978_idtable,i2c_client_get_device_id()will returnNULL. This leads to a NULL pointer dereference when accessingid->driver_data.
Quoted 6 lines
While this vulnerability existed in the old code with
i2c_match_id(), adding a NULL check here might be a good idea while the code is being refactored.I never know if this is real. Any idea ?
The AI is right on both parts, the second being the important one that this was preexisting. i2c_match_id() should never return NULL in practice as we must have matched on something to have gotten probe()'d in the first place. And for the same reason i2c_client_get_device_id() shouldn't ever return NULL either. So no change here.
But I see how this would confuse an AI as both functions have a "return NULL;" statement in them. This problem of returning the matched ID in-band with returning 0 for errors (which could also be a valid ID) is one of the motivating reasons for me removing i2c_match_id(). Even though its replacement has the same issue, once we get everyone over to the same single API then switching that one API over to something safer becomes possible.
Andrew
Quoted 9 lines
Thanks,
GuenterQuoted 6 lines
drivers/hwmon/pmbus/ltc2978.c +0
-0if (data->id != id->driver_data) dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",Signature · 1 line
2.39.2
PATCH v2 11/11 hwmon: (pmbus/max16601) Remove use of i2c_match_id()
In reply to Andrew DavisThe function i2c_match_id() is used to fetch the matching ID from the i2c_device_id table. This can instead be done with i2c_client_get_device_id(). For this driver functionality should not change. Switch over to remove the last couple users of the i2c_match_id() function from kernel.
--- drivers/hwmon/pmbus/max16601.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/pmbus/max16601.c +1 -1
@@ -318,7 +318,7 @@ static int max16601_probe(struct i2c_client *client)if (chip_id <0 )return chip_id; - id =i2c_match_id (max16601_id, client); + id =i2c_client_get_device_id (client);if (chip_id != id->driver_data) dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",
Signature · 1 line
2.39.2
Guenter Roeck
maintainerQuoted 9 lines · Andrew Davis
Hello all,
Much like we already have done for HWMON top level drivers, we are removing the
i2c_match_id()function from all hwmon/pmbus/ drivers now. Usingi2c_get_match_data()can simplify code and has some other benefits described in the patches.I don't have most of this hardware, so testing is very welcome :)
It looks safe to me, so I'll apply the series. There is a potential pre-existing problem in the ltc2978 driver, but that is really unrelated to this series, and it is better to fix that separately anyway.
Thanks,
Guenter
Quoted 33 lines
Thanks,
AndrewChanges for v2:
- Moved enum value [2/11] to prevent 0 from matching valid device
- Trimmed all commit messages to only patch relevant infoAndrew Davis (11):
hwmon: (pmbus/bel-pfe) Remove use of i2c_match_id() hwmon: (pmbus/ibm-cffps) Remove use of i2c_match_id() hwmon: (pmbus/isl68137) Remove use of i2c_match_id() hwmon: (pmbus/max20730) Remove use of i2c_match_id() hwmon: (pmbus/max34440) Remove use of i2c_match_id() hwmon: (pmbus) Remove use of i2c_match_id() hwmon: (pmbus/q54sj108a2) Remove use of i2c_match_id() hwmon: (pmbus/tps53679) Remove use of i2c_match_id() hwmon: (pmbus/fsp-3y) Remove use of i2c_match_id() hwmon: (pmbus/ltc2978) Remove use of i2c_match_id() hwmon: (pmbus/max16601) Remove use of i2c_match_id()drivers/hwmon/pmbus/bel-pfe.c | 5 +---- drivers/hwmon/pmbus/fsp-3y.c | 13 ++++++------- drivers/hwmon/pmbus/ibm-cffps.c | 16 +++------------- drivers/hwmon/pmbus/isl68137.c | 4 +--- drivers/hwmon/pmbus/ltc2978.c | 2 +- drivers/hwmon/pmbus/max16601.c | 2 +- drivers/hwmon/pmbus/max20730.c | 5 +---- drivers/hwmon/pmbus/max34440.c | 4 +--- drivers/hwmon/pmbus/pmbus.c | 4 +--- drivers/hwmon/pmbus/q54sj108a2.c | 5 +---- drivers/hwmon/pmbus/tps53679.c | 5 +---- 11 files changed, 18 insertions(+), 47 deletions(-)