1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/config.h>
23#include <linux/mm.h>
24#include <linux/init.h>
25#include <linux/smp_lock.h>
26#include <linux/nmi.h>
27#include <linux/interrupt.h>
28#include <linux/kernel_stat.h>
29#include <linux/completion.h>
30#include <linux/prefetch.h>
31#include <linux/compiler.h>
32
33#include <asm/uaccess.h>
34#include <asm/mmu_context.h>
35
36extern void timer_bh(void);
37extern void tqueue_bh(void);
38extern void immediate_bh(void);
39
40
41
42
43
44unsigned securebits = SECUREBITS_DEFAULT;
45
46extern void mem_use(void);
47
48
49
50
51
52
53
54
55
56
57
58
59#if HZ < 200
60#define TICK_SCALE(x) ((x) >> 2)
61#elif HZ < 400
62#define TICK_SCALE(x) ((x) >> 1)
63#elif HZ < 800
64#define TICK_SCALE(x) (x)
65#elif HZ < 1600
66#define TICK_SCALE(x) ((x) << 1)
67#else
68#define TICK_SCALE(x) ((x) << 2)
69#endif
70
71#define NICE_TO_TICKS(nice) (TICK_SCALE(20-(nice))+1)
72
73
74
75
76
77
78
79struct task_struct * init_tasks[NR_CPUS] = {&init_task, };
80
81
82
83
84
85
86
87
88
89
90
91
92spinlock_t runqueue_lock __cacheline_aligned = SPIN_LOCK_UNLOCKED;
93rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED;
94
95static LIST_HEAD(runqueue_head);
96
97
98
99
100
101static union {
102 struct schedule_data {
103 struct task_struct * curr;
104 cycles_t last_schedule;
105 } schedule_data;
106 char __pad [SMP_CACHE_BYTES];
107} aligned_data [NR_CPUS] __cacheline_aligned = { {{&init_task,0}}};
108
109#define cpu_curr(cpu) aligned_data[(cpu)].schedule_data.curr
110#define last_schedule(cpu) aligned_data[(cpu)].schedule_data.last_schedule
111
112struct kernel_stat kstat;
113extern struct task_struct *child_reaper;
114
115#ifdef CONFIG_SMP
116
117#define idle_task(cpu) (init_tasks[cpu_number_map(cpu)])
118#define can_schedule(p,cpu) \
119 ((p)->cpus_runnable & (p)->cpus_allowed & (1UL << cpu))
120
121#else
122
123#define idle_task(cpu) (&init_task)
124#define can_schedule(p,cpu) (1)
125
126#endif
127
128void scheduling_functions_start_here(void) { }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144static inline int goodness(struct task_struct * p, int this_cpu, struct mm_struct *this_mm)
145{
146 int weight;
147
148
149
150
151
152
153 weight = -1;
154 if (p->policy & SCHED_YIELD)
155 goto out;
156
157
158
159
160 if (p->policy == SCHED_OTHER) {
161
162
163
164
165
166
167
168 weight = p->counter;
169 if (!weight)
170 goto out;
171
172#ifdef CONFIG_SMP
173
174
175 if (p->processor == this_cpu)
176 weight += PROC_CHANGE_PENALTY;
177#endif
178
179
180 if (p->mm == this_mm || !p->mm)
181 weight += 1;
182 weight += 20 - p->nice;
183 goto out;
184 }
185
186
187
188
189
190
191 weight = 1000 + p->rt_priority;
192out:
193 return weight;
194}
195
196
197
198
199
200static inline int preemption_goodness(struct task_struct * prev, struct task_struct * p, int cpu)
201{
202 return goodness(p, cpu, prev->active_mm) - goodness(prev, cpu, prev->active_mm);
203}
204
205
206
207
208
209
210static FASTCALL(void reschedule_idle(struct task_struct * p));
211
212static void fastcall reschedule_idle(struct task_struct * p)
213{
214#ifdef CONFIG_SMP
215 int this_cpu = smp_processor_id();
216 struct task_struct *tsk, *target_tsk;
217 int cpu, best_cpu, i, max_prio;
218 cycles_t oldest_idle;
219
220
221
222
223
224 best_cpu = p->processor;
225 if (can_schedule(p, best_cpu)) {
226 tsk = idle_task(best_cpu);
227 if (cpu_curr(best_cpu) == tsk) {
228 int need_resched;
229send_now_idle:
230
231
232
233
234
235 need_resched = tsk->need_resched;
236 tsk->need_resched = 1;
237 if ((best_cpu != this_cpu) && !need_resched)
238 smp_send_reschedule(best_cpu);
239 return;
240 }
241 }
242
243
244
245
246
247
248
249
250 oldest_idle = (cycles_t) -1;
251 target_tsk = NULL;
252 max_prio = 0;
253
254 for (i = 0; i < smp_num_cpus; i++) {
255 cpu = cpu_logical_map(i);
256 if (!can_schedule(p, cpu))
257 continue;
258 tsk = cpu_curr(cpu);
259
260
261
262
263
264 if (tsk == idle_task(cpu)) {
265#if defined(__i386__) && defined(CONFIG_SMP)
266
267
268
269
270 if (smp_num_siblings == 2) {
271 if (cpu_curr(cpu_sibling_map[cpu]) ==
272 idle_task(cpu_sibling_map[cpu])) {
273 oldest_idle = last_schedule(cpu);
274 target_tsk = tsk;
275 break;
276 }
277
278 }
279#endif
280 if (last_schedule(cpu) < oldest_idle) {
281 oldest_idle = last_schedule(cpu);
282 target_tsk = tsk;
283 }
284 } else {
285 if (oldest_idle == (cycles_t)-1) {
286 int prio = preemption_goodness(tsk, p, cpu);
287
288 if (prio > max_prio) {
289 max_prio = prio;
290 target_tsk = tsk;
291 }
292 }
293 }
294 }
295 tsk = target_tsk;
296 if (tsk) {
297 if (oldest_idle != (cycles_t)-1) {
298 best_cpu = tsk->processor;
299 goto send_now_idle;
300 }
301 tsk->need_resched = 1;
302 if (tsk->processor != this_cpu)
303 smp_send_reschedule(tsk->processor);
304 }
305 return;
306
307
308#else
309 int this_cpu = smp_processor_id();
310 struct task_struct *tsk;
311
312 tsk = cpu_curr(this_cpu);
313 if (preemption_goodness(tsk, p, this_cpu) > 0)
314 tsk->need_resched = 1;
315#endif
316}
317
318
319
320
321
322
323
324
325
326
327
328
329static inline void add_to_runqueue(struct task_struct * p)
330{
331 list_add_tail(&p->run_list, &runqueue_head);
332 nr_running++;
333}
334
335static inline void move_last_runqueue(struct task_struct * p)
336{
337 list_del(&p->run_list);
338 list_add_tail(&p->run_list, &runqueue_head);
339}
340
341
342
343
344
345
346
347
348
349static inline int try_to_wake_up(struct task_struct * p, int synchronous)
350{
351 unsigned long flags;
352 int success = 0;
353
354
355
356
357 spin_lock_irqsave(&runqueue_lock, flags);
358 p->state = TASK_RUNNING;
359 if (task_on_runqueue(p))
360 goto out;
361 add_to_runqueue(p);
362 if (!synchronous || !(p->cpus_allowed & (1UL << smp_processor_id())))
363 reschedule_idle(p);
364 success = 1;
365out:
366 spin_unlock_irqrestore(&runqueue_lock, flags);
367 return success;
368}
369
370inline int fastcall wake_up_process(struct task_struct * p)
371{
372 return try_to_wake_up(p, 0);
373}
374
375static void process_timeout(unsigned long __data)
376{
377 struct task_struct * p = (struct task_struct *) __data;
378
379 wake_up_process(p);
380}
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408signed long fastcall schedule_timeout(signed long timeout)
409{
410 struct timer_list timer;
411 unsigned long expire;
412
413 switch (timeout)
414 {
415 case MAX_SCHEDULE_TIMEOUT:
416
417
418
419
420
421
422
423 schedule();
424 goto out;
425 default:
426
427
428
429
430
431
432
433 if (timeout < 0)
434 {
435 printk(KERN_ERR "schedule_timeout: wrong timeout "
436 "value %lx from %p\n", timeout,
437 __builtin_return_address(0));
438 current->state = TASK_RUNNING;
439 goto out;
440 }
441 }
442
443 expire = timeout + jiffies;
444
445 init_timer(&timer);
446 timer.expires = expire;
447 timer.data = (unsigned long) current;
448 timer.function = process_timeout;
449
450 add_timer(&timer);
451 schedule();
452 del_timer_sync(&timer);
453
454 timeout = expire - jiffies;
455
456 out:
457 return timeout < 0 ? 0 : timeout;
458}
459
460
461
462
463
464
465static inline void __schedule_tail(struct task_struct *prev)
466{
467#ifdef CONFIG_SMP
468 int policy;
469
470
471
472
473
474
475
476
477
478
479
480 policy = prev->policy;
481 prev->policy = policy & ~SCHED_YIELD;
482 wmb();
483
484
485
486
487
488
489 task_lock(prev);
490 task_release_cpu(prev);
491 mb();
492 if (prev->state == TASK_RUNNING)
493 goto needs_resched;
494
495out_unlock:
496 task_unlock(prev);
497 return;
498
499
500
501
502
503
504
505
506
507
508
509needs_resched:
510 {
511 unsigned long flags;
512
513
514
515
516
517 if ((prev == idle_task(smp_processor_id())) ||
518 (policy & SCHED_YIELD))
519 goto out_unlock;
520
521 spin_lock_irqsave(&runqueue_lock, flags);
522 if ((prev->state == TASK_RUNNING) && !task_has_cpu(prev))
523 reschedule_idle(prev);
524 spin_unlock_irqrestore(&runqueue_lock, flags);
525 goto out_unlock;
526 }
527#else
528 prev->policy &= ~SCHED_YIELD;
529#endif
530}
531
532asmlinkage void schedule_tail(struct task_struct *prev)
533{
534 __schedule_tail(prev);
535}
536
537
538
539
540
541
542
543
544
545
546
547asmlinkage void schedule(void)
548{
549 struct schedule_data * sched_data;
550 struct task_struct *prev, *next, *p;
551 struct list_head *tmp;
552 int this_cpu, c;
553
554
555 spin_lock_prefetch(&runqueue_lock);
556
557 BUG_ON(!current->active_mm);
558need_resched_back:
559 prev = current;
560 this_cpu = prev->processor;
561
562 if (unlikely(in_interrupt())) {
563 printk("Scheduling in interrupt\n");
564 BUG();
565 }
566
567 release_kernel_lock(prev, this_cpu);
568
569
570
571
572
573 sched_data = & aligned_data[this_cpu].schedule_data;
574
575 spin_lock_irq(&runqueue_lock);
576
577
578 if (unlikely(prev->policy == SCHED_RR))
579 if (!prev->counter) {
580 prev->counter = NICE_TO_TICKS(prev->nice);
581 move_last_runqueue(prev);
582 }
583
584 switch (prev->state) {
585 case TASK_INTERRUPTIBLE:
586 if (signal_pending(prev)) {
587 prev->state = TASK_RUNNING;
588 break;
589 }
590 default:
591 del_from_runqueue(prev);
592 case TASK_RUNNING:;
593 }
594 prev->need_resched = 0;
595
596
597
598
599
600repeat_schedule:
601
602
603
604 next = idle_task(this_cpu);
605 c = -1000;
606 list_for_each(tmp, &runqueue_head) {
607 p = list_entry(tmp, struct task_struct, run_list);
608 if (can_schedule(p, this_cpu)) {
609 int weight = goodness(p, this_cpu, prev->active_mm);
610 if (weight > c)
611 c = weight, next = p;
612 }
613 }
614
615
616 if (unlikely(!c)) {
617 struct task_struct *p;
618
619 spin_unlock_irq(&runqueue_lock);
620 read_lock(&tasklist_lock);
621 for_each_task(p)
622 p->counter = (p->counter >> 1) + NICE_TO_TICKS(p->nice);
623 read_unlock(&tasklist_lock);
624 spin_lock_irq(&runqueue_lock);
625 goto repeat_schedule;
626 }
627
628
629
630
631
632
633 sched_data->curr = next;
634 task_set_cpu(next, this_cpu);
635 spin_unlock_irq(&runqueue_lock);
636
637 if (unlikely(prev == next)) {
638
639 prev->policy &= ~SCHED_YIELD;
640 goto same_process;
641 }
642
643#ifdef CONFIG_SMP
644
645
646
647
648
649
650
651 sched_data->last_schedule = get_cycles();
652
653
654
655
656
657
658
659#endif
660
661 kstat.context_swtch++;
662
663
664
665
666
667
668
669
670
671 prepare_to_switch();
672 {
673 struct mm_struct *mm = next->mm;
674 struct mm_struct *oldmm = prev->active_mm;
675 if (!mm) {
676 BUG_ON(next->active_mm);
677 next->active_mm = oldmm;
678 atomic_inc(&oldmm->mm_count);
679 enter_lazy_tlb(oldmm, next, this_cpu);
680 } else {
681 BUG_ON(next->active_mm != mm);
682 switch_mm(oldmm, mm, next, this_cpu);
683 }
684
685 if (!prev->mm) {
686 prev->active_mm = NULL;
687 mmdrop(oldmm);
688 }
689 }
690
691
692
693
694
695 switch_to(prev, next, prev);
696 __schedule_tail(prev);
697
698same_process:
699 reacquire_kernel_lock(current);
700 if (current->need_resched)
701 goto need_resched_back;
702 return;
703}
704
705
706
707
708
709
710
711
712
713
714static inline void __wake_up_common (wait_queue_head_t *q, unsigned int mode,
715 int nr_exclusive, const int sync)
716{
717 struct list_head *tmp;
718 struct task_struct *p;
719
720 CHECK_MAGIC_WQHEAD(q);
721 WQ_CHECK_LIST_HEAD(&q->task_list);
722
723 list_for_each(tmp,&q->task_list) {
724 unsigned int state;
725 wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
726
727 CHECK_MAGIC(curr->__magic);
728 p = curr->task;
729 state = p->state;
730 if (state & mode) {
731 WQ_NOTE_WAKER(curr);
732 if (try_to_wake_up(p, sync) && (curr->flags&WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
733 break;
734 }
735 }
736}
737
738void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode, int nr)
739{
740 if (q) {
741 unsigned long flags;
742 wq_read_lock_irqsave(&q->lock, flags);
743 __wake_up_common(q, mode, nr, 0);
744 wq_read_unlock_irqrestore(&q->lock, flags);
745 }
746}
747
748void fastcall __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr)
749{
750 if (q) {
751 unsigned long flags;
752 wq_read_lock_irqsave(&q->lock, flags);
753 __wake_up_common(q, mode, nr, 1);
754 wq_read_unlock_irqrestore(&q->lock, flags);
755 }
756}
757
758void fastcall complete(struct completion *x)
759{
760 unsigned long flags;
761
762 spin_lock_irqsave(&x->wait.lock, flags);
763 x->done++;
764 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, 0);
765 spin_unlock_irqrestore(&x->wait.lock, flags);
766}
767
768void fastcall wait_for_completion(struct completion *x)
769{
770 spin_lock_irq(&x->wait.lock);
771 if (!x->done) {
772 DECLARE_WAITQUEUE(wait, current);
773
774 wait.flags |= WQ_FLAG_EXCLUSIVE;
775 __add_wait_queue_tail(&x->wait, &wait);
776 do {
777 __set_current_state(TASK_UNINTERRUPTIBLE);
778 spin_unlock_irq(&x->wait.lock);
779 schedule();
780 spin_lock_irq(&x->wait.lock);
781 } while (!x->done);
782 __remove_wait_queue(&x->wait, &wait);
783 }
784 x->done--;
785 spin_unlock_irq(&x->wait.lock);
786}
787
788#define SLEEP_ON_VAR \
789 unsigned long flags; \
790 wait_queue_t wait; \
791 init_waitqueue_entry(&wait, current);
792
793#define SLEEP_ON_HEAD \
794 wq_write_lock_irqsave(&q->lock,flags); \
795 __add_wait_queue(q, &wait); \
796 wq_write_unlock(&q->lock);
797
798#define SLEEP_ON_TAIL \
799 wq_write_lock_irq(&q->lock); \
800 __remove_wait_queue(q, &wait); \
801 wq_write_unlock_irqrestore(&q->lock,flags);
802
803void fastcall interruptible_sleep_on(wait_queue_head_t *q)
804{
805 SLEEP_ON_VAR
806
807 current->state = TASK_INTERRUPTIBLE;
808
809 SLEEP_ON_HEAD
810 schedule();
811 SLEEP_ON_TAIL
812}
813
814long fastcall interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
815{
816 SLEEP_ON_VAR
817
818 current->state = TASK_INTERRUPTIBLE;
819
820 SLEEP_ON_HEAD
821 timeout = schedule_timeout(timeout);
822 SLEEP_ON_TAIL
823
824 return timeout;
825}
826
827void fastcall sleep_on(wait_queue_head_t *q)
828{
829 SLEEP_ON_VAR
830
831 current->state = TASK_UNINTERRUPTIBLE;
832
833 SLEEP_ON_HEAD
834 schedule();
835 SLEEP_ON_TAIL
836}
837
838long fastcall sleep_on_timeout(wait_queue_head_t *q, long timeout)
839{
840 SLEEP_ON_VAR
841
842 current->state = TASK_UNINTERRUPTIBLE;
843
844 SLEEP_ON_HEAD
845 timeout = schedule_timeout(timeout);
846 SLEEP_ON_TAIL
847
848 return timeout;
849}
850
851void scheduling_functions_end_here(void) { }
852
853#if CONFIG_SMP
854
855
856
857
858
859
860
861
862
863void set_cpus_allowed(struct task_struct *p, unsigned long new_mask)
864{
865 new_mask &= cpu_online_map;
866 BUG_ON(!new_mask);
867
868 p->cpus_allowed = new_mask;
869
870
871
872
873
874
875 if (!(p->cpus_runnable & p->cpus_allowed)) {
876 if (p != current) {
877 p->need_resched = 1;
878 smp_send_reschedule(p->processor);
879 }
880
881
882
883
884
885 while (!(p->cpus_runnable & p->cpus_allowed))
886 schedule();
887 }
888}
889#endif
890
891#ifndef __alpha__
892
893
894
895
896
897
898
899asmlinkage long sys_nice(int increment)
900{
901 long newprio;
902
903
904
905
906
907
908 if (increment < 0) {
909 if (!capable(CAP_SYS_NICE))
910 return -EPERM;
911 if (increment < -40)
912 increment = -40;
913 }
914 if (increment > 40)
915 increment = 40;
916
917 newprio = current->nice + increment;
918 if (newprio < -20)
919 newprio = -20;
920 if (newprio > 19)
921 newprio = 19;
922 current->nice = newprio;
923 return 0;
924}
925
926#endif
927
928static inline struct task_struct *find_process_by_pid(pid_t pid)
929{
930 struct task_struct *tsk = current;
931
932 if (pid)
933 tsk = find_task_by_pid(pid);
934 return tsk;
935}
936
937static int setscheduler(pid_t pid, int policy,
938 struct sched_param *param)
939{
940 struct sched_param lp;
941 struct task_struct *p;
942 int retval;
943
944 retval = -EINVAL;
945 if (!param || pid < 0)
946 goto out_nounlock;
947
948 retval = -EFAULT;
949 if (copy_from_user(&lp, param, sizeof(struct sched_param)))
950 goto out_nounlock;
951
952
953
954
955 read_lock_irq(&tasklist_lock);
956 spin_lock(&runqueue_lock);
957
958 p = find_process_by_pid(pid);
959
960 retval = -ESRCH;
961 if (!p)
962 goto out_unlock;
963
964 if (policy < 0)
965 policy = p->policy;
966 else {
967 retval = -EINVAL;
968 if (policy != SCHED_FIFO && policy != SCHED_RR &&
969 policy != SCHED_OTHER)
970 goto out_unlock;
971 }
972
973
974
975
976
977 retval = -EINVAL;
978 if (lp.sched_priority < 0 || lp.sched_priority > 99)
979 goto out_unlock;
980 if ((policy == SCHED_OTHER) != (lp.sched_priority == 0))
981 goto out_unlock;
982
983 retval = -EPERM;
984 if ((policy == SCHED_FIFO || policy == SCHED_RR) &&
985 !capable(CAP_SYS_NICE))
986 goto out_unlock;
987 if ((current->euid != p->euid) && (current->euid != p->uid) &&
988 !capable(CAP_SYS_NICE))
989 goto out_unlock;
990
991 retval = 0;
992 p->policy = policy;
993 p->rt_priority = lp.sched_priority;
994
995 current->need_resched = 1;
996
997out_unlock:
998 spin_unlock(&runqueue_lock);
999 read_unlock_irq(&tasklist_lock);
1000
1001out_nounlock:
1002 return retval;
1003}
1004
1005asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
1006 struct sched_param *param)
1007{
1008 return setscheduler(pid, policy, param);
1009}
1010
1011asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param *param)
1012{
1013 return setscheduler(pid, -1, param);
1014}
1015
1016asmlinkage long sys_sched_getscheduler(pid_t pid)
1017{
1018 struct task_struct *p;
1019 int retval;
1020
1021 retval = -EINVAL;
1022 if (pid < 0)
1023 goto out_nounlock;
1024
1025 retval = -ESRCH;
1026 read_lock(&tasklist_lock);
1027 p = find_process_by_pid(pid);
1028 if (p)
1029 retval = p->policy & ~SCHED_YIELD;
1030 read_unlock(&tasklist_lock);
1031
1032out_nounlock:
1033 return retval;
1034}
1035
1036asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param *param)
1037{
1038 struct task_struct *p;
1039 struct sched_param lp;
1040 int retval;
1041
1042 retval = -EINVAL;
1043 if (!param || pid < 0)
1044 goto out_nounlock;
1045
1046 read_lock(&tasklist_lock);
1047 p = find_process_by_pid(pid);
1048 retval = -ESRCH;
1049 if (!p)
1050 goto out_unlock;
1051 lp.sched_priority = p->rt_priority;
1052 read_unlock(&tasklist_lock);
1053
1054
1055
1056
1057 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
1058
1059out_nounlock:
1060 return retval;
1061
1062out_unlock:
1063 read_unlock(&tasklist_lock);
1064 return retval;
1065}
1066
1067asmlinkage long sys_sched_yield(void)
1068{
1069
1070
1071
1072
1073
1074
1075
1076
1077 int nr_pending = nr_running;
1078
1079#if CONFIG_SMP
1080 int i;
1081
1082
1083 for (i = 0; i < smp_num_cpus; i++) {
1084 int cpu = cpu_logical_map(i);
1085 if (aligned_data[cpu].schedule_data.curr != idle_task(cpu))
1086 nr_pending--;
1087 }
1088#else
1089
1090 nr_pending--;
1091#endif
1092 if (nr_pending) {
1093
1094
1095
1096
1097 if (current->policy == SCHED_OTHER)
1098 current->policy |= SCHED_YIELD;
1099 current->need_resched = 1;
1100
1101 spin_lock_irq(&runqueue_lock);
1102 move_last_runqueue(current);
1103 spin_unlock_irq(&runqueue_lock);
1104 }
1105 return 0;
1106}
1107
1108
1109
1110
1111
1112
1113
1114void yield(void)
1115{
1116 set_current_state(TASK_RUNNING);
1117 sys_sched_yield();
1118 schedule();
1119}
1120
1121void __cond_resched(void)
1122{
1123 set_current_state(TASK_RUNNING);
1124 schedule();
1125}
1126
1127asmlinkage long sys_sched_get_priority_max(int policy)
1128{
1129 int ret = -EINVAL;
1130
1131 switch (policy) {
1132 case SCHED_FIFO:
1133 case SCHED_RR:
1134 ret = 99;
1135 break;
1136 case SCHED_OTHER:
1137 ret = 0;
1138 break;
1139 }
1140 return ret;
1141}
1142
1143asmlinkage long sys_sched_get_priority_min(int policy)
1144{
1145 int ret = -EINVAL;
1146
1147 switch (policy) {
1148 case SCHED_FIFO:
1149 case SCHED_RR:
1150 ret = 1;
1151 break;
1152 case SCHED_OTHER:
1153 ret = 0;
1154 }
1155 return ret;
1156}
1157
1158asmlinkage long sys_sched_rr_get_interval(pid_t pid, struct timespec *interval)
1159{
1160 struct timespec t;
1161 struct task_struct *p;
1162 int retval = -EINVAL;
1163
1164 if (pid < 0)
1165 goto out_nounlock;
1166
1167 retval = -ESRCH;
1168 read_lock(&tasklist_lock);
1169 p = find_process_by_pid(pid);
1170 if (p)
1171 jiffies_to_timespec(p->policy & SCHED_FIFO ? 0 : NICE_TO_TICKS(p->nice),
1172 &t);
1173 read_unlock(&tasklist_lock);
1174 if (p)
1175 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
1176out_nounlock:
1177 return retval;
1178}
1179
1180static void show_task(struct task_struct * p)
1181{
1182 unsigned long free = 0;
1183 int state;
1184 static const char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
1185
1186 printk("%-13.13s ", p->comm);
1187 state = p->state ? ffz(~p->state) + 1 : 0;
1188 if (((unsigned) state) < sizeof(stat_nam)/sizeof(char *))
1189 printk(stat_nam[state]);
1190 else
1191 printk(" ");
1192#if (BITS_PER_LONG == 32)
1193 if (p == current)
1194 printk(" current ");
1195 else
1196 printk(" %08lX ", thread_saved_pc(&p->thread));
1197#else
1198 if (p == current)
1199 printk(" current task ");
1200 else
1201 printk(" %016lx ", thread_saved_pc(&p->thread));
1202#endif
1203 {
1204 unsigned long * n = (unsigned long *) (p+1);
1205 while (!*n)
1206 n++;
1207 free = (unsigned long) n - (unsigned long)(p+1);
1208 }
1209 printk("%5lu %5d %6d ", free, p->pid, p->p_pptr->pid);
1210 if (p->p_cptr)
1211 printk("%5d ", p->p_cptr->pid);
1212 else
1213 printk(" ");
1214 if (p->p_ysptr)
1215 printk("%7d", p->p_ysptr->pid);
1216 else
1217 printk(" ");
1218 if (p->p_osptr)
1219 printk(" %5d", p->p_osptr->pid);
1220 else
1221 printk(" ");
1222 if (!p->mm)
1223 printk(" (L-TLB)\n");
1224 else
1225 printk(" (NOTLB)\n");
1226
1227 {
1228 extern void show_trace_task(struct task_struct *tsk);
1229 show_trace_task(p);
1230 }
1231}
1232
1233char * render_sigset_t(sigset_t *set, char *buffer)
1234{
1235 int i = _NSIG, x;
1236 do {
1237 i -= 4, x = 0;
1238 if (sigismember(set, i+1)) x |= 1;
1239 if (sigismember(set, i+2)) x |= 2;
1240 if (sigismember(set, i+3)) x |= 4;
1241 if (sigismember(set, i+4)) x |= 8;
1242 *buffer++ = (x < 10 ? '0' : 'a' - 10) + x;
1243 } while (i >= 4);
1244 *buffer = 0;
1245 return buffer;
1246}
1247
1248void show_state(void)
1249{
1250 struct task_struct *p;
1251
1252#if (BITS_PER_LONG == 32)
1253 printk("\n"
1254 " free sibling\n");
1255 printk(" task PC stack pid father child younger older\n");
1256#else
1257 printk("\n"
1258 " free sibling\n");
1259 printk(" task PC stack pid father child younger older\n");
1260#endif
1261 read_lock(&tasklist_lock);
1262 for_each_task(p) {
1263
1264
1265
1266
1267 touch_nmi_watchdog();
1268 show_task(p);
1269 }
1270 read_unlock(&tasklist_lock);
1271}
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285void reparent_to_init(void)
1286{
1287 struct task_struct *this_task = current;
1288
1289 write_lock_irq(&tasklist_lock);
1290
1291
1292 REMOVE_LINKS(this_task);
1293 this_task->p_pptr = child_reaper;
1294 this_task->p_opptr = child_reaper;
1295 SET_LINKS(this_task);
1296
1297
1298 this_task->exit_signal = SIGCHLD;
1299
1300
1301
1302 spin_lock(&runqueue_lock);
1303
1304 this_task->ptrace = 0;
1305 this_task->nice = DEF_NICE;
1306 this_task->policy = SCHED_OTHER;
1307
1308
1309
1310 this_task->cap_effective = CAP_INIT_EFF_SET;
1311 this_task->cap_inheritable = CAP_INIT_INH_SET;
1312 this_task->cap_permitted = CAP_FULL_SET;
1313 this_task->keep_capabilities = 0;
1314 memcpy(this_task->rlim, init_task.rlim, sizeof(*(this_task->rlim)));
1315 switch_uid(INIT_USER);
1316
1317 spin_unlock(&runqueue_lock);
1318 write_unlock_irq(&tasklist_lock);
1319}
1320
1321
1322
1323
1324
1325
1326void daemonize(void)
1327{
1328 struct fs_struct *fs;
1329
1330
1331
1332
1333
1334
1335
1336 exit_mm(current);
1337
1338 current->session = 1;
1339 current->pgrp = 1;
1340 current->tty = NULL;
1341
1342
1343
1344 exit_fs(current);
1345 fs = init_task.fs;
1346 current->fs = fs;
1347 atomic_inc(&fs->count);
1348 exit_files(current);
1349 current->files = init_task.files;
1350 atomic_inc(¤t->files->count);
1351}
1352
1353extern unsigned long wait_init_idle;
1354
1355void __init init_idle(void)
1356{
1357 struct schedule_data * sched_data;
1358 sched_data = &aligned_data[smp_processor_id()].schedule_data;
1359
1360 if (current != &init_task && task_on_runqueue(current)) {
1361 printk("UGH! (%d:%d) was on the runqueue, removing.\n",
1362 smp_processor_id(), current->pid);
1363 del_from_runqueue(current);
1364 }
1365 sched_data->curr = current;
1366 sched_data->last_schedule = get_cycles();
1367 clear_bit(current->processor, &wait_init_idle);
1368}
1369
1370extern void init_timervecs (void);
1371
1372void __init sched_init(void)
1373{
1374
1375
1376
1377
1378 int cpu = smp_processor_id();
1379 int nr;
1380
1381 init_task.processor = cpu;
1382
1383 for(nr = 0; nr < PIDHASH_SZ; nr++)
1384 pidhash[nr] = NULL;
1385
1386 init_timervecs();
1387
1388 init_bh(TIMER_BH, timer_bh);
1389 init_bh(TQUEUE_BH, tqueue_bh);
1390 init_bh(IMMEDIATE_BH, immediate_bh);
1391
1392
1393
1394
1395 atomic_inc(&init_mm.mm_count);
1396 enter_lazy_tlb(&init_mm, current, cpu);
1397}
1398