Skip to contentSkip to conversation

power: supply: charger-manager: 1 fix and 2 minor clean-ups

PATCH In review Christophe JAILLET 3 participants 8 messages Aug 6, 2017 – Aug 12, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover power: supply: charger-manager: 1 fix and 2 minor clean-ups

This patch series improves 'charger_manager_probe()'. The first patch is about a missing memory allocation failure test.
The 2 others are just things spotted in this function:
- a comment that is no more up to date
- a style issue (which saves 1 line)

Christophe JAILLET (3):
power: supply: charger-manager: Fix a NULL pointer dereference in
'charger_manager_probe()'
power: supply: charger-manager: Fix a comment
power: supply: charger-manager: Slighly simplify code

 drivers/power/supply/charger-manager.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/3 power: supply: charger-manager: Fix a NULL pointer dereference in 'charger_manager_probe()'

'devm_kzalloc()' can return NULL. Return -ENOMEM in this case in order to avoid a NULL pointer dereference later on.

---
 drivers/power/supply/charger-manager.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/power/supply/charger-manager.c +2 -0
index adc3761831e1..52a49857e20f 100644
@@ -1645,6 +1645,8 @@ static int charger_manager_probe(struct platform_device *pdev)
 	/* Initialize alarm timer */
 	if (alarmtimer_get_rtcdev()) {
 		cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
+		if (!cm_timer)
+			return -ENOMEM;
 		alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
 	}
 
Signature · 1 line
2.11.0
#2 lore raw

PATCH 2/3 power: supply: charger-manager: Fix a comment

In reply to Christophe JAILLET

Update a comment which is no more up to date since commit 2ed9e9b653095.

---
 drivers/power/supply/charger-manager.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/supply/charger-manager.c +2 -2
index 52a49857e20f..83c08c8e2c32 100644
@@ -1651,8 +1651,8 @@ static int charger_manager_probe(struct platform_device *pdev)
 	}
 
 	/*
-	 * The following two do not need to be errors.
-	 * Users may intentionally ignore those two features.
+	 * Some of the following do not need to be errors.
+	 * Users may intentionally ignore those features.
 	 */
 	if (desc->fullbatt_uV = 0) {
 		dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Signature · 1 line
2.11.0
#3 lore raw

PATCH 3/3 power: supply: charger-manager: Slighly simplify code

In reply to Christophe JAILLET

Use 'sizeof(*var)' instead of the equivalent 'sizeof(data structure type)' because it is less verbose.

---
 drivers/power/supply/charger-manager.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/charger-manager.c +1 -2
index 83c08c8e2c32..6502fa7c2106 100644
@@ -1632,8 +1632,7 @@ static int charger_manager_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	cm = devm_kzalloc(&pdev->dev,
-			sizeof(struct charger_manager),	GFP_KERNEL);
+	cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL);
 	if (!cm)
 		return -ENOMEM;
 
Signature · 1 line
2.11.0
#4 lore raw

PATCH 1/1 scsi: mpt3sas: Fix memory allocation failure test in 'mpt3sas_base_attach()'

In reply to Christophe JAILLET

In the lines above this test, 8 'kzalloc' are performed, but only 7 results are tested.

Add the missing one (i.e. '!ioc->port_enable_cmds.reply').

---
 drivers/scsi/mpt3sas/mpt3sas_base.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/mpt3sas/mpt3sas_base.c +4 -4
index 1a5b6e40fb5c..8a44636ab0b5 100644
@@ -5494,10 +5494,10 @@ mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
 	mutex_init(&ioc->ctl_cmds.mutex);
 
-	if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
-	    !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
-	    !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
-	    !ioc->ctl_cmds.sense) {
+	if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
+	    !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
+	    !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
+	    !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
 		r = -ENOMEM;
 		goto out_free_resources;
 	}
Signature · 1 line
2.11.0
#5 lore raw

Re: [PATCH] scsi: mpt3sas: Fix memory allocation failure test in 'mpt3sas_base_attach()'

Am 07.08.2017 00:51, schrieb Christophe JAILLET:

Quoted 19 lines

In the lines above this test, 8 'kzalloc' are performed, but only 7 results are tested.

Add the missing one (i.e. '!ioc->port_enable_cmds.reply').

---
 drivers/scsi/mpt3sas/mpt3sas_base.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/mpt3sas/mpt3sas_base.c +0 -1
index 1a5b6e40fb5c..8a44636ab0b5 100644
@@ -5494,10 +5494,10 @@ mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
 	mutex_init(&ioc->ctl_cmds.mutex);
 
-	if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||

drivers/scsi/mpt3sas/mpt3sas_base.c mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc) · line 5503

Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +4 -3
-	    !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
-	    !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
-	    !ioc->ctl_cmds.sense) {
+	if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
+	    !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
+	    !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
+	    !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
 		r = -ENOMEM;
 		goto out_free_resources;
 	}

obviously it is better to follow the pattern "malloc() , check". Even the programmer lost track.

Bonus points if you malloc the buffers in one step.

just my 2 cents,

re,
wh

#6 lore raw

Re: [PATCH] scsi: mpt3sas: Fix memory allocation failure test in 'mpt3sas_base_attach()'

Quoted 20 lines · walter harms

Am 07.08.2017 00:51, schrieb Christophe JAILLET:

Quoted 18 lines

In the lines above this test, 8 'kzalloc' are performed, but only 7 results are tested.

Add the missing one (i.e. '!ioc->port_enable_cmds.reply').

---
  drivers/scsi/mpt3sas/mpt3sas_base.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/mpt3sas/mpt3sas_base.c +0 -0
index 1a5b6e40fb5c..8a44636ab0b5 100644
@@ -5494,10 +5494,10 @@ mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
  	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  	mutex_init(&ioc->ctl_cmds.mutex);
 
Quoted 13 lines

drivers/scsi/mpt3sas/mpt3sas_base.c mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc) · line 5503

Quoted 11 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +4 -4
-	if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
-	    !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
-	    !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
-	    !ioc->ctl_cmds.sense) {
+	if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
+	    !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
+	    !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
+	    !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
  		r = -ENOMEM;
  		goto out_free_resources;
  	}

obviously it is better to follow the pattern "malloc() , check".

Agreed, but it is also more verbose. Leavig it as-is, is IMHO, good enough.

Quoted 3 lines

Even the programmer lost track.

Bonus points if you malloc the buffers in one step.

Most of the allocation are 'kzalloc(ioc->reply_sz, GFP_KERNEL);', so a kcalloc could be used instead.
However, the 'kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);' breaks this logic and allocating all at once would lead to spaghetti code for no reason.

Moreover, I don't have any idea how big can be 'ioc->reply_sz', even if I guess it should be small.
So allocating all at once, could fail where several steps would work.

So I won't play for the bonus points :).

Best regards.
CJ

Quoted 7 lines

just my 2 cents,

re,
wh

#7 lore raw
In reply to Christophe JAILLET

Hi,

Quoted 6 lines · Christophe JAILLET

This patch series improves 'charger_manager_probe()'. The first patch is about a missing memory allocation failure test.
The 2 others are just things spotted in this function:
- a comment that is no more up to date
- a style issue (which saves 1 line)

Quoted 8 lines

Christophe JAILLET (3):
power: supply: charger-manager: Fix a NULL pointer dereference in
'charger_manager_probe()'
power: supply: charger-manager: Fix a comment
power: supply: charger-manager: Slighly simplify code

 drivers/power/supply/charger-manager.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Thanks, queued.

-- Sebastian