background.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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. static int jffs2_garbage_collect_thread(void *);
  22. void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
  23. {
  24. assert_spin_locked(&c->erase_completion_lock);
  25. if (c->gc_task && jffs2_thread_should_wake(c))
  26. send_sig(SIGHUP, c->gc_task, 1);
  27. }
  28. /* This must only ever be called when no GC thread is currently running */
  29. int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
  30. {
  31. struct task_struct *tsk;
  32. int ret = 0;
  33. BUG_ON(c->gc_task);
  34. init_completion(&c->gc_thread_start);
  35. init_completion(&c->gc_thread_exit);
  36. tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index);
  37. if (IS_ERR(tsk)) {
  38. pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n",
  39. -PTR_ERR(tsk));
  40. complete(&c->gc_thread_exit);
  41. ret = PTR_ERR(tsk);
  42. } else {
  43. /* Wait for it... */
  44. jffs2_dbg(1, "Garbage collect thread is pid %d\n", tsk->pid);
  45. wait_for_completion(&c->gc_thread_start);
  46. ret = tsk->pid;
  47. }
  48. return ret;
  49. }
  50. void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
  51. {
  52. int wait = 0;
  53. spin_lock(&c->erase_completion_lock);
  54. if (c->gc_task) {
  55. jffs2_dbg(1, "Killing GC task %d\n", c->gc_task->pid);
  56. send_sig(SIGKILL, c->gc_task, 1);
  57. wait = 1;
  58. }
  59. spin_unlock(&c->erase_completion_lock);
  60. if (wait)
  61. wait_for_completion(&c->gc_thread_exit);
  62. }
  63. static int jffs2_garbage_collect_thread(void *_c)
  64. {
  65. struct jffs2_sb_info *c = _c;
  66. sigset_t hupmask;
  67. siginitset(&hupmask, sigmask(SIGHUP));
  68. allow_signal(SIGKILL);
  69. allow_signal(SIGSTOP);
  70. allow_signal(SIGCONT);
  71. allow_signal(SIGHUP);
  72. c->gc_task = current;
  73. complete(&c->gc_thread_start);
  74. set_user_nice(current, 10);
  75. set_freezable();
  76. for (;;) {
  77. sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
  78. again:
  79. spin_lock(&c->erase_completion_lock);
  80. if (!jffs2_thread_should_wake(c)) {
  81. set_current_state (TASK_INTERRUPTIBLE);
  82. spin_unlock(&c->erase_completion_lock);
  83. jffs2_dbg(1, "%s(): sleeping...\n", __func__);
  84. schedule();
  85. } else {
  86. spin_unlock(&c->erase_completion_lock);
  87. }
  88. /* Problem - immediately after bootup, the GCD spends a lot
  89. * of time in places like jffs2_kill_fragtree(); so much so
  90. * that userspace processes (like gdm and X) are starved
  91. * despite plenty of cond_resched()s and renicing. Yield()
  92. * doesn't help, either (presumably because userspace and GCD
  93. * are generally competing for a higher latency resource -
  94. * disk).
  95. * This forces the GCD to slow the hell down. Pulling an
  96. * inode in with read_inode() is much preferable to having
  97. * the GC thread get there first. */
  98. schedule_timeout_interruptible(msecs_to_jiffies(50));
  99. if (kthread_should_stop()) {
  100. jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
  101. goto die;
  102. }
  103. /* Put_super will send a SIGKILL and then wait on the sem.
  104. */
  105. while (signal_pending(current) || freezing(current)) {
  106. siginfo_t info;
  107. unsigned long signr;
  108. if (try_to_freeze())
  109. goto again;
  110. signr = dequeue_signal_lock(current, &current->blocked, &info);
  111. switch(signr) {
  112. case SIGSTOP:
  113. jffs2_dbg(1, "%s(): SIGSTOP received\n",
  114. __func__);
  115. set_current_state(TASK_STOPPED);
  116. schedule();
  117. break;
  118. case SIGKILL:
  119. jffs2_dbg(1, "%s(): SIGKILL received\n",
  120. __func__);
  121. goto die;
  122. case SIGHUP:
  123. jffs2_dbg(1, "%s(): SIGHUP received\n",
  124. __func__);
  125. break;
  126. default:
  127. jffs2_dbg(1, "%s(): signal %ld received\n",
  128. __func__, signr);
  129. }
  130. }
  131. /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
  132. sigprocmask(SIG_BLOCK, &hupmask, NULL);
  133. jffs2_dbg(1, "%s(): pass\n", __func__);
  134. if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  135. pr_notice("No space for garbage collection. Aborting GC thread\n");
  136. goto die;
  137. }
  138. }
  139. die:
  140. spin_lock(&c->erase_completion_lock);
  141. c->gc_task = NULL;
  142. spin_unlock(&c->erase_completion_lock);
  143. complete_and_exit(&c->gc_thread_exit, 0);
  144. }