0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014
0015 #include <linux/kernel.h>
0016 #include <linux/jffs2.h>
0017 #include <linux/mtd/mtd.h>
0018 #include <linux/completion.h>
0019 #include <linux/sched/signal.h>
0020 #include <linux/freezer.h>
0021 #include <linux/kthread.h>
0022 #include "nodelist.h"
0023
0024
0025 static int jffs2_garbage_collect_thread(void *);
0026
0027 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
0028 {
0029 assert_spin_locked(&c->erase_completion_lock);
0030 if (c->gc_task && jffs2_thread_should_wake(c))
0031 send_sig(SIGHUP, c->gc_task, 1);
0032 }
0033
0034
0035 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
0036 {
0037 struct task_struct *tsk;
0038 int ret = 0;
0039
0040 BUG_ON(c->gc_task);
0041
0042 init_completion(&c->gc_thread_start);
0043 init_completion(&c->gc_thread_exit);
0044
0045 tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index);
0046 if (IS_ERR(tsk)) {
0047 pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n",
0048 -PTR_ERR(tsk));
0049 complete(&c->gc_thread_exit);
0050 ret = PTR_ERR(tsk);
0051 } else {
0052
0053 jffs2_dbg(1, "Garbage collect thread is pid %d\n", tsk->pid);
0054 wait_for_completion(&c->gc_thread_start);
0055 ret = tsk->pid;
0056 }
0057
0058 return ret;
0059 }
0060
0061 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
0062 {
0063 int wait = 0;
0064 spin_lock(&c->erase_completion_lock);
0065 if (c->gc_task) {
0066 jffs2_dbg(1, "Killing GC task %d\n", c->gc_task->pid);
0067 send_sig(SIGKILL, c->gc_task, 1);
0068 wait = 1;
0069 }
0070 spin_unlock(&c->erase_completion_lock);
0071 if (wait)
0072 wait_for_completion(&c->gc_thread_exit);
0073 }
0074
0075 static int jffs2_garbage_collect_thread(void *_c)
0076 {
0077 struct jffs2_sb_info *c = _c;
0078 sigset_t hupmask;
0079
0080 siginitset(&hupmask, sigmask(SIGHUP));
0081 allow_signal(SIGKILL);
0082 allow_signal(SIGSTOP);
0083 allow_signal(SIGHUP);
0084
0085 c->gc_task = current;
0086 complete(&c->gc_thread_start);
0087
0088 set_user_nice(current, 10);
0089
0090 set_freezable();
0091 for (;;) {
0092 sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
0093 again:
0094 spin_lock(&c->erase_completion_lock);
0095 if (!jffs2_thread_should_wake(c)) {
0096 set_current_state (TASK_INTERRUPTIBLE);
0097 spin_unlock(&c->erase_completion_lock);
0098 jffs2_dbg(1, "%s(): sleeping...\n", __func__);
0099 schedule();
0100 } else {
0101 spin_unlock(&c->erase_completion_lock);
0102 }
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113 schedule_timeout_interruptible(msecs_to_jiffies(50));
0114
0115 if (kthread_should_stop()) {
0116 jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
0117 goto die;
0118 }
0119
0120
0121
0122 while (signal_pending(current) || freezing(current)) {
0123 unsigned long signr;
0124
0125 if (try_to_freeze())
0126 goto again;
0127
0128 signr = kernel_dequeue_signal();
0129
0130 switch(signr) {
0131 case SIGSTOP:
0132 jffs2_dbg(1, "%s(): SIGSTOP received\n",
0133 __func__);
0134 kernel_signal_stop();
0135 break;
0136
0137 case SIGKILL:
0138 jffs2_dbg(1, "%s(): SIGKILL received\n",
0139 __func__);
0140 goto die;
0141
0142 case SIGHUP:
0143 jffs2_dbg(1, "%s(): SIGHUP received\n",
0144 __func__);
0145 break;
0146 default:
0147 jffs2_dbg(1, "%s(): signal %ld received\n",
0148 __func__, signr);
0149 }
0150 }
0151
0152 sigprocmask(SIG_BLOCK, &hupmask, NULL);
0153
0154 jffs2_dbg(1, "%s(): pass\n", __func__);
0155 if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
0156 pr_notice("No space for garbage collection. Aborting GC thread\n");
0157 goto die;
0158 }
0159 }
0160 die:
0161 spin_lock(&c->erase_completion_lock);
0162 c->gc_task = NULL;
0163 spin_unlock(&c->erase_completion_lock);
0164 kthread_complete_and_exit(&c->gc_thread_exit, 0);
0165 }