From patchwork Thu Mar 15 17:08:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zubin Mithra X-Patchwork-Id: 10285459 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 E855960291 for ; Thu, 15 Mar 2018 18:11:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D7F5028BED for ; Thu, 15 Mar 2018 18:11:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CC9A328BEF; Thu, 15 Mar 2018 18:11:20 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 36BA128BED for ; Thu, 15 Mar 2018 18:11:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=qfDDgOQlFrRAj5T806k5Q/TF3OFUaMsilGhTVTCcDk8=; b=lghAlZsWWrbEAs/NOEtkXmqMRq 4SC23cFy3V0ddw7Tiy31ioRG5ZkhXqpo/HsYDnJpFgNfaHsK0sWQjMMzC6xe8vSK268W19BbmhESG Fa4uNu0r7mFsFq89Gxfh/0H7JHOxAxRyTFeYCwPH2PRMNDkxxihNVWYUW8KHza5W/AKhLBMfZ5MO5 lZXdO6Ef3ONZroZhLYyUEyiEktobDo1IFSiL+59cdDYUeU5Hu6vqOdR/AK3bIvlwzDSVsdaR50rPX Ip5tQJSrfAkH8Hr5kSnugCmMPV+zFyEzu9Jf+9yvem5bhT+UZz41uF+gxXUN5s5vL1DswC/5QEs9R SlJjYJfQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1ewXLN-00087D-Kz; Thu, 15 Mar 2018 18:11:13 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1ewXK0-0005vc-PR for linux-arm-kernel@bombadil.infradead.org; Thu, 15 Mar 2018 18:09:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=LWcTH6WUudRdrp46xvWnecI9ckah86TPMRlKyujzGeo=; b=FQ95QkN0nwUIYpQB2PMtUN9Iq SW9ZhPe4YEisDkRNLJEV0BOs5EbAHpgMX5aXmaDQmQkS28I011JzOiGAIhpgff9Ilb2EagcYehfsX OLaN1d8JbCk7E765sT2DNWCeojKjqnEDZGX0xP4TBHafAzHKJ/yGpLskQb5sVrmBiiAkgwiB9vAfB Nylez0XvyS5SjVTEzgKVe6wHhXct9BU4oiidhBzgYKc/Y3Fe9GIbbbbtL3J+2/IYROs+W4rcKeFs/ hdhCsx1qepi/0vz2Dc3g7dn1EwwbGwK3SXPmM4xT/9nBUWru+n8c59pTeqYlNJ9/roKte9HltPxB2 fWsvYVMfQ==; Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]) by merlin.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1ewWO2-0006Ah-JY for linux-arm-kernel@lists.infradead.org; Thu, 15 Mar 2018 17:09:55 +0000 Received: by mail-wr0-x244.google.com with SMTP id d10so9088470wrf.3 for ; Thu, 15 Mar 2018 10:09:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LWcTH6WUudRdrp46xvWnecI9ckah86TPMRlKyujzGeo=; b=Sk6lmv/rzVgQ2TszeN/Drm0cSBIBkTpR3ewAzFGMfjm7ubMq+4Hy+STJQMv1m4l/bH Ea/qpNBdgS0HBih0G2MDrL4PDxwAAYj9RqL2p4vFMvihRqzhDMKWdgvFhEgQxXdOL/av R54GHAtoK09m1baRVOZUlFkjBnM/EE9fgg0MI= 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:in-reply-to :references; bh=LWcTH6WUudRdrp46xvWnecI9ckah86TPMRlKyujzGeo=; b=d8MsWaTqLCki733a9oIWuesTNhEO1Iq+vE2SJXMy1hDbYai/2yCFr9ryvspIWiFIEH N4MdATmMf0XblikfsiSVGVOQog0j/9qdAhEJVYN/VfMFiOKITdQUO9uD9pK+/GGYJc0m +8Qt2A9TXfFBeJR7Dss53Wha/sGoWG/rJAWmFZyDbUGWrR1ST/jps7BZ9Pd/l1WB/45i /79NNUKMdI8474/50mBuh8R+ApcMoehYgJ4VNxOf0ipf5CzlYF4+mARzFdKUbfuVFGxY 0MMZIe/YjuZl1tMS6PRLQOrLbUNSlVZNLXPMvWot3sBzcw+S6nYavhpikPaxws2ylOAT iRWw== X-Gm-Message-State: AElRT7F/EBOOQTSQYsyb/oGcBKqXEjt6cB1S3CRdTdiI1jw16Iy9qhtd KH9GcFgIwk3/89zKUQMeT/iv/UO9w54= X-Google-Smtp-Source: AG47ELt4OnRffngKjapJPpxNtUITrmIO7HANRdTADKRLTNEFVBWw4oOFiska4Uu9hZHQg1lpWW2opQ== X-Received: by 10.223.145.67 with SMTP id j61mr8186506wrj.152.1521133782434; Thu, 15 Mar 2018 10:09:42 -0700 (PDT) Received: from zsm1.lan (host-2-98-223-64.as13285.net. [2.98.223.64]) by smtp.googlemail.com with ESMTPSA id j21sm7273460wrb.49.2018.03.15.10.09.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Mar 2018 10:09:41 -0700 (PDT) From: Zubin Mithra To: linux-arm-kernel@lists.infradead.org Subject: [RFC 3/3] arm: add refcounting for task stacks Date: Thu, 15 Mar 2018 17:08:59 +0000 Message-Id: <20180315170859.93893-4-zsm@chromium.org> X-Mailer: git-send-email 2.16.2.804.g6dcf76e118-goog In-Reply-To: <20180315170859.93893-1-zsm@chromium.org> References: <20180315170859.93893-1-zsm@chromium.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180315_130954_691695_4C558B3A X-CRM114-Status: GOOD ( 20.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, keescook@chromium.org, Zubin Mithra , ard.biesheuvel@linaro.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP When CONFIG_THREAD_INFO_IN_TASK is enabled, thread stacks may be freed before a stack is destroyed. This patch adds in recounting to ensure freed stacks are not used. If CONFIG_THREAD_INFO_IN_TASK is not used, no refcounting is performed. Signed-off-by: Zubin Mithra --- arch/arm/kernel/process.c | 21 +++++++++++++++------ arch/arm/kernel/stacktrace.c | 7 +++++++ arch/arm/kernel/traps.c | 5 +++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c index 1523cb18b109..d3f87d3049da 100644 --- a/arch/arm/kernel/process.c +++ b/arch/arm/kernel/process.c @@ -297,7 +297,7 @@ EXPORT_SYMBOL(dump_fpu); unsigned long get_wchan(struct task_struct *p) { struct stackframe frame; - unsigned long stack_page; + unsigned long stack_page, ret = 0; int count = 0; if (!p || p == current || p->state == TASK_RUNNING) return 0; @@ -306,16 +306,25 @@ unsigned long get_wchan(struct task_struct *p) frame.sp = thread_saved_sp(p); frame.lr = 0; /* recovered from the stack */ frame.pc = thread_saved_pc(p); - stack_page = (unsigned long)task_stack_page(p); + + stack_page = (unsigned long)try_get_task_stack(p); + if (!stack_page) + return 0; + do { if (frame.sp < stack_page || frame.sp >= stack_page + THREAD_SIZE || unwind_frame(&frame) < 0) - return 0; - if (!in_sched_functions(frame.pc)) - return frame.pc; + goto out; + if (!in_sched_functions(frame.pc)) { + ret = frame.pc; + goto out; + } } while (count ++ < 16); - return 0; + +out: + put_task_stack(p); + return ret; } unsigned long arch_randomize_brk(struct mm_struct *mm) diff --git a/arch/arm/kernel/stacktrace.c b/arch/arm/kernel/stacktrace.c index d519b8e0797f..ff20d5941724 100644 --- a/arch/arm/kernel/stacktrace.c +++ b/arch/arm/kernel/stacktrace.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -105,6 +106,9 @@ static noinline void __save_stack_trace(struct task_struct *tsk, struct stack_trace_data data; struct stackframe frame; + if (!try_get_task_stack(tsk)) + return; + data.trace = trace; data.skip = trace->skip; data.no_sched_functions = nosched; @@ -118,6 +122,7 @@ static noinline void __save_stack_trace(struct task_struct *tsk, */ if (trace->nr_entries < trace->max_entries) trace->entries[trace->nr_entries++] = ULONG_MAX; + put_task_stack(tsk); return; #else frame.fp = thread_saved_fp(tsk); @@ -137,6 +142,8 @@ static noinline void __save_stack_trace(struct task_struct *tsk, walk_stackframe(&frame, save_trace, &data); if (trace->nr_entries < trace->max_entries) trace->entries[trace->nr_entries++] = ULONG_MAX; + + put_task_stack(tsk); } void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) diff --git a/arch/arm/kernel/traps.c b/arch/arm/kernel/traps.c index 5e3633c24e63..3eb923033591 100644 --- a/arch/arm/kernel/traps.c +++ b/arch/arm/kernel/traps.c @@ -216,6 +216,9 @@ static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk) if (!tsk) tsk = current; + if (!try_get_task_stack(tsk)) + return; + if (regs) { fp = frame_pointer(regs); mode = processor_mode(regs); @@ -239,6 +242,8 @@ static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk) if (ok) c_backtrace(fp, mode); + + put_task_stack(tsk); } #endif