Skip to content

Commit

Permalink
Merge tag 'tag-chrome-platform-for-v6.8' of git://git.kernel.org/pub/…
Browse files Browse the repository at this point in the history
…scm/linux/kernel/git/chrome-platform/linux

Pull chrome platform updates from Tzung-Bi Shih:

 - Implement quickselect for median in cros-ec-sensorhub

 - Fix an out of boundary array access in cros-ec-vbc

 - Cleanups and fix typos

* tag 'tag-chrome-platform-for-v6.8' of git://git.kernel.org/pub/scm/linux/kernel/git/chrome-platform/linux:
  platform/chrome/wilco_ec: Remove usage of the deprecated ida_simple_xx() API
  platform/chrome: cros_ec_vbc: Fix -Warray-bounds warnings
  platform/chrome: sensorhub: Implement quickselect for median calculation
  platform/chrome: sensorhub: Fix typos
  • Loading branch information
torvalds committed Jan 10, 2024
2 parents 7da7107 + 57eb6dc commit 3efcce4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 32 deletions.
74 changes: 51 additions & 23 deletions drivers/platform/chrome/cros_ec_sensorhub_ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ EXPORT_SYMBOL_GPL(cros_ec_sensorhub_unregister_push_data);
* @sensorhub: Sensor Hub object
* @on: true when events are requested.
*
* To be called before sleeping or when noone is listening.
* To be called before sleeping or when no one is listening.
* Return: 0 on success, or an error when we can not communicate with the EC.
*
*/
Expand Down Expand Up @@ -133,33 +133,61 @@ int cros_ec_sensorhub_ring_fifo_enable(struct cros_ec_sensorhub *sensorhub,
return ret;
}

static int cros_ec_sensor_ring_median_cmp(const void *pv1, const void *pv2)
static void cros_ec_sensor_ring_median_swap(s64 *a, s64 *b)
{
s64 v1 = *(s64 *)pv1;
s64 v2 = *(s64 *)pv2;

if (v1 > v2)
return 1;
else if (v1 < v2)
return -1;
else
return 0;
s64 tmp = *a;
*a = *b;
*b = tmp;
}

/*
* cros_ec_sensor_ring_median: Gets median of an array of numbers
*
* For now it's implemented using an inefficient > O(n) sort then return
* the middle element. A more optimal method would be something like
* quickselect, but given that n = 64 we can probably live with it in the
* name of clarity.
* It's implemented using the quickselect algorithm, which achieves an
* average time complexity of O(n) the middle element. In the worst case,
* the runtime of quickselect could regress to O(n^2). To mitigate this,
* algorithms like median-of-medians exist, which can guarantee O(n) even
* in the worst case. However, these algorithms come with a higher
* overhead and are more complex to implement, making quickselect a
* pragmatic choice for our use case.
*
* Warning: the input array gets modified (sorted)!
* Warning: the input array gets modified!
*/
static s64 cros_ec_sensor_ring_median(s64 *array, size_t length)
{
sort(array, length, sizeof(s64), cros_ec_sensor_ring_median_cmp, NULL);
return array[length / 2];
int lo = 0;
int hi = length - 1;

while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int pivot, i;

if (array[lo] > array[mid])
cros_ec_sensor_ring_median_swap(&array[lo], &array[mid]);
if (array[lo] > array[hi])
cros_ec_sensor_ring_median_swap(&array[lo], &array[hi]);
if (array[mid] < array[hi])
cros_ec_sensor_ring_median_swap(&array[mid], &array[hi]);

pivot = array[hi];
i = lo - 1;

for (int j = lo; j < hi; j++)
if (array[j] < pivot)
cros_ec_sensor_ring_median_swap(&array[++i], &array[j]);

/* The pivot's index corresponds to i+1. */
cros_ec_sensor_ring_median_swap(&array[i + 1], &array[hi]);
if (i + 1 == length / 2)
return array[i + 1];
if (i + 1 > length / 2)
hi = i;
else
lo = i + 2;
}

/* Should never reach here. */
return -1;
}

