background.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/signal.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(SIGHUP);
  71. c->gc_task = current;
  72. complete(&c->gc_thread_start);
  73. set_user_nice(current, 10);
  74. set_freezable();
  75. for (;;) {
  76. sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
  77. again:
  78. spin_lock(&c->erase_completion_lock);
  79. if (!jffs2_thread_should_wake(c)) {
  80. set_current_state (TASK_INTERRUPTIBLE);
  81. spin_unlock(&c->erase_completion_lock);
  82. jffs2_dbg(1, "%s(): sleeping...\n", __func__);
  83. schedule();
  84. } else {
  85. spin_unlock(&c->erase_completion_lock);
  86. }
  87. /* Problem - immediately after bootup, the GCD spends a lot
  88. * of time in places like jffs2_kill_fragtree(); so much so
  89. * that userspace processes (like gdm and X) are starved
  90. * despite plenty of cond_resched()s and renicing. Yield()
  91. * doesn't help, either (presumably because userspace and GCD
  92. * are generally competing for a higher latency resource -
  93. * disk).
  94. * This forces the GCD to slow the hell down. Pulling an
  95. * inode in with read_inode() is much preferable to having
  96. * the GC thread get there first. */
  97. schedule_timeout_interruptible(msecs_to_jiffies(50));
  98. if (kthread_should_stop()) {
  99. jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
  100. goto die;
  101. }
  102. /* Put_super will send a SIGKILL and then wait on the sem.
  103. */
  104. while (signal_pending(current) || freezing(current)) {
  105. unsigned long signr;
  106. if (try_to_freeze())
  107. goto again;
  108. signr = kernel_dequeue_signal();
  109. switch(signr) {
  110. case SIGSTOP:
  111. jffs2_dbg(1, "%s(): SIGSTOP received\n",
  112. __func__);
  113. kernel_signal_stop();
  114. break;
  115. case SIGKILL:
  116. jffs2_dbg(1, "%s(): SIGKILL received\n",
  117. __func__);
  118. goto die;
  119. case SIGHUP:
  120. jffs2_dbg(1, "%s(): SIGHUP received\n",
  121. __func__);
  122. break;
  123. default:
  124. jffs2_dbg(1, "%s(): signal %ld received\n",
  125. __func__, signr);
  126. }
  127. }
  128. /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
  129. sigprocmask(SIG_BLOCK, &hupmask, NULL);
  130. jffs2_dbg(1, "%s(): pass\n", __func__);
  131. if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  132. pr_notice("No space for garbage collection. Aborting GC thread\n");
  133. goto die;
  134. }
  135. }
  136. die:
  137. spin_lock(&c->erase_completion_lock);
  138. c->gc_task = NULL;
  139. spin_unlock(&c->erase_completion_lock);
  140. complete_and_exit(&c->gc_thread_exit, 0);
  141. }