Skip to content

Commit acd042b

Browse files
Evgeniy Polyakovdavem330
authored andcommitted
[CONNECTOR]: async connector mode.
If input message rate from userspace is too high, do not drop them, but try to deliver using work queue allocation. Failing there is some kind of congestion control. It also removes warn_on on this condition, which scares people. Signed-off-by: Evgeniy Polyakov <johnpol@2ka.mipt.ru> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b9d717a commit acd042b

3 files changed

Lines changed: 72 additions & 55 deletions

File tree

‎drivers/connector/cn_queue.c‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@
3131
#include <linux/connector.h>
3232
#include <linux/delay.h>
3333

34-
static void cn_queue_wrapper(void *data)
34+
void cn_queue_wrapper(void *data)
3535
{
36-
struct cn_callback_entry *cbq = data;
36+
struct cn_callback_data *d = data;
3737

38-
cbq->cb->callback(cbq->cb->priv);
39-
cbq->destruct_data(cbq->ddata);
40-
cbq->ddata = NULL;
38+
d->callback(d->callback_priv);
39+
40+
d->destruct_data(d->ddata);
41+
d->ddata = NULL;
42+
43+
kfree(d->free);
4144
}
4245

43-
static struct cn_callback_entry *cn_queue_alloc_callback_entry(struct cn_callback *cb)
46+
static struct cn_callback_entry *cn_queue_alloc_callback_entry(char *name, struct cb_id *id, void (*callback)(void *))
4447
{
4548
struct cn_callback_entry *cbq;
4649

@@ -50,8 +53,11 @@ static struct cn_callback_entry *cn_queue_alloc_callback_entry(struct cn_callbac
5053
return NULL;
5154
}
5255

53-
cbq->cb = cb;
54-
INIT_WORK(&cbq->work, &cn_queue_wrapper, cbq);
56+
snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
57+
memcpy(&cbq->id.id, id, sizeof(struct cb_id));
58+
cbq->data.callback = callback;
59+
60+
INIT_WORK(&cbq->work, &cn_queue_wrapper, &cbq->data);
5561
return cbq;
5662
}
5763

@@ -68,12 +74,12 @@ int cn_cb_equal(struct cb_id *i1, struct cb_id *i2)
6874
return ((i1->idx == i2->idx) && (i1->val == i2->val));
6975
}
7076

