162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * JFFS2 -- Journalling Flash File System, Version 2. 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Copyright © 2001-2007 Red Hat, Inc. 562306a36Sopenharmony_ci * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Created by David Woodhouse <dwmw2@infradead.org> 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * For licensing information, see the file 'LICENCE' in this directory. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <linux/kernel.h> 1662306a36Sopenharmony_ci#include <linux/jffs2.h> 1762306a36Sopenharmony_ci#include <linux/mtd/mtd.h> 1862306a36Sopenharmony_ci#include <linux/completion.h> 1962306a36Sopenharmony_ci#include <linux/sched/signal.h> 2062306a36Sopenharmony_ci#include <linux/freezer.h> 2162306a36Sopenharmony_ci#include <linux/kthread.h> 2262306a36Sopenharmony_ci#include "nodelist.h" 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_cistatic int jffs2_garbage_collect_thread(void *); 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_civoid jffs2_garbage_collect_trigger(struct jffs2_sb_info *c) 2862306a36Sopenharmony_ci{ 2962306a36Sopenharmony_ci assert_spin_locked(&c->erase_completion_lock); 3062306a36Sopenharmony_ci if (c->gc_task && jffs2_thread_should_wake(c)) 3162306a36Sopenharmony_ci send_sig(SIGHUP, c->gc_task, 1); 3262306a36Sopenharmony_ci} 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/* This must only ever be called when no GC thread is currently running */ 3562306a36Sopenharmony_ciint jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c) 3662306a36Sopenharmony_ci{ 3762306a36Sopenharmony_ci struct task_struct *tsk; 3862306a36Sopenharmony_ci int ret = 0; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci BUG_ON(c->gc_task); 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci init_completion(&c->gc_thread_start); 4362306a36Sopenharmony_ci init_completion(&c->gc_thread_exit); 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index); 4662306a36Sopenharmony_ci if (IS_ERR(tsk)) { 4762306a36Sopenharmony_ci pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n", 4862306a36Sopenharmony_ci -PTR_ERR(tsk)); 4962306a36Sopenharmony_ci complete(&c->gc_thread_exit); 5062306a36Sopenharmony_ci ret = PTR_ERR(tsk); 5162306a36Sopenharmony_ci } else { 5262306a36Sopenharmony_ci /* Wait for it... */ 5362306a36Sopenharmony_ci jffs2_dbg(1, "Garbage collect thread is pid %d\n", tsk->pid); 5462306a36Sopenharmony_ci wait_for_completion(&c->gc_thread_start); 5562306a36Sopenharmony_ci ret = tsk->pid; 5662306a36Sopenharmony_ci } 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci return ret; 5962306a36Sopenharmony_ci} 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_civoid jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c) 6262306a36Sopenharmony_ci{ 6362306a36Sopenharmony_ci int wait = 0; 6462306a36Sopenharmony_ci spin_lock(&c->erase_completion_lock); 6562306a36Sopenharmony_ci if (c->gc_task) { 6662306a36Sopenharmony_ci jffs2_dbg(1, "Killing GC task %d\n", c->gc_task->pid); 6762306a36Sopenharmony_ci send_sig(SIGKILL, c->gc_task, 1); 6862306a36Sopenharmony_ci wait = 1; 6962306a36Sopenharmony_ci } 7062306a36Sopenharmony_ci spin_unlock(&c->erase_completion_lock); 7162306a36Sopenharmony_ci if (wait) 7262306a36Sopenharmony_ci wait_for_completion(&c->gc_thread_exit); 7362306a36Sopenharmony_ci} 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic int jffs2_garbage_collect_thread(void *_c) 7662306a36Sopenharmony_ci{ 7762306a36Sopenharmony_ci struct jffs2_sb_info *c = _c; 7862306a36Sopenharmony_ci sigset_t hupmask; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci siginitset(&hupmask, sigmask(SIGHUP)); 8162306a36Sopenharmony_ci allow_signal(SIGKILL); 8262306a36Sopenharmony_ci allow_signal(SIGSTOP); 8362306a36Sopenharmony_ci allow_signal(SIGHUP); 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci c->gc_task = current; 8662306a36Sopenharmony_ci complete(&c->gc_thread_start); 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci set_user_nice(current, 10); 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci set_freezable(); 9162306a36Sopenharmony_ci for (;;) { 9262306a36Sopenharmony_ci sigprocmask(SIG_UNBLOCK, &hupmask, NULL); 9362306a36Sopenharmony_ci again: 9462306a36Sopenharmony_ci spin_lock(&c->erase_completion_lock); 9562306a36Sopenharmony_ci if (!jffs2_thread_should_wake(c)) { 9662306a36Sopenharmony_ci set_current_state (TASK_INTERRUPTIBLE); 9762306a36Sopenharmony_ci spin_unlock(&c->erase_completion_lock); 9862306a36Sopenharmony_ci jffs2_dbg(1, "%s(): sleeping...\n", __func__); 9962306a36Sopenharmony_ci schedule(); 10062306a36Sopenharmony_ci } else { 10162306a36Sopenharmony_ci spin_unlock(&c->erase_completion_lock); 10262306a36Sopenharmony_ci } 10362306a36Sopenharmony_ci /* Problem - immediately after bootup, the GCD spends a lot 10462306a36Sopenharmony_ci * of time in places like jffs2_kill_fragtree(); so much so 10562306a36Sopenharmony_ci * that userspace processes (like gdm and X) are starved 10662306a36Sopenharmony_ci * despite plenty of cond_resched()s and renicing. Yield() 10762306a36Sopenharmony_ci * doesn't help, either (presumably because userspace and GCD 10862306a36Sopenharmony_ci * are generally competing for a higher latency resource - 10962306a36Sopenharmony_ci * disk). 11062306a36Sopenharmony_ci * This forces the GCD to slow the hell down. Pulling an 11162306a36Sopenharmony_ci * inode in with read_inode() is much preferable to having 11262306a36Sopenharmony_ci * the GC thread get there first. */ 11362306a36Sopenharmony_ci schedule_timeout_interruptible(msecs_to_jiffies(50)); 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci if (kthread_should_stop()) { 11662306a36Sopenharmony_ci jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__); 11762306a36Sopenharmony_ci goto die; 11862306a36Sopenharmony_ci } 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci /* Put_super will send a SIGKILL and then wait on the sem. 12162306a36Sopenharmony_ci */ 12262306a36Sopenharmony_ci while (signal_pending(current) || freezing(current)) { 12362306a36Sopenharmony_ci unsigned long signr; 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci if (try_to_freeze()) 12662306a36Sopenharmony_ci goto again; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci signr = kernel_dequeue_signal(); 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci switch(signr) { 13162306a36Sopenharmony_ci case SIGSTOP: 13262306a36Sopenharmony_ci jffs2_dbg(1, "%s(): SIGSTOP received\n", 13362306a36Sopenharmony_ci __func__); 13462306a36Sopenharmony_ci kernel_signal_stop(); 13562306a36Sopenharmony_ci break; 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci case SIGKILL: 13862306a36Sopenharmony_ci jffs2_dbg(1, "%s(): SIGKILL received\n", 13962306a36Sopenharmony_ci __func__); 14062306a36Sopenharmony_ci goto die; 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci case SIGHUP: 14362306a36Sopenharmony_ci jffs2_dbg(1, "%s(): SIGHUP received\n", 14462306a36Sopenharmony_ci __func__); 14562306a36Sopenharmony_ci break; 14662306a36Sopenharmony_ci default: 14762306a36Sopenharmony_ci jffs2_dbg(1, "%s(): signal %ld received\n", 14862306a36Sopenharmony_ci __func__, signr); 14962306a36Sopenharmony_ci } 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */ 15262306a36Sopenharmony_ci sigprocmask(SIG_BLOCK, &hupmask, NULL); 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci jffs2_dbg(1, "%s(): pass\n", __func__); 15562306a36Sopenharmony_ci if (jffs2_garbage_collect_pass(c) == -ENOSPC) { 15662306a36Sopenharmony_ci pr_notice("No space for garbage collection. Aborting GC thread\n"); 15762306a36Sopenharmony_ci goto die; 15862306a36Sopenharmony_ci } 15962306a36Sopenharmony_ci } 16062306a36Sopenharmony_ci die: 16162306a36Sopenharmony_ci spin_lock(&c->erase_completion_lock); 16262306a36Sopenharmony_ci c->gc_task = NULL; 16362306a36Sopenharmony_ci spin_unlock(&c->erase_completion_lock); 16462306a36Sopenharmony_ci kthread_complete_and_exit(&c->gc_thread_exit, 0); 16562306a36Sopenharmony_ci} 166