Re: [PATCH v12 15/17] counter: Implement events_queue_size sysfs attribute
From: Jarkko Nikula
Date: Tue Jul 06 2021 - 08:05:56 EST
Hi
On 7/5/21 11:19 AM, William Breathitt Gray wrote:
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.
...
diff --git a/drivers/counter/counter-chrdev.c b/drivers/counter/counter-chrdev.c
index 92805b1f65b8..13644c87d02a 100644
--- a/drivers/counter/counter-chrdev.c
+++ b/drivers/counter/counter-chrdev.c
@@ -323,6 +323,9 @@ static int counter_chrdev_open(struct inode *inode, struct file *filp)
typeof(*counter),
chrdev);
+ if (!mutex_trylock(&counter->chrdev_lock))
+ return -EBUSY;
+
get_device(&counter->dev);
filp->private_data = counter;
@@ -339,6 +342,7 @@ static int counter_chrdev_release(struct inode *inode, struct file *filp)
return err;
put_device(&counter->dev);
+ mutex_unlock(&counter->chrdev_lock);
return 0;
}
I got two separate mutex warnings from counter_chrdev_open() by doing
blind "cat /dev/counter0". First one due mutex being uninitialized:
[ 441.057342] DEBUG_LOCKS_WARN_ON(lock->magic != lock)
[ 441.057355] WARNING: CPU: 2 PID: 366 at kernel/locking/mutex.c:1416
mutex_trylock+0xf2/0x130
...
[ 441.217331] Call Trace:
[ 441.220062] counter_chrdev_open+0x21/0x60 [counter]
...
which I fixed trivially by (please be free to use it)
--- a/drivers/counter/counter-chrdev.c
+++ b/drivers/counter/counter-chrdev.c
@@ -364,6 +364,7 @@ int counter_chrdev_add(struct counter_device *const
counter)
spin_lock_init(&counter->events_list_lock);
init_waitqueue_head(&counter->events_wait);
mutex_init(&counter->events_lock);
+ mutex_init(&counter->chrdev_lock);
/* Initialize character device */
cdev_init(&counter->chrdev, &counter_fops);
and after that
[ 16.564403] ================================================
[ 16.570725] WARNING: lock held when returning to user space!
[ 16.577044] 5.13.0-next-20210706+ #4 Not tainted
[ 16.582198] ------------------------------------------------
[ 16.588507] cat/331 is leaving the kernel with locks still held!
[ 16.595214] 1 lock held by cat/331:
[ 16.599103] #0: ffff888102bb3630
(&counter->chrdev_lock){+.+.}-{3:3}, at: counter_chrdev_open+0x21/0x60
[counter]
Jarkko