From patchwork Thu Feb 9 04:03:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hoeun Ryu X-Patchwork-Id: 9563999 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 F0DED6020C for ; Thu, 9 Feb 2017 04:05:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F029284EB for ; Thu, 9 Feb 2017 04:05:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 519BF28502; Thu, 9 Feb 2017 04:05:01 +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=-4.1 required=2.0 tests=BAYES_00, 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 89828284EB for ; Thu, 9 Feb 2017 04:04:59 +0000 (UTC) Received: (qmail 5940 invoked by uid 550); 9 Feb 2017 04:04:58 -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 5919 invoked from network); 9 Feb 2017 04:04:57 -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=qpVrtBKIYaHO5EzEB4eZfCCbS7+59J9UlVG05bafypU=; b=q1dIgdVwPpfY6WfEjeAdXnG9/prpl2GS2rTATaer8QlIf7YwU4cBm1IPWyHXqaCoJA ireEZCLSAb/fZHj+mBOKirLwhBuIrbriek7rgo/wTqP7RaMB7NbJnXiveQxwa048dHDi o87jAypBaxs/yQTXqFchVyjjfcksvtcXcwZfSdNJWMHdyqPYeztLNpLAJWEtxCLw7HTh 8kfBRGCU1kfA6g0Z+ect0EOxv2X7A3RyUyLnDs4l5fuTIrkcCrTImimgwM25vQomzyh1 PbcZE+0nz5+f1FoxX7tgNiWTE31nVHvnwQ0teFbYa2wAVzAhQJUeDhrRCntP3meBH4dr d2SQ== 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=qpVrtBKIYaHO5EzEB4eZfCCbS7+59J9UlVG05bafypU=; b=msTP+KzV4boxvb9KyuU+yZobv8tf7qa2HyNQogvSmcFXFOaS9RKhAVlvq0HUk2oos4 i7WUVg2QN2k8/6kga7wC3c1cTbOoN74ucuuxQxrsD6Ehep94EcVHMPGoAv7rSAraXyX4 sByGQuPsPGZ+Vd0GnjJeuE8cJZgMwV2KUKyHkixwn9km9oG3hs7jjXOx3doPRZdsph+y VBpiyiNx2oEESlQBx0jcWEpd3Ytk1mREi/7Re43VMQMGi3FgtmvodRbBFt2KONrqvIyb eLMdx2frnj1OnjaNFsZMFYjYwKE1b7K/a/bMvsapRBGYOdPx22x/qYMCfhwitsreNi+q nwvw== X-Gm-Message-State: AMke39mN7M1+qc2+0OTMpUJ6sUd4ToYWYAy0r1KQe8epO+ZTBjj0Y6qlsZC9BpaPL/wt4Q== X-Received: by 10.98.14.217 with SMTP id 86mr1289763pfo.79.1486613085685; Wed, 08 Feb 2017 20:04:45 -0800 (PST) From: Hoeun Ryu To: Andrew Morton , Michal Hocko , Ingo Molnar , Andy Lutomirski , Kees Cook , "Eric W. Biederman" , Mateusz Guzik Cc: linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, Hoeun Ryu Date: Thu, 9 Feb 2017 13:03:46 +0900 Message-Id: <1486613040-30555-1-git-send-email-hoeun.ryu@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [kernel-hardening] [PATCH v2 1/2] 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 --- Changes in v2: remove cpuhp callback for `starup`, only `teardown` callback is installed. kernel/fork.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/kernel/fork.c b/kernel/fork.c index 61284d8..7911ed2 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -170,6 +170,22 @@ void __weak arch_release_thread_stack(unsigned long *stack) static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); #endif +static int free_vm_stack_cache(unsigned int cpu) +{ + int i; + + for (i = 0; i < NR_CACHED_STACKS; i++) { + struct vm_struct *vm_stack = this_cpu_read(cached_stacks[i]); + if (!vm_stack) + continue; + + vfree(vm_stack->addr); + this_cpu_write(cached_stacks[i], NULL); + } + + return 0; +} + static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) { #ifdef CONFIG_VMAP_STACK @@ -456,6 +472,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_AP_ONLINE_DYN, "vm_stack_cache", + NULL, free_vm_stack_cache); +#endif } int __weak arch_dup_task_struct(struct task_struct *dst,