Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938741AbcJXTh5 (ORCPT ); Mon, 24 Oct 2016 15:37:57 -0400 Received: from mail-yb0-f170.google.com ([209.85.213.170]:37982 "EHLO mail-yb0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753745AbcJXThy (ORCPT ); Mon, 24 Oct 2016 15:37:54 -0400 MIME-Version: 1.0 In-Reply-To: References: <20161024160814.3126-1-roman.penyaev@profitbricks.com> From: Roman Penyaev Date: Mon, 24 Oct 2016 21:37:32 +0200 Message-ID: Subject: Re: [PATCH v2 1/2] kthread: allocate kthread structure using kmalloc To: Andy Lutomirski Cc: Andy Lutomirski , Oleg Nesterov , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Tejun Heo , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1924 Lines: 52 On Mon, Oct 24, 2016 at 7:08 PM, Andy Lutomirski wrote: > On Mon, Oct 24, 2016 at 9:08 AM, Roman Pen > wrote: >> This patch avoids allocation of kthread structure on a stack, and simply >> uses kmalloc. Allocation on a stack became a huge problem (with memory >> corruption and all other not nice consequences) after the commit 2deb4be28 >> by Andy Lutomirski, which rewinds the stack on oops, thus ooopsed kthread >> steps on a garbage memory while completion of task->vfork_done structure >> on the following path: > > This is IMO a *huge* improvement. > > Shouldn't the patch also remove the try_get_task_stack() / > put_task_stack() hackery in kthread.c, though? Do you mean that commit from Oleg https://patchwork.kernel.org/patch/9335295/ ? Hm, I missed that to_live_kthread() function completely. So, yes, we can remove put/get_task_stack(), but only replacing on get/put_kthread. So still need to be careful with the refs. Oleg, could you please take a look on the hunk below, just a quick thought. (get_kthread is simple atomic kthread->refs++). -- Roman --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -64,9 +64,20 @@ static inline struct kthread *to_kthread(struct task_struct *k) static struct kthread *to_live_kthread(struct task_struct *k) { struct completion *vfork = ACCESS_ONCE(k->vfork_done); - if (likely(vfork) && try_get_task_stack(k)) - return __to_kthread(vfork); - return NULL; + struct kthread *kthread = NULL; + + BUG_ON(!(k->flags & PF_KTHREAD)); + if (likely(vfork)) { + task_lock(k); + vfork = ACCESS_ONCE(k->vfork_done); + if (likely(vfork)) { + kthread = __to_kthread(vfork); + get_kthread(kthread); + } + task_unlock(k); + } + + return kthread; }