Commit 3efcce4a authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'tag-chrome-platform-for-v6.8' of...

Merge tag 'tag-chrome-platform-for-v6.8' of git://git.kernel.org/pub/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
parents 7da71072 57eb6dcd
Loading
Loading
Loading
Loading
+51 −23
Original line number Diff line number Diff line
@@ -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;
}

/*
@@ -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
@@ -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
@@ -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;
@@ -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].
 *
 */
+8 −4
Original line number Diff line number Diff line
@@ -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);

@@ -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;
+2 −2
Original line number Diff line number Diff line
@@ -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;
}

@@ -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);
}

+3 −3
Original line number Diff line number Diff line
@@ -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;
	}

@@ -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;
	}

@@ -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);
}