hwmon: (nct6775) Add support for weighted fan control
Subsystems (2)
Hello Guenter Roeck,
The patch bbd8decd4123: "hwmon: (nct6775) Add support for weighted fan control" from Dec 4, 2012, leads to the following static checker warning:
drivers/hwmon/nct6775.c:1562 nct6775_update_pwm() warn: dead code because of 'j == 1' and 'j < (56 / 8 + 0)'
drivers/hwmon/nct6775.c
1503 static void nct6775_update_pwm(struct device *dev) 1504 {
1505 struct nct6775_data *data = dev_get_drvdata(dev);
1506 int i, j;
1507 int fanmodecfg, reg;
1508 bool duty_is_dc;
1509
1510 for (i = 0; i < data->pwm_num; i++) {
1511 if (!(data->has_pwm & BIT(i)))
1512 continue;
1513
1514 duty_is_dc = data->REG_PWM_MODE[i] &&
1515 (nct6775_read_value(data, data->REG_PWM_MODE[i])
1516 & data->PWM_MODE_MASK[i]);
1517 data->pwm_mode[i] = !duty_is_dc;
1518
1519 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]);
1520 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^j is set to ARRAY_SIZE(data->REG_PWM) which is 7 at the end of this loop.
1521 if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
1522 data->pwm[j][i]
1523 = nct6775_read_value(data,
1524 data->REG_PWM[j][i]);
1525 }
1526 }
1527
1528 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
1529 (fanmodecfg >> 4) & 7);
1530
1531 if (!data->temp_tolerance[0][i] ||
1532 data->pwm_enable[i] != speed_cruise)
1533 data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
1534 if (!data->target_speed_tolerance[i] ||
1535 data->pwm_enable[i] == speed_cruise) {
1536 u8 t = fanmodecfg & 0x0f;
1537
1538 if (data->REG_TOLERANCE_H) {
1539 t |= (nct6775_read_value(data,
1540 data->REG_TOLERANCE_H[i]) & 0x70) >> 1;
1541 }
1542 data->target_speed_tolerance[i] = t;
1543 }
1544
1545 data->temp_tolerance[1][i] =
1546 nct6775_read_value(data,
1547 data->REG_CRITICAL_TEMP_TOLERANCE[i]);
1548
1549 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]);
1550 data->pwm_temp_sel[i] = reg & 0x1f;
1551 /* If fan can stop, report floor as 0 */
1552 if (reg & 0x80)
1553 data->pwm[2][i] = 0;
1554
1555 if (!data->REG_WEIGHT_TEMP_SEL[i])
1556 continue;
1557
1558 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]);
1559 data->pwm_weight_temp_sel[i] = reg & 0x1f;
1560 /* If weight is disabled, report weight source as 0 */
1561 if (j == 1 && !(reg & 0x80))
^^^^^^So it can't be 1 here. Did you intend to say "i == 1"?
1562 data->pwm_weight_temp_sel[i] = 0;
1563
1564 /* Weight temp data */
1565 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
1566 data->weight_temp[j][i]
1567 = nct6775_read_value(data,
1568 data->REG_WEIGHT_TEMP[j][i]);
1569 }
1570 }
1571 }regards,
dan carpenter
Guenter Roeck
maintainerHi Dan,
Quoted 75 lines · Dan Carpenter
Hello Guenter Roeck,
The patch
bbd8decd4123: "hwmon: (nct6775) Add support for weighted fan control" from Dec 4, 2012, leads to the following static checker warning:drivers/hwmon/nct6775.c:1562 nct6775_update_pwm() warn: dead code because of 'j == 1' and 'j < (56 / 8 + 0)'
drivers/hwmon/nct6775.c
1503 static voidnct6775_update_pwm(struct device *dev)1504 { 1505 struct nct6775_data *data = dev_get_drvdata(dev); 1506 int i, j; 1507 int fanmodecfg, reg; 1508 bool duty_is_dc; 1509 1510 for (i = 0; i < data->pwm_num; i++) { 1511 if (!(data->has_pwm & BIT(i))) 1512 continue; 1513 1514 duty_is_dc = data->REG_PWM_MODE[i] && 1515 (nct6775_read_value(data, data->REG_PWM_MODE[i]) 1516 & data->PWM_MODE_MASK[i]); 1517 data->pwm_mode[i] = !duty_is_dc; 1518 1519 fanmodecfg = nct6775_read_value(data, data->REG_FAN_MODE[i]); 1520 for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^j is set to
ARRAY_SIZE(data->REG_PWM)which is 7 at the end of this loop.1521 if (data->REG_PWM[j] && data->REG_PWM[j][i]) { 1522 data->pwm[j][i] 1523 = nct6775_read_value(data, 1524 data->REG_PWM[j][i]); 1525 } 1526 } 1527 1528 data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i], 1529 (fanmodecfg >> 4) & 7); 1530 1531 if (!data->temp_tolerance[0][i] || 1532 data->pwm_enable[i] != speed_cruise) 1533 data->temp_tolerance[0][i] = fanmodecfg & 0x0f; 1534 if (!data->target_speed_tolerance[i] || 1535 data->pwm_enable[i] == speed_cruise) { 1536 u8 t = fanmodecfg & 0x0f; 1537 1538 if (data->REG_TOLERANCE_H) { 1539 t |= (nct6775_read_value(data, 1540 data->REG_TOLERANCE_H[i]) & 0x70) >> 1; 1541 } 1542 data->target_speed_tolerance[i] = t; 1543 } 1544 1545 data->temp_tolerance[1][i] = 1546 nct6775_read_value(data, 1547 data->REG_CRITICAL_TEMP_TOLERANCE[i]); 1548 1549 reg = nct6775_read_value(data, data->REG_TEMP_SEL[i]); 1550 data->pwm_temp_sel[i] = reg & 0x1f; 1551 /* If fan can stop, report floor as 0 */ 1552 if (reg & 0x80) 1553 data->pwm[2][i] = 0; 1554 1555 if (!data->REG_WEIGHT_TEMP_SEL[i]) 1556 continue; 1557 1558 reg = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i]); 1559 data->pwm_weight_temp_sel[i] = reg & 0x1f; 1560 /* If weight is disabled, report weight source as 0 */ 1561 if (j == 1 && !(reg & 0x80)) ^^^^^^
Quoted 2 lines
So it can't be 1 here. Did you intend to say "i == 1"?
Nice catch. No, the condition does not depend on an index and should simply be
if (!(reg & 0x80))
No idea what if anything I was thinking when I wrote the code.
It would be great if you can send a patch.
Thanks,
Guenter
Quoted 14 lines
1562 data->pwm_weight_temp_sel[i] = 0; 1563 1564 /* Weight temp data */ 1565 for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) { 1566 data->weight_temp[j][i] 1567 = nct6775_read_value(data, 1568 data->REG_WEIGHT_TEMP[j][i]); 1569 } 1570 } 1571 }regards,
dan carpenter
PATCH 1/2 hwmon: (nct6775) Set weight source to zero correctly
This is dead code because j can never be 1 at this point. We had intended to just test if the bit was clear.
drivers/hwmon/nct6775.c +1 -1
@@ -1558,7 +1558,7 @@ static void nct6775_update_pwm(struct device *dev) reg =nct6775_read_value (data, data->REG_WEIGHT_TEMP_SEL[i]); data->pwm_weight_temp_sel[i] = reg &0x1f ;/* If weight is disabled, report weight source as 0 */ -if (j =1 && !(reg &0x80 )) +if (!(reg &0x80 )) data->pwm_weight_temp_sel[i] =0 ;/* Weight temp data */
Re: [PATCH 1/2] hwmon: (nct6775) Set weight source to zero correctly
Quoted 5 lines · Dan Carpenter
This is dead code because j can never be 1 at this point. We had intended to just test if the bit was clear.
Fixes bbd8decd4123 ("hwmon: (nct6775) Add support for weighted fan control")Signed-off-by Dan Carpenter <[email protected]>
Applied.
Thanks,
Guenter
Quoted 14 lines
drivers/hwmon/nct6775.c +1
-1@@ -1558,7 +1558,7 @@ static void nct6775_update_pwm(struct device *dev) reg =nct6775_read_value (data, data->REG_WEIGHT_TEMP_SEL[i]); data->pwm_weight_temp_sel[i] = reg &0x1f ;/* If weight is disabled, report weight source as 0 */ -if (j =1 && !(reg &0x80 )) +if (!(reg &0x80 )) data->pwm_weight_temp_sel[i] =0 ;/* Weight temp data */
PATCH 2/2 hwmon: (nct6775) Clean up a condition
In reply to Guenter RoeckI removed the "dsw_en &&" chunk of the condition because we know that "dsw_en" is set.
drivers/hwmon/nct6775.c +1 -2
@@ -3533,8 +3533,7 @@ nct6775_check_fan_inputs(struct nct6775_data *data)if (!fan6pin) fan6pin = (regval_2a &BIT (4 )) && - (!dsw_en || - (dsw_en && (regval_ed &BIT (4 )))); + (!dsw_en || (regval_ed &BIT (4 )));if (!pwm6pin) pwm6pin = (regval_2a &BIT (3 )) && (regval_ed &BIT (2 ));
Re: [PATCH 2/2] hwmon: (nct6775) Clean up a condition
Quoted 4 lines · Dan Carpenter
I removed the "
dsw_en&&" chunk of the condition because we know that "dsw_en" is set.Signed-off-by Dan Carpenter <[email protected]>
Applied to hwmon-next.
Thanks,
Guenter
Quoted 15 lines
drivers/hwmon/nct6775.c +1
-2@@ -3533,8 +3533,7 @@ nct6775_check_fan_inputs(struct nct6775_data *data)if (!fan6pin) fan6pin = (regval_2a &BIT (4 )) && - (!dsw_en || - (dsw_en && (regval_ed &BIT (4 )))); + (!dsw_en || (regval_ed &BIT (4 )));if (!pwm6pin) pwm6pin = (regval_2a &BIT (3 )) && (regval_ed &BIT (2 ));
Done. Btw, I saw another that looks legit.
drivers/hwmon/nct6775.c:1687 nct6775_update_device()
error: buffer overflow 'data->FAN_PULSE_SHIFT' 6 <= 6
drivers/hwmon/nct6775.c
1667 data->in[i][2] = nct6775_read_value(data,
1668 data->REG_IN_MINMAX[1][i]);
1669 }
1670
1671 /* Measured fan speeds and limits */
1672 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
^^^^^^^^^^^^^^^^^^^^^^^^This is a 7 element array.
1673 u16 reg;
1674
1675 if (!(data->has_fan & BIT(i)))
^^^^^^^^^^^^^^^^^^^^^^
I probably wouldn't have reported this originally because Smatch is bad at these checks. (I just haven't gotten around to it yet). But we do have fan7pin so it looks like BIT(6) can be set.
1676 continue;
1677
1678 reg = nct6775_read_value(data, data->REG_FAN[i]);
1679 data->rpm[i] = data->fan_from_reg(reg,
1680 data->fan_div[i]);
1681
1682 if (data->has_fan_min & BIT(i))
1683 data->fan_min[i] = nct6775_read_value(data,
1684 data->REG_FAN_MIN[i]);
1685 data->fan_pulses[i] =
1686 (nct6775_read_value(data, data->REG_FAN_PULSES[i])
1687 >> data->FAN_PULSE_SHIFT[i]) & 0x03;
^^^^^^^^^^^^^^^^^^^^^^^^FAN_PULSE_SHIFT is either 5 or 6 elements.
1688 1689 nct6775_select_fan_div(dev, data, i, reg); 1690 } 1691 1692 nct6775_update_pwm(dev); 1693 nct6775_update_pwm_limits(dev); 1694
regards,
dan carpenter
Guenter Roeck
maintainerHi Dan,
Quoted 2 lines · Dan Carpenter
Done. Btw, I saw another that looks legit.
Thanks ...
Quoted 35 lines
drivers/hwmon/nct6775.c:1687nct6775_update_device()
error: buffer overflow 'data->FAN_PULSE_SHIFT' 6 <= 6
drivers/hwmon/nct6775.c
1667 data->in[i][2] = nct6775_read_value(data,1668 data->REG_IN_MINMAX[1][i]); 1669 } 1670 1671 /* Measured fan speeds and limits */ 1672 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) { ^^^^^^^^^^^^^^^^^^^^^^^^This is a 7 element array.
1673 u16 reg; 1674 1675 if (!(data->has_fan & BIT(i))) ^^^^^^^^^^^^^^^^^^^^^^I probably wouldn't have reported this originally because Smatch is bad at these checks. (I just haven't gotten around to it yet). But we do have fan7pin so it looks like BIT(6) can be set.
1676 continue; 1677 1678 reg = nct6775_read_value(data, data->REG_FAN[i]); 1679 data->rpm[i] = data->fan_from_reg(reg, 1680 data->fan_div[i]); 1681 1682 if (data->has_fan_min & BIT(i)) 1683 data->fan_min[i] = nct6775_read_value(data, 1684 data->REG_FAN_MIN[i]); 1685 data->fan_pulses[i] = 1686 (nct6775_read_value(data, data->REG_FAN_PULSES[i]) 1687 >> data->FAN_PULSE_SHIFT[i]) & 0x03; ^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines
FAN_PULSE_SHIFTis either 5 or 6 elements.
Array size of xxx_REG_FAN_PULSES[] and xxx_FAN_PULSE_SHIFT[] should probably be NUM_FAN to be on the safe side. There is a slight secondary problem, though:
data->REG_FAN_PULSES[i] should not be read if it is 0. It doesn't matter much - it results in an unnecessary read from register 0 - but it is still undesirable.
Care to send another patch ?
Thanks,
Guenter
Quoted 11 lines
1688 1689 nct6775_select_fan_div(dev, data, i, reg); 1690 } 1691 1692 nct6775_update_pwm(dev); 1693 nct6775_update_pwm_limits(dev); 1694regards,
dan carpenter
Quoted 45 lines · Guenter Roeck
Hi Dan,
Quoted 2 lines · Dan Carpenter
Done. Btw, I saw another that looks legit.
Thanks ...
Quoted 35 lines
drivers/hwmon/nct6775.c:1687nct6775_update_device()
error: buffer overflow 'data->FAN_PULSE_SHIFT' 6 <= 6
drivers/hwmon/nct6775.c
1667 data->in[i][2] = nct6775_read_value(data,1668 data->REG_IN_MINMAX[1][i]); 1669 } 1670 1671 /* Measured fan speeds and limits */ 1672 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) { ^^^^^^^^^^^^^^^^^^^^^^^^This is a 7 element array.
1673 u16 reg; 1674 1675 if (!(data->has_fan & BIT(i))) ^^^^^^^^^^^^^^^^^^^^^^I probably wouldn't have reported this originally because Smatch is bad at these checks. (I just haven't gotten around to it yet). But we do have fan7pin so it looks like BIT(6) can be set.
1676 continue; 1677 1678 reg = nct6775_read_value(data, data->REG_FAN[i]); 1679 data->rpm[i] = data->fan_from_reg(reg, 1680 data->fan_div[i]); 1681 1682 if (data->has_fan_min & BIT(i)) 1683 data->fan_min[i] = nct6775_read_value(data, 1684 data->REG_FAN_MIN[i]); 1685 data->fan_pulses[i] = 1686 (nct6775_read_value(data, data->REG_FAN_PULSES[i]) 1687 >> data->FAN_PULSE_SHIFT[i]) & 0x03; ^^^^^^^^^^^^^^^^^^^^^^^^Quoted 2 lines
FAN_PULSE_SHIFTis either 5 or 6 elements.
Quoted 6 lines
Array size of xxx_REG_FAN_PULSES[] and xxx_FAN_PULSE_SHIFT[] should probably be
NUM_FANto be on the safe side. There is a slight secondary problem, though:
data->REG_FAN_PULSES[i] should not be read if it is 0. It doesn't matter much - it results in an unnecessary read from register 0 - but it is still undesirable.Care to send another patch ?
Can you do that one and give me a Reported-by tag? It seems slightly more involved and I am dumb.
regards,
dan carpenter
Guenter Roeck
maintainerQuoted 51 lines · Dan Carpenter
Quoted 50 lines · Guenter Roeck
Hi Dan,
Quoted 2 lines · Dan Carpenter
Done. Btw, I saw another that looks legit.
Thanks ...
Quoted 35 lines
drivers/hwmon/nct6775.c:1687nct6775_update_device()
error: buffer overflow 'data->FAN_PULSE_SHIFT' 6 <= 6
drivers/hwmon/nct6775.c1667 data->in[i][2] = nct6775_read_value(data, 1668 data->REG_IN_MINMAX[1][i]); 1669 } 1670 1671 /* Measured fan speeds and limits */ 1672 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) { ^^^^^^^^^^^^^^^^^^^^^^^^This is a 7 element array.
1673 u16 reg; 1674 1675 if (!(data->has_fan & BIT(i))) ^^^^^^^^^^^^^^^^^^^^^^I probably wouldn't have reported this originally because Smatch is bad at these checks. (I just haven't gotten around to it yet). But we do have fan7pin so it looks like BIT(6) can be set.
1676 continue; 1677 1678 reg = nct6775_read_value(data, data->REG_FAN[i]); 1679 data->rpm[i] = data->fan_from_reg(reg, 1680 data->fan_div[i]); 1681 1682 if (data->has_fan_min & BIT(i)) 1683 data->fan_min[i] = nct6775_read_value(data, 1684 data->REG_FAN_MIN[i]); 1685 data->fan_pulses[i] = 1686 (nct6775_read_value(data, data->REG_FAN_PULSES[i]) 1687 >> data->FAN_PULSE_SHIFT[i]) & 0x03; ^^^^^^^^^^^^^^^^^^^^^^^^Quoted 2 lines
FAN_PULSE_SHIFTis either 5 or 6 elements.Array size of xxx_REG_FAN_PULSES[] and xxx_FAN_PULSE_SHIFT[] should probably be
NUM_FANto be on the safe side. There is a slight secondary problem, though:
data->REG_FAN_PULSES[i] should not be read if it is 0. It doesn't matter much - it results in an unnecessary read from register 0 - but it is still undesirable.
Quoted 5 lines
Quoted 1 line
Care to send another patch ?
Can you do that one and give me a Reported-by tag? It seems slightly more involved and I am dumb.
Sure, NP.
Guenter