fork.c (568ac888215c7fb2fabe8ea739b00ec3c1f5d440) fork.c (ba14a194a434ccc8f733e263ad2ce941e35e5787)
1/*
2 * linux/kernel/fork.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * 'fork.c' contains the help-routines for the 'fork' system call

--- 144 unchanged lines hidden (view full) ---

153}
154
155#ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR
156
157/*
158 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
159 * kmemcache based allocator.
160 */
1/*
2 * linux/kernel/fork.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * 'fork.c' contains the help-routines for the 'fork' system call

--- 144 unchanged lines hidden (view full) ---

153}
154
155#ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR
156
157/*
158 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
159 * kmemcache based allocator.
160 */
161# if THREAD_SIZE >= PAGE_SIZE
162static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
163 int node)
161# if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)
162static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
164{
163{
164#ifdef CONFIG_VMAP_STACK
165 void *stack = __vmalloc_node_range(THREAD_SIZE, THREAD_SIZE,
166 VMALLOC_START, VMALLOC_END,
167 THREADINFO_GFP | __GFP_HIGHMEM,
168 PAGE_KERNEL,
169 0, node,
170 __builtin_return_address(0));
171
172 /*
173 * We can't call find_vm_area() in interrupt context, and
174 * free_thread_stack() can be called in interrupt context,
175 * so cache the vm_struct.
176 */
177 if (stack)
178 tsk->stack_vm_area = find_vm_area(stack);
179 return stack;
180#else
165 struct page *page = alloc_pages_node(node, THREADINFO_GFP,
166 THREAD_SIZE_ORDER);
167
168 return page ? page_address(page) : NULL;
181 struct page *page = alloc_pages_node(node, THREADINFO_GFP,
182 THREAD_SIZE_ORDER);
183
184 return page ? page_address(page) : NULL;
185#endif
169}
170
186}
187
171static inline void free_thread_stack(unsigned long *stack)
188static inline void free_thread_stack(struct task_struct *tsk)
172{
189{
173 __free_pages(virt_to_page(stack), THREAD_SIZE_ORDER);
190 if (task_stack_vm_area(tsk))
191 vfree(tsk->stack);
192 else
193 __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER);
174}
175# else
176static struct kmem_cache *thread_stack_cache;
177
178static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
179 int node)
180{
181 return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
182}
183
194}
195# else
196static struct kmem_cache *thread_stack_cache;
197
198static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
199 int node)
200{
201 return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
202}
203
184static void free_thread_stack(unsigned long *stack)
204static void free_thread_stack(struct task_struct *tsk)
185{
205{
186 kmem_cache_free(thread_stack_cache, stack);
206 kmem_cache_free(thread_stack_cache, tsk->stack);
187}
188
189void thread_stack_cache_init(void)
190{
191 thread_stack_cache = kmem_cache_create("thread_stack", THREAD_SIZE,
192 THREAD_SIZE, 0, NULL);
193 BUG_ON(thread_stack_cache == NULL);
194}

--- 13 unchanged lines hidden (view full) ---

208struct kmem_cache *fs_cachep;
209
210/* SLAB cache for vm_area_struct structures */
211struct kmem_cache *vm_area_cachep;
212
213/* SLAB cache for mm_struct structures (tsk->mm) */
214static struct kmem_cache *mm_cachep;
215
207}
208
209void thread_stack_cache_init(void)
210{
211 thread_stack_cache = kmem_cache_create("thread_stack", THREAD_SIZE,
212 THREAD_SIZE, 0, NULL);
213 BUG_ON(thread_stack_cache == NULL);
214}

--- 13 unchanged lines hidden (view full) ---

