分类: LINUX
2007-06-13 23:02:49
1 /* 2 * mm/pdflush.c - worker threads for writing back filesystem data 3 * 4 * Copyright (C) 2002, Linus Torvalds. 5 * 6 * 09Apr2002 akpm@zip.com.au 7 * Initial version 8 * 29Feb2004 kaos@sgi.com 9 * Move worker thread creation to kthread to avoid chewing 10 * up stack space with nested calls to kernel_thread. 11 */ |
root 5 1 5 0 1 21:31 ? 00:00:00 [kthread] root 114 5 114 0 1 21:31 ? 00:00:00 [pdflush] root 115 5 115 0 1 21:31 ? 00:00:00 [pdflush] |
12 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 25 |
26 /* 27 * Minimum and maximum number of pdflush instances 28 */ 29 #define MIN_PDFLUSH_THREADS 2 30 #define MAX_PDFLUSH_THREADS 8 31 32 static void start_one_pdflush_thread(void); 33 34 |
35 /* 36 * The pdflush threads are worker threads for writing back dirty data. 37 * Ideally, we'd like one thread per active disk spindle. But the disk 38 * topology is very hard to divine at this level. Instead, we take 39 * care in various places to prevent more than one pdflush thread from 40 * performing writeback against a single filesystem. pdflush threads 41 * have the PF_FLUSHER flag set in current->flags to aid in this. 42 */ 43 |
43 44 /* 45 * All the pdflush threads. Protected by pdflush_lock 46 */ 47 static LIST_HEAD(pdflush_list); 48 static DEFINE_SPINLOCK(pdflush_lock); 49 50 /* 51 * The count of currently-running pdflush threads. Protected 52 * by pdflush_lock. 53 * 54 * Readable by sysctl, but not writable. Published to userspace at 55 * /proc/sys/vm/nr_pdflush_threads. 56 */ 57 int nr_pdflush_threads = 0; 58 59 /* 60 * The time at which the pdflush thread pool last went empty 61 */ 62 static unsigned long last_empty_jifs; 63 |
64 /* 65 * The pdflush thread. 66 * 67 * Thread pool management algorithm: 68 * 69 * - The minimum and maximum number of pdflush instances are bound 70 * by MIN_PDFLUSH_THREADS and MAX_PDFLUSH_THREADS. 71 * 72 * - If there have been no idle pdflush instances for 1 second, create 73 * a new one. 74 * 75 * - If the least-recently-went-to-sleep pdflush thread has been asleep 76 * for more than one second, terminate a thread. 77 */ 78 |
79 /* 80 * A structure for passing work to a pdflush thread. Also for passing 81 * state information between pdflush threads. Protected by pdflush_lock. 82 */ 83 struct pdflush_work { 84 struct task_struct *who; /* The thread */ 85 void (*fn)(unsigned long); /* A callback function */ 86 unsigned long arg0; /* An argument to the callback */ 87 struct list_head list; /* On pdflush_list, when idle */ 88 unsigned long when_i_went_to_sleep; 89 }; 90 |
232 static int __init pdflush_init(void) 233 { 234 int i; 235 236 for (i = 0; i < MIN_PDFLUSH_THREADS; i++) 237 start_one_pdflush_thread(); 238 return 0; 239 } 240 241 module_init(pdflush_init); |
609 /* If it has an init func, it must have an exit func to unload */ 610 if ((mod->init != NULL && mod->exit == NULL) 611 || mod->unsafe) { 612 forced = try_force(flags); 613 if (!forced) { 614 /* This module can't be removed */ 615 ret = -EBUSY; 616 goto out; 617 } 618 } |
498 #ifdef CONFIG_MODULE_FORCE_UNLOAD 499 static inline int try_force(unsigned int flags) 500 { 501 int ret = (flags & O_TRUNC); 502 if (ret) 503 add_taint(TAINT_FORCED_MODULE); 504 return ret; 505 } 506 #else 507 static inline int try_force(unsigned int flags) 508 { 509 return 0; 510 } 511 #endif /* CONFIG_MODULE_FORCE_UNLOAD */ |
227 static void start_one_pdflush_thread(void) 228 { 229 kthread_run(pdflush, NULL, "pdflush"); 230 } 231 |
164 /* 165 * Of course, my_work wants to be just a local in __pdflush(). It is 166 * separated out in this manner to hopefully prevent the compiler from 167 * performing unfortunate optimisations against the auto variables. Because 168 * these are visible to other tasks and CPUs. (No problem has actually 169 * been observed. This is just paranoia). 170 */ 这段注释比较有意思,为了防止编译器将局部变量my_work优化成寄存器变量,所以这里整个处理流程转变成了pdflush套__pdflush的方式。实际上,局部变量的采用相对于动态申请内存,无论是在空间利用率还是在时间效率上都是有好处的。 171 static int pdflush(void *dummy) 172 { 173 struct pdflush_work my_work; 174 cpumask_t cpus_allowed; 175 176 /* 177 * pdflush can spend a lot of time doing encryption via dm-crypt. We 178 * don't want to do that at keventd's priority. 179 */ 180 set_user_nice(current, 0); 微调优先级,提高系统的整体响应。 181 182 /* 183 * Some configs put our parent kthread in a limited cpuset, 184 * which kthread() overrides, forcing cpus_allowed == CPU_MASK_ALL. 185 * Our needs are more modest - cut back to our cpusets cpus_allowed. 186 * This is needed as pdflush's are dynamically created and destroyed. 187 * The boottime pdflush's are easily placed w/o these 2 lines. 188 */ 189 cpus_allowed = cpuset_cpus_allowed(current); 190 set_cpus_allowed(current, cpus_allowed); 设置允许运行的CPU集合掩码。 191 192 return __pdflush(&my_work); 193 } |
91 static int __pdflush(struct pdflush_work *my_work) 92 { 93 current->flags |= PF_FLUSHER; 94 my_work->fn = NULL; 95 my_work->who = current; 96 INIT_LIST_HEAD(&my_work->list); 做些初始化动作。 97 98 spin_lock_irq(&pdflush_lock); 因为要对nr_pdflush_threads和pdflush_list操作,所以需要加互斥锁,为了避免意外(pdflush任务的添加可能在硬中断上下文),故同时关闭硬中断。 99 nr_pdflush_threads++; 将nr_pdflush_threads的计数加1,因为多了一个pdflush内核线程实例。 100 for ( ; ; ) { 101 struct pdflush_work *pdf; 102 103 set_current_state(TASK_INTERRUPTIBLE); 104 list_move(&my_work->list, &pdflush_list); 105 my_work->when_i_went_to_sleep = jiffies; 106 spin_unlock_irq(&pdflush_lock); 107 108 schedule(); 将自己加入空闲线程列表pdflush_list,然后让出cpu,等待被调度。 109 if (try_to_freeze()) { 110 spin_lock_irq(&pdflush_lock); 111 continue; 112 } 如果正在冻结当前进程,继续循环。 113 114 spin_lock_irq(&pdflush_lock); 115 if (!list_empty(&my_work->list)) { 116 printk("pdflush: bogus wakeup!\n"); 117 my_work->fn = NULL; 118 continue; 119 } 120 if (my_work->fn == NULL) { 121 printk("pdflush: NULL work function\n"); 122 continue; 123 } 124 spin_unlock_irq(&pdflush_lock); 上面是对被意外唤醒情况的处理。 125 126 (*my_work->fn)(my_work->arg0); 127 带参数arg0执行任务函数。 128 /* 129 * Thread creation: For how long have there been zero 130 * available threads? 131 */ 132 if (jiffies - last_empty_jifs > 1 * HZ) { 133 /* unlocked list_empty() test is OK here */ 134 if (list_empty(&pdflush_list)) { 135 /* unlocked test is OK here */ 136 if (nr_pdflush_threads < MAX_PDFLUSH_THREADS) 137 start_one_pdflush_thread(); 138 } 139 } 如果pdflush_list为空超过1妙,并且线程数量还有可以增长的余地,则重新启动一个新的pdflush线程实例。 140 141 spin_lock_irq(&pdflush_lock); 142 my_work->fn = NULL; 143 144 /* 145 * Thread destruction: For how long has the sleepiest 146 * thread slept? 147 */ 148 if (list_empty(&pdflush_list)) 149 continue; 如果pdflush_list依然为空,继续循环。 150 if (nr_pdflush_threads <= MIN_PDFLUSH_THREADS) 151 continue; 如果线程数量不大于最小线程数,继续循环。 152 pdf = list_entry(pdflush_list.prev, struct pdflush_work, list); 153 if (jiffies - pdf->when_i_went_to_sleep > 1 * HZ) { 154 /* Limit exit rate */ 155 pdf->when_i_went_to_sleep = jiffies; 156 break; /* exeunt */ 157 } 如果pdflush_list的最后一个内核线程睡眠超过1秒,可能系统变得较为轻闲,结束本线程。为什么是最后一个?因为这个list是作为栈来使用的,所以栈底的元素也肯定就是最老的元素。 158 } 159 nr_pdflush_threads--; 160 spin_unlock_irq(&pdflush_lock); 161 return 0; nr_pdflush_threads减1,退出本线程。 162 } 163 |
200 /* SIG_IGN makes children autoreap: see do_notify_parent(). */ 201 sa.sa.sa_handler = SIG_IGN; 202 sa.sa.sa_flags = 0; 203 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD)); 204 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0); |
POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN. POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)). Never- theless, the historical BSD and System V behaviours for ignoring SIGCHLD differ, so that the only completely portable method of ensuring that terminated children do not become zombies is to catch the SIGCHLD signal and perform a wait(2) or similar. |
135 /* unlocked test is OK here */ 136 if (nr_pdflush_threads < MAX_PDFLUSH_THREADS) 137 start_one_pdflush_thread(); |
152 pdf = list_entry(pdflush_list.prev, struct pdflush_work, list); 153 if (jiffies - pdf->when_i_went_to_sleep > 1 * HZ) { 154 /* Limit exit rate */ 155 pdf->when_i_went_to_sleep = jiffies; 156 break; /* exeunt */ 157 } |
195 /* 196 * Attempt to wake up a pdflush thread, and get it to do some work for you. 197 * Returns zero if it indeed managed to find a worker thread, and passed your 198 * payload to it. 199 */ 200 int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0) 201 { 202 unsigned long flags; 203 int ret = 0; 204 205 if (fn == NULL) 206 BUG(); /* Hard to diagnose if it's deferred */ 207 208 spin_lock_irqsave(&pdflush_lock, flags); 209 if (list_empty(&pdflush_list)) { 210 spin_unlock_irqrestore(&pdflush_lock, flags); 211 ret = -1; 212 } else { 213 struct pdflush_work *pdf; 214 215 pdf = list_entry(pdflush_list.next, struct pdflush_work, list); 216 list_del_init(&pdf->list); 217 if (list_empty(&pdflush_list)) 218 last_empty_jifs = jiffies; 219 pdf->fn = fn; 220 pdf->arg0 = arg0; 221 wake_up_process(pdf->who); 222 spin_unlock_irqrestore(&pdflush_lock, flags); 223 } 224 return ret; 225 } 226 |