Skip to content

Commit 22a8578

Browse files
ezequielgarciadedekind
authored andcommitted
mtd: mtd_blkdevs: Replace request handler kthread with a workqueue
By replacing a kthread with a workqueue, the code is now a bit clearer. There's also a slight reduction of code size (numbers apply for x86): Before: text data bss dec hex filename 3248 36 0 3284 cd4 drivers/mtd/mtd_blkdevs.o After: text data bss dec hex filename 3150 36 0 3186 c72 drivers/mtd/mtd_blkdevs.o Due to lack of real hardware, tests have been performed on an emulated environment with mtdswap and mtdblock over nandsim devices. Some real testing should be done, before merging this patch. Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com> Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
1 parent 9329c5e commit 22a8578

2 files changed

Lines changed: 18 additions & 33 deletions

File tree

‎drivers/mtd/mtd_blkdevs.c‎

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <linux/hdreg.h>
3333
#include <linux/init.h>
3434
#include <linux/mutex.h>
35-
#include <linux/kthread.h>
3635
#include <asm/uaccess.h>
3736

3837
#include "mtdcore.h"
@@ -121,24 +120,22 @@ static int do_blktrans_request(struct mtd_blktrans_ops *tr,
121120

122121
int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
123122
{
124-
if (kthread_should_stop())
125-
return 1;
126-
127123
return dev->bg_stop;
128124
}
129125
EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
130126

131-
static int mtd_blktrans_thread(void *arg)
127+
static void mtd_blktrans_work(struct work_struct *work)
132128
{
133-
struct mtd_blktrans_dev *dev = arg;
129+
struct mtd_blktrans_dev *dev =
130+
container_of(work, struct mtd_blktrans_dev, work);
134131
struct mtd_blktrans_ops *tr = dev->tr;
135132
struct request_queue *rq = dev->rq;
136133
struct request *req = NULL;
137134
int background_done = 0;
138135

139136
spin_lock_irq(rq->queue_lock);
140137

141-
while (!kthread_should_stop()) {
138+
while (1) {
142139
int res;
143140

144141
dev->bg_stop = false;
@@ -156,15 +153,7 @@ static int mtd_blktrans_thread(void *arg)
156153
background_done = !dev->bg_stop;
157154
continue;
158155
}
159-
set_current_state(TASK_INTERRUPTIBLE);
160-
161-
if (kthread_should_stop())
162-
set_current_state(TASK_RUNNING);
163-
164-
spin_unlock_irq(rq->queue_lock);
165-
schedule();
166-
spin_lock_irq(rq->queue_lock);
167-
continue;
156+
break;
168157
}
169158

170159
spin_unlock_irq(rq->queue_lock);
@@ -185,8 +174,6 @@ static int mtd_blktrans_thread(void *arg)
185174
__blk_end_request_all(req, -EIO);
186175

187176
spin_unlock_irq(rq->queue_lock);
188-
189-
return 0;
190177
}
191178

192179
static void mtd_blktrans_request(struct request_queue *rq)
@@ -199,10 +186,8 @@ static void mtd_blktrans_request(struct request_queue *rq)
199186
if (!dev)
200187
while ((req = blk_fetch_request(rq)) != NULL)
201188
__blk_end_request_all(req, -ENODEV);
202-
else {
203-
dev->bg_stop = true;
204-
wake_up_process(dev->thread);
205-
}
189+
else
190+
queue_work(dev->wq, &dev->work);
206191
}
207192

208193
static int blktrans_open(struct block_device *bdev, fmode_t mode)
@@ -437,14 +422,13 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
437422

438423
gd->queue = new->rq;
439424

440-
/* Create processing thread */
441-
/* TODO: workqueue ? */
442-
new->thread = kthread_run(mtd_blktrans_thread, new,
443-
"%s%d", tr->name, new->mtd->index);
444-
if (IS_ERR(new->thread)) {
445-
ret = PTR_ERR(new->thread);
425+
/* Create processing workqueue */
426+
new->wq = alloc_workqueue("%s%d", 0, 0,
427+
tr->name, new->mtd->index);
428+
if (!new->wq)
446429
goto error4;
447-
}
430+
INIT_WORK(&new->work, mtd_blktrans_work);
431+
448432
gd->driverfs_dev = &new->mtd->dev;
449433

450434
if (new->readonly)
@@ -484,9 +468,8 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
484468
/* Stop new requests to arrive */
485469
del_gendisk(old->disk);
486470

487-
488-
/* Stop the thread */
489-
kthread_stop(old->thread);
471+
/* Stop workqueue. This will perform any pending request. */
472+
destroy_workqueue(old->wq);
490473

491474
/* Kill current requests */
492475
spin_lock_irqsave(&old->queue_lock, flags);

‎include/linux/mtd/blktrans.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/mutex.h>
2424
#include <linux/kref.h>
2525
#include <linux/sysfs.h>
26+
#include <linux/workqueue.h>
2627

2728
struct hd_geometry;
2829
struct mtd_info;
@@ -43,7 +44,8 @@ struct mtd_blktrans_dev {
4344
struct kref ref;
4445
struct gendisk *disk;
4546
struct attribute_group *disk_attributes;
46-
struct task_struct *thread;
47+
struct workqueue_struct *wq;
48+
struct work_struct work;
4749
struct request_queue *rq;
4850
spinlock_t queue_lock;
4951
void *priv;

0 commit comments

Comments
 (0)