lkml.org 
[lkml]   [2020]   [Dec]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH v6 3/5] counter: Add character device interface
From
Date
On 11/22/20 2:29 PM, William Breathitt Gray wrote:
> This patch introduces a character device interface for the Counter
> subsystem. Device data is exposed through standard character device read
> operations. Device data is gathered when a Counter event is pushed by
> the respective Counter device driver. Configuration is handled via ioctl
> operations on the respective Counter character device node.
>
> Cc: David Lechner <david@lechnology.com>
> Cc: Gwendal Grignou <gwendal@chromium.org>
> Cc: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> ---



> diff --git a/drivers/counter/counter-chrdev.c b/drivers/counter/counter-chrdev.c
> new file mode 100644
> index 000000000000..96fa7fbeef92
> --- /dev/null
> +++ b/drivers/counter/counter-chrdev.c


> +static ssize_t counter_chrdev_read(struct file *filp, char __user *buf,
> + size_t len, loff_t *f_ps)
> +{
> + struct counter_device *const counter = filp->private_data;
> + int err;
> + unsigned long flags;
> + unsigned int copied;
> +
> + if (len < sizeof(struct counter_event))
> + return -EINVAL;
> +
> + do {
> + if (kfifo_is_empty(&counter->events)) {
> + if (filp->f_flags & O_NONBLOCK)
> + return -EAGAIN;
> +
> + err = wait_event_interruptible(counter->events_wait,
> + !kfifo_is_empty(&counter->events));
> + if (err)
> + return err;
> + }
> +
> + raw_spin_lock_irqsave(&counter->events_lock, flags);

As mentioned in my previous review, I think it would be fine to use a
mutex here instead of disabling interrupts. In fact, I think copy_to_user()
might sleep, in which case we really don't want to call this with
interrupts disabled.

> + err = kfifo_to_user(&counter->events, buf, len, &copied);
> + raw_spin_unlock_irqrestore(&counter->events_lock, flags);
> + if (err)
> + return err;
> + } while (!copied);
> +
> + return copied;
> +}
> +


