Skip to content

Commit d1b3b9c

Browse files
ian-abbottgregkh
authored andcommitted
comedi: kcomedilib: Add loop checking variants of open and close
Add `comedi_open_from(path, from)` and `comedi_close_from(dev, from)` as variants of the existing `comedi_from(path)` and `comedi_close(dev)`. The additional `from` parameter is a minor device number that tells the function that the COMEDI device is being opened or closed from another COMEDI device if the value is in the range [0, `COMEDI_NUM_BOARD_MINORS`-1]. In that case the function will refuse to open the device if it would lead to a chain of devices opening each other. (It will also impose a limit on the number of simultaneous opens from one device to another because we need to count those.) The new functions are intended to be used by the "comedi_bond" driver, which is the only driver that uses the existing `comedi_open()` and `comedi_close()` functions. The new functions will be used to avoid some possible deadlock situations. Replace the existing, exported `comedi_open()` and `comedi_close()` functions with inline wrapper functions that call the newly exported `comedi_open_from()` and `comedi_close_from()` functions. Signed-off-by: Ian Abbott <abbotti@mev.co.uk> Link: https://patch.msgid.link/20251027153748.4569-2-abbotti@mev.co.uk Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5149525 commit d1b3b9c

2 files changed

Lines changed: 147 additions & 7 deletions

File tree

drivers/comedi/kcomedilib/kcomedilib_main.c

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/fcntl.h>
1616
#include <linux/mm.h>
1717
#include <linux/io.h>
18+
#include <linux/bitmap.h>
1819

1920
#include <linux/comedi.h>
2021
#include <linux/comedi/comedidev.h>
@@ -24,7 +25,104 @@ MODULE_AUTHOR("David Schleef <ds@schleef.org>");
2425
MODULE_DESCRIPTION("Comedi kernel library");
2526
MODULE_LICENSE("GPL");
2627

27-
struct comedi_device *comedi_open(const char *filename)
28+
static DEFINE_MUTEX(kcomedilib_to_from_lock);
29+
30+
/*
31+
* Row index is the "to" node, column index is the "from" node, element value
32+
* is the number of links from the "from" node to the "to" node.
33+
*/
34+
static unsigned char
35+
kcomedilib_to_from[COMEDI_NUM_BOARD_MINORS][COMEDI_NUM_BOARD_MINORS];
36+
37+
static bool kcomedilib_set_link_from_to(unsigned int from, unsigned int to)
38+
{
39+
DECLARE_BITMAP(destinations[2], COMEDI_NUM_BOARD_MINORS);
40+
unsigned int cur = 0;
41+
bool okay = true;
42+
43+
/*
44+
* Allow "from" node to be out of range (no loop checking),
45+
* but require "to" node to be in range.
46+
*/
47+
if (to >= COMEDI_NUM_BOARD_MINORS)
48+
return false;
49+
if (from >= COMEDI_NUM_BOARD_MINORS)
50+
return true;
51+
52+
/*
53+
* Check that kcomedilib_to_from[to][from] can be made non-zero
54+
* without creating a loop.
55+
*
56+
* Termination of the loop-testing code relies on the assumption that
57+
* kcomedilib_to_from[][] does not contain any loops.
58+
*
59+
* Start with a set destinations set containing "from" as the only
60+
* element and work backwards looking for loops.
61+
*/
62+
bitmap_zero(destinations[cur], COMEDI_NUM_BOARD_MINORS);
63+
set_bit(from, destinations[cur]);
64+
mutex_lock(&kcomedilib_to_from_lock);
65+
do {
66+
unsigned int next = 1 - cur;
67+
unsigned int t = 0;
68+
69+
if (test_bit(to, destinations[cur])) {
70+
/* Loop detected. */
71+
okay = false;
72+
break;
73+
}
74+
/* Create next set of destinations. */
75+
bitmap_zero(destinations[next], COMEDI_NUM_BOARD_MINORS);
76+
while ((t = find_next_bit(destinations[cur],
77+
COMEDI_NUM_BOARD_MINORS,
78+
t)) < COMEDI_NUM_BOARD_MINORS) {
79+
unsigned int f;
80+
81+
for (f = 0; f < COMEDI_NUM_BOARD_MINORS; f++) {
82+
if (kcomedilib_to_from[t][f])
83+
set_bit(f, destinations[next]);
84+
}
85+
t++;
86+
}
87+
cur = next;
88+
} while (!bitmap_empty(destinations[cur], COMEDI_NUM_BOARD_MINORS));
89+
if (okay) {
90+
/* Allow a maximum of 255 links from "from" to "to". */
91+
if (kcomedilib_to_from[to][from] < 255)
92+
kcomedilib_to_from[to][from]++;
93+
else
94+
okay = false;
95+
}
96+
mutex_unlock(&kcomedilib_to_from_lock);
97+
return okay;
98+
}
99+
100+
static void kcomedilib_clear_link_from_to(unsigned int from, unsigned int to)
101+
{
102+
if (to < COMEDI_NUM_BOARD_MINORS && from < COMEDI_NUM_BOARD_MINORS) {
103+
mutex_lock(&kcomedilib_to_from_lock);
104+
if (kcomedilib_to_from[to][from])
105+
kcomedilib_to_from[to][from]--;
106+
mutex_unlock(&kcomedilib_to_from_lock);
107+
}
108+
}
109+
110+
/**
111+
* comedi_open_from() - Open a COMEDI device from the kernel with loop checks
112+
* @filename: Fake pathname of the form "/dev/comediN".
113+
* @from: Device number it is being opened from (if in range).
114+
*
115+
* Converts @filename to a COMEDI device number and "opens" it if it exists
116+
* and is attached to a low-level COMEDI driver.
117+
*
118+
* If @from is in range, refuse to open the device if doing so would form a
119+
* loop of devices opening each other. There is also a limit of 255 on the
120+
* number of concurrent opens from one device to another.
121+
*
122+
* Return: A pointer to the COMEDI device on success.
123+
* Return %NULL on failure.
124+
*/
125+
struct comedi_device *comedi_open_from(const char *filename, int from)
28126
{
29127
struct comedi_device *dev, *retval = NULL;
30128
unsigned int minor;
@@ -43,7 +141,7 @@ struct comedi_device *comedi_open(const char *filename)
43141
return NULL;
44142

45143
down_read(&dev->attach_lock);
46-
if (dev->attached)
144+
if (dev->attached && kcomedilib_set_link_from_to(from, minor))
47145
retval = dev;
48146
else
49147
retval = NULL;
@@ -54,14 +152,26 @@ struct comedi_device *comedi_open(const char *filename)
54152

55153
return retval;
56154
}
57-
EXPORT_SYMBOL_GPL(comedi_open);
155+
EXPORT_SYMBOL_GPL(comedi_open_from);
58156

