Skip to content

Commit

Permalink
counter: Implement events_queue_size sysfs attribute
Browse files Browse the repository at this point in the history
The events_queue_size sysfs attribute provides a way for users to
dynamically configure the Counter events queue size for the Counter
character device interface. The size is in number of struct
counter_event data structures. The number of elements will be rounded-up
to a power of 2 due to a requirement of the kfifo_alloc function called
during reallocation of the queue.

Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
  • Loading branch information
vilhelmgray authored and intel-lab-lkp committed Jul 13, 2021
1 parent c7ff52e commit a16ea08
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Documentation/ABI/testing/sysfs-bus-counter
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ Description:
shorter or equal to configured value are ignored. Value 0 means
filter is disabled.

What: /sys/bus/counter/devices/counterX/events_queue_size
KernelVersion: 5.15
Contact: linux-iio@vger.kernel.org
Description:
Size of the Counter events queue in number of struct
counter_event data structures. The number of elements will be
rounded-up to a power of 2.

What: /sys/bus/counter/devices/counterX/name
KernelVersion: 5.2
Contact: linux-iio@vger.kernel.org
Expand Down
6 changes: 6 additions & 0 deletions drivers/counter/counter-chrdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Generic Counter character device interface
* Copyright (C) 2020 William Breathitt Gray
*/
#include <linux/bitops.h>
#include <linux/cdev.h>
#include <linux/counter.h>
#include <linux/err.h>
Expand Down Expand Up @@ -336,6 +337,9 @@ static int counter_chrdev_open(struct inode *inode, struct file *filp)
typeof(*counter),
chrdev);

if (test_and_set_bit_lock(0, counter->chrdev_lock))
return -EBUSY;

get_device(&counter->dev);
filp->private_data = counter;

Expand All @@ -352,6 +356,7 @@ static int counter_chrdev_release(struct inode *inode, struct file *filp)
return err;

put_device(&counter->dev);
clear_bit_unlock(0, counter->chrdev_lock);

return 0;
}
Expand All @@ -376,6 +381,7 @@ int counter_chrdev_add(struct counter_device *const counter)
mutex_init(&counter->events_lock);

/* Initialize character device */
clear_bit(0, counter->chrdev_lock);
cdev_init(&counter->chrdev, &counter_fops);

/* Allocate Counter events queue */
Expand Down
45 changes: 45 additions & 0 deletions drivers/counter/counter-sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* Generic Counter sysfs interface
* Copyright (C) 2020 William Breathitt Gray
*/
#include <linux/bitops.h>
#include <linux/counter.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/kfifo.h>
#include <linux/kstrtox.h>
#include <linux/list.h>
#include <linux/string.h>
Expand Down Expand Up @@ -785,12 +787,49 @@ static int counter_num_counts_read(struct counter_device *counter, u8 *val)
return 0;
}

static int counter_events_queue_size_read(struct counter_device *counter,
u64 *val)
{
*val = kfifo_size(&counter->events);
return 0;
}

static int counter_events_queue_size_write(struct counter_device *counter,
u64 val)
{
DECLARE_KFIFO_PTR(events, struct counter_event);
int err = 0;

/* Verify chrdev is not currently being used */
if (test_and_set_bit_lock(0, counter->chrdev_lock))
return -EBUSY;

/* Allocate new events queue */
err = kfifo_alloc(&events, val, GFP_KERNEL);
if (err)
goto exit_early;

/* Swap in new events queue */
kfifo_free(&counter->events);
counter->events.kfifo = events.kfifo;

exit_early:
clear_bit_unlock(0, counter->chrdev_lock);

return err;
}

static struct counter_comp counter_num_signals_comp =
COUNTER_COMP_DEVICE_U8("num_signals", counter_num_signals_read, NULL);

static struct counter_comp counter_num_counts_comp =
COUNTER_COMP_DEVICE_U8("num_counts", counter_num_counts_read, NULL);

static struct counter_comp counter_events_queue_size_comp =
COUNTER_COMP_DEVICE_U64("events_queue_size",
counter_events_queue_size_read,
counter_events_queue_size_write);

static int counter_sysfs_attr_add(struct counter_device *const counter,
struct counter_attribute_group *cattr_group)
{
Expand Down Expand Up @@ -829,6 +868,12 @@ static int counter_sysfs_attr_add(struct counter_device *const counter,
if (err < 0)
return err;

/* Create events_queue_size attribute */
err = counter_attr_create(dev, cattr_group,
&counter_events_queue_size_comp, scope, NULL);
if (err < 0)
return err;

/* Create an attribute for each extension */
for (i = 0; i < counter->num_ext; i++) {
ext = &counter->ext[i];
Expand Down
2 changes: 2 additions & 0 deletions include/linux/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ struct counter_ops {
* @events: queue of detected Counter events
* @events_wait: wait queue to allow blocking reads of Counter events
* @events_lock: lock to protect Counter events queue read operations
* @chrdev_lock: lock to limit chrdev to a single open at a time
*/
struct counter_device {
const char *name;
Expand All @@ -323,6 +324,7 @@ struct counter_device {
DECLARE_KFIFO_PTR(events, struct counter_event);
wait_queue_head_t events_wait;
struct mutex events_lock;
DECLARE_BITMAP(chrdev_lock, 1);
};

int counter_register(struct counter_device *const counter);
Expand Down

0 comments on commit a16ea08

Please sign in to comment.