FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
linux/kernel/fork.c at master · codewhy/linux · GitHub
codewhy
/
linux
Public
forked from
torvalds/linux
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
linux
/
kernel
/
fork.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1815 lines (1560 loc) · 42.9 KB
Breadcrumbs
linux
/
kernel
/
fork.c
Copy path
File metadata and controls
1815 lines (1560 loc) · 42.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* linux/kernel/fork.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/*
* 'fork.c' contains the help-routines for the 'fork' system call
* (see also entry.S and others).
* Fork is rather simple, once you get the hang of it, but the memory
* management can be a bitch. See 'mm/memory.c': 'copy_page_range()'
*/
#include
<linux/slab.h>
#include
<linux/init.h>
#include
<linux/unistd.h>
#include
<linux/module.h>
#include
<linux/vmalloc.h>
#include
<linux/completion.h>
#include
<linux/personality.h>
#include
<linux/mempolicy.h>
#include
<linux/sem.h>
#include
<linux/file.h>
#include
<linux/fdtable.h>
#include
<linux/iocontext.h>
#include
<linux/key.h>
#include
<linux/binfmts.h>
#include
<linux/mman.h>
#include
<linux/mmu_notifier.h>
#include
<linux/fs.h>
#include
<linux/nsproxy.h>
#include
<linux/capability.h>
#include
<linux/cpu.h>
#include
<linux/cgroup.h>
#include
<linux/security.h>
#include
<linux/hugetlb.h>
#include
<linux/swap.h>
#include
<linux/syscalls.h>
#include
<linux/jiffies.h>
#include
<linux/futex.h>
#include
<linux/compat.h>
#include
<linux/kthread.h>
#include
<linux/task_io_accounting_ops.h>
#include
<linux/rcupdate.h>
#include
<linux/ptrace.h>
#include
<linux/mount.h>
#include
<linux/audit.h>
#include
<linux/memcontrol.h>
#include
<linux/ftrace.h>
#include
<linux/profile.h>
#include
<linux/rmap.h>
#include
<linux/ksm.h>
#include
<linux/acct.h>
#include
<linux/tsacct_kern.h>
#include
<linux/cn_proc.h>
#include
<linux/freezer.h>
#include
<linux/delayacct.h>
#include
<linux/taskstats_kern.h>
#include
<linux/random.h>
#include
<linux/tty.h>
#include
<linux/blkdev.h>
#include
<linux/fs_struct.h>
#include
<linux/magic.h>
#include
<linux/perf_event.h>
#include
<linux/posix-timers.h>
#include
<linux/user-return-notifier.h>
#include
<linux/oom.h>
#include
<linux/khugepaged.h>
#include
<linux/signalfd.h>
#include
<asm/pgtable.h>
#include
<asm/pgalloc.h>
#include
<asm/uaccess.h>
#include
<asm/mmu_context.h>
#include
<asm/cacheflush.h>
#include
<asm/tlbflush.h>
#include
<trace/events/sched.h>
#define
CREATE_TRACE_POINTS
#include
<trace/events/task.h>
/*
* Protected counters by write_lock_irq(&tasklist_lock)
*/
unsigned long
total_forks
;
/* Handle normal Linux uptimes. */
int
nr_threads
;
/* The idle threads do not count.. */
int
max_threads
;
/* tunable limit on nr_threads */
DEFINE_PER_CPU
(
unsigned long,
process_counts
)
=
0
;
__cacheline_aligned
DEFINE_RWLOCK
(
tasklist_lock
);
/* outer */
#ifdef
CONFIG_PROVE_RCU
int
lockdep_tasklist_lock_is_held
(
void
)
{
return
lockdep_is_held
(
&
tasklist_lock
);
}
EXPORT_SYMBOL_GPL
(
lockdep_tasklist_lock_is_held
);
#endif
/* #ifdef CONFIG_PROVE_RCU */
int
nr_processes
(
void
)
{
int
cpu
;
int
total
=
0
;
for_each_possible_cpu
(
cpu
)
total
+=
per_cpu
(
process_counts
,
cpu
);
return
total
;
}
#ifndef
__HAVE_ARCH_TASK_STRUCT_ALLOCATOR
# define
alloc_task_struct_node
(
node
) \
kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
# define
free_task_struct
(
tsk
) \
kmem_cache_free(task_struct_cachep, (tsk))
static
struct
kmem_cache
*
task_struct_cachep
;
#endif
#ifndef
__HAVE_ARCH_THREAD_INFO_ALLOCATOR
static
struct
thread_info
*
alloc_thread_info_node
(
struct
task_struct
*
tsk
,
int
node
)
{
#ifdef
CONFIG_DEBUG_STACK_USAGE
gfp_t
mask
=
GFP_KERNEL
|
__GFP_ZERO
;
#else
gfp_t
mask
=
GFP_KERNEL
;
#endif
struct
page
*
page
=
alloc_pages_node
(
node
,
mask
,
THREAD_SIZE_ORDER
);
return
page
?
page_address
(
page
) :
NULL
;
}
static
inline
void
free_thread_info
(
struct
thread_info
*
ti
)
{
free_pages
((
unsigned long
)
ti
,
THREAD_SIZE_ORDER
);
}
#endif
/* SLAB cache for signal_struct structures (tsk->signal) */
static
struct
kmem_cache
*
signal_cachep
;
/* SLAB cache for sighand_struct structures (tsk->sighand) */
struct
kmem_cache
*
sighand_cachep
;
/* SLAB cache for files_struct structures (tsk->files) */
struct
kmem_cache
*
files_cachep
;
/* SLAB cache for fs_struct structures (tsk->fs) */
struct
kmem_cache
*
fs_cachep
;
/* SLAB cache for vm_area_struct structures */
struct
kmem_cache
*
vm_area_cachep
;
/* SLAB cache for mm_struct structures (tsk->mm) */
static
struct
kmem_cache
*
mm_cachep
;
static
void
account_kernel_stack
(
struct
thread_info
*
ti
,
int
account
)
{
struct
zone
*
zone
=
page_zone
(
virt_to_page
(
ti
));
mod_zone_page_state
(
zone
,
NR_KERNEL_STACK
,
account
);
}
void
free_task
(
struct
task_struct
*
tsk
)
{
account_kernel_stack
(
tsk
->
stack
,
-1
);
free_thread_info
(
tsk
->
stack
);
rt_mutex_debug_task_free
(
tsk
);
ftrace_graph_exit_task
(
tsk
);
free_task_struct
(
tsk
);
}
EXPORT_SYMBOL
(
free_task
);
static
inline
void
free_signal_struct
(
struct
signal_struct
*
sig
)
{
taskstats_tgid_free
(
sig
);
sched_autogroup_exit
(
sig
);
kmem_cache_free
(
signal_cachep
,
sig
);
}
static
inline
void
put_signal_struct
(
struct
signal_struct
*
sig
)
{
if
(
atomic_dec_and_test
(
&
sig
->
sigcnt
))
free_signal_struct
(
sig
);
}
void
__put_task_struct
(
struct
task_struct
*
tsk
)
{
WARN_ON
(!
tsk
->
exit_state
);
WARN_ON
(
atomic_read
(
&
tsk
->
usage
));
WARN_ON
(
tsk
==
current
);
exit_creds
(
tsk
);
delayacct_tsk_free
(
tsk
);
put_signal_struct
(
tsk
->
signal
);
if
(!
profile_handoff_task
(
tsk
))
free_task
(
tsk
);
}
EXPORT_SYMBOL_GPL
(
__put_task_struct
);
/*
* macro override instead of weak attribute alias, to workaround
* gcc 4.1.0 and 4.1.1 bugs with weak attribute and empty functions.
*/
#ifndef
arch_task_cache_init
#define
arch_task_cache_init
()
#endif
void
__init
fork_init
(
unsigned long
mempages
)
{
#ifndef
__HAVE_ARCH_TASK_STRUCT_ALLOCATOR
#ifndef
ARCH_MIN_TASKALIGN
#define
ARCH_MIN_TASKALIGN
L1_CACHE_BYTES
#endif
/* create a slab on which task_structs can be allocated */
task_struct_cachep
=
kmem_cache_create
(
"task_struct"
,
sizeof
(
struct
task_struct
),
ARCH_MIN_TASKALIGN
,
SLAB_PANIC
|
SLAB_NOTRACK
,
NULL
);
#endif
/* do the arch specific task caches init */
arch_task_cache_init
();
/*
* The default maximum number of threads is set to a safe
* value: the thread structures can take up at most half
* of memory.
*/
max_threads
=
mempages
/ (
8
*
THREAD_SIZE
/
PAGE_SIZE
);
/*
* we need to allow at least 20 threads to boot a system
*/
if
(
max_threads
<
20
)
max_threads
=
20
;
init_task
.
signal
->
rlim
[
RLIMIT_NPROC
].
rlim_cur
=
max_threads
/
2
;
init_task
.
signal
->
rlim
[
RLIMIT_NPROC
].
rlim_max
=
max_threads
/
2
;
init_task
.
signal
->
rlim
[
RLIMIT_SIGPENDING
]
=
init_task
.
signal
->
rlim
[
RLIMIT_NPROC
];
}
int
__attribute__((
weak
))
arch_dup_task_struct
(
struct
task_struct
*
dst
,
struct
task_struct
*
src
)
{
*
dst
=
*
src
;
return
0
;
}
static
struct
task_struct
*
dup_task_struct
(
struct
task_struct
*
orig
)
{
struct
task_struct
*
tsk
;
struct
thread_info
*
ti
;
unsigned long
*
stackend
;
int
node
=
tsk_fork_get_node
(
orig
);
int
err
;
prepare_to_copy
(
orig
);
tsk
=
alloc_task_struct_node
(
node
);
if
(!
tsk
)
return
NULL
;
ti
=
alloc_thread_info_node
(
tsk
,
node
);
if
(!
ti
) {
free_task_struct
(
tsk
);
return
NULL
;
}
err
=
arch_dup_task_struct
(
tsk
,
orig
);
if
(
err
)
goto
out
;
tsk
->
stack
=
ti
;
setup_thread_stack
(
tsk
,
orig
);
clear_user_return_notifier
(
tsk
);
clear_tsk_need_resched
(
tsk
);
stackend
=
end_of_stack
(
tsk
);
*
stackend
=
STACK_END_MAGIC
;
/* for overflow detection */
#ifdef
CONFIG_CC_STACKPROTECTOR
tsk
->
stack_canary
=
get_random_int
();
#endif
/*
* One for us, one for whoever does the "release_task()" (usually
* parent)
*/
atomic_set
(
&
tsk
->
usage
,
2
);
#ifdef
CONFIG_BLK_DEV_IO_TRACE
tsk
->
btrace_seq
=
0
;
#endif
tsk
->
splice_pipe
=
NULL
;
account_kernel_stack
(
ti
,
1
);
return
tsk
;
out
:
free_thread_info
(
ti
);
free_task_struct
(
tsk
);
return
NULL
;
}
#ifdef
CONFIG_MMU
static
int
dup_mmap
(
struct
mm_struct
*
mm
,
struct
mm_struct
*
oldmm
)
{
struct
vm_area_struct
*
mpnt
,
*
tmp
,
*
prev
,
*
*
pprev
;
struct
rb_node
*
*
rb_link
,
*
rb_parent
;
int
retval
;
unsigned long
charge
;
struct
mempolicy
*
pol
;
down_write
(
&
oldmm
->
mmap_sem
);
flush_cache_dup_mm
(
oldmm
);
/*
* Not linked in yet - no deadlock potential:
*/
down_write_nested
(
&
mm
->
mmap_sem
,
SINGLE_DEPTH_NESTING
);
mm
->
locked_vm
=
0
;
mm
->
mmap
=
NULL
;
mm
->
mmap_cache
=
NULL
;
mm
->
free_area_cache
=
oldmm
->
mmap_base
;
mm
->
cached_hole_size
=
~
0UL
;
mm
->
map_count
=
0
;
cpumask_clear
(
mm_cpumask
(
mm
));
mm
->
mm_rb
=
RB_ROOT
;
rb_link
=
&
mm
->
mm_rb
.
rb_node
;
rb_parent
=
NULL
;
pprev
=
&
mm
->
mmap
;
retval
=
ksm_fork
(
mm
,
oldmm
);
if
(
retval
)
goto
out
;
retval
=
khugepaged_fork
(
mm
,
oldmm
);
if
(
retval
)
goto
out
;
prev
=
NULL
;
for
(
mpnt
=
oldmm
->
mmap
;
mpnt
;
mpnt
=
mpnt
->
vm_next
) {
struct
file
*
file
;
if
(
mpnt
->
vm_flags
&
VM_DONTCOPY
) {
long
pages
=
vma_pages
(
mpnt
);
mm
->
total_vm
-=
pages
;
vm_stat_account
(
mm
,
mpnt
->
vm_flags
,
mpnt
->
vm_file
,
-
pages
);
continue
;
}
charge
=
0
;
if
(
mpnt
->
vm_flags
&
VM_ACCOUNT
) {
unsigned
int
len
=
(
mpnt
->
vm_end
-
mpnt
->
vm_start
) >>
PAGE_SHIFT
;
if
(
security_vm_enough_memory
(
len
))
goto
fail_nomem
;
charge
=
len
;
}
tmp
=
kmem_cache_alloc
(
vm_area_cachep
,
GFP_KERNEL
);
if
(!
tmp
)
goto
fail_nomem
;
*
tmp
=
*
mpnt
;
INIT_LIST_HEAD
(
&
tmp
->
anon_vma_chain
);
pol
=
mpol_dup
(
vma_policy
(
mpnt
));
retval
=
PTR_ERR
(
pol
);
if
(
IS_ERR
(
pol
))
goto
fail_nomem_policy
;
vma_set_policy
(
tmp
,
pol
);
tmp
->
vm_mm
=
mm
;
if
(
anon_vma_fork
(
tmp
,
mpnt
))
goto
fail_nomem_anon_vma_fork
;
tmp
->
vm_flags
&= ~
VM_LOCKED
;
tmp
->
vm_next
=
tmp
->
vm_prev
=
NULL
;
file
=
tmp
->
vm_file
;
if
(
file
) {
struct
inode
*
inode
=
file
->
f_path
.
dentry
->
d_inode
;
struct
address_space
*
mapping
=
file
->
f_mapping
;
get_file
(
file
);
if
(
tmp
->
vm_flags
&
VM_DENYWRITE
)
atomic_dec
(
&
inode
->
i_writecount
);
mutex_lock
(
&
mapping
->
i_mmap_mutex
);
if
(
tmp
->
vm_flags
&
VM_SHARED
)
mapping
->
i_mmap_writable
++
;
flush_dcache_mmap_lock
(
mapping
);
/* insert tmp into the share list, just after mpnt */
vma_prio_tree_add
(
tmp
,
mpnt
);
flush_dcache_mmap_unlock
(
mapping
);
mutex_unlock
(
&
mapping
->
i_mmap_mutex
);
}
/*
* Clear hugetlb-related page reserves for children. This only
* affects MAP_PRIVATE mappings. Faults generated by the child
* are not guaranteed to succeed, even if read-only
*/
if
(
is_vm_hugetlb_page
(
tmp
))
reset_vma_resv_huge_pages
(
tmp
);
/*
* Link in the new vma and copy the page table entries.
*/
*
pprev
=
tmp
;
pprev
=
&
tmp
->
vm_next
;
tmp
->
vm_prev
=
prev
;
prev
=
tmp
;
__vma_link_rb
(
mm
,
tmp
,
rb_link
,
rb_parent
);
rb_link
=
&
tmp
->
vm_rb
.
rb_right
;
rb_parent
=
&
tmp
->
vm_rb
;
mm
->
map_count
++
;
retval
=
copy_page_range
(
mm
,
oldmm
,
mpnt
);
if
(
tmp
->
vm_ops
&&
tmp
->
vm_ops
->
open
)
tmp
->
vm_ops
->
open
(
tmp
);
if
(
retval
)
goto
out
;
}
/* a new mm has just been created */
arch_dup_mmap
(
oldmm
,
mm
);
retval
=
0
;
out
:
up_write
(
&
mm
->
mmap_sem
);
flush_tlb_mm
(
oldmm
);
up_write
(
&
oldmm
->
mmap_sem
);
return
retval
;
fail_nomem_anon_vma_fork
:
mpol_put
(
pol
);
fail_nomem_policy
:
kmem_cache_free
(
vm_area_cachep
,
tmp
);
fail_nomem
:
retval
=
-
ENOMEM
;
vm_unacct_memory
(
charge
);
goto
out
;
}
static
inline
int
mm_alloc_pgd
(
struct
mm_struct
*
mm
)
{
mm
->
pgd
=
pgd_alloc
(
mm
);
if
(
unlikely
(!
mm
->
pgd
))
return
-
ENOMEM
;
return
0
;
}
static
inline
void
mm_free_pgd
(
struct
mm_struct
*
mm
)
{
pgd_free
(
mm
,
mm
->
pgd
);
}
#else
#define
dup_mmap
(
mm
,
oldmm
) (0)
#define
mm_alloc_pgd
(
mm
) (0)
#define
mm_free_pgd
(
mm
)
#endif
/* CONFIG_MMU */
__cacheline_aligned_in_smp
DEFINE_SPINLOCK
(
mmlist_lock
);
#define
allocate_mm
() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
#define
free_mm
(
mm
) (kmem_cache_free(mm_cachep, (mm)))
static
unsigned long
default_dump_filter
=
MMF_DUMP_FILTER_DEFAULT
;
static
int
__init
coredump_filter_setup
(
char
*
s
)
{
default_dump_filter
=
(
simple_strtoul
(
s
,
NULL
,
0
) <<
MMF_DUMP_FILTER_SHIFT
)
&
MMF_DUMP_FILTER_MASK
;
return
1
;
}
__setup
(
"coredump_filter="
,
coredump_filter_setup
);
#include
<linux/init_task.h>
static
void
mm_init_aio
(
struct
mm_struct
*
mm
)
{
#ifdef
CONFIG_AIO
spin_lock_init
(
&
mm
->
ioctx_lock
);
INIT_HLIST_HEAD
(
&
mm
->
ioctx_list
);
#endif
}
static
struct
mm_struct
*
mm_init
(
struct
mm_struct
*
mm
,
struct
task_struct
*
p
)
{
atomic_set
(
&
mm
->
mm_users
,
1
);
atomic_set
(
&
mm
->
mm_count
,
1
);
init_rwsem
(
&
mm
->
mmap_sem
);
INIT_LIST_HEAD
(
&
mm
->
mmlist
);
mm
->
flags
=
(
current
->
mm
) ?
(
current
->
mm
->
flags
&
MMF_INIT_MASK
) :
default_dump_filter
;
mm
->
core_state
=
NULL
;
mm
->
nr_ptes
=
0
;
memset
(
&
mm
->
rss_stat
,
0
,
sizeof
(
mm
->
rss_stat
));
spin_lock_init
(
&
mm
->
page_table_lock
);
mm
->
free_area_cache
=
TASK_UNMAPPED_BASE
;
mm
->
cached_hole_size
=
~
0UL
;
mm_init_aio
(
mm
);
mm_init_owner
(
mm
,
p
);
if
(
likely
(!
mm_alloc_pgd
(
mm
))) {
mm
->
def_flags
=
0
;
mmu_notifier_mm_init
(
mm
);
return
mm
;
}
free_mm
(
mm
);
return
NULL
;
}
/*
* Allocate and initialize an mm_struct.
*/
struct
mm_struct
*
mm_alloc
(
void
)
{
struct
mm_struct
*
mm
;
mm
=
allocate_mm
();
if
(!
mm
)
return
NULL
;
memset
(
mm
,
0
,
sizeof
(
*
mm
));
mm_init_cpumask
(
mm
);
return
mm_init
(
mm
,
current
);
}
/*
* Called when the last reference to the mm
* is dropped: either by a lazy thread or by
* mmput. Free the page directory and the mm.
*/
void
__mmdrop
(
struct
mm_struct
*
mm
)
{
BUG_ON
(
mm
==
&
init_mm
);
mm_free_pgd
(
mm
);
destroy_context
(
mm
);
mmu_notifier_mm_destroy
(
mm
);
#ifdef
CONFIG_TRANSPARENT_HUGEPAGE
VM_BUG_ON
(
mm
->
pmd_huge_pte
);
#endif
free_mm
(
mm
);
}
EXPORT_SYMBOL_GPL
(
__mmdrop
);
/*
* Decrement the use count and release all resources for an mm.
*/
void
mmput
(
struct
mm_struct
*
mm
)
{
might_sleep
();
if
(
atomic_dec_and_test
(
&
mm
->
mm_users
)) {
exit_aio
(
mm
);
ksm_exit
(
mm
);
khugepaged_exit
(
mm
);
/* must run before exit_mmap */
exit_mmap
(
mm
);
set_mm_exe_file
(
mm
,
NULL
);
if
(!
list_empty
(
&
mm
->
mmlist
)) {
spin_lock
(
&
mmlist_lock
);
list_del
(
&
mm
->
mmlist
);
spin_unlock
(
&
mmlist_lock
);
}
put_swap_token
(
mm
);
if
(
mm
->
binfmt
)
module_put
(
mm
->
binfmt
->
module
);
mmdrop
(
mm
);
}
}
EXPORT_SYMBOL_GPL
(
mmput
);
/*
* We added or removed a vma mapping the executable. The vmas are only mapped
* during exec and are not mapped with the mmap system call.
* Callers must hold down_write() on the mm's mmap_sem for these
*/
void
added_exe_file_vma
(
struct
mm_struct
*
mm
)
{
mm
->
num_exe_file_vmas
++
;
}
void
removed_exe_file_vma
(
struct
mm_struct
*
mm
)
{
mm
->
num_exe_file_vmas
--
;
if
((
mm
->
num_exe_file_vmas
==
0
)
&&
mm
->
exe_file
) {
fput
(
mm
->
exe_file
);
mm
->
exe_file
=
NULL
;
}
}
void
set_mm_exe_file
(
struct
mm_struct
*
mm
,
struct
file
*
new_exe_file
)
{
if
(
new_exe_file
)
get_file
(
new_exe_file
);
if
(
mm
->
exe_file
)
fput
(
mm
->
exe_file
);
mm
->
exe_file
=
new_exe_file
;
mm
->
num_exe_file_vmas
=
0
;
}
struct
file
*
get_mm_exe_file
(
struct
mm_struct
*
mm
)
{
struct
file
*
exe_file
;
/* We need mmap_sem to protect against races with removal of
* VM_EXECUTABLE vmas */
down_read
(
&
mm
->
mmap_sem
);
exe_file
=
mm
->
exe_file
;
if
(
exe_file
)
get_file
(
exe_file
);
up_read
(
&
mm
->
mmap_sem
);
return
exe_file
;
}
static
void
dup_mm_exe_file
(
struct
mm_struct
*
oldmm
,
struct
mm_struct
*
newmm
)
{
/* It's safe to write the exe_file pointer without exe_file_lock because
* this is called during fork when the task is not yet in /proc */
newmm
->
exe_file
=
get_mm_exe_file
(
oldmm
);
}
/**
* get_task_mm - acquire a reference to the task's mm
*
* Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning
* this kernel workthread has transiently adopted a user mm with use_mm,
* to do its AIO) is not set and if so returns a reference to it, after
* bumping up the use count. User must release the mm via mmput()
* after use. Typically used by /proc and ptrace.
*/
struct
mm_struct
*
get_task_mm
(
struct
task_struct
*
task
)
{
struct
mm_struct
*
mm
;
task_lock
(
task
);
mm
=
task
->
mm
;
if
(
mm
) {
if
(
task
->
flags
&
PF_KTHREAD
)
mm
=
NULL
;
else
atomic_inc
(
&
mm
->
mm_users
);
}
task_unlock
(
task
);
return
mm
;
}
EXPORT_SYMBOL_GPL
(
get_task_mm
);
struct
mm_struct
*
mm_access
(
struct
task_struct
*
task
,
unsigned
int
mode
)
{
struct
mm_struct
*
mm
;
int
err
;
err
=
mutex_lock_killable
(
&
task
->
signal
->
cred_guard_mutex
);
if
(
err
)
return
ERR_PTR
(
err
);
mm
=
get_task_mm
(
task
);
if
(
mm
&&
mm
!=
current
->
mm
&&
!
ptrace_may_access
(
task
,
mode
)) {
mmput
(
mm
);
mm
=
ERR_PTR
(
-
EACCES
);
}
mutex_unlock
(
&
task
->
signal
->
cred_guard_mutex
);
return
mm
;
}
static
void
complete_vfork_done
(
struct
task_struct
*
tsk
)
{
struct
completion
*
vfork
;
task_lock
(
tsk
);
vfork
=
tsk
->
vfork_done
;
if
(
likely
(
vfork
)) {
tsk
->
vfork_done
=
NULL
;
complete
(
vfork
);
}
task_unlock
(
tsk
);
}
static
int
wait_for_vfork_done
(
struct
task_struct
*
child
,
struct
completion
*
vfork
)
{
int
killed
;
freezer_do_not_count
();
killed
=
wait_for_completion_killable
(
vfork
);
freezer_count
();
if
(
killed
) {
task_lock
(
child
);
child
->
vfork_done
=
NULL
;
task_unlock
(
child
);
}
put_task_struct
(
child
);
return
killed
;
}
/* Please note the differences between mmput and mm_release.
* mmput is called whenever we stop holding onto a mm_struct,
* error success whatever.
*
* mm_release is called after a mm_struct has been removed
* from the current process.
*
* This difference is important for error handling, when we
* only half set up a mm_struct for a new process and need to restore
* the old one. Because we mmput the new mm_struct before
* restoring the old one. . .
* Eric Biederman 10 January 1998
*/
void
mm_release
(
struct
task_struct
*
tsk
,
struct
mm_struct
*
mm
)
{
/* Get rid of any futexes when releasing the mm */
#ifdef
CONFIG_FUTEX
if
(
unlikely
(
tsk
->
robust_list
)) {
exit_robust_list
(
tsk
);
tsk
->
robust_list
=
NULL
;
}
#ifdef
CONFIG_COMPAT
if
(
unlikely
(
tsk
->
compat_robust_list
)) {
compat_exit_robust_list
(
tsk
);
tsk
->
compat_robust_list
=
NULL
;
}
#endif
if
(
unlikely
(!
list_empty
(
&
tsk
->
pi_state_list
)))
exit_pi_state_list
(
tsk
);
#endif
/* Get rid of any cached register state */
deactivate_mm
(
tsk
,
mm
);
if
(
tsk
->
vfork_done
)
complete_vfork_done
(
tsk
);
/*
* If we're exiting normally, clear a user-space tid field if
* requested. We leave this alone when dying by signal, to leave
* the value intact in a core dump, and to save the unnecessary
* trouble, say, a killed vfork parent shouldn't touch this mm.
* Userland only wants this done for a sys_exit.
*/
if
(
tsk
->
clear_child_tid
) {
if
(!(
tsk
->
flags
&
PF_SIGNALED
)
&&
atomic_read
(
&
mm
->
mm_users
)
>
1
) {
/*
* We don't check the error code - if userspace has
* not set up a proper pointer then tough luck.
*/
put_user
(
0
,
tsk
->
clear_child_tid
);
sys_futex
(
tsk
->
clear_child_tid
,
FUTEX_WAKE
,
1
,
NULL
,
NULL
,
0
);
}
tsk
->
clear_child_tid
=
NULL
;
}
}
/*
* Allocate a new mm structure and copy contents from the
* mm structure of the passed in task structure.
*/
struct
mm_struct
*
dup_mm
(
struct
task_struct
*
tsk
)
{
struct
mm_struct
*
mm
,
*
oldmm
=
current
->
mm
;
int
err
;
if
(!
oldmm
)
return
NULL
;
mm
=
allocate_mm
();
if
(!
mm
)
goto
fail_nomem
;
memcpy
(
mm
,
oldmm
,
sizeof
(
*
mm
));
mm_init_cpumask
(
mm
);
/* Initializing for Swap token stuff */
mm
->
token_priority
=
0
;
mm
->
last_interval
=
0
;
#ifdef
CONFIG_TRANSPARENT_HUGEPAGE
mm
->
pmd_huge_pte
=
NULL
;
#endif
if
(!
mm_init
(
mm
,
tsk
))
goto
fail_nomem
;
if
(
init_new_context
(
tsk
,
mm
))
goto
fail_nocontext
;
dup_mm_exe_file
(
oldmm
,
mm
);
err
=
dup_mmap
(
mm
,
oldmm
);
if
(
err
)
goto
free_pt
;
mm
->
hiwater_rss
=
get_mm_rss
(
mm
);
mm
->
hiwater_vm
=
mm
->
total_vm
;
if
(
mm
->
binfmt
&&
!
try_module_get
(
mm
->
binfmt
->
module
))
goto
free_pt
;
return
mm
;
free_pt
:
/* don't put binfmt in mmput, we haven't got module yet */
mm
->
binfmt
=
NULL
;
mmput
(
mm
);
fail_nomem
:
return
NULL
;
fail_nocontext
:
/*
* If init_new_context() failed, we cannot use mmput() to free the mm
* because it calls destroy_context()
*/
mm_free_pgd
(
mm
);
free_mm
(
mm
);
return
NULL
;
}
static
int
copy_mm
(
unsigned long
clone_flags
,
struct
task_struct
*
tsk
)
{
struct
mm_struct
*
mm
,
*
oldmm
;
int
retval
;
tsk
->
min_flt
=
tsk
->
maj_flt
=
0
;
tsk
->
nvcsw
=
tsk
->
nivcsw
=
0
;
#ifdef
CONFIG_DETECT_HUNG_TASK
tsk
->
last_switch_count
=
tsk
->
nvcsw
+
tsk
->
nivcsw
;
#endif
tsk
->
mm
=
NULL
;
tsk
->
active_mm
=
NULL
;
/*
* Are we cloning a kernel thread?
*
* We need to steal a active VM for that..
*/
oldmm
=
current
->
mm
;
if
(!
oldmm
)
return
0
;
if
(
clone_flags
&
CLONE_VM
) {
atomic_inc
(
&
oldmm
->
mm_users
);
mm
=
oldmm
;
goto
good_mm
;
}
retval
=
-
ENOMEM
;
mm
=
dup_mm
(
tsk
);
if
(!
mm
)
goto
fail_nomem
;
good_mm
:
/* Initializing for Swap token stuff */
mm
->
token_priority
=
0
;
mm
->
last_interval
=
0
;
tsk
->
mm
=
mm
;
tsk
->
active_mm
=
mm
;
return
0
;
fail_nomem
:
return
retval
;
}
static
int
copy_fs
(
unsigned long
clone_flags
,
struct
task_struct
*
tsk
)
{
struct
fs_struct
*
fs
=
current
->
fs
;
if
(
clone_flags
&
CLONE_FS
) {
/* tsk->fs is already what we want */
spin_lock
(
&
fs
->
lock
);
if
(
fs
->
in_exec
) {
spin_unlock
(
&
fs
->
lock
);
return
-
EAGAIN
;
}
fs
->
users
++
;
spin_unlock
(
&
fs
->
lock
);
return
0
;
}
tsk
->
fs
=
copy_fs_struct
(
fs
);
if
(!
tsk
->
fs
)
return
-
ENOMEM
;
return
0
;
}
static
int
copy_files
(
unsigned long
clone_flags
,
struct
task_struct
*
tsk
)
{
struct
files_struct
*
oldf
,
*
newf
;
int
error
=
0
;
/*
* A background process may not have any files ...
*/
oldf
=
current
->
files
;
if
(!
oldf
)
goto
out
;
if
(
clone_flags
&
CLONE_FILES
) {
atomic_inc
(
&
oldf
->
count
);
goto
out
;
}
newf
=
dup_fd
(
oldf
,
&
error
);
if
(!
newf
)
goto
out
;
tsk
->
files
=
newf
;
error
=
0
;
out
:
return
error
;
}
static
int
copy_io
(
unsigned long
clone_flags
,
struct
task_struct
*
tsk
)
{
#ifdef
CONFIG_BLOCK
struct
io_context
*
ioc
=
current
->
io_context
;
struct
io_context
*
new_ioc
;
if
(!
ioc
)
return
0
;
/*
* Share io context with parent, if CLONE_IO is set
*/
if
(
clone_flags
&
CLONE_IO
) {
tsk
->
io_context
=
ioc_task_link
(
ioc
);
if
(
unlikely
(!
tsk
->
io_context
))
return
-
ENOMEM
;
}
else
if
(
ioprio_valid
(
ioc
->
ioprio
)) {
new_ioc
=
get_task_io_context
(
tsk
,
GFP_KERNEL
,
NUMA_NO_NODE
);
if
(
unlikely
(!
new_ioc
))
return
-
ENOMEM
;
new_ioc
->
ioprio
=
ioc
->
ioprio
;
put_io_context
(
new_ioc
);
}
#endif
return
0
;
}
static
int
copy_sighand
(
unsigned long
clone_flags
,
struct
task_struct
*
tsk
)
{
struct
sighand_struct
*
sig
;
if
(
clone_flags
&
CLONE_SIGHAND
) {
atomic_inc
(
&
current
->
sighand
->
count
);
return
0
;
}
sig
=
kmem_cache_alloc
(
sighand_cachep
,
GFP_KERNEL
);
rcu_assign_pointer
(
tsk
->
sighand
,
sig
);
if
(!
sig
)
return
-
ENOMEM
;
atomic_set
(
&
sig
->
count
,
1
);
memcpy
(
sig
->
action
,
current
->
sighand
->
action
,
sizeof
(
sig
->
action
));
return
0
;
}
void
__cleanup_sighand
(
struct
sighand_struct
*
sighand
)
{
if
(
atomic_dec_and_test
(
&
sighand
->
count
)) {
signalfd_cleanup
(
sighand
);
kmem_cache_free
(
sighand_cachep
,
sighand
);
}
}
/*
* Initialize POSIX timer handling for a thread group.
*/
static
void
posix_cpu_timers_init_group
(
struct
signal_struct
*
sig
)
{
unsigned long
cpu_limit
;
/* Thread group counters. */
thread_group_cputime_init
(
sig
);
cpu_limit
=
ACCESS_ONCE
(
sig
->
rlim
[
RLIMIT_CPU
].
rlim_cur
);
if
(
cpu_limit
!=
RLIM_INFINITY
) {
sig
->
cputime_expires
.
prof_exp
=
secs_to_cputime
(
cpu_limit
);
sig
->
cputimer
.
running
=
1
;
}
/* The timer lists. */
INIT_LIST_HEAD
(
&
sig
->
cpu_timers
[
0
]);
INIT_LIST_HEAD
(
&
sig
->
cpu_timers
[
1
]);
INIT_LIST_HEAD
(
&
sig
->
cpu_timers
[
2
]);
}
static
int
copy_signal
(
unsigned long
clone_flags
,
struct
task_struct
*
tsk
)
{
struct
signal_struct
*
sig
;
if
(
clone_flags
&
CLONE_THREAD
)
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL