Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1378997

[PATCH v6 18/20] memstick/r592: convert r592_io kthread into kthread worker API

From Petr Mladek <pmladek@suse.com>
Newsgroups linux.kernel
Subject [PATCH v6 18/20] memstick/r592: convert r592_io kthread into kthread worker API
Date 2016-04-14 17:20 +0200
Message-ID <rnRdM-3Ro-23@gated-at.bofh.it> (permalink)
References <rnRdL-3Ro-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v6 00/20] kthread: Use kthread worker API more widely Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 18/20] memstick/r592: convert r592_io kthread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 15/20] ipmi: Convert kipmi kthread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 07/20] kthread: Initial support for delayed kthread work Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 19/20] thermal/intel_powerclamp: Remove duplicated code that starts the kthread Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 16/20] IB/fmr_pool: Convert the cleanup thread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 12/20] ring_buffer: Convert benchmark kthreads into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 20/20] thermal/intel_powerclamp: Convert the kthread to kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 02/20] kthread: Allow to call __kthread_create_on_node() with va_list args Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 03/20] kthread: Add create_kthread_worker*() Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 11/20] mm/huge_page: Convert khugepaged() into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 17/20] memstick/r592: Better synchronize debug messages in r592_io kthread Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 05/20] kthread: Add destroy_kthread_worker() Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 13/20] hung_task: Convert hungtaskd into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-04-14 17:20 +0200
  [PATCH v6 01/20] kthread/smpboot: Do not park in kthread_create_on_cpu() Petr Mladek <pmladek@suse.com> - 2016-04-14 17:30 +0200
  [PATCH v6 06/20] kthread: Detect when a kthread work is used by more workers Petr Mladek <pmladek@suse.com> - 2016-04-14 17:30 +0200
  [PATCH v6 08/20] kthread: Allow to cancel kthread work Petr Mladek <pmladek@suse.com> - 2016-04-14 17:30 +0200
  [PATCH v6 04/20] kthread: Add drain_kthread_worker() Petr Mladek <pmladek@suse.com> - 2016-04-14 17:30 +0200

csiph-web