228struct kmem_cache *fs_cachep;
229
230/* SLAB cache for vm_area_struct structures */
231struct kmem_cache *vm_area_cachep;
232
233/* SLAB cache for mm_struct structures (tsk->mm) */
234static struct kmem_cache *mm_cachep;
235
216static void account_kernel_stack(unsigned long *stack, int account)
236static void account_kernel_stack(struct task_struct *tsk, int account)
217{
237{
218 /* All stack pages are in the same zone and belong to the same memcg. */
219 struct page *first_page = virt_to_page(stack);
238 void *stack = task_stack_page(tsk);
239 struct vm_struct *vm = task_stack_vm_area(tsk);
220
240
221 mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
222 THREAD_SIZE / 1024 * account);
241 BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0);
223
242
224 memcg_kmem_update_page_stat(
225 first_page, MEMCG_KERNEL_STACK_KB,
226 account * (THREAD_SIZE / 1024));
243 if (vm) {
244 int i;
245
246 BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
247
248 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
249 mod_zone_page_state(page_zone(vm->pages[i]),
250 NR_KERNEL_STACK_KB,
251 PAGE_SIZE / 1024 * account);
252 }
253
254 /* All stack pages belong to the same memcg. */
255 memcg_kmem_update_page_stat(vm->pages[0], MEMCG_KERNEL_STACK_KB,
256 account * (THREAD_SIZE / 1024));
257 } else {
258 /*
259 * All stack pages are in the same zone and belong to the
260 * same memcg.
261 */
262 struct page *first_page = virt_to_page(stack);
263
264 mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
265 THREAD_SIZE / 1024 * account);
266
267 memcg_kmem_update_page_stat(first_page, MEMCG_KERNEL_STACK_KB,
268 account * (THREAD_SIZE / 1024));
269 }
227}
228
229void free_task(struct task_struct *tsk)
230{
270}
271
272void free_task(struct task_struct *tsk)
273{
231 account_kernel_stack(tsk->stack, -1);
274 account_kernel_stack(tsk, -1);
232 arch_release_thread_stack(tsk->stack);
275 arch_release_thread_stack(tsk->stack);
233 free_thread_stack(tsk->stack);
276 free_thread_stack(tsk);
234 rt_mutex_debug_task_free(tsk);
235 ftrace_graph_exit_task(tsk);
236 put_seccomp_filter(tsk);
237 arch_release_task_struct(tsk);
238 free_task_struct(tsk);
239}
240EXPORT_SYMBOL(free_task);
241

--- 95 unchanged lines hidden (view full) ---

337 stackend = end_of_stack(tsk);
338 *stackend = STACK_END_MAGIC; /* for overflow detection */
339}
340
341static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
342{
343 struct task_struct *tsk;
344 unsigned long *stack;
277 rt_mutex_debug_task_free(tsk);
278 ftrace_graph_exit_task(tsk);
279 put_seccomp_filter(tsk);
280 arch_release_task_struct(tsk);
281 free_task_struct(tsk);
282}
283EXPORT_SYMBOL(free_task);
284

--- 95 unchanged lines hidden (view full) ---

