Skip to content

Commit af336ca

Browse files
ausyskingregkh
authored andcommitted
mei: limit the number of queued writes
Limit the number of queued writes per client. Writes above this threshold are blocked till place in the transmit queue is available. The limit is configurable via sysfs and defaults to 50. The implementation should provide blocking I/O behavior. Prior to this change one would end up in the hands of OOM. Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com> Signed-off-by: Tomas Winkler <tomas.winkler@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 257355a commit af336ca

7 files changed

Lines changed: 154 additions & 35 deletions

File tree

‎Documentation/ABI/testing/sysfs-class-mei‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ Contact: Tomas Winkler <tomas.winkler@intel.com>
4545
Description: Display the driver HBM protocol version.
4646

4747
The HBM protocol version supported by the driver.
48+
49+
What: /sys/class/mei/meiN/tx_queue_limit
50+
Date: Jan 2018
51+
KernelVersion: 4.16
52+
Contact: Tomas Winkler <tomas.winkler@intel.com>
53+
Description: Configure tx queue limit
54+
55+
Set maximal number of pending writes
56+
per opened session.

‎drivers/misc/mei/bus.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
7474
goto out;
7575
}
7676

77+
while (cl->tx_cb_queued >= bus->tx_queue_limit) {
78+
mutex_unlock(&bus->device_lock);
79+
rets = wait_event_interruptible(cl->tx_wait,
80+
cl->writing_state == MEI_WRITE_COMPLETE ||
81+
(!mei_cl_is_connected(cl)));
82+
mutex_lock(&bus->device_lock);
83+
if (rets) {
84+
if (signal_pending(current))
85+
rets = -EINTR;
86+
goto out;
87+
}
88+
if (!mei_cl_is_connected(cl)) {
89+
rets = -ENODEV;
90+
goto out;
91+
}
92+
}
93+
7794
cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
7895
if (!cb) {
7996
rets = -ENOMEM;

‎drivers/misc/mei/client.c‎

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,36 @@ void mei_io_cb_free(struct mei_cl_cb *cb)
349349
kfree(cb);
350350
}
351351

352+
/**
353+
* mei_tx_cb_queue - queue tx callback
354+
*
355+
* Locking: called under "dev->device_lock" lock
356+
*
357+
* @cb: mei callback struct
358+
* @head: an instance of list to queue on
359+
*/
360+
static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
361+
struct list_head *head)
362+
{
363+
list_add_tail(&cb->list, head);
364+
cb->cl->tx_cb_queued++;
365+
}
366+
367+
/**
368+
* mei_tx_cb_dequeue - dequeue tx callback
369+
*
370+
* Locking: called under "dev->device_lock" lock
371+
*
372+
* @cb: mei callback struct to dequeue and free
373+
*/
374+
static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
375+
{
376+
if (!WARN_ON(cb->cl->tx_cb_queued == 0))
377+
cb->cl->tx_cb_queued--;
378+
379+
mei_io_cb_free(cb);
380+
}
381+
352382
/**
353383
* mei_io_cb_init - allocate and initialize io callback
354384
*
@@ -377,49 +407,37 @@ static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
377407
}
378408

379409
/**
380-
* __mei_io_list_flush_cl - removes and frees cbs belonging to cl.
410+
* mei_io_list_flush_cl - removes cbs belonging to the cl.
381411
*
382412
* @head: an instance of our list structure
383-
* @cl: host client, can be NULL for flushing the whole list
384-
* @free: whether to free the cbs
413+
* @cl: host client
385414
*/
386-
static void __mei_io_list_flush_cl(struct list_head *head,
387-
const struct mei_cl *cl, bool free)
415+
static void mei_io_list_flush_cl(struct list_head *head,
416+
const struct mei_cl *cl)
388417
{
389418
struct mei_cl_cb *cb, *next;
390419

391-
/* enable removing everything if no cl is specified */
392420
list_for_each_entry_safe(cb, next, head, list) {
393-
if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
421+
if (mei_cl_cmp_id(cl, cb->cl))
394422
list_del_init(&cb->list);
395-
if (free)
396-
mei_io_cb_free(cb);
397-
}
398423
}
399424
}
400425

401426
/**
402-
* mei_io_list_flush_cl - removes list entry belonging to cl.
427+
* mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
403428
*
404429
* @head: An instance of our list structure
405430
* @cl: host client
406431
*/
407-
static inline void mei_io_list_flush_cl(struct list_head *head,
408-
const struct mei_cl *cl)
432+
static void mei_io_tx_list_free_cl(struct list_head *head,
433+
const struct mei_cl *cl)
409434
{
410-
__mei_io_list_flush_cl(head, cl, false);
411-
}
435+
struct mei_cl_cb *cb, *next;
412436

