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>,
Doug Ledford <dledford@redhat.com>,
Sean Hefty <sean.hefty@intel.com>,
Hal Rosenstock <hal.rosenstock@gmail.com>,
linux-rdma@vger.kernel.org
Subject: [PATCH v5 16/20] IB/fmr_pool: Convert the cleanup thread into kthread worker API
Date: Mon, 22 Feb 2016 15:57:06 +0100 [thread overview]
Message-ID: <1456153030-12400-17-git-send-email-pmladek@suse.com> (raw)
In-Reply-To: <1456153030-12400-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 frm_pool kthread into the kthread worker
API because I am not sure how busy the thread is. It is well
possible that it does not need a dedicated kthread and workqueues
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 work function.
It preserves the check for a spurious queuing (wake up). Then it
processes one request. Finally, it re-queues itself if more requests
are pending.
Otherwise, wake_up_process() is replaced by queuing the work.
Important: The change is only compile tested. I did not find an easy
way how to check it in a real life.
Signed-off-by: Petr Mladek <pmladek@suse.com>
CC: Doug Ledford <dledford@redhat.com>
CC: Sean Hefty <sean.hefty@intel.com>
CC: Hal Rosenstock <hal.rosenstock@gmail.com>
CC: linux-rdma@vger.kernel.org
---
drivers/infiniband/core/fmr_pool.c | 54 ++++++++++++++++++--------------------
1 file changed, 25 insertions(+), 29 deletions(-)
diff --git a/drivers/infiniband/core/fmr_pool.c b/drivers/infiniband/core/fmr_pool.c
index 6ac3683c144b..8f0307b74fcd 100644
--- a/drivers/infiniband/core/fmr_pool.c
+++ b/drivers/infiniband/core/fmr_pool.c
@@ -96,7 +96,8 @@ struct ib_fmr_pool {
void * arg);
void *flush_arg;
- struct task_struct *thread;
+ struct kthread_worker *worker;
+ struct kthread_work work;
atomic_t req_ser;
atomic_t flush_ser;
@@ -174,29 +175,26 @@ static void ib_fmr_batch_release(struct ib_fmr_pool *pool)
spin_unlock_irq(&pool->pool_lock);
}
-static int ib_fmr_cleanup_thread(void *pool_ptr)
+static void ib_fmr_cleanup_func(struct kthread_work *work)
{
- struct ib_fmr_pool *pool = pool_ptr;
+ struct ib_fmr_pool *pool = container_of(work, struct ib_fmr_pool, work);
- do {
- if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0) {
- ib_fmr_batch_release(pool);
-
- atomic_inc(&pool->flush_ser);
- wake_up_interruptible(&pool->force_wait);
+ /*
+ * The same request might be queued twice when it appears and
+ * by re-queuing from this work.
+ */
+ if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0)
+ return;
- if (pool->flush_function)
- pool->flush_function(pool, pool->flush_arg);
- }
+ ib_fmr_batch_release(pool);
+ atomic_inc(&pool->flush_ser);
+ wake_up_interruptible(&pool->force_wait);
- set_current_state(TASK_INTERRUPTIBLE);
- if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) >= 0 &&
- !kthread_should_stop())
- schedule();
- __set_current_state(TASK_RUNNING);
- } while (!kthread_should_stop());
+ if (pool->flush_function)
+ pool->flush_function(pool, pool->flush_arg);
- return 0;
+ if (atomic_read(&pool->flush_ser) - atomic_read(&pool->req_ser) < 0)
+ queue_kthread_work(pool->worker, &pool->work);
}
/**
@@ -270,15 +268,13 @@ struct ib_fmr_pool *ib_create_fmr_pool(struct ib_pd *pd,
atomic_set(&pool->flush_ser, 0);
init_waitqueue_head(&pool->force_wait);
- pool->thread = kthread_run(ib_fmr_cleanup_thread,
- pool,
- "ib_fmr(%s)",
- device->name);
- if (IS_ERR(pool->thread)) {
- printk(KERN_WARNING PFX "couldn't start cleanup thread\n");
- ret = PTR_ERR(pool->thread);
+ pool->worker = create_kthread_worker(0, "ib_fmr(%s)", device->name);
+ if (IS_ERR(pool->worker)) {
+ pr_warn(PFX "couldn't start cleanup kthread worker\n");
+ ret = PTR_ERR(pool->worker);
goto out_free_pool;
}
+ init_kthread_work(&pool->work, ib_fmr_cleanup_func);
{
struct ib_pool_fmr *fmr;
@@ -346,7 +342,7 @@ void ib_destroy_fmr_pool(struct ib_fmr_pool *pool)
LIST_HEAD(fmr_list);
int i;
- kthread_stop(pool->thread);
+ destroy_kthread_worker(pool->worker);
ib_fmr_batch_release(pool);
i = 0;
@@ -396,7 +392,7 @@ int ib_flush_fmr_pool(struct ib_fmr_pool *pool)
spin_unlock_irq(&pool->pool_lock);
serial = atomic_inc_return(&pool->req_ser);
- wake_up_process(pool->thread);
+ queue_kthread_work(pool->worker, &pool->work);
if (wait_event_interruptible(pool->force_wait,
atomic_read(&pool->flush_ser) - serial >= 0))
@@ -510,7 +506,7 @@ int ib_fmr_pool_unmap(struct ib_pool_fmr *fmr)
list_add_tail(&fmr->list, &pool->dirty_list);
if (++pool->dirty_len >= pool->dirty_watermark) {
atomic_inc(&pool->req_ser);
- wake_up_process(pool->thread);
+ queue_kthread_work(pool->worker, &pool->work);
}
}
}
--
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-02-22 14:57 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-22 14:56 [PATCH v5 00/20] kthread: Use kthread worker API more widely Petr Mladek
2016-02-22 14:56 ` [PATCH v5 02/20] kthread: Allow to call __kthread_create_on_node() with va_list args Petr Mladek
[not found] ` <1456153030-12400-1-git-send-email-pmladek-IBi9RG/b67k@public.gmane.org>
2016-02-22 14:56 ` [PATCH v5 01/20] kthread/smpboot: Do not park in kthread_create_on_cpu() Petr Mladek
2016-02-22 14:56 ` [PATCH v5 03/20] kthread: Add create_kthread_worker*() Petr Mladek
2016-02-22 15:48 ` kbuild test robot
2016-02-24 15:56 ` Petr Mladek
2016-02-22 14:56 ` [PATCH v5 04/20] kthread: Add drain_kthread_worker() Petr Mladek
2016-02-25 12:35 ` Peter Zijlstra
[not found] ` <20160225123551.GG6357-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2016-02-26 15:23 ` Petr Mladek
[not found] ` <20160226152309.GH3305-KsEp0d+Q8qECVLCxKZUutA@public.gmane.org>
2016-02-27 15:18 ` Peter Zijlstra
2016-02-22 14:56 ` [PATCH v5 05/20] kthread: Add destroy_kthread_worker() Petr Mladek
[not found] ` <1456153030-12400-6-git-send-email-pmladek-IBi9RG/b67k@public.gmane.org>
2016-02-25 12:36 ` Peter Zijlstra
[not found] ` <20160225123641.GH6357-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2016-02-26 15:15 ` Petr Mladek
2016-02-22 14:56 ` [PATCH v5 06/20] kthread: Detect when a kthread work is used by more workers Petr Mladek
2016-02-22 14:56 ` [PATCH v5 07/20] kthread: Initial support for delayed kthread work Petr Mladek
2016-02-22 16:06 ` kbuild test robot
2016-02-24 16:09 ` Petr Mladek
2016-02-22 14:56 ` [PATCH v5 08/20] kthread: Allow to cancel " Petr Mladek
[not found] ` <1456153030-12400-9-git-send-email-pmladek-IBi9RG/b67k@public.gmane.org>
2016-02-22 16:50 ` kbuild test robot
2016-02-24 16:18 ` Petr Mladek
2016-02-25 12:59 ` Peter Zijlstra
[not found] ` <20160225125932.GI6357-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2016-02-26 15:38 ` Petr Mladek
2016-02-26 16:25 ` Peter Zijlstra
[not found] ` <20160226162552.GB6356-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2016-02-26 17:00 ` Petr Mladek
[not found] ` <20160226170056.GA12548-KsEp0d+Q8qECVLCxKZUutA@public.gmane.org>
2016-02-27 15:16 ` Peter Zijlstra
2016-02-22 14:56 ` [PATCH v5 09/20] kthread: Allow to modify delayed " Petr Mladek
2016-02-22 14:57 ` [PATCH v5 10/20] kthread: Better support freezable kthread workers Petr Mladek
2016-02-22 17:33 ` kbuild test robot
2016-02-24 16:25 ` Petr Mladek
[not found] ` <1456153030-12400-11-git-send-email-pmladek-IBi9RG/b67k@public.gmane.org>
2016-02-25 13:01 ` Peter Zijlstra
[not found] ` <20160225130115.GJ6357-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org>
2016-02-26 15:43 ` Petr Mladek
2016-02-22 14:57 ` [PATCH v5 11/20] mm/huge_page: Convert khugepaged() into kthread worker API Petr Mladek
2016-02-22 14:57 ` [PATCH v5 12/20] ring_buffer: Convert benchmark kthreads " Petr Mladek
2016-02-22 14:57 ` [PATCH v5 13/20] hung_task: Convert hungtaskd " Petr Mladek
2016-02-22 14:57 ` [PATCH v5 14/20] kmemleak: Convert kmemleak kthread " Petr Mladek
2016-02-22 14:57 ` [PATCH v5 15/20] ipmi: Convert kipmi " Petr Mladek
2016-02-22 14:57 ` Petr Mladek [this message]
2016-02-22 14:57 ` [PATCH v5 17/20] memstick/r592: Better synchronize debug messages in r592_io kthread Petr Mladek
2016-02-22 14:57 ` [PATCH v5 18/20] memstick/r592: convert r592_io kthread into kthread worker API Petr Mladek
2016-02-22 14:57 ` [PATCH v5 19/20] thermal/intel_powerclamp: Remove duplicated code that starts the kthread Petr Mladek
2016-02-22 14:57 ` [PATCH v5 20/20] thermal/intel_powerclamp: Convert the kthread to kthread worker API Petr Mladek
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=1456153030-12400-17-git-send-email-pmladek@suse.com \
--to=pmladek@suse.com \
--cc=akpm@linux-foundation.org \
--cc=bp@suse.de \
--cc=dledford@redhat.com \
--cc=hal.rosenstock@gmail.com \
--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=linux-rdma@vger.kernel.org \
--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=sean.hefty@intel.com \
--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).