380 stackend = end_of_stack(tsk);
381 *stackend = STACK_END_MAGIC; /* for overflow detection */
382}
383
384static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
385{
386 struct task_struct *tsk;
387 unsigned long *stack;
388 struct vm_struct *stack_vm_area;
345 int err;
346
347 if (node == NUMA_NO_NODE)
348 node = tsk_fork_get_node(orig);
349 tsk = alloc_task_struct_node(node);
350 if (!tsk)
351 return NULL;
352
353 stack = alloc_thread_stack_node(tsk, node);
354 if (!stack)
355 goto free_tsk;
356
389 int err;
390
391 if (node == NUMA_NO_NODE)
392 node = tsk_fork_get_node(orig);
393 tsk = alloc_task_struct_node(node);
394 if (!tsk)
395 return NULL;
396
397 stack = alloc_thread_stack_node(tsk, node);
398 if (!stack)
399 goto free_tsk;
400
401 stack_vm_area = task_stack_vm_area(tsk);
402
357 err = arch_dup_task_struct(tsk, orig);
403 err = arch_dup_task_struct(tsk, orig);
404
405 /*
406 * arch_dup_task_struct() clobbers the stack-related fields. Make
407 * sure they're properly initialized before using any stack-related
408 * functions again.
409 */
410 tsk->stack = stack;
411#ifdef CONFIG_VMAP_STACK
412 tsk->stack_vm_area = stack_vm_area;
413#endif
414
358 if (err)
359 goto free_stack;
360
415 if (err)
416 goto free_stack;
417
361 tsk->stack = stack;
362#ifdef CONFIG_SECCOMP
363 /*
364 * We must handle setting up seccomp filters once we're under
365 * the sighand lock in case orig has changed between now and
366 * then. Until then, filter must be NULL to avoid messing up
367 * the usage counts on the error path calling free_task.
368 */
369 tsk->seccomp.filter = NULL;

--- 15 unchanged lines hidden (view full) ---

385 atomic_set(&tsk->usage, 2);
386#ifdef CONFIG_BLK_DEV_IO_TRACE
387 tsk->btrace_seq = 0;
388#endif
389 tsk->splice_pipe = NULL;
390 tsk->task_frag.page = NULL;
391 tsk->wake_q.next = NULL;
392
418#ifdef CONFIG_SECCOMP
419 /*
420 * We must handle setting up seccomp filters once we're under
421 * the sighand lock in case orig has changed between now and
422 * then. Until then, filter must be NULL to avoid messing up
423 * the usage counts on the error path calling free_task.
424 */
425 tsk->seccomp.filter = NULL;

--- 15 unchanged lines hidden (view full) ---

441 atomic_set(&tsk->usage, 2);
442#ifdef CONFIG_BLK_DEV_IO_TRACE
443 tsk->btrace_seq = 0;
444#endif
445 tsk->splice_pipe = NULL;
446 tsk->task_frag.page = NULL;
447 tsk->wake_q.next = NULL;
448
393 account_kernel_stack(stack, 1);
449 account_kernel_stack(tsk, 1);
394
395 kcov_task_init(tsk);
396
397 return tsk;
398
399free_stack:
450
451 kcov_task_init(tsk);
452
453 return tsk;
454
455free_stack:
400 free_thread_stack(stack);
456 free_thread_stack(tsk);
401free_tsk:
402 free_task_struct(tsk);
403 return NULL;
404}
405
406#ifdef CONFIG_MMU
407static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
408{

--- 990 unchanged lines hidden (view full) ---

1399 acct_clear_integrals(p);
1400
1401 posix_cpu_timers_init(p);
1402
1403 p->start_time = ktime_get_ns();
1404 p->real_start_time = ktime_get_boot_ns();
1405 p->io_context = NULL;
1406 p->audit_context = NULL;
457free_tsk:
458 free_task_struct(tsk);
459 return NULL;
460}
461
462#ifdef CONFIG_MMU
463static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
464{

--- 990 unchanged lines hidden (view full) ---

1455 acct_clear_integrals(p);
1456
1457 posix_cpu_timers_init(p);
1458
1459 p->start_time = ktime_get_ns();
1460 p->real_start_time = ktime_get_boot_ns();
1461 p->io_context = NULL;
1462 p->audit_context = NULL;
1463 threadgroup_change_begin(current);
1407 cgroup_fork(p);
1408#ifdef CONFIG_NUMA
1409 p->mempolicy = mpol_dup(p->mempolicy);
1410 if (IS_ERR(p->mempolicy)) {
1411 retval = PTR_ERR(p->mempolicy);
1412 p->mempolicy = NULL;
1413 goto bad_fork_cleanup_threadgroup_lock;
1414 }

--- 135 unchanged lines hidden (view full) ---

1550 p->nr_dirtied = 0;
1551 p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
1552 p->dirty_paused_when = 0;
1553
1554 p->pdeath_signal = 0;
1555 INIT_LIST_HEAD(&p->thread_group);
1556 p->task_works = NULL;
1557
1464 cgroup_fork(p);
1465#ifdef CONFIG_NUMA
1466 p->mempolicy = mpol_dup(p->mempolicy);
1467 if (IS_ERR(p->mempolicy)) {
1468 retval = PTR_ERR(p->mempolicy);
1469 p->mempolicy = NULL;
1470 goto bad_fork_cleanup_threadgroup_lock;
1471 }

--- 135 unchanged lines hidden (view full) ---

1607 p->nr_dirtied = 0;
1608 p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
1609 p->dirty_paused_when = 0;
1610
1611 p->pdeath_signal = 0;
1612 INIT_LIST_HEAD(&p->thread_group);
1613 p->task_works = NULL;
1614
1558 threadgroup_change_begin(current);
1559 /*
1560 * Ensure that the cgroup subsystem policies allow the new process to be
1561 * forked. It should be noted the the new process's css_set can be changed
1562 * between here and cgroup_post_fork() if an organisation operation is in
1563 * progress.
1564 */
1565 retval = cgroup_can_fork(p);
1566 if (retval)

--- 84 unchanged lines hidden (view full) ---

1651 trace_task_newtask(p, clone_flags);
1652 uprobe_copy_process(p, clone_flags);
1653
1654 return p;
1655
1656bad_fork_cancel_cgroup:
1657 cgroup_cancel_fork(p);
1658bad_fork_free_pid:
1615 /*
1616 * Ensure that the cgroup subsystem policies allow the new process to be
1617 * forked. It should be noted the the new process's css_set can be changed
1618 * between here and cgroup_post_fork() if an organisation operation is in
1619 * progress.
1620 */
1621 retval = cgroup_can_fork(p);
1622 if (retval)

--- 84 unchanged lines hidden (view full) ---

1707 trace_task_newtask(p, clone_flags);
1708 uprobe_copy_process(p, clone_flags);
1709
1710 return p;
1711
1712bad_fork_cancel_cgroup:
1713 cgroup_cancel_fork(p);
1714bad_fork_free_pid:
1659 threadgroup_change_end(current);
1660 if (pid != &init_struct_pid)
1661 free_pid(pid);
1662bad_fork_cleanup_thread:
1663 exit_thread(p);
1664bad_fork_cleanup_io:
1665 if (p->io_context)
1666 exit_io_context(p);
1667bad_fork_cleanup_namespaces:

--- 16 unchanged lines hidden (view full) ---

1684 audit_free(p);
1685bad_fork_cleanup_perf:
1686 perf_event_free_task(p);
1687bad_fork_cleanup_policy:
1688#ifdef CONFIG_NUMA
1689 mpol_put(p->mempolicy);
1690bad_fork_cleanup_threadgroup_lock:
1691#endif
1715 if (pid != &init_struct_pid)
1716 free_pid(pid);
1717bad_fork_cleanup_thread:
1718 exit_thread(p);
1719bad_fork_cleanup_io:
1720 if (p->io_context)
1721 exit_io_context(p);
1722bad_fork_cleanup_namespaces:

--- 16 unchanged lines hidden (view full) ---

1739 audit_free(p);
1740bad_fork_cleanup_perf:
1741 perf_event_free_task(p);
1742bad_fork_cleanup_policy:
1743#ifdef CONFIG_NUMA
1744 mpol_put(p->mempolicy);
1745bad_fork_cleanup_threadgroup_lock:
1746#endif
1747 threadgroup_change_end(current);
1692 delayacct_tsk_free(p);
1693bad_fork_cleanup_count:
1694 atomic_dec(&p->cred->user->processes);
1695 exit_creds(p);
1696bad_fork_free:
1697 free_task(p);
1698fork_out:
1699 return ERR_PTR(retval);

--- 458 unchanged lines hidden ---
1748 delayacct_tsk_free(p);
1749bad_fork_cleanup_count:
1750 atomic_dec(&p->cred->user->processes);
1751 exit_creds(p);
1752bad_fork_free:
1753 free_task(p);
1754fork_out:
1755 return ERR_PTR(retval);

--- 458 unchanged lines hidden ---