From patchwork Tue Feb 14 12:40:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hoeun Ryu X-Patchwork-Id: 9571131 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C4E1360586 for ; Tue, 14 Feb 2017 03:41:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B3F3426E98 for ; Tue, 14 Feb 2017 03:41:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A852727D5E; Tue, 14 Feb 2017 03:41:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.1 required=2.0 tests=BAYES_00, DATE_IN_FUTURE_06_12, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id D73B127C05 for ; Tue, 14 Feb 2017 03:41:17 +0000 (UTC) Received: (qmail 18297 invoked by uid 550); 14 Feb 2017 03:41:16 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 18279 invoked from network); 14 Feb 2017 03:41:15 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=3r7EFBs/GxaP6U0T8x9+dyb/MyMyXPCZJNusZGQgsgA=; b=d0ya33SXTwgoB1yDPHdG/sdAHgpYlYzCMh/hHbsKDTvuX87ERSvvGKKvm54tOP8sLs XUOyTPA55h4kHy23ocIyCYNtc/etByxhGSNboHPgMQ4XJoeDGlKfSYGf3FhNTqH8vB8Y A+shWQWiMot7CGRIH6vdtteXIdL0cgmSeTKfqYPSltuBFrgZLm9MtEnniF4NSOSq6We+ jvekxzy7T21opat4yB7S2nGUWeum4/qQta1Fm9N6FvZ4wBB6zZ0zmQOMu2+lwBV/AVWZ YwtOGT2OTH7kiPs7vS76DWKWeozPD4bo/f2dDmXxaoYZmYPRt3zBWhYJRvh/vd9PXgnA dAIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=3r7EFBs/GxaP6U0T8x9+dyb/MyMyXPCZJNusZGQgsgA=; b=fslKqyCUWnUGBmMjbPvjzjec7EwVz7lNLnAK3d1KJ0Doxpi9NicFmYFAUvvkG3W5lO 7V3NIDGQxVwTjQ9779av0gzUcg80JR1Dwn7EZAln+AtyZYt8WgHFgyd4SY/DyzqhaDO6 Ix+SUxPer4Lfi5AhQj6GteovX34A+FCpOWAJp/UPMFQ/UtjbPDmciDModu0OnsjcuVli IzX0/cjzOZ9HovCxQy2a7n92Sx5VB1AKFbvLRyHigNe7M7g68cHLPmk9csQEFJvUIyLt OLOuRck/yk9B+K6cwRXyJccEVBQD6rPTM//KUs5KZBk23P7HLQMA3lF5HrH06tMHTLts 9ZQQ== X-Gm-Message-State: AMke39mBqnxWm+2MT5NLPvujIZmaYcsEv3q8r/loLAwOu3uxOqfuD3cZQ2iFBlZL2KCWVw== X-Received: by 10.98.7.150 with SMTP id 22mr29257151pfh.69.1487043663497; Mon, 13 Feb 2017 19:41:03 -0800 (PST) From: Hoeun Ryu To: Thomas Gleixner , Andrew Morton , Michal Hocko , Ingo Molnar , Andy Lutomirski , Kees Cook , "Eric W. Biederman" , Oleg Nesterov , Mateusz Guzik Cc: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Hoeun Ryu Date: Tue, 14 Feb 2017 21:40:19 +0900 Message-Id: <1487076043-17802-1-git-send-email-hoeun.ryu@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [kernel-hardening] [PATCH v7] fork: free vmapped stacks in cache when cpus are offline X-Virus-Scanned: ClamAV using ClamSMTP Using virtually mapped stack, kernel stacks are allocated via vmalloc. In the current implementation, two stacks per cpu can be cached when tasks are freed and the cached stacks are used again in task duplications. but the cached stacks may remain unfreed even when cpu are offline. By adding a cpu hotplug callback to free the cached stacks when a cpu goes offline, the pages of the cached stacks are not wasted. Signed-off-by: Hoeun Ryu Acked-by: Michal Hocko Reviewed-by: Thomas Gleixner --- v7: - identical to v6. - add Reviewed-by: Thomas Gleixner v6: - rollback to v4, completely identical. - add Acked-by: Mical Hocko v5: - wrap cpuhp_setup_state() in a new function, vm_stack_cache_init() which actually do nothing when !CONFIG_VMAP_STACK - add __may_unused to free_vm_stack_cache() v4: - use CPUHP_BP_PREPARE_DYN state for cpuhp setup - fix minor coding style v3: - fix misuse of per-cpu api - fix location of function definition within CONFIG_VMAP_STACK v2: - remove cpuhp callback for `startup`, only `teardown` callback is installed kernel/fork.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/kernel/fork.c b/kernel/fork.c index 937ba59..3ad0274 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -168,6 +168,24 @@ void __weak arch_release_thread_stack(unsigned long *stack) */ #define NR_CACHED_STACKS 2 static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); + +static int free_vm_stack_cache(unsigned int cpu) +{ + struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); + int i; + + for (i = 0; i < NR_CACHED_STACKS; i++) { + struct vm_struct *vm_stack = cached_vm_stacks[i]; + + if (!vm_stack) + continue; + + vfree(vm_stack->addr); + cached_vm_stacks[i] = NULL; + } + + return 0; +} #endif static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) @@ -456,6 +474,11 @@ void __init fork_init(void) for (i = 0; i < UCOUNT_COUNTS; i++) { init_user_ns.ucount_max[i] = max_threads/2; } + +#ifdef CONFIG_VMAP_STACK + cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", + NULL, free_vm_stack_cache); +#endif } int __weak arch_dup_task_struct(struct task_struct *dst,