Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp1155947imd; Sat, 3 Nov 2018 19:40:21 -0700 (PDT) X-Google-Smtp-Source: AJdET5dUJE5XpSmV9dqCUpTEkgEuC99AIln+yihzAcp6uiRpE0GbXJZGkttNFLtym2n4z1Ll2+QT X-Received: by 2002:a17:902:d88f:: with SMTP id b15-v6mr13821734plz.207.1541299221286; Sat, 03 Nov 2018 19:40:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541299221; cv=none; d=google.com; s=arc-20160816; b=q9XkSfZq6Zix2YzwyDAXNLQ/lCrGVDXfi3SznCap24HtOh6/aCbokwtRFIdVAS0TiZ i/xkmHsuf0aE8IixRZUR7BHQQHXQV0Q1bvrF9sHbwkwL0M80IWofbIlOcr/pAGa89AQC g5xb50t0uK8BFyT9ter+GSGn802gE7pjYNxaSWGDtfObuo1ds7op+q7qwRcHZM9xChpr 27mlA2icaVPFD1v9IOg4OfXn7tnnnuyKWsP41XwiZ77Kw7c5Gq/r+QFow1M3JeBldN/q Cz3But4qyBG9lAnUJt+mFCnL3us/r/vDic8jbmPOr1L+wVbH7BaW0fwVOqJwZesOJmbL ZiGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=sWLJnklQkM0EIh+q5roRt5TNO5QVoFrKWfSXow5VePk=; b=BgTa1aWgJf1g3AoG0Tk+a5bU3Yfrh1Jn/p6wolqCYfqSnf6W9EDAtxXnnspLuvYGew ObLGBJx152/rzxZzTYhX4clTzx56lSpcUbBA/Hv/a0XhUp79XwCr1HQnRzxWjroMSh5y QO2HVoHme6bMll9srP8e42o8SXgZD0rD28T4qCQu2dFYZXLvBFu9HFdv0/ULW4BT275a rYE9y9pYMCpeJtGT7DWgr+W2EvUqntUMWgBc/CwNUs+ESSabSwe+bpROP1avH5Ro0r8M Ys2JKBRWq0k06K9XMItxwKmQ7XToVhb2qQgzTZiN1Ecacw/GPycD1n9YzAgG2dfU840R o49g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vB4dADvn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 31-v6si7148364plc.140.2018.11.03.19.40.06; Sat, 03 Nov 2018 19:40:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vB4dADvn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729072AbeKDLuG (ORCPT + 99 others); Sun, 4 Nov 2018 06:50:06 -0500 Received: from mail.kernel.org ([198.145.29.99]:36370 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726994AbeKDLuF (ORCPT ); Sun, 4 Nov 2018 06:50:05 -0500 Received: from devbox (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AD6A02081F; Sun, 4 Nov 2018 02:25:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541298310; bh=VJ3VKhlb5KgdksOoXIjrH1tkpm9CsEhQgLyeG7IOMrM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=vB4dADvnSqfK+qgK/TRc/A2hleCGkMeMqB/NpqLzqSa4uyQXBVn6nS9HuH2l07H0P rDGJTRCPBWiRxt5thctkI/1aDnNd0HOutJNQJj9LkTbYNYs8wrnI2YbHBK4Y+PnXnK 0cJ9SQLm8bFG+oOzuKf19gR1kFlRLemVL6k5jD7U= Date: Sun, 4 Nov 2018 11:25:06 +0900 From: Masami Hiramatsu To: Steven Rostedt Cc: Josh Poimboeuf , Aleksa Sarai , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Jonathan Corbet , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Shuah Khan , Alexei Starovoitov , Daniel Borkmann , Brendan Gregg , Christian Brauner , Aleksa Sarai , netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v3 1/2] kretprobe: produce sane stack traces Message-Id: <20181104112506.77464e6750a1517865a29c9f@kernel.org> In-Reply-To: <20181103133021.6676708c@vmware.local.home> References: <20181101083551.3805-1-cyphar@cyphar.com> <20181101083551.3805-2-cyphar@cyphar.com> <20181101204720.6ed3fe37@vmware.local.home> <20181102050509.tw3dhvj5urudvtjl@yavin> <20181102065932.bdt4pubbrkvql4mp@yavin> <20181102091658.1bc979a4@gandalf.local.home> <20181102154325.bt6xoysl4xdl33wd@treble> <20181102121307.32e99414@gandalf.local.home> <20181103220012.55ecd97e671c43e4959c8b62@kernel.org> <20181103091341.3d32683e@vmware.local.home> <20181104013430.9d3e91b8ebbae7dcb6860ef1@kernel.org> <20181103133021.6676708c@vmware.local.home> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 3 Nov 2018 13:30:21 -0400 Steven Rostedt wrote: > On Sun, 4 Nov 2018 01:34:30 +0900 > Masami Hiramatsu wrote: > > > > > I was thinking of a bitmask that represents the handlers, and use that > > > to map which handler gets called for which shadow entry for a > > > particular task. > > > > Hmm, I doubt that is too complicated and not scalable. I rather like to see > > the open shadow entry... > > It can scale and not too complex (I already played a little with it). > But that said, I'm not committed to it, and using the shadow stack is > also an interesting idea. > > > > > entry: [[original_retaddr][function][modified_retaddr]] > > > > So if there are many users on same function, the entries will be like this > > > > [[original_return_address][function][trampoline_A]] > > [[trampline_A][function][trampoline_B]] > > [[trampline_B][function][trampoline_C]] > > > > And on the top of the stack, there is trampline_C instead of original_return_address. > > In this case, return to trampoline_C(), it jumps back to trampline_B() and then > > it jumps back to trampline_A(). And eventually it jumps back to > > original_return_address. > > Where are trampolines A, B, and C made? Do we also need to dynamically > create them? If I register multiple function tracing ones, each one > will need its own trampoline? > No, I think tramplines are very limited. currently we will only have ftrace and kretprobe trampolines. > > This way, we don't need allocate another bitmap/pages for the shadow stack. > > We only need a shadow stack for each task. > > Also, unwinder can easily find the trampline_C from the shadow stack and restores > > original_return_address. (of course trampline_A,B,C must be registered so that > > search function can skip it.) > > What I was thinking was to store a count and the functions to be called: > > > [original_return_address] > [function_A] > [function_B] > [function_C] > [ 3 ] > > Then the trampoline that processes the return codes for ftrace (and > kretprobes and everyone else) can simply do: > > count = pop_shadow_stack(); > for (i = 0; i < count; i++) { > func = pop_shadow_stack(); > func(...); > } > return_address = pop_shadow_stack(); Ah, that's a good idea. I think we also have to store the called function entry address with the number header, but basically I agree with you. If we have a space to store a data with the function address, that is also good to kretprobe. systemtap heavily uses "entry data" for saving some data at function entry for exit handler. > That way we only need to register a function to the return handler and > it will be called, without worrying about making trampolines. There > will just be a single trampoline that handles all the work. OK, and could you make it independent from func graph tracer, so that CONFIG_KPROBES=y but CONFIG_FUNCTION_GRAPH_TRACER=n kernel can support kretprobes too. Thank you, -- Masami Hiramatsu