> +static int counter_add_watch(struct counter_device *const counter,
> + const unsigned long arg)
> +{
> + void __user *const uwatch = (void __user *)arg;
> + struct counter_watch watch;
> + struct counter_comp_node comp_node = {0};
> + size_t parent, id;
> + struct counter_comp *ext;
> + size_t num_ext;
> +
> + if (copy_from_user(&watch, uwatch, sizeof(watch)))
> + return -EFAULT;
> +
> + /* Dummy components can skip evaluation */
> + if (watch.component.type == COUNTER_COMPONENT_DUMMY)

I think "none" would be a better name than "dummy". Then it just
naturally makes sense why we would skip the evaluation.

> + goto dummy_component;
> +
> + parent = watch.component.parent;
> +
> + /* Configure parent component info for comp node */
> + switch (watch.component.scope) {
> + case COUNTER_SCOPE_DEVICE:
> + ext = counter->ext;
> + num_ext = counter->num_ext;
> + break;
> + case COUNTER_SCOPE_SIGNAL:
> + if (parent >= counter->num_signals)
> + return -EINVAL;
> + parent = array_index_nospec(parent, counter->num_signals);
> +
> + comp_node.parent = counter->signals + parent;
> +
> + ext = counter->signals[parent].ext;
> + num_ext = counter->signals[parent].num_ext;
> + break;
> + case COUNTER_SCOPE_COUNT:
> + if (parent >= counter->num_counts)
> + return -EINVAL;
> + parent = array_index_nospec(parent, counter->num_counts);
> +
> + comp_node.parent = counter->counts + parent;
> +
> + ext = counter->counts[parent].ext;
> + num_ext = counter->counts[parent].num_ext;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + id = watch.component.id;
> +
> + /* Configure component info for comp node */
> + switch (watch.component.type) {
> + case COUNTER_COMPONENT_SIGNAL:
> + if (watch.component.scope != COUNTER_SCOPE_SIGNAL)
> + return -EINVAL;
> +
> + comp_node.comp.type = COUNTER_COMP_SIGNAL_LEVEL;
> + comp_node.comp.signal_u8_read = counter->ops->signal_read;
> + break;
> + case COUNTER_COMPONENT_COUNT:
> + if (watch.component.scope != COUNTER_SCOPE_COUNT)
> + return -EINVAL;
> +
> + comp_node.comp.type = COUNTER_COMP_U64;
> + comp_node.comp.count_u64_read = counter->ops->count_read;
> + break;
> + case COUNTER_COMPONENT_FUNCTION:
> + if (watch.component.scope != COUNTER_SCOPE_COUNT)
> + return -EINVAL;
> +
> + comp_node.comp.type = COUNTER_COMP_FUNCTION;
> + comp_node.comp.count_u8_read = counter->ops->function_read;
> + break;
> + case COUNTER_COMPONENT_SYNAPSE_ACTION:
> + if (watch.component.scope != COUNTER_SCOPE_COUNT)
> + return -EINVAL;
> + if (id >= counter->counts[parent].num_synapses)
> + return -EINVAL;
> + id = array_index_nospec(id, counter->counts[parent].num_synapses);
> +
> + comp_node.comp.type = COUNTER_COMP_SYNAPSE_ACTION;
> + comp_node.comp.action_read = counter->ops->action_read;
> + comp_node.comp.priv = counter->counts[parent].synapses + id;
> + break;
> + case COUNTER_COMPONENT_EXTENSION:
> + if (id >= num_ext)
> + return -EINVAL;
> + id = array_index_nospec(id, num_ext);
> +
> + comp_node.comp = ext[id];
> + break;
> + default:
> + return -EINVAL;
> + }
> + /* Check if any read callback is set; this is part of a union */
> + if (!comp_node.comp.count_u8_read)
> + return -EOPNOTSUPP;
> +
> +dummy_component:
> + comp_node.component = watch.component;


In my experiments, I added a events_validate driver callback here to
validate each event as it is added. This way the user can know exactly
which event caused the problem rather than waiting for the event_config
callback.


> +
> + return counter_set_event_node(counter, &watch, &comp_node);
> +}
> +


> +static int counter_chrdev_open(struct inode *inode, struct file *filp)
> +{
> + struct counter_device *const counter = container_of(inode->i_cdev,
> + typeof(*counter),
> + chrdev);
> +
> + get_device(&counter->dev);
> + filp->private_data = counter;
> +
> + return nonseekable_open(inode, filp);
> +}
> +
> +static int counter_chrdev_release(struct inode *inode, struct file *filp)
> +{
> + struct counter_device *const counter = filp->private_data;
> + unsigned long flags;
> +
> + raw_spin_lock_irqsave(&counter->events_lock, flags);
> + counter_events_list_free(&counter->events_list);

Do we need to call the events_config callback here?

> + raw_spin_unlock_irqrestore(&counter->events_lock, flags);
> + counter_events_list_free(&counter->next_events_list);
> +
> + put_device(&counter->dev);
> +
> + return 0;
> +}


> +/**
> + * counter_push_event - queue event for userspace reading
> + * @counter: pointer to Counter structure
> + * @event: triggered event
> + * @channel: event channel
> + *
> + * Note: If no one is watching for the respective event, it is silently
> + * discarded.
> + *
> + * RETURNS:
> + * 0 on success, negative error number on failure.
> + */
> +int counter_push_event(struct counter_device *const counter, const u8 event,
> + const u8 channel)
> +{
> + struct counter_event ev = {0};
> + unsigned int copied = 0;
> + unsigned long flags;
> + struct counter_event_node *event_node;
> + struct counter_comp_node *comp_node;
> + int err = 0;
> +
> + ev.timestamp = ktime_get_ns();
> + ev.watch.event = event;
> + ev.watch.channel = channel;
> +
> + raw_spin_lock_irqsave(&counter->events_lock, flags);
> +
> + /* Search for event in the list */
> + list_for_each_entry(event_node, &counter->events_list, l)
> + if (event_node->event == event &&
> + event_node->channel == channel)
> + break;
> +
> + /* If event is not in the list */
> + if (&event_node->l == &counter->events_list)
> + goto exit_early;
> +
> + /* Read and queue relevant comp for userspace */
> + list_for_each_entry(comp_node, &event_node->comp_list, l) {
> + err = counter_get_data(counter, comp_node, &ev.value);
> + if (err)
> + goto exit_early;

It looks like this will skip an event even if just one watch value
has an error. This doesn't seem so great.


> +
> + ev.watch.component = comp_node->component;
> +
> + copied += kfifo_put(&counter->events, ev);
> + }
> +
> + if (copied)
> + wake_up_poll(&counter->events_wait, EPOLLIN);
> +
> +exit_early:
> + raw_spin_unlock_irqrestore(&counter->events_lock, flags);
> +
> + return err;

Interrupt handlers can't really do anything about the error here.
It could be nice to instead add an error field to the userspace
event struct, then userspace would know when an error occurred
and could do something about it. And it also fixes the one-error-
skips-all problem I mentioned above.

> +}
> +EXPORT_SYMBOL_GPL(counter_push_event);



> diff --git a/include/linux/counter.h b/include/linux/counter.h
> index 3f3f8ba6c1b4..98cd7c035968 100644
> --- a/include/linux/counter.h


>
> +/**
> + * struct counter_event_node - Counter Event node
> + * @l: list of current watching Counter events
> + * @event: event that triggers
> + * @channel: event channel
> + * @comp_list: list of components to watch when event triggers
> + */
> +struct counter_event_node {
> + struct list_head l;
> + u8 event;
> + u8 channel;
> + struct list_head comp_list;
> +};
> +


Unless this is needed outside of the drivers/counter/ directory, I
would suggest putting it in drivers/counter/counter-chrdev.h instead
of include/linux/counter.h.

\
 
 \ /
  Last update: 2020-12-14 01:00    [W:0.124 / U:4.748 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site