background.c 4.3 KB

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