background.c 4.3 KB

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