Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp672094pxb; Wed, 24 Feb 2021 11:48:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjgSwu2EXBdXxOUYruKNI4IEuW1mlmHcFhc1IjRhjHwT2dYAhUij5LpgENnA3uuE9n7Sl3 X-Received: by 2002:a50:e183:: with SMTP id k3mr35382684edl.45.1614196117722; Wed, 24 Feb 2021 11:48:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614196117; cv=none; d=google.com; s=arc-20160816; b=KRHB3/4fjfiPLwBkE6jT+0aiJtHJYxW+r6q5sbn8U0KNrH3FOdH0j6jqm89SIp0P5V 3GwQgUWwnhwu0rPWCgTmOM4IGrZOrCOZAxHiN4rvWvFmKigB/8itV4uKmKZLrUq3oZ3h 9zd+KLEWXR6VrPobC27MsV1l8H88a+Cb6rX/wX4Z062K4ECSs5k+Fsmw/GjNIvi+c5Hx K2Nx1DfGOMgNGm3kS3FDCkQyCYCyc9uzbufv5MzCM0fko+ouVBdItyqkWlC90M5inKBZ bzKM377M/6+MsDQ/Hr5qfMzeZRBpiu/6Jxmro+fKoI3B8Hp1vuHF8WqZ4+2stxlTLVzF kbpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ja3X7tGBUHPzcz3GH9Ny24Khyiqh/kt3vIjZC5Ge0+8=; b=RjXdURFD/pSICj/7pUN4l/isN/0BV3is72icf8oXufSWPJ1vo5dwofuYqmgouR2pBD E0Z07cC/i62ZMxwale8rQwe59SYEgAgp3aPCjzjhxmUCqFQ6zxqEtdyALC0IBlgjwOaj u0yg2sZ+E5L/ckLdy3vRgV8+hb+QvYhZEd8NhRAqZhcMoWKQWqHkZ9TZvuzxDfuVkocY XwKnqQoOlKrm5HswbuSNCG9tYhNEvNptN8nHpPjFsgJnt37/Q248mBbZepjJVDu1fgQp o2dYrJFfc3dOL5Gp9TShys7VWO+ZctTGh5xPsdGIfO6Y6nKndmpwOPH55hwggZSfgILL YvsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qjFNcpfr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id x19si1755709edd.134.2021.02.24.11.48.13; Wed, 24 Feb 2021 11:48:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qjFNcpfr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233674AbhBXTqI (ORCPT + 99 others); Wed, 24 Feb 2021 14:46:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:51378 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232392AbhBXTqF (ORCPT ); Wed, 24 Feb 2021 14:46:05 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8D55964EDD for ; Wed, 24 Feb 2021 19:45:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614195924; bh=LfQXoknJnKVbOBg04Drxni9CXS4PBfq5QMqoj1lDLe4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=qjFNcpfrEj7+RKhcovMLEWE+fr7MyNKQDA6oH+yhSwA2DlW1XSz12WmN6dnwW0Wdx IHYAkvxKEH0G+ngeFKxFWAxHkfAJRmJZko6qKOp2YTbb6MzzmhckWc9yue3xu96jVg xYkWQI2uq/7X2Yr+iAC2dqFbL9F4gqOHxQSnuynAWPIXCyccs+0/6/x+zPiJvOLHpb B8PjzFBOpjKWARxgxbnEGg4ooTnuvVxRrCKdLx5d0wSanGIWEN1S6oHVLxUxxh1ZOe +Wy7vgNyHgX5GkFLcKVjjGNIuavk7ddiG6ZazmHMsxVpVhsmsTDLvuFb/rRLKvo/PQ CTbz1GrO0xEtQ== Received: by mail-ej1-f51.google.com with SMTP id k13so4965801ejs.10 for ; Wed, 24 Feb 2021 11:45:24 -0800 (PST) X-Gm-Message-State: AOAM530J42QXz2tVypEt23zb0Q0CZzb1M93S7xQww6pLTIGD2tIGvZfz gj2Enl4lp0Dvl3popXP89dZ80rNA4h3xuvOKZCu6hQ== X-Received: by 2002:a17:906:7d87:: with SMTP id v7mr29214938ejo.214.1614195922091; Wed, 24 Feb 2021 11:45:22 -0800 (PST) MIME-Version: 1.0 References: <20210224101756.bbdf95b9b6dfc982bff21324@kernel.org> In-Reply-To: <20210224101756.bbdf95b9b6dfc982bff21324@kernel.org> From: Andy Lutomirski Date: Wed, 24 Feb 2021 11:45:10 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Why do kprobes and uprobes singlestep? To: Masami Hiramatsu Cc: Andy Lutomirski , Oleg Nesterov , Peter Zijlstra , LKML , Anil S Keshavamurthy , "David S. Miller" , X86 ML , Andrew Cooper Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 23, 2021 at 5:18 PM Masami Hiramatsu wrote: > > On Tue, 23 Feb 2021 15:24:19 -0800 > Andy Lutomirski wrote: > > > A while back, I let myself be convinced that kprobes genuinely need to > > single-step the kernel on occasion, and I decided that this sucked but > > I could live with it. it would, however, be Really Really Nice (tm) > > if we could have a rule that anyone running x86 Linux who single-steps > > the kernel (e.g. kgdb and nothing else) gets to keep all the pieces > > when the system falls apart around them. Specifically, if we don't > > allow kernel single-stepping and if we suitably limit kernel > > instruction breakpoints (the latter isn't actually a major problem), > > then we don't really really need to use IRET to return to the kernel, > > and that means we can avoid some massive NMI nastiness. > > Would you mean using "pop regs + popf + ret" instead of IRET after > int3 handled for avoiding IRET releasing the NMI mask? Yeah, it is > possible. I don't complain about that. Yes, more or less. > > However, what is the relationship between the IRET and single-stepping? > I think we can do same thing in do_debug... Because there is no way to single-step without using IRET. POPF; RET will trap after RET and you won't make forward progress. > > > But I was contemplating the code, and I'm no longer convinced. > > Uprobes seem to single-step user code for no discernable reason. > > (They want to trap after executing an out of line instruction, AFAICT. > > Surely INT3 or even CALL after the out-of-line insn would work as well > > or better.) Why does kprobe single-step? I spend a while staring at > > the code, and it was entirely unclear to me what the purpose of the > > single-step is. > > For kprobes, there are 2 major reasons for (still relaying on) single stepping. > One is to provide post_handler, another is executing the original code, > which is replaced by int3, without modifying code nor emulation. I don't follow. Suppose we execute out of line. If we originally have: INSN we replace it with: INT3 and we have, out of line: INSN [but with displacement modified if it's RIP-relative] right now, we single-step the out of line copy. But couldn't we instead do: INSN [but with displacement modified if it's RIP-relative] INT3 or even INSN [but with displacement modified if it's RIP-relative] JMP kprobe_post_handler and avoid single-stepping? I guess I see the point for CALL, JMP and RET, but it seems like we could emulate those cases instead fairly easily.