xref: /openbmc/linux/fs/jffs2/background.c (revision da320f05)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6  *
7  * Created by David Woodhouse <dwmw2@infradead.org>
8  *
9  * For licensing information, see the file 'LICENCE' in this directory.
10  *
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/jffs2.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/completion.h>
17 #include <linux/sched.h>
18 #include <linux/freezer.h>
19 #include <linux/kthread.h>
20 #include "nodelist.h"
21 
22 
23 static int jffs2_garbage_collect_thread(void *);
24 
25 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
26 {
27 	assert_spin_locked(&c->erase_completion_lock);
28 	if (c->gc_task && jffs2_thread_should_wake(c))
29 		send_sig(SIGHUP, c->gc_task, 1);
30 }
31 
32 /* This must only ever be called when no GC thread is currently running */
33 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
34 {
35 	struct task_struct *tsk;
36 	int ret = 0;
37 
38 	BUG_ON(c->gc_task);
39 
40 	init_completion(&c->gc_thread_start);
41 	init_completion(&c->gc_thread_exit);
42 
43 	tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index);
44 	if (IS_ERR(tsk)) {
45 		pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n",
46 			-PTR_ERR(tsk));
47 		complete(&c->gc_thread_exit);
48 		ret = PTR_ERR(tsk);
49 	} else {
50 		/* Wait for it... */
51 		jffs2_dbg(1, "JFFS2: Garbage collect thread is pid %d\n",
52 			  tsk->pid);
53 		wait_for_completion(&c->gc_thread_start);
54 		ret = tsk->pid;
55 	}
56 
57 	return ret;
58 }
59 
60 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
61 {
62 	int wait = 0;
63 	spin_lock(&c->erase_completion_lock);
64 	if (c->gc_task) {
65 		jffs2_dbg(1, "jffs2: Killing GC task %d\n", c->gc_task->pid);
66 		send_sig(SIGKILL, c->gc_task, 1);
67 		wait = 1;
68 	}
69 	spin_unlock(&c->erase_completion_lock);
70 	if (wait)
71 		wait_for_completion(&c->gc_thread_exit);
72 }
73 
74 static int jffs2_garbage_collect_thread(void *_c)
75 {
76 	struct jffs2_sb_info *c = _c;
77 
78 	allow_signal(SIGKILL);
79 	allow_signal(SIGSTOP);
80 	allow_signal(SIGCONT);
81 
82 	c->gc_task = current;
83 	complete(&c->gc_thread_start);
84 
85 	set_user_nice(current, 10);
86 
87 	set_freezable();
88 	for (;;) {
89 		allow_signal(SIGHUP);
90 	again:
91 		spin_lock(&c->erase_completion_lock);
92 		if (!jffs2_thread_should_wake(c)) {
93 			set_current_state (TASK_INTERRUPTIBLE);
94 			spin_unlock(&c->erase_completion_lock);
95 			jffs2_dbg(1, "%s(): sleeping...\n", __func__);
96 			schedule();
97 		} else
98 			spin_unlock(&c->erase_completion_lock);
99 
100 
101 		/* Problem - immediately after bootup, the GCD spends a lot
102 		 * of time in places like jffs2_kill_fragtree(); so much so
103 		 * that userspace processes (like gdm and X) are starved
104 		 * despite plenty of cond_resched()s and renicing.  Yield()
105 		 * doesn't help, either (presumably because userspace and GCD
106 		 * are generally competing for a higher latency resource -
107 		 * disk).
108 		 * This forces the GCD to slow the hell down.   Pulling an
109 		 * inode in with read_inode() is much preferable to having
110 		 * the GC thread get there first. */
111 		schedule_timeout_interruptible(msecs_to_jiffies(50));
112 
113 		if (kthread_should_stop()) {
114 			jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
115 			goto die;
116 		}
117 
118 		/* Put_super will send a SIGKILL and then wait on the sem.
119 		 */
120 		while (signal_pending(current) || freezing(current)) {
121 			siginfo_t info;
122 			unsigned long signr;
123 
124 			if (try_to_freeze())
125 				goto again;
126 
127 			signr = dequeue_signal_lock(current, &current->blocked, &info);
128 
129 			switch(signr) {
130 			case SIGSTOP:
131 				jffs2_dbg(1, "%s(): SIGSTOP received\n",
132 					  __func__);
133 				set_current_state(TASK_STOPPED);
134 				schedule();
135 				break;
136 
137 			case SIGKILL:
138 				jffs2_dbg(1, "%s(): SIGKILL received\n",
139 					  __func__);
140 				goto die;
141 
142 			case SIGHUP:
143 				jffs2_dbg(1, "%s(): SIGHUP received\n",
144 					  __func__);
145 				break;
146 			default:
147 				jffs2_dbg(1, "%s(): signal %ld received\n",
148 					  __func__, signr);
149 			}
150 		}
151 		/* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
152 		disallow_signal(SIGHUP);
153 
154 		jffs2_dbg(1, "%s(): pass\n", __func__);
155 		if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
156 			pr_notice("No space for garbage collection. Aborting GC thread\n");
157 			goto die;
158 		}
159 	}
160  die:
161 	spin_lock(&c->erase_completion_lock);
162 	c->gc_task = NULL;
163 	spin_unlock(&c->erase_completion_lock);
164 	complete_and_exit(&c->gc_thread_exit, 0);
165 }
166