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