71-
int cn_queue_add_callback(struct cn_queue_dev *dev, struct cn_callback *cb)
77+
int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *))
7278
{
7379
struct cn_callback_entry *cbq, *__cbq;
7480
int found = 0;
7581

76-
cbq = cn_queue_alloc_callback_entry(cb);
82+
cbq = cn_queue_alloc_callback_entry(name, id, callback);
7783
if (!cbq)
7884
return -ENOMEM;
7985

@@ -82,7 +88,7 @@ int cn_queue_add_callback(struct cn_queue_dev *dev, struct cn_callback *cb)
8288

8389
spin_lock_bh(&dev->queue_lock);
8490
list_for_each_entry(__cbq, &dev->queue_list, callback_entry) {
85-
if (cn_cb_equal(&__cbq->cb->id, &cb->id)) {
91+
if (cn_cb_equal(&__cbq->id.id, id)) {
8692
found = 1;
8793
break;
8894
}
@@ -99,7 +105,7 @@ int cn_queue_add_callback(struct cn_queue_dev *dev, struct cn_callback *cb)
99105

100106
cbq->nls = dev->nls;
101107
cbq->seq = 0;
102-
cbq->group = cbq->cb->id.idx;
108+
cbq->group = cbq->id.id.idx;
103109

104110
return 0;
105111
}
@@ -111,7 +117,7 @@ void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id)
111117

112118
spin_lock_bh(&dev->queue_lock);
113119
list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) {
114-
if (cn_cb_equal(&cbq->cb->id, id)) {
120+
if (cn_cb_equal(&cbq->id.id, id)) {
115121
list_del(&cbq->callback_entry);
116122
found = 1;
117123
break;

‎drivers/connector/connector.c‎

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int cn_netlink_send(struct cn_msg *msg, u32 __group, int gfp_mask)
8484
spin_lock_bh(&dev->cbdev->queue_lock);
8585
list_for_each_entry(__cbq, &dev->cbdev->queue_list,
8686
callback_entry) {
87-
if (cn_cb_equal(&__cbq->cb->id, &msg->id)) {
87+
if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
8888
found = 1;
8989
group = __cbq->group;
9090
}
@@ -127,42 +127,56 @@ static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), v
127127
{
128128
struct cn_callback_entry *__cbq;
129129
struct cn_dev *dev = &cdev;
130-
int found = 0;
130+
int err = -ENODEV;
131131

132132
spin_lock_bh(&dev->cbdev->queue_lock);
133133
list_for_each_entry(__cbq, &dev->cbdev->queue_list, callback_entry) {
134-
if (cn_cb_equal(&__cbq->cb->id, &msg->id)) {
135-
/*
136-
* Let's scream if there is some magic and the
137-
* data will arrive asynchronously here.
138-
* [i.e. netlink messages will be queued].
139-
* After the first warning I will fix it
140-
* quickly, but now I think it is
141-
* impossible. --zbr (2004_04_27).
142-
*/
134+
if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
143135
if (likely(!test_bit(0, &__cbq->work.pending) &&
144-
__cbq->ddata == NULL)) {
145-
__cbq->cb->priv = msg;
136+
__cbq->data.ddata == NULL)) {
137+
__cbq->data.callback_priv = msg;
146138

147-
__cbq->ddata = data;
148-
__cbq->destruct_data = destruct_data;
139+
__cbq->data.ddata = data;
140+
__cbq->data.destruct_data = destruct_data;
149141

150142
if (queue_work(dev->cbdev->cn_queue,
151143
&__cbq->work))
152-
found = 1;
144+
err = 0;
153145
} else {
154-
printk("%s: cbq->data=%p, "
155-
"work->pending=%08lx.\n",
156-
__func__, __cbq->ddata,
157-
__cbq->work.pending);
158-
WARN_ON(1);
146+
struct work_struct *w;
147+
struct cn_callback_data *d;
148+
149+
w = kzalloc(sizeof(*w) + sizeof(*d), GFP_ATOMIC);
150+
if (w) {
151+
d = (struct cn_callback_data *)(w+1);
152+
153+
d->callback_priv = msg;
154+
d->callback = __cbq->data.callback;
155+
d->ddata = data;
156+
d->destruct_data = destruct_data;
157+
d->free = w;
158+
159+
INIT_LIST_HEAD(&w->entry);
160+
w->pending = 0;
161+
w->func = &cn_queue_wrapper;
162+
w->data = d;
163+
init_timer(&w->timer);
164+
165+
if (queue_work(dev->cbdev->cn_queue, w))
166+
err = 0;
167+
else {
168+
kfree(w);
169+
err = -EINVAL;
170+
}
171+
} else
172+
err = -ENOMEM;
159173
}
160174
break;
161175
}
162176
}
163177
spin_unlock_bh(&dev->cbdev->queue_lock);
164178

165-
return found ? 0 : -ENODEV;
179+
return err;
166180
}
167181

168182
/*
@@ -291,22 +305,10 @@ int cn_add_callback(struct cb_id *id, char *name, void (*callback)(void *))
291305
{
292306
int err;
293307
struct cn_dev *dev = &cdev;
294-
struct cn_callback *cb;
295-
296-
cb = kzalloc(sizeof(*cb), GFP_KERNEL);
297-
if (!cb)
298-
return -ENOMEM;
299-
300-
scnprintf(cb->name, sizeof(cb->name), "%s", name);
301308

302-
memcpy(&cb->id, id, sizeof(cb->id));
303-
cb->callback = callback;
304-
305-
err = cn_queue_add_callback(dev->cbdev, cb);
306-
if (err) {
307-
kfree(cb);
309+
err = cn_queue_add_callback(dev->cbdev, name, id, callback);
310+
if (err)
308311
return err;
309-
}
310312

311313
cn_notify(id, 0);
312314

‎include/linux/connector.h‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,19 @@ struct cn_queue_dev {
104104
struct sock *nls;
105105
};
106106

107-
struct cn_callback {
107+
struct cn_callback_id {
108108
unsigned char name[CN_CBQ_NAMELEN];
109-
110109
struct cb_id id;
110+
};
111+
112+
struct cn_callback_data {
113+
void (*destruct_data) (void *);
114+
void *ddata;
115+
116+
void *callback_priv;
111117
void (*callback) (void *);
112-
void *priv;
118+
119+
void *free;
113120
};
114121

115122
struct cn_callback_entry {
@@ -118,8 +125,8 @@ struct cn_callback_entry {
118125
struct work_struct work;
119126
struct cn_queue_dev *pdev;
120127

121-
void (*destruct_data) (void *);
122-
void *ddata;
128+
struct cn_callback_id id;
129+
struct cn_callback_data data;
123130

124131
int seq, group;
125132
struct sock *nls;
@@ -144,14 +151,16 @@ int cn_add_callback(struct cb_id *, char *, void (*callback) (void *));
144151
void cn_del_callback(struct cb_id *);
145152
int cn_netlink_send(struct cn_msg *, u32, int);
146153

147-
int cn_queue_add_callback(struct cn_queue_dev *dev, struct cn_callback *cb);
154+
int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *));
148155
void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id);
149156

150157
struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *);
151158
void cn_queue_free_dev(struct cn_queue_dev *dev);
152159

153160
int cn_cb_equal(struct cb_id *, struct cb_id *);
154161

162+
void cn_queue_wrapper(void *data);
163+
155164
extern int cn_already_initialized;
156165

157166
#endif /* __KERNEL__ */

0 commit comments

Comments
 (0)