From patchwork Mon Jul 7 13:45:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean Pihet X-Patchwork-Id: 4494871 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C1DAF9F392 for ; Mon, 7 Jul 2014 13:47:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D946B20221 for ; Mon, 7 Jul 2014 13:47:56 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EBD9620211 for ; Mon, 7 Jul 2014 13:47:55 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1X49Ez-0003lT-MH; Mon, 07 Jul 2014 13:45:57 +0000 Received: from mail-we0-f180.google.com ([74.125.82.180]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1X49Ev-0003a2-FT for linux-arm-kernel@lists.infradead.org; Mon, 07 Jul 2014 13:45:54 +0000 Received: by mail-we0-f180.google.com with SMTP id x48so4432348wes.25 for ; Mon, 07 Jul 2014 06:45:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=pzYKBSE6TdT8QTvDr4ohcwHz2ZyaQIOvRU1IuR7HnBg=; b=C/Ez3XZtwl/OmOQpI8gb4uZqY2pzRgGcZak64YqCzf+pAivtFwxMGyZHf6tYMeok2F Eup/cb71YAJExz/d3A+N600bVJoLqF9q5QRIc6T9opSEtxJaqzc2giBbp2gPTfBC6uDB LB44Oa3d9R+hWbR8v1lcVNPrivzN2IYWfLZ1mWcWKZGj03hGUMCNdmr4C/CjAmJKLLab z1Tgz3sqpiB9W412Bc4EYK0orD0cLBH0yhgfukGuFyWLkiYzjj36a+Nqj9DXq+7kXsPm s1+mDoUa1jtJVKdtTaMQT5dKQKANcd1nU60bdAW14O2Qp9gKwABuwmrDAOVm9e2GiHmo 5yKw== X-Gm-Message-State: ALoCoQkG7lUMysSt9qzjPfKgZ9wAJ/Td63GLBr+Qj89NYP/I6gnksgI6jH5W4cthImn0BEXtwTyk X-Received: by 10.194.9.198 with SMTP id c6mr3239282wjb.131.1404740732238; Mon, 07 Jul 2014 06:45:32 -0700 (PDT) Received: from rachael.linaro.local ([213.122.173.130]) by mx.google.com with ESMTPSA id e18sm26653901wic.0.2014.07.07.06.45.31 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Jul 2014 06:45:31 -0700 (PDT) From: Jean Pihet To: linux-kernel@vger.kernel.org, linaro-kernel@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Will Deacon Subject: [PATCH 2/3] ARM: perf: disable the pagefault handler when reading from user space Date: Mon, 7 Jul 2014 15:45:09 +0200 Message-Id: <1404740710-14691-3-git-send-email-jean.pihet@linaro.org> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1404740710-14691-1-git-send-email-jean.pihet@linaro.org> References: <1404740710-14691-1-git-send-email-jean.pihet@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140707_064553_667249_CFFBD5AB X-CRM114-Status: GOOD ( 17.37 ) X-Spam-Score: -0.7 (/) Cc: Arnaldo Carvalho de Melo , steve.capper@linaro.org, Jean Pihet , Jiri Olsa , Ingo Molnar X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Under perf, the fp unwinding scheme requires access to user space memory and can provoke a pagefault via call to __copy_from_user_inatomic from user_backtrace. This unwinding can take place in response to an interrupt (__perf_event_overflow). This is undesirable as we may already have mmap_sem held for write. One example being a process that calls mprotect just as a the PMU counters overflow. An example that can provoke this behaviour: perf record -e event:tocapture --call-graph fp ./application_to_test This patch addresses this issue by disabling pagefaults briefly in user_backtrace (as is done in the other architectures: ARM64, x86, Sparc etc.). Without the patch a deadlock occurs when __perf_event_overflow is called while reading the data from the user space: [ INFO: possible recursive locking detected ] 3.16.0-rc2-00038-g0ed7ff6 #46 Not tainted --------------------------------------------- stress/1634 is trying to acquire lock: (&mm->mmap_sem){++++++}, at: [] do_page_fault+0xa8/0x428 but task is already holding lock: (&mm->mmap_sem){++++++}, at: [] SyS_mprotect+0xa8/0x1c8 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(&mm->mmap_sem); lock(&mm->mmap_sem); *** DEADLOCK *** May be due to missing lock nesting notation 2 locks held by stress/1634: #0: (&mm->mmap_sem){++++++}, at: [] SyS_mprotect+0xa8/0x1c8 #1: (rcu_read_lock){......}, at: [] __perf_event_overflow+0x120/0x294 stack backtrace: CPU: 1 PID: 1634 Comm: stress Not tainted 3.16.0-rc2-00038-g0ed7ff6 #46 [] (unwind_backtrace) from [] (show_stack+0x20/0x24) [] (show_stack) from [] (dump_stack+0x7c/0x98) [] (dump_stack) from [] (__lock_acquire+0x1484/0x1cf0) [] (__lock_acquire) from [] (lock_acquire+0xa4/0x11c) [] (lock_acquire) from [] (down_read+0x40/0x7c) [] (down_read) from [] (do_page_fault+0xa8/0x428) [] (do_page_fault) from [] (do_DataAbort+0x44/0xa8) [] (do_DataAbort) from [] (__dabt_svc+0x3c/0x60) Exception stack(0xed7c5ae0 to 0xed7c5b28) 5ae0: ed7c5b5c b6dadff4 ffffffec 00000000 b6dadff4 ebc08000 00000000 ebc08000 5b00: 0000007e 00000000 ed7c4000 ed7c5b94 00000014 ed7c5b2c c001a438 c0236c60 5b20: 00000013 ffffffff [] (__dabt_svc) from [] (__copy_from_user+0xa4/0x3a4) Signed-off-by: Jean Pihet Cc: Will Deacon Acked-by: Steve Capper --- arch/arm/kernel/perf_event.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/arm/kernel/perf_event.c b/arch/arm/kernel/perf_event.c index 6493c4c..f5aeca2 100644 --- a/arch/arm/kernel/perf_event.c +++ b/arch/arm/kernel/perf_event.c @@ -560,11 +560,16 @@ user_backtrace(struct frame_tail __user *tail, struct perf_callchain_entry *entry) { struct frame_tail buftail; + unsigned long err; - /* Also check accessibility of one struct frame_tail beyond */ if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) return NULL; - if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail))) + + pagefault_disable(); + err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail)); + pagefault_enable(); + + if (err) return NULL; perf_callchain_store(entry, buftail.lr);