Skip to content

Commit ef69bb2

Browse files
jan-kiszkadavem330
authored andcommitted
CAPI: Rework controller state notifier
Another step towards proper locking: Rework the callback provided to capidrv for controller state changes. This is so far attached to an application, which would require us to hold the corresponding lock across notification calls. But there is no direct relation between a controller up/down event and an application, so let's decouple them and provide a notifier call chain for those events instead. This notifier chain is first of all used internally. Here we request the highest priority to unsure that housekeeping work is done before any other notifications. The chain is exported via [un]register_capictr_notifier to our only user, capidrv, to replace the racy and unfixable capi20_set_callback. Signed-off-by: Jan Kiszka <jan.kiszka@web.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3efecf7 commit ef69bb2

3 files changed

Lines changed: 72 additions & 79 deletions

File tree

‎drivers/isdn/capi/capidrv.c‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,19 +2210,24 @@ static int capidrv_delcontr(u16 contr)
22102210
}
22112211

22122212

2213-
static void lower_callback(unsigned int cmd, u32 contr, void *data)
2213+
static int
2214+
lower_callback(struct notifier_block *nb, unsigned long val, void *v)
22142215
{
2216+
capi_profile profile;
2217+
u32 contr = (long)v;
22152218

2216-
switch (cmd) {
2217-
case KCI_CONTRUP:
2219+
switch (val) {
2220+
case CAPICTR_UP:
22182221
printk(KERN_INFO "capidrv: controller %hu up\n", contr);
2219-
(void) capidrv_addcontr(contr, (capi_profile *) data);
2222+
if (capi20_get_profile(contr, &profile) == CAPI_NOERROR)
2223+
(void) capidrv_addcontr(contr, &profile);
22202224
break;
2221-
case KCI_CONTRDOWN:
2225+
case CAPICTR_DOWN:
22222226
printk(KERN_INFO "capidrv: controller %hu down\n", contr);
22232227
(void) capidrv_delcontr(contr);
22242228
break;
22252229
}
2230+
return NOTIFY_OK;
22262231
}
22272232

22282233
/*
@@ -2262,6 +2267,10 @@ static void __exit proc_exit(void)
22622267
remove_proc_entry("capi/capidrv", NULL);
22632268
}
22642269

2270+
static struct notifier_block capictr_nb = {
2271+
.notifier_call = lower_callback,
2272+
};
2273+
22652274
static int __init capidrv_init(void)
22662275
{
22672276
capi_profile profile;
@@ -2278,7 +2287,7 @@ static int __init capidrv_init(void)
22782287
return -EIO;
22792288
}
22802289

2281-
capi20_set_callback(&global.ap, lower_callback);
2290+
register_capictr_notifier(&capictr_nb);
22822291

22832292
errcode = capi20_get_profile(0, &profile);
22842293
if (errcode != CAPI_NOERROR) {
@@ -2300,6 +2309,7 @@ static int __init capidrv_init(void)
23002309

23012310
static void __exit capidrv_exit(void)
23022311
{
2312+
unregister_capictr_notifier(&capictr_nb);
23032313
capi20_release(&global.ap);
23042314

23052315
proc_exit();

‎drivers/isdn/capi/kcapi.c‎

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ module_param(showcapimsgs, uint, 0);
4444

4545
/* ------------------------------------------------------------- */
4646

47-
struct capi_notifier {
47+
struct capictr_event {
4848
struct work_struct work;
49-
unsigned int cmd;
49+
unsigned int type;
5050
u32 controller;
51-
u16 applid;
52-
u32 ncci;
5351
};
5452

5553
/* ------------------------------------------------------------- */
@@ -71,6 +69,8 @@ struct capi_ctr *capi_controller[CAPI_MAXCONTR];
7169

7270
static int ncontrollers;
7371

72+
static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list);
73+
7474
/* -------- controller ref counting -------------------------------------- */
7575

7676
static inline struct capi_ctr *
@@ -165,8 +165,6 @@ static void release_appl(struct capi_ctr *ctr, u16 applid)
165165
capi_ctr_put(ctr);
166166
}
167167

168-
/* -------- KCI_CONTRUP --------------------------------------- */
169-
170168
static void notify_up(u32 contr)
171169
{
172170
struct capi20_appl *ap;
@@ -188,16 +186,11 @@ static void notify_up(u32 contr)
188186
if (!ap || ap->release_in_progress)
189187
continue;
190188
register_appl(ctr, applid, &ap->rparam);
191-
if (ap->callback && !ap->release_in_progress)
192-
ap->callback(KCI_CONTRUP, contr,
193-
&ctr->profile);
194189
}
195190
} else
196191
printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
197192
}
198193

