power: qpnp-fg-gen3: Fix race condition in Time To Empty (TTE)

There is a possible race condition in TTE where it might cause
Out of Bound array access because the buffer gets cleared.
Fix this by adding a lock to TTE.

Change-Id: I2074c42aac9655967a8f77a84362507c3954cf20
Signed-off-by: Umang Chheda <uchheda@codeaurora.org>
This commit is contained in:
Umang Chheda 2020-01-23 14:57:00 +05:30 committed by Gerrit - the friendly Code Review server
parent 91aa9b064a
commit 55da599469

View File

@ -3270,15 +3270,18 @@ static int fg_get_time_to_empty(struct fg_dev *fg, int *val)
struct fg_gen3_chip *chip = container_of(fg, struct fg_gen3_chip, fg);
int rc, ibatt_avg, msoc, full_soc, act_cap_mah, divisor;
mutex_lock(&chip->ttf.lock);
rc = fg_circ_buf_median(&chip->ttf.ibatt, &ibatt_avg);
if (rc < 0) {
/* try to get instantaneous current */
rc = fg_get_battery_current(fg, &ibatt_avg);
if (rc < 0) {
pr_err("failed to get battery current, rc=%d\n", rc);
mutex_unlock(&chip->ttf.lock);
return rc;
}
}
mutex_unlock(&chip->ttf.lock);
ibatt_avg /= MILLI_UNIT;
/* clamp ibatt_avg to 100mA */