background.c 4.2 KB

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