199-
/* -------- KCI_CONTRDOWN ------------------------------------- */
200-
201194
static void ctr_down(struct capi_ctr *ctr)
202195
{
203196
struct capi20_appl *ap;
@@ -215,11 +208,8 @@ static void ctr_down(struct capi_ctr *ctr)
215208

216209
for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
217210
ap = get_capi_appl_by_nr(applid);
218-
if (ap && !ap->release_in_progress) {
219-
if (ap->callback)
220-
ap->callback(KCI_CONTRDOWN, ctr->cnr, NULL);
211+
if (ap && !ap->release_in_progress)
221212
capi_ctr_put(ctr);
222-
}
223213
}
224214
}
225215

@@ -237,45 +227,63 @@ static void notify_down(u32 contr)
237227
printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
238228
}
239229

240-
static void notify_handler(struct work_struct *work)
230+
static int
231+
notify_handler(struct notifier_block *nb, unsigned long val, void *v)
241232
{
242-
struct capi_notifier *np =
243-
container_of(work, struct capi_notifier, work);
233+
u32 contr = (long)v;
244234

245-
switch (np->cmd) {
246-
case KCI_CONTRUP:
247-
notify_up(np->controller);
235+
switch (val) {
236+
case CAPICTR_UP:
237+
notify_up(contr);
248238
break;
249-
case KCI_CONTRDOWN:
250-
notify_down(np->controller);
239+
case CAPICTR_DOWN:
240+
notify_down(contr);
251241
break;
252242
}
243+
return NOTIFY_OK;
244+
}
245+
246+
static void do_notify_work(struct work_struct *work)
247+
{
248+
struct capictr_event *event =
249+
container_of(work, struct capictr_event, work);
253250

254-
kfree(np);
251+
blocking_notifier_call_chain(&ctr_notifier_list, event->type,
252+
(void *)(long)event->controller);
253+
kfree(event);
255254
}
256255

257256
/*
258257
* The notifier will result in adding/deleteing of devices. Devices can
259258
* only removed in user process, not in bh.
260259
*/
261-
static int notify_push(unsigned int cmd, u32 controller, u16 applid, u32 ncci)
260+
static int notify_push(unsigned int event_type, u32 controller)
262261
{
263-
struct capi_notifier *np = kmalloc(sizeof(*np), GFP_ATOMIC);
262+
struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
264263

265-
if (!np)
264+
if (!event)
266265
return -ENOMEM;
267266

268-
INIT_WORK(&np->work, notify_handler);
269-
np->cmd = cmd;
270-
np->controller = controller;
271-
np->applid = applid;
272-
np->ncci = ncci;
267+
INIT_WORK(&event->work, do_notify_work);
268+
event->type = event_type;
269+
event->controller = controller;
273270

274-
schedule_work(&np->work);
271+
schedule_work(&event->work);
275272
return 0;
276273
}
277274

278-
275+
int register_capictr_notifier(struct notifier_block *nb)
276+
{
277+
return blocking_notifier_chain_register(&ctr_notifier_list, nb);
278+
}
279+
EXPORT_SYMBOL_GPL(register_capictr_notifier);
280+
281+
int unregister_capictr_notifier(struct notifier_block *nb)
282+
{
283+
return blocking_notifier_chain_unregister(&ctr_notifier_list, nb);
284+
}
285+
EXPORT_SYMBOL_GPL(unregister_capictr_notifier);
286+
279287
/* -------- Receiver ------------------------------------------ */
280288

281289
static void recv_handler(struct work_struct *work)
@@ -401,7 +409,7 @@ void capi_ctr_ready(struct capi_ctr *ctr)
401409
printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
402410
ctr->cnr, ctr->name);
403411

404-
notify_push(KCI_CONTRUP, ctr->cnr, 0, 0);
412+
notify_push(CAPICTR_UP, ctr->cnr);
405413
}
406414

407415
EXPORT_SYMBOL(capi_ctr_ready);
@@ -418,7 +426,7 @@ void capi_ctr_down(struct capi_ctr *ctr)
418426
{
419427
printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
420428

421-
notify_push(KCI_CONTRDOWN, ctr->cnr, 0, 0);
429+
notify_push(CAPICTR_DOWN, ctr->cnr);
422430
}
423431

