From: Petr Mladek <pmladek@suse.cz>
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: Richard Weinberger <richard@nod.at>,
Steven Rostedt <rostedt@goodmis.org>,
David Woodhouse <dwmw2@infradead.org>,
linux-mtd@lists.infradead.org,
Trond Myklebust <trond.myklebust@primarydata.com>,
Anna Schumaker <anna.schumaker@netapp.com>,
linux-nfs@vger.kernel.org, Chris Mason <clm@fb.com>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
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>,
live-patching@vger.kernel.org, linux-api@vger.kernel.org,
linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.cz>
Subject: [RFC PATCH 11/18] jffs2: Convert jffs2_gcd_mtd kthread into the iterant API
Date: Fri, 5 Jun 2015 17:01:10 +0200 [thread overview]
Message-ID: <1433516477-5153-12-git-send-email-pmladek@suse.cz> (raw)
In-Reply-To: <1433516477-5153-1-git-send-email-pmladek@suse.cz>
The new iterant kthread API allows to define a common checkpoint for
freezing, parking, termination, and even signal handling. It will allow
to maintain kthreads more easily and make the operations more reliable[*].
The kthread function is split into optional init(), func(), destroy() parts
where func() is called in a cycle. The common check point is after
each func() finishes. See kthread_iterant_fn() for more details.
func() does not need to call schedule(). Instead, we call
set_kthread_iterant_int_sleep() and let the generic code
to handle the sleeping properly.
There are few small functional changes:
+ SIGSTOP uses the standard handler and could not print debug
messages any longer
+ there is no need to enable or define sighandler for SIGCONT;
it is handled by prepare_signal() and could not get disabled
+ the debug message for default signal handler was removed; It is
handled in kernel_do_signal() a standard way. In reality, this
never happens because all signals are disabled except for the
four ones enabled by kthread_sigaction();
+ we call complete() instead of complete_and_exit(); do_exit() is
called in kthread() anyway after the function returns.
[*] In fact, there was a bug in the original code. It tried to process
a non-existing signal when the system was freezing. See the common
check for pending signal and freezing.
Signed-off-by: Petr Mladek <pmladek@suse.cz>
---
fs/jffs2/background.c | 156 ++++++++++++++++++++++++--------------------------
1 file changed, 74 insertions(+), 82 deletions(-)
diff --git a/fs/jffs2/background.c b/fs/jffs2/background.c
index 6af076b8f60f..50c16048ba2d 100644
--- a/fs/jffs2/background.c
+++ b/fs/jffs2/background.c
@@ -21,99 +21,86 @@
#include <linux/kthread.h>
#include "nodelist.h"
-static int jffs2_garbage_collect_thread(void *_c)
+static void jffs2_garbage_collect_thread_sighup(int sig)
+{
+ jffs2_dbg(1, "%s(): SIGHUP received\n", __func__);
+}
+
+static void jffs2_garbage_collect_thread_sigkill(int sig)
+{
+ jffs2_dbg(1, "%s(): SIGKILL received\n", __func__);
+ kthread_stop_current();
+}
+
+static void jffs2_garbage_collect_thread_init(void *_c)
{
struct jffs2_sb_info *c = _c;
- sigset_t hupmask;
- siginitset(&hupmask, sigmask(SIGHUP));
- allow_signal(SIGKILL);
- allow_signal(SIGSTOP);
- allow_signal(SIGCONT);
- allow_signal(SIGHUP);
+ kthread_sigaction(SIGKILL, jffs2_garbage_collect_thread_sigkill);
+ kthread_sigaction(SIGHUP, jffs2_garbage_collect_thread_sighup);
+ kthread_sigaction(SIGSTOP, KTHREAD_SIG_DFL);
c->gc_task = current;
complete(&c->gc_thread_start);
set_user_nice(current, 10);
+};
+
+static void jffs2_garbage_collect_thread_func(void *_c)
+{
+ struct jffs2_sb_info *c = _c;
+ sigset_t hupmask;
+
+ siginitset(&hupmask, sigmask(SIGHUP));
- set_freezable();
- for (;;) {
- sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
- again:
- spin_lock(&c->erase_completion_lock);
- if (!jffs2_thread_should_wake(c)) {
- set_current_state (TASK_INTERRUPTIBLE);
- spin_unlock(&c->erase_completion_lock);
- jffs2_dbg(1, "%s(): sleeping...\n", __func__);
- schedule();
- } else {
- spin_unlock(&c->erase_completion_lock);
- }
- /* Problem - immediately after bootup, the GCD spends a lot
- * of time in places like jffs2_kill_fragtree(); so much so
- * that userspace processes (like gdm and X) are starved
- * despite plenty of cond_resched()s and renicing. Yield()
- * doesn't help, either (presumably because userspace and GCD
- * are generally competing for a higher latency resource -
- * disk).
- * This forces the GCD to slow the hell down. Pulling an
- * inode in with read_inode() is much preferable to having
- * the GC thread get there first. */
- schedule_timeout_interruptible(msecs_to_jiffies(50));
-
- if (kthread_should_stop()) {
- jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
- goto die;
- }
-
- /* Put_super will send a SIGKILL and then wait on the sem.
- */
- while (signal_pending(current) || freezing(current)) {
- siginfo_t info;
- unsigned long signr;
-
- if (try_to_freeze())
- goto again;
-
- signr = dequeue_signal_lock(current, ¤t->blocked, &info);
-
- switch(signr) {
- case SIGSTOP:
- jffs2_dbg(1, "%s(): SIGSTOP received\n",
- __func__);
- set_current_state(TASK_STOPPED);
- schedule();
- break;
-
- case SIGKILL:
- jffs2_dbg(1, "%s(): SIGKILL received\n",
- __func__);
- goto die;
-
- case SIGHUP:
- jffs2_dbg(1, "%s(): SIGHUP received\n",
- __func__);
- break;
- default:
- jffs2_dbg(1, "%s(): signal %ld received\n",
- __func__, signr);
- }
- }
- /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
- sigprocmask(SIG_BLOCK, &hupmask, NULL);
-
- jffs2_dbg(1, "%s(): pass\n", __func__);
- if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
- pr_notice("No space for garbage collection. Aborting GC thread\n");
- goto die;
- }
+ spin_lock(&c->erase_completion_lock);
+ if (!jffs2_thread_should_wake(c)) {
+ set_kthread_iterant_int_sleep();
+ spin_unlock(&c->erase_completion_lock);
+ jffs2_dbg(1, "%s(): sleeping...\n", __func__);
+ return;
+ }
+ spin_unlock(&c->erase_completion_lock);
+
+ /* Problem - immediately after bootup, the GCD spends a lot
+ * of time in places like jffs2_kill_fragtree(); so much so
+ * that userspace processes (like gdm and X) are starved
+ * despite plenty of cond_resched()s and renicing. Yield()
+ * doesn't help, either (presumably because userspace and GCD
+ * are generally competing for a higher latency resource -
+ * disk).
+ * This forces the GCD to slow the hell down. Pulling an
+ * inode in with read_inode() is much preferable to having
+ * the GC thread get there first.
+ */
+ schedule_timeout_interruptible(msecs_to_jiffies(50));
+
+ if (kthread_should_stop()) {
+ jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
+ return;
}
- die:
+
+ try_to_freeze();
+
+ /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
+ sigprocmask(SIG_BLOCK, &hupmask, NULL);
+
+ jffs2_dbg(1, "%s(): pass\n", __func__);
+ if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
+ pr_notice("No space for garbage collection. Aborting GC thread\n");
+ kthread_stop_current();
+ }
+ sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
+}
+
+static void jffs2_garbage_collect_thread_destroy(void *_c)
+{
+ struct jffs2_sb_info *c = _c;
+
spin_lock(&c->erase_completion_lock);
c->gc_task = NULL;
spin_unlock(&c->erase_completion_lock);
- complete_and_exit(&c->gc_thread_exit, 0);
+ complete(&c->gc_thread_exit);
}
void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
@@ -126,16 +113,21 @@ void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
/* This must only ever be called when no GC thread is currently running */
int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
{
+ static struct kthread_iterant kti = {
+ .init = jffs2_garbage_collect_thread_init,
+ .func = jffs2_garbage_collect_thread_func,
+ .destroy = jffs2_garbage_collect_thread_destroy,
+ };
struct task_struct *tsk;
int ret = 0;
BUG_ON(c->gc_task);
+ kti.data = c;
init_completion(&c->gc_thread_start);
init_completion(&c->gc_thread_exit);
- tsk = kthread_run(jffs2_garbage_collect_thread, c,
- "jffs2_gcd_mtd%d", c->mtd->index);
+ tsk = kthread_iterant_run(&kti, "jffs2_gcd_mtd%d", c->mtd->index);
if (IS_ERR(tsk)) {
pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n",
-PTR_ERR(tsk));
--
1.8.5.6
next prev parent reply other threads:[~2015-06-05 15:05 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-05 15:00 [RFC PATCH 00/18] kthreads/signal: Safer kthread API and signal handling Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 01/18] kthread: Allow to call __kthread_create_on_node() with va_list args Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 02/18] kthread: Add API for iterant kthreads Petr Mladek
2015-06-09 6:23 ` Tejun Heo
2015-06-15 12:46 ` Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 03/18] kthread: Add kthread_stop_current() Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 04/18] signal: Rename kernel_sigaction() to kthread_sigaction() and clean it up Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 05/18] freezer/scheduler: Add freezable_cond_resched() Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 06/18] signal/kthread: Initial implementation of kthread signal handling Petr Mladek
2015-06-06 21:58 ` Oleg Nesterov
2015-06-08 13:51 ` Petr Mladek
2015-06-08 21:13 ` Oleg Nesterov
2015-06-15 13:13 ` Petr Mladek
2015-06-15 19:14 ` Oleg Nesterov
2015-06-16 7:54 ` Petr Mladek
2015-06-09 7:10 ` Tejun Heo
2015-06-09 12:15 ` Jiri Kosina
2015-06-10 3:13 ` Tejun Heo
2015-06-05 15:01 ` [RFC PATCH 07/18] kthread: Make iterant kthreads freezable by default Petr Mladek
2015-06-09 7:20 ` Tejun Heo
2015-06-09 15:53 ` Petr Mladek
2015-06-10 4:31 ` Tejun Heo
2015-06-12 13:24 ` Petr Mladek
2015-06-13 23:22 ` Tejun Heo
2015-06-15 9:28 ` Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 08/18] kthread: Allow to get struct kthread_iterant from task_struct Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 09/18] kthread: Make it easier to correctly sleep in iterant kthreads Petr Mladek
2015-06-05 16:10 ` Peter Zijlstra
2015-06-08 10:01 ` Petr Mladek
2015-06-08 11:39 ` Peter Zijlstra
2015-06-09 15:25 ` Petr Mladek
2015-06-10 9:05 ` Peter Zijlstra
2015-06-09 7:32 ` Tejun Heo
2015-06-08 17:48 ` Steven Rostedt
2015-06-10 9:07 ` Peter Zijlstra
2015-06-10 14:07 ` Steven Rostedt
2015-06-11 4:28 ` Jiri Kosina
2015-06-05 15:01 ` [RFC PATCH 10/18] jffs2: Remove forward definition of jffs2_garbage_collect_thread() Petr Mladek
2015-06-05 15:01 ` Petr Mladek [this message]
2015-06-06 21:16 ` [RFC PATCH 11/18] jffs2: Convert jffs2_gcd_mtd kthread into the iterant API Oleg Nesterov
2015-06-06 21:32 ` Jiri Kosina
2015-06-06 22:30 ` Oleg Nesterov
2015-06-06 22:44 ` Jiri Kosina
2015-06-06 22:58 ` Oleg Nesterov
2015-06-05 15:01 ` [RFC PATCH 12/18] lockd: Convert the central lockd service to kthread_iterant API Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 13/18] ring_buffer: Use iterant kthreads API in the ring buffer benchmark Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 14/18] ring_buffer: Allow to cleanly freeze the ring buffer benchmark kthreads Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 15/18] ring_buffer: Allow to exit the ring buffer benchmark immediately Petr Mladek
2015-06-08 17:44 ` Steven Rostedt
2015-06-15 15:23 ` Petr Mladek
2015-06-15 15:33 ` Steven Rostedt
2015-06-15 15:54 ` Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 16/18] kthread: Support interruptible sleep with a timeout by iterant kthreads Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 17/18] ring_buffer: Use the new API for a sleep with a timeout in the benchmark Petr Mladek
2015-06-05 15:01 ` [RFC PATCH 18/18] jffs2: Use the new API for a sleep with a timeout Petr Mladek
2015-06-05 16:22 ` [RFC PATCH 00/18] kthreads/signal: Safer kthread API and signal handling Peter Zijlstra
2015-06-09 6:14 ` Tejun Heo
2015-06-10 10:40 ` Peter Zijlstra
2015-06-11 22:02 ` Tejun Heo
2015-06-09 6:10 ` Tejun Heo
2015-06-09 7:58 ` Tejun Heo
2015-06-17 11:34 ` Christoph Hellwig
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=1433516477-5153-12-git-send-email-pmladek@suse.cz \
--to=pmladek@suse.cz \
--cc=akpm@linux-foundation.org \
--cc=anna.schumaker@netapp.com \
--cc=bp@suse.de \
--cc=clm@fb.com \
--cc=dwmw2@infradead.org \
--cc=jkosina@suse.cz \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=linux-nfs@vger.kernel.org \
--cc=live-patching@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=richard@nod.at \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=trond.myklebust@primarydata.com \
/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
all inboxes | Powered by JetHome®