/*
Expand All @@ -175,8 +203,8 @@ static s64 cros_ec_sensor_ring_median(s64 *array, size_t length)
*
* While a and b are recorded at accurate times (due to the EC real time
* nature); c is pretty untrustworthy, even though it's recorded the
* first thing in ec_irq_handler(). There is a very good change we'll get
* added lantency due to:
* first thing in ec_irq_handler(). There is a very good chance we'll get
* added latency due to:
* other irqs
* ddrfreq
* cpuidle
Expand Down Expand Up @@ -511,7 +539,7 @@ cros_ec_sensor_ring_process_event(struct cros_ec_sensorhub *sensorhub,
* ringbuffer.
*
* This is the new spreading code, assumes every sample's timestamp
* preceeds the sample. Run if tight_timestamps == true.
* precedes the sample. Run if tight_timestamps == true.
*
* Sometimes the EC receives only one interrupt (hence timestamp) for
* a batch of samples. Only the first sample will have the correct
Expand Down Expand Up @@ -595,7 +623,7 @@ cros_ec_sensor_ring_spread_add(struct cros_ec_sensorhub *sensorhub,
} else {
/*
* Push first sample in the batch to the,
* kifo, it's guaranteed to be correct, the
* kfifo, it's guaranteed to be correct, the
* rest will follow later on.
*/
sample_idx = 1;
Expand Down Expand Up @@ -701,7 +729,7 @@ cros_ec_sensor_ring_spread_add(struct cros_ec_sensorhub *sensorhub,
* last_out -->
*
*
* We spread time for the samples using perod p = (current - TS1)/4.
* We spread time for the samples using period p = (current - TS1)/4.
* between TS1 and TS2: [TS1+p/4, TS1+2p/4, TS1+3p/4, current_timestamp].
*
*/
Expand Down
12 changes: 8 additions & 4 deletions drivers/platform/chrome/cros_ec_vbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
struct device *dev = kobj_to_dev(kobj);
struct cros_ec_dev *ec = to_cros_ec_dev(dev);
struct cros_ec_device *ecdev = ec->ec_dev;
struct ec_params_vbnvcontext *params;
struct cros_ec_command *msg;
/*
* This should be a pointer to the same type as op field in
* struct ec_params_vbnvcontext.
*/
uint32_t *params_op;
int err;
const size_t para_sz = sizeof(params->op);
const size_t para_sz = sizeof(*params_op);
const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
const size_t payload = max(para_sz, resp_sz);

Expand All @@ -32,8 +36,8 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
return -ENOMEM;

/* NB: we only kmalloc()ated enough space for the op field */
params = (struct ec_params_vbnvcontext *)msg->data;
params->op = EC_VBNV_CONTEXT_OP_READ;
params_op = (uint32_t *)msg->data;
*params_op = EC_VBNV_CONTEXT_OP_READ;

msg->version = EC_VER_VBNV_CONTEXT;
msg->command = EC_CMD_VBNV_CONTEXT;
Expand Down
4 changes: 2 additions & 2 deletions drivers/platform/chrome/wilco_ec/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static int event_device_add(struct acpi_device *adev)
free_dev_data:
hangup_device(dev_data);
free_minor:
ida_simple_remove(&event_ida, minor);
ida_free(&event_ida, minor);
return error;
}

Expand All @@ -504,7 +504,7 @@ static void event_device_remove(struct acpi_device *adev)
struct event_device_data *dev_data = adev->driver_data;

cdev_device_del(&dev_data->cdev, &dev_data->dev);
ida_simple_remove(&event_ida, MINOR(dev_data->dev.devt));
ida_free(&event_ida, MINOR(dev_data->dev.devt));
hangup_device(dev_data);
}

Expand Down
6 changes: 3 additions & 3 deletions drivers/platform/chrome/wilco_ec/telemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static int telem_device_probe(struct platform_device *pdev)

dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
if (!dev_data) {
ida_simple_remove(&telem_ida, minor);
ida_free(&telem_ida, minor);
return -ENOMEM;
}

Expand All @@ -393,7 +393,7 @@ static int telem_device_probe(struct platform_device *pdev)
error = cdev_device_add(&dev_data->cdev, &dev_data->dev);
if (error) {
put_device(&dev_data->dev);
ida_simple_remove(&telem_ida, minor);
ida_free(&telem_ida, minor);
return error;
}

Expand All @@ -405,7 +405,7 @@ static void telem_device_remove(struct platform_device *pdev)
struct telem_device_data *dev_data = platform_get_drvdata(pdev);

cdev_device_del(&dev_data->cdev, &dev_data->dev);
ida_simple_remove(&telem_ida, MINOR(dev_data->dev.devt));
ida_free(&telem_ida, MINOR(dev_data->dev.devt));
put_device(&dev_data->dev);
}

Expand Down

0 comments on commit 3efcce4

Please sign in to comment.