59-
int comedi_close(struct comedi_device *dev)
157+
/**
158+
* comedi_close_from() - Close a COMEDI device from the kernel with loop checks
159+
* @dev: COMEDI device.
160+
* @from: Device number it was opened from (if in range).
161+
*
162+
* Closes a COMEDI device previously opened by comedi_open_from().
163+
*
164+
* If @from is in range, it should be match the one used by comedi_open_from().
165+
*
166+
* Returns: 0
167+
*/
168+
int comedi_close_from(struct comedi_device *dev, int from)
60169
{
170+
kcomedilib_clear_link_from_to(from, dev->minor);
61171
comedi_dev_put(dev);
62172
return 0;
63173
}
64-
EXPORT_SYMBOL_GPL(comedi_close);
174+
EXPORT_SYMBOL_GPL(comedi_close_from);
65175

66176
static int comedi_do_insn(struct comedi_device *dev,
67177
struct comedi_insn *insn,

include/linux/comedi/comedilib.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,38 @@
1010
#ifndef _LINUX_COMEDILIB_H
1111
#define _LINUX_COMEDILIB_H
1212

13-
struct comedi_device *comedi_open(const char *path);
14-
int comedi_close(struct comedi_device *dev);
13+
struct comedi_device *comedi_open_from(const char *path, int from);
14+
15+
/**
16+
* comedi_open() - Open a COMEDI device from the kernel
17+
* @filename: Fake pathname of the form "/dev/comediN".
18+
*
19+
* Converts @filename to a COMEDI device number and "opens" it if it exists
20+
* and is attached to a low-level COMEDI driver.
21+
*
22+
* Return: A pointer to the COMEDI device on success.
23+
* Return %NULL on failure.
24+
*/
25+
static inline struct comedi_device *comedi_open(const char *path)
26+
{
27+
return comedi_open_from(path, -1);
28+
}
29+
30+
int comedi_close_from(struct comedi_device *dev, int from);
31+
32+
/**
33+
* comedi_close() - Close a COMEDI device from the kernel
34+
* @dev: COMEDI device.
35+
*
36+
* Closes a COMEDI device previously opened by comedi_open().
37+
*
38+
* Returns: 0
39+
*/
40+
static inline int comedi_close(struct comedi_device *dev)
41+
{
42+
return comedi_close_from(dev, -1);
43+
}
44+
1545
int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev,
1646
unsigned int chan, unsigned int *io);
1747
int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,

0 commit comments

Comments
 (0)