Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp584178imd; Sat, 3 Nov 2018 06:24:13 -0700 (PDT) X-Google-Smtp-Source: AJdET5fIR48sDjz71Z9H8VOqtmDtvMHGqShPqFkrqtBA5wTB7raggbkK4U91ferOZoAvUiuKGKUu X-Received: by 2002:a17:902:c01:: with SMTP id 1-v6mr15444170pls.15.1541251453688; Sat, 03 Nov 2018 06:24:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541251453; cv=none; d=google.com; s=arc-20160816; b=VGC1pK/3FwHT/ocbf1BOweIBy34OXGnKwvpljkEHmhHqewlh96wH8l72/IpAFjvxAr wIzq84XKulpAQS4MyY+PyA7JDDu4ZREP//Hqn7AS+a7+ObVtMc0TQTyeLXSLnhBuxO6u 12z0q7VsjS1ZsAVsUsAjcqywymd9DhSTBCYw3Lj4cb3+PiPIbn1YB47J+SQFlOF2kXAE LsYyuK3FpGijKA34CfkIcEg936ole+qS2NxEB+vm1dEG8gVHozma6kDpjH/NqVfA6qe8 QukTVsahcI/qEixlOm7D3T4vhgxaYkWJYqfSJ8dwitF0ntXF42pZiyLbxJuJEj5at6KI CQLg== 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=ojeVpUW2VHIf6bkq2aNryKTPLzOnDLRDKC978LfHzTM=; b=cUqoVkAvfVOVWdGnA+rLlLXbmqtx2DgQaNATG3qLsTFMsEyT8kY7GqDS1a9CV8aA00 h84F6U6o5pZYubw2WQVSAnmLmQ4WvM0bzao+C+0ZP0TcbQYA31fzNSf5vm+0EPe8cWPL czGn1vJWP9LEyHs3NR4eoDT/PPrySobKdC27sU+ChXqcLCVDkwGQq9HmEECY3jsBv/GF JnVodIVBrA4hN9GrVoi7sfc4bKmhVBn8HTRoH/HKUeQtdh+uoXcwE3jEdsuoMsmQvTm3 W3zUjquBBVkTMWZ9aAHxHalqIV1S1/kOFUGpAr6AdPz9Jl5Z7P8EXCXT8PIm2qM3+bzH RqyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AZqi2RVK; 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 j38-v6si20502952pgl.138.2018.11.03.06.23.58; Sat, 03 Nov 2018 06:24:13 -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=AZqi2RVK; 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 S1728716AbeKCWe4 (ORCPT + 99 others); Sat, 3 Nov 2018 18:34:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:51846 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728425AbeKCWe4 (ORCPT ); Sat, 3 Nov 2018 18:34:56 -0400 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 6ECD82082D; Sat, 3 Nov 2018 13:23:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541251414; bh=IRJuIrTyRLHkUXVWkpzjpBqgAvg2f3aaSK3GrWimAV4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=AZqi2RVK+snXR2GlHGnQmFluIGWuvtvEXppkPtNHNUIJMA9Hxd0i818HvoPuDYieX Xxtd71hFYjXXNt40iyWz3K9KDGmzwS81e+3ltIkxcJGGNRIjT1JHeztGVJjgndbL9H xcPnishGSjH8v+aYcJqgPT0++bzPFpdSMrRyXBak= Date: Sat, 3 Nov 2018 22:23:29 +0900 From: Masami Hiramatsu To: Steven Rostedt Cc: Aleksa Sarai , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Masami Hiramatsu , 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, Josh Poimboeuf Subject: Re: [PATCH v3 1/2] kretprobe: produce sane stack traces Message-Id: <20181103222329.4e2a4188d297724cbb4c9883@kernel.org> In-Reply-To: <20181102091658.1bc979a4@gandalf.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> 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 Fri, 2 Nov 2018 09:16:58 -0400 Steven Rostedt wrote: > On Fri, 2 Nov 2018 17:59:32 +1100 > Aleksa Sarai wrote: > > > As an aside, I just tested with the frame unwinder and it isn't thrown > > off-course by kretprobe_trampoline (though obviously the stack is still > > wrong). So I think we just need to hook into the ORC unwinder to get it > > to continue skipping up the stack, as well as add the rewriting code for > > the stack traces (for all unwinders I guess -- though ideally we should > > I agree that this is the right solution. > > > do this without having to add the same code to every architecture). > > True, and there's an art to consolidating the code between > architectures. > > I'm currently looking at function graph and seeing if I can consolidate > it too. And I'm also trying to get multiple uses to hook into its > infrastructure. I think I finally figured out a way to do so. For supporting multiple users without any memory allocation, I think each user should consume the shadow stack and store on it. My old generic retstack implementation did that. https://github.com/mhiramat/linux/commit/8804f76580cd863d555854b41b9c6df719f8087e I hope this may give you any insites. My idea is to generalize shadow stack, not func graph tracer, since I don't like making kretprobe depends on func graph tracer, but only the shadow stack. > > The reason it is difficult, is that you need to maintain state between > the entry of a function and the exit for each task and callback that is > registered. Hence, it's a 3x tuple (function stack, task, callbacks). > And this must be maintained with preemption. A task may sleep for > minutes, and the state needs to be retained. Would you mean preeempt_disable()? Anyway, we just need to increment index atomically, don't we? > The only state that must be retained is the function stack with the > task, because if that gets out of sync, the system crashes. But the > callback state can be removed. > > Here's what is there now: > > When something is registered with the function graph tracer, every > task gets a shadowed stack. A hook is added to fork to add shadow > stacks to new tasks. Once a shadow stack is added to a task, that > shadow stack is never removed until the task exits. > > When the function is entered, the real return code is stored in the > shadow stack and the trampoline address is put in its place. > > On return, the trampoline is called, and it will pop off the return > code from the shadow stack and return to that. > > The issue with multiple users, is that different users may want to > trace different functions. On entry, the user could say it doesn't want > to trace the current function, and the return part must not be called > on exit. Keeping track of which user needs the return called is the > tricky part. So that I think only the "shadow stack" part should be generalized. > > Here's what I plan on implementing: > > Along with a shadow stack, I was going to add a 4096 byte (one page) > array that holds 64 8 byte masks to every task as well. This will allow > 64 simultaneous users (which is rather extreme). If we need to support > more, we could allocate another page for all tasks. The 8 byte mask > will represent each depth (allowing to do this for 64 function call > stack depth, which should also be enough). > > Each user will be assigned one of the masks. Each bit in the mask > represents the depth of the shadow stack. When a function is called, > each user registered with the function graph tracer will get called > (if they asked to be called for this function, via the ftrace_ops > hashes) and if they want to trace the function, then the bit is set in > the mask for that stack depth. > > When the function exits the function and we pop off the return code > from the shadow stack, we then look at all the bits set for the > corresponding users, and call their return callbacks, and ignore > anything that is not set. > It sounds too complicated... why we don't just open the shadow stack for each user? Of course it may requires a bit "repeat" unwind on the shadow stack, but it is simple. Thank you, > When a user is unregistered, it the corresponding bits that represent > it are cleared, and it the return callback will not be called. But the > tasks being traced will still have their shadow stack to allow it to > get back to normal. > > I'll hopefully have a prototype ready by plumbers. > > And this too will require each architecture to probably change. As a > side project to this, I'm going to try to consolidate the function > graph code among all the architectures as well. Not an easy task. > > -- Steve -- Masami Hiramatsu