424432
EXPORT_SYMBOL(capi_ctr_down);
@@ -633,7 +641,6 @@ u16 capi20_register(struct capi20_appl *ap)
633641
ap->nrecvdatapkt = 0;
634642
ap->nsentctlpkt = 0;
635643
ap->nsentdatapkt = 0;
636-
ap->callback = NULL;
637644
mutex_init(&ap->recv_mtx);
638645
skb_queue_head_init(&ap->recv_queue);
639646
INIT_WORK(&ap->recv_work, recv_handler);
@@ -1137,30 +1144,6 @@ int capi20_manufacturer(unsigned int cmd, void __user *data)
11371144

11381145
EXPORT_SYMBOL(capi20_manufacturer);
11391146

1140-
/* temporary hack */
1141-
1142-
/**
1143-
* capi20_set_callback() - set CAPI application notification callback function
1144-
* @ap: CAPI application descriptor structure.
1145-
* @callback: callback function (NULL to remove).
1146-
*
1147-
* If not NULL, the callback function will be called to notify the
1148-
* application of the addition or removal of a controller.
1149-
* The first argument (cmd) will tell whether the controller was added
1150-
* (KCI_CONTRUP) or removed (KCI_CONTRDOWN).
1151-
* The second argument (contr) will be the controller number.
1152-
* For cmd==KCI_CONTRUP the third argument (data) will be a pointer to the
1153-
* new controller's capability profile structure.
1154-
*/
1155-
1156-
void capi20_set_callback(struct capi20_appl *ap,
1157-
void (*callback) (unsigned int cmd, __u32 contr, void *data))
1158-
{
1159-
ap->callback = callback;
1160-
}
1161-
1162-
EXPORT_SYMBOL(capi20_set_callback);
1163-
11641147
/* ------------------------------------------------------------- */
11651148
/* -------- Init & Cleanup ------------------------------------- */
11661149
/* ------------------------------------------------------------- */
@@ -1169,10 +1152,17 @@ EXPORT_SYMBOL(capi20_set_callback);
11691152
* init / exit functions
11701153
*/
11711154

1155+
static struct notifier_block capictr_nb = {
1156+
.notifier_call = notify_handler,
1157+
.priority = INT_MAX,
1158+
};
1159+
11721160
static int __init kcapi_init(void)
11731161
{
11741162
int err;
11751163

1164+
register_capictr_notifier(&capictr_nb);
1165+
11761166
err = cdebug_init();
11771167
if (!err)
11781168
kcapi_proc_init();

‎include/linux/kernelcapi.h‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ typedef struct kcapi_carddef {
4848
#include <linux/list.h>
4949
#include <linux/skbuff.h>
5050
#include <linux/workqueue.h>
51-
52-
#define KCI_CONTRUP 0 /* arg: struct capi_profile */
53-
#define KCI_CONTRDOWN 1 /* arg: NULL */
51+
#include <linux/notifier.h>
5452

5553
struct capi20_appl {
5654
u16 applid;
@@ -67,11 +65,6 @@ struct capi20_appl {
6765
struct sk_buff_head recv_queue;
6866
struct work_struct recv_work;
6967
int release_in_progress;
70-
71-
/* ugly hack to allow for notification of added/removed
72-
* controllers. The Right Way (tm) is known. XXX
73-
*/
74-
void (*callback) (unsigned int cmd, __u32 contr, void *data);
7568
};
7669

7770
u16 capi20_isinstalled(void);
@@ -84,11 +77,11 @@ u16 capi20_get_serial(u32 contr, u8 serial[CAPI_SERIAL_LEN]);
8477
u16 capi20_get_profile(u32 contr, struct capi_profile *profp);
8578
int capi20_manufacturer(unsigned int cmd, void __user *data);
8679

87-
/* temporary hack XXX */
88-
void capi20_set_callback(struct capi20_appl *ap,
89-
void (*callback) (unsigned int cmd, __u32 contr, void *data));
90-
80+
#define CAPICTR_UP 0
81+
#define CAPICTR_DOWN 1
9182

83+
int register_capictr_notifier(struct notifier_block *nb);
84+
int unregister_capictr_notifier(struct notifier_block *nb);
9285

9386
#define CAPI_NOERROR 0x0000
9487

0 commit comments

Comments
 (0)