From: Petr Mladek <pmladek@suse.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Oleg Nesterov <oleg@redhat.com>, Tejun Heo <tj@kernel.org>,
Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
Josh Triplett <josh@joshtriplett.org>,
Thomas Gleixner <tglx@linutronix.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
Jiri Kosina <jkosina@suse.cz>, Borislav Petkov <bp@suse.de>,
Michal Hocko <mhocko@suse.cz>,
linux-mm@kvack.org, Vlastimil Babka <vbabka@suse.cz>,
linux-api@vger.kernel.org, linux-kernel@vger.kernel.org,
Petr Mladek <pmladek@suse.com>,
Maxim Levitsky <maximlevitsky@gmail.com>
Subject: [PATCH v6 18/20] memstick/r592: convert r592_io kthread into kthread worker API
Date: Thu, 14 Apr 2016 17:14:37 +0200 [thread overview]
Message-ID: <1460646879-617-19-git-send-email-pmladek@suse.com> (raw)
In-Reply-To: <1460646879-617-1-git-send-email-pmladek@suse.com>
Kthreads are currently implemented as an infinite loop. Each
has its own variant of checks for terminating, freezing,
awakening. In many cases it is unclear to say in which state
it is and sometimes it is done a wrong way.
The plan is to convert kthreads into kthread_worker or workqueues
API. It allows to split the functionality into separate operations.
It helps to make a better structure. Also it defines a clean state
where no locks are taken, IRQs blocked, the kthread might sleep
or even be safely migrated.
The kthread worker API is useful when we want to have a dedicated
single thread for the work. It helps to make sure that it is
available when needed. Also it allows a better control, e.g.
define a scheduling priority.
This patch converts the r592_io kthread into the kthread worker
API. I am not sure how busy the kthread is and if anyone would
like to control the resources. It is well possible that a workqueue
would be perfectly fine. Well, the conversion between kthread
worker API and workqueues is pretty trivial.
The patch moves one iteration from the kthread into the kthread
worker function. It helps to remove all the hackery with process
state and kthread_should_stop().
The work is queued instead of waking the thread.
The work is explicitly canceled before the worker is destroyed.
It is self-queuing and it might take a long time until the queue
is drained, otherwise.
Important: The change is only compile tested. I did not find an easy
way how to check it in use.
Signed-off-by: Petr Mladek <pmladek@suse.com>
CC: Maxim Levitsky <maximlevitsky@gmail.com>
---
drivers/memstick/host/r592.c | 58 ++++++++++++++++++++------------------------
drivers/memstick/host/r592.h | 3 ++-
2 files changed, 28 insertions(+), 33 deletions(-)
diff --git a/drivers/memstick/host/r592.c b/drivers/memstick/host/r592.c
index 7d29d6549110..1d8547b12eca 100644
--- a/drivers/memstick/host/r592.c
+++ b/drivers/memstick/host/r592.c
@@ -562,40 +562,32 @@ out:
return;
}
-/* Main request processing thread */
-static int r592_process_thread(void *data)
+/* Main request processing work */
+static void r592_process_func(struct kthread_work *work)
{
int error;
- struct r592_device *dev = (struct r592_device *)data;
-
- while (!kthread_should_stop()) {
- if (!dev->io_started) {
- dbg_verbose("IO: started");
- dev->io_started = true;
- }
-
- set_current_state(TASK_INTERRUPTIBLE);
- error = memstick_next_req(dev->host, &dev->req);
+ struct r592_device *dev =
+ container_of(work, struct r592_device, io_work);
- if (error) {
- if (error == -ENXIO || error == -EAGAIN) {
- dbg_verbose("IO: done");
- } else {
- dbg("IO: unknown error from "
- "memstick_next_req %d", error);
- }
- dev->io_started = false;
+ if (!dev->io_started) {
+ dbg_verbose("IO: started");
+ dev->io_started = true;
+ }
- if (kthread_should_stop())
- set_current_state(TASK_RUNNING);
+ error = memstick_next_req(dev->host, &dev->req);
- schedule();
+ if (error) {
+ if (error == -ENXIO || error == -EAGAIN) {
+ dbg_verbose("IO: done");
} else {
- set_current_state(TASK_RUNNING);
- r592_execute_tpc(dev);
+ dbg("IO: unknown error from memstick_next_req %d",
+ error);
}
+ dev->io_started = false;
+ } else {
+ r592_execute_tpc(dev);
+ queue_kthread_work(dev->io_worker, &dev->io_work);
}
- return 0;
}
/* Reprogram chip to detect change in card state */
@@ -720,7 +712,7 @@ static void r592_submit_req(struct memstick_host *host)
if (dev->req)
return;
- wake_up_process(dev->io_thread);
+ queue_kthread_work(dev->io_worker, &dev->io_work);
}
static const struct pci_device_id r592_pci_id_tbl[] = {
@@ -778,9 +770,10 @@ static int r592_probe(struct pci_dev *pdev, const struct pci_device_id *id)
r592_check_dma(dev);
dev->io_started = false;
- dev->io_thread = kthread_run(r592_process_thread, dev, "r592_io");
- if (IS_ERR(dev->io_thread)) {
- error = PTR_ERR(dev->io_thread);
+ init_kthread_work(&dev->io_work, r592_process_func);
+ dev->io_worker = create_kthread_worker(0, "r592_io");
+ if (IS_ERR(dev->io_worker)) {
+ error = PTR_ERR(dev->io_worker);
goto error5;
}
@@ -806,7 +799,7 @@ error6:
dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page,
dev->dummy_dma_page_physical_address);
- kthread_stop(dev->io_thread);
+ destroy_kthread_worker(dev->io_worker);
error5:
iounmap(dev->mmio);
error4:
@@ -826,7 +819,8 @@ static void r592_remove(struct pci_dev *pdev)
/* Stop the processing thread.
That ensures that we won't take any more requests */
- kthread_stop(dev->io_thread);
+ cancel_kthread_work_sync(&dev->io_work);
+ destroy_kthread_worker(dev->io_worker);
r592_enable_device(dev, false);
diff --git a/drivers/memstick/host/r592.h b/drivers/memstick/host/r592.h
index aa8f0f22f4ce..1ac71380ac04 100644
--- a/drivers/memstick/host/r592.h
+++ b/drivers/memstick/host/r592.h
@@ -139,7 +139,8 @@ struct r592_device {
spinlock_t irq_lock;
struct timer_list detect_timer;
- struct task_struct *io_thread;
+ struct kthread_worker *io_worker;
+ struct kthread_work io_work;
bool io_started;
bool parallel_mode;
--
1.8.5.6
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2016-04-14 15:14 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-14 15:14 [PATCH v6 00/20] kthread: Use kthread worker API more widely Petr Mladek
2016-04-14 15:14 ` [PATCH v6 01/20] kthread/smpboot: Do not park in kthread_create_on_cpu() Petr Mladek
2016-04-14 15:14 ` [PATCH v6 02/20] kthread: Allow to call __kthread_create_on_node() with va_list args Petr Mladek
2016-04-14 15:14 ` [PATCH v6 03/20] kthread: Add create_kthread_worker*() Petr Mladek
2016-04-14 15:14 ` [PATCH v6 04/20] kthread: Add drain_kthread_worker() Petr Mladek
2016-04-14 15:14 ` [PATCH v6 05/20] kthread: Add destroy_kthread_worker() Petr Mladek
2016-04-14 15:14 ` [PATCH v6 06/20] kthread: Detect when a kthread work is used by more workers Petr Mladek
2016-04-14 15:14 ` [PATCH v6 07/20] kthread: Initial support for delayed kthread work Petr Mladek
2016-04-14 15:14 ` [PATCH v6 08/20] kthread: Allow to cancel " Petr Mladek
2016-04-14 15:14 ` [PATCH v6 09/20] kthread: Allow to modify delayed " Petr Mladek
2016-04-14 15:14 ` [PATCH v6 10/20] kthread: Better support freezable kthread workers Petr Mladek
2016-04-14 15:14 ` [PATCH v6 11/20] mm/huge_page: Convert khugepaged() into kthread worker API Petr Mladek
2016-04-14 15:14 ` [PATCH v6 12/20] ring_buffer: Convert benchmark kthreads " Petr Mladek
2016-04-14 15:14 ` [PATCH v6 13/20] hung_task: Convert hungtaskd " Petr Mladek
2016-04-14 15:14 ` [PATCH v6 14/20] kmemleak: Convert kmemleak kthread " Petr Mladek
2016-04-14 15:14 ` [PATCH v6 15/20] ipmi: Convert kipmi " Petr Mladek
2016-04-14 15:14 ` [PATCH v6 16/20] IB/fmr_pool: Convert the cleanup thread " Petr Mladek
[not found] ` <1460646879-617-17-git-send-email-pmladek-IBi9RG/b67k@public.gmane.org>
2016-05-18 15:36 ` Doug Ledford
2016-04-14 15:14 ` [PATCH v6 17/20] memstick/r592: Better synchronize debug messages in r592_io kthread Petr Mladek
2016-04-14 15:14 ` Petr Mladek [this message]
2016-04-14 15:14 ` [PATCH v6 19/20] thermal/intel_powerclamp: Remove duplicated code that starts the kthread Petr Mladek
2016-04-14 15:14 ` [PATCH v6 20/20] thermal/intel_powerclamp: Convert the kthread to kthread worker API Petr Mladek
2016-08-25 8:33 ` Sebastian Andrzej Siewior
2016-08-25 11:37 ` Petr Mladek
2016-08-25 11:44 ` Sebastian Andrzej Siewior
2016-04-22 18:30 ` [PATCH v6 00/20] kthread: Use kthread worker API more widely Tejun Heo
2016-05-11 10:52 ` Petr Mladek
2016-05-25 20:42 ` Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1460646879-617-19-git-send-email-pmladek@suse.com \
--to=pmladek@suse.com \
--cc=akpm@linux-foundation.org \
--cc=bp@suse.de \
--cc=jkosina@suse.cz \
--cc=josh@joshtriplett.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=maximlevitsky@gmail.com \
--cc=mhocko@suse.cz \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).