413-
/**
414-
* mei_io_list_free_cl - removes cb belonging to cl and free them
415-
*
416-
* @head: An instance of our list structure
417-
* @cl: host client
418-
*/
419-
static inline void mei_io_list_free_cl(struct list_head *head,
420-
const struct mei_cl *cl)
421-
{
422-
__mei_io_list_flush_cl(head, cl, true);
437+
list_for_each_entry_safe(cb, next, head, list) {
438+
if (mei_cl_cmp_id(cl, cb->cl))
439+
mei_tx_cb_dequeue(cb);
440+
}
423441
}
424442

425443
/**
@@ -538,8 +556,8 @@ int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
538556
dev = cl->dev;
539557

540558
cl_dbg(dev, cl, "remove list entry belonging to cl\n");
541-
mei_io_list_free_cl(&cl->dev->write_list, cl);
542-
mei_io_list_free_cl(&cl->dev->write_waiting_list, cl);
559+
mei_io_tx_list_free_cl(&cl->dev->write_list, cl);
560+
mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl);
543561
mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
544562
mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
545563
mei_io_list_free_fp(&cl->rd_pending, fp);
@@ -756,8 +774,8 @@ static void mei_cl_set_disconnected(struct mei_cl *cl)
756774
return;
757775

758776
cl->state = MEI_FILE_DISCONNECTED;
759-
mei_io_list_free_cl(&dev->write_list, cl);
760-
mei_io_list_free_cl(&dev->write_waiting_list, cl);
777+
mei_io_tx_list_free_cl(&dev->write_list, cl);
778+
mei_io_tx_list_free_cl(&dev->write_waiting_list, cl);
761779
mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
762780
mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
763781
mei_cl_wake_all(cl);
@@ -1693,9 +1711,9 @@ int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
16931711

16941712
out:
16951713
if (mei_hdr.msg_complete)
1696-
list_add_tail(&cb->list, &dev->write_waiting_list);
1714+
mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
16971715
else
1698-
list_add_tail(&cb->list, &dev->write_list);
1716+
mei_tx_cb_enqueue(cb, &dev->write_list);
16991717

17001718
cb = NULL;
17011719
if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
@@ -1741,7 +1759,7 @@ void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
17411759

17421760
switch (cb->fop_type) {
17431761
case MEI_FOP_WRITE:
1744-
mei_io_cb_free(cb);
1762+
mei_tx_cb_dequeue(cb);
17451763
cl->writing_state = MEI_WRITE_COMPLETE;
17461764
if (waitqueue_active(&cl->tx_wait)) {
17471765
wake_up_interruptible(&cl->tx_wait);

‎drivers/misc/mei/debugfs.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static ssize_t mei_dbgfs_read_active(struct file *fp, char __user *ubuf,
9797
int pos = 0;
9898
int ret;
9999

100-
#define HDR " |me|host|state|rd|wr|\n"
100+
#define HDR " |me|host|state|rd|wr|wrq\n"
101101

102102
if (!dev)
103103
return -ENODEV;
@@ -130,9 +130,10 @@ static ssize_t mei_dbgfs_read_active(struct file *fp, char __user *ubuf,
130130
list_for_each_entry(cl, &dev->file_list, link) {
131131

132132
pos += scnprintf(buf + pos, bufsz - pos,
133-
"%3d|%2d|%4d|%5d|%2d|%2d|\n",
133+
"%3d|%2d|%4d|%5d|%2d|%2d|%3u\n",
134134
i, mei_cl_me_id(cl), cl->host_client_id, cl->state,
135-
!list_empty(&cl->rd_completed), cl->writing_state);
135+
!list_empty(&cl->rd_completed), cl->writing_state,
136+
cl->tx_cb_queued);
136137
i++;
137138
}
138139
out:

‎drivers/misc/mei/init.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ void mei_device_init(struct mei_device *dev,
383383
INIT_LIST_HEAD(&dev->write_waiting_list);
384384
INIT_LIST_HEAD(&dev->ctrl_wr_list);
385385
INIT_LIST_HEAD(&dev->ctrl_rd_list);
386+
dev->tx_queue_limit = MEI_TX_QUEUE_LIMIT_DEFAULT;
386387

387388
INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
388389
INIT_WORK(&dev->reset_work, mei_reset_work);

‎drivers/misc/mei/main.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,27 @@ static ssize_t mei_write(struct file *file, const char __user *ubuf,
291291
goto out;
292292
}
293293

294+
while (cl->tx_cb_queued >= dev->tx_queue_limit) {
295+
if (file->f_flags & O_NONBLOCK) {
296+
rets = -EAGAIN;
297+
goto out;
298+
}
299+
mutex_unlock(&dev->device_lock);
300+
rets = wait_event_interruptible(cl->tx_wait,
301+
cl->writing_state == MEI_WRITE_COMPLETE ||
302+
(!mei_cl_is_connected(cl)));
303+
mutex_lock(&dev->device_lock);
304+
if (rets) {
305+
if (signal_pending(current))
306+
rets = -EINTR;
307+
goto out;
308+
}
309+
if (!mei_cl_is_connected(cl)) {
310+
rets = -ENODEV;
311+
goto out;
312+
}
313+
}
314+
294315
*offset = 0;
295316
cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
296317
if (!cb) {
@@ -580,6 +601,12 @@ static __poll_t mei_poll(struct file *file, poll_table *wait)
580601
mei_cl_read_start(cl, mei_cl_mtu(cl), file);
581602
}
582603

604+
if (req_events & (POLLOUT | POLLWRNORM)) {
605+
poll_wait(file, &cl->tx_wait, wait);
606+
if (cl->tx_cb_queued < dev->tx_queue_limit)
607+
mask |= POLLOUT | POLLWRNORM;
608+
}
609+
583610
out:
584611
mutex_unlock(&dev->device_lock);
585612
return mask;
@@ -749,10 +776,48 @@ static ssize_t hbm_ver_drv_show(struct device *device,
749776
}
750777
static DEVICE_ATTR_RO(hbm_ver_drv);
751778

779+
static ssize_t tx_queue_limit_show(struct device *device,
780+
struct device_attribute *attr, char *buf)
781+
{
782+
struct mei_device *dev = dev_get_drvdata(device);
783+
u8 size = 0;
784+
785+
mutex_lock(&dev->device_lock);
786+
size = dev->tx_queue_limit;
787+
mutex_unlock(&dev->device_lock);
788+
789+
return snprintf(buf, PAGE_SIZE, "%u\n", size);
790+
}
791+
792+
static ssize_t tx_queue_limit_store(struct device *device,
793+
struct device_attribute *attr,
794+
const char *buf, size_t count)
795+
{
796+
struct mei_device *dev = dev_get_drvdata(device);
797+
u8 limit;
798+
unsigned int inp;
799+
int err;
800+
801+
err = kstrtouint(buf, 10, &inp);
802+
if (err)
803+
return err;
804+
if (inp > MEI_TX_QUEUE_LIMIT_MAX || inp < MEI_TX_QUEUE_LIMIT_MIN)
805+
return -EINVAL;
806+
limit = inp;
807+
808+
mutex_lock(&dev->device_lock);
809+
dev->tx_queue_limit = limit;
810+
mutex_unlock(&dev->device_lock);
811+
812+
return count;
813+
}
814+
static DEVICE_ATTR_RW(tx_queue_limit);
815+
752816
static struct attribute *mei_attrs[] = {
753817
&dev_attr_fw_status.attr,
754818
&dev_attr_hbm_ver.attr,
755819
&dev_attr_hbm_ver_drv.attr,
820+
&dev_attr_tx_queue_limit.attr,
756821
NULL
757822
};
758823
ATTRIBUTE_GROUPS(mei);

‎drivers/misc/mei/mei_dev.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ struct mei_cl_cb {
210210
* @timer_count: watchdog timer for operation completion
211211
* @notify_en: notification - enabled/disabled
212212
* @notify_ev: pending notification event
213+
* @tx_cb_queued: number of tx callbacks in queue
213214
* @writing_state: state of the tx
214215
* @rd_pending: pending read credits
215216
* @rd_completed: completed read
@@ -234,13 +235,18 @@ struct mei_cl {
234235
u8 timer_count;
235236
u8 notify_en;
236237
u8 notify_ev;
238+
u8 tx_cb_queued;
237239
enum mei_file_transaction_states writing_state;
238240
struct list_head rd_pending;
239241
struct list_head rd_completed;
240242

241243
struct mei_cl_device *cldev;
242244
};
243245

246+
#define MEI_TX_QUEUE_LIMIT_DEFAULT 50
247+
#define MEI_TX_QUEUE_LIMIT_MAX 255
248+
#define MEI_TX_QUEUE_LIMIT_MIN 30
249+
244250
/**
245251
* struct mei_hw_ops - hw specific ops
246252
*
@@ -359,6 +365,7 @@ const char *mei_pg_state_str(enum mei_pg_state state);
359365
* @write_waiting_list : write completion list
360366
* @ctrl_wr_list : pending control write list
361367
* @ctrl_rd_list : pending control read list
368+
* @tx_queue_limit: tx queues per client linit
362369
*
363370
* @file_list : list of opened handles
364371
* @open_handle_count: number of opened handles
@@ -423,6 +430,7 @@ struct mei_device {
423430
struct list_head write_waiting_list;
424431
struct list_head ctrl_wr_list;
425432
struct list_head ctrl_rd_list;
433+
u8 tx_queue_limit;
426434

427435
struct list_head file_list;
428436
long open_handle_count;

0 commit comments

Comments
 (0)