Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp312374ybg; Tue, 22 Oct 2019 21:22:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqw6YBgv5JhEEjRQR6qtFyUI1s6vlOG6Bnymha8dG+A+3Af3+86Qr4JF7EYYqwEgiw/81ZHg X-Received: by 2002:a17:906:4895:: with SMTP id v21mr26559056ejq.2.1571804559456; Tue, 22 Oct 2019 21:22:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571804559; cv=none; d=google.com; s=arc-20160816; b=HlDrPOFq7OzgsV4IzvSOcqxKjQnYFa5dyQUH5c30hEqgCH8vTcG+zqzp50vD2vKq1W dR8rOmLdN/N7tazYx570WqzukgyP9qZ6knaYle/pt7t1zXn7w+PdsTandIC4JowMCafW S6sA8Y9vEaoXtqdizApiufyZizlALgh2BXeZ+917pg+MZ1MjNoCvMght/3zUMAXPJoTv ecEo42OSjlvOrcaOJmw74yQpTSMCxIvcFiIS0rqQ64EDQincQxAMbaQGKOqaMpXmHuNu o3/TYoho5ibWUuA/Os3ehsx/4MbHmUEiVtMjm0rn4C1YLlnaPWAW9vh1pgtrENn0O4XT QmeA== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Y/LNPigbQ9TB22k2a7RkBNKb5Cm/9R5+HbTyQH04m7U=; b=qm4jHGokKBmxtPosMVG2yZH25jhu5MqVdW0ax8s0zaORyZ8jhH8Xd0iudOT/vAhzvS 4nyNGNDkyOiHFuVlUJkCR9ugQ4Dz1uSUkwXBnwNyAq5XekSyE6ybDAk/gHJSd+DGWe7v vP2RCK//HWBuI2bzIwIftObTi8UUesd4X57eUgipxmTGWny6YvfDTRi3fEakUqnJZazb HT2iVPaVKFyWnVYuD9xd2LfsCB28269J+MBziFKtqloNLA0dTOb7lJvFLYxwtMTWH1sk s/1PAIS/TkMnBMUujiBjz8xMYVvsaGReNv3o/x3S0XAok6/D2dze6cHUPBfJSvJ9WU2Z QKkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=O1Vynvw3; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rh23si11513802ejb.75.2019.10.22.21.22.15; Tue, 22 Oct 2019 21:22:39 -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=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=O1Vynvw3; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726925AbfJWEUn (ORCPT + 99 others); Wed, 23 Oct 2019 00:20:43 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:51873 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725270AbfJWEUn (ORCPT ); Wed, 23 Oct 2019 00:20:43 -0400 Received: by mail-wm1-f65.google.com with SMTP id q70so12371875wme.1 for ; Tue, 22 Oct 2019 21:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Y/LNPigbQ9TB22k2a7RkBNKb5Cm/9R5+HbTyQH04m7U=; b=O1Vynvw3eN1/NbNkI1c0SyDg2V/EkrhR9wp6vkwC4xTXDLAluIn8cynh/VXr4ZdtLc YkLjkqyRjDf2WIMYXKfuyQYtOQE30/5EgnWBn65L/7X+wul5U/YEsA8xDxSAh58xQrpr /+BujZ4ra4A6iDundfSpVzKYMhWNuxHpJ+5BNaIgsAJOGXUX9A6p3vCOU1iyHIhOMVsf s+8w+vDRWfin/kSpbgmwYNMs/hgi+9teFu96AFopk3VG7DhHb/gkkLgNe0fNdzTbRNbi 9uEI5ryHesei0FaCHhUvwDVkmOKtyqRWRgkhJ1C+GbW1NqdHhJILuUsf8wKazszK2Ghl pc/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Y/LNPigbQ9TB22k2a7RkBNKb5Cm/9R5+HbTyQH04m7U=; b=tn3iHYrTZzoYT0pXVVceAFeJfpg8yYzJN28o8hxrjRLWtiDvYdv6NbBr5374pR3Pks PhXPNOp9I7ROXK1+rZK36ZckLZpkbiX7tH/5/vPQ7YPXjBS9jVVEBUfftoZMHNctSmXE nwlMh4/5t9OA75tD4WBhGr1tfFlTjiD/s2H3s3Pet/xRsF3qAozIoOtxjyxrlvJw5ZRt IjQU3VlzI0BHwrc+LOdn1DPzRFIAdoj/OQXmAMI+97QOBpXkZTB9X6iGtm2GQLF344dt N6CnjbbfKfe9PbgkfdCUwZoYLHCYjSjf6/9AE/CEqQSDFMSEvVsMVWngQnS2VXXnoQdV xdPA== X-Gm-Message-State: APjAAAW7qMn/3C78rbG9C9u6siEfGfnOM4IbEe9iTYDq17TpmyeEYP73 b+BUnQZqUXlz/2MX/8B/A8lcHrSIW7J84aN/8uB7Aw== X-Received: by 2002:a1c:20d8:: with SMTP id g207mr5789097wmg.79.1571804439825; Tue, 22 Oct 2019 21:20:39 -0700 (PDT) MIME-Version: 1.0 References: <20191022215841.2qsmhd6vxi4mwade@ast-mbp.dhcp.thefacebook.com> <7364B113-DD65-423D-BED3-FF90C4DF8334@amacapital.net> <20191022234921.n5nplxlyq25mksxg@ast-mbp.dhcp.thefacebook.com> In-Reply-To: <20191022234921.n5nplxlyq25mksxg@ast-mbp.dhcp.thefacebook.com> From: Andy Lutomirski Date: Tue, 22 Oct 2019 21:20:27 -0700 Message-ID: Subject: Re: [PATCH 3/3] x86/ftrace: Use text_poke() To: Alexei Starovoitov Cc: Steven Rostedt , Peter Zijlstra , Daniel Bristot de Oliveira , LKML , X86 ML , Nadav Amit , Andy Lutomirski , Dave Hansen , Song Liu , Masami Hiramatsu Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 22, 2019 at 4:49 PM Alexei Starovoitov wrote: > > On Tue, Oct 22, 2019 at 03:45:26PM -0700, Andy Lutomirski wrote: > > > > > > >> On Oct 22, 2019, at 2:58 PM, Alexei Starovoitov wrote: > > >> > > >> =EF=BB=BFOn Tue, Oct 22, 2019 at 05:04:30PM -0400, Steven Rostedt wr= ote: > > >> I gave a solution for this. And that is to add another flag to allow > > >> for just the minimum to change the ip. And we can even add another f= lag > > >> to allow for changing the stack if needed (to emulate a call with th= e > > >> same parameters). > > > > > > your solution is to reduce the overhead. > > > my solution is to remove it competely. See the difference? > > > > > >> By doing this work, live kernel patching will also benefit. Because = it > > >> is also dealing with the unnecessary overhead of saving regs. > > >> And we could possibly even have kprobes benefit from this if a kprob= e > > >> doesn't need full regs. > > > > > > Neither of two statements are true. The per-function generated trampo= line > > > I'm talking about is bpf specific. For a function with two arguments = it's just: > > > push rbp > > > mov rbp, rsp > > > push rdi > > > push rsi > > > lea rdi,[rbp-0x10] > > > call jited_bpf_prog > > > pop rsi > > > pop rdi > > > leave > > > ret > > > > Why are you saving rsi? You said upthread that you=E2=80=99re saving t= he args, but rsi is already available in rsi. > > because rsi is caller saved. The above example is for probing something > like tcp_set_state(struct sock *sk, int state) that everyone used to > kprobe until we got a tracepoint there. > The main bpf prog has only one argument R1 =3D=3D rdi on x86, > but it's allowed to clobber all caller saved regs. > Just like x86 function that accepts one argument in rdi can clobber rsi a= nd others. > So it's essential to save 'sk' and 'state' for tcp_set_state() > to continue as nothing happened. Oh, right, you're hijacking the very first instruction, so you know that the rest of the arg regs as well as rax are unused. But I find it hard to believe that this is a particularly meaningful optimization compared to the version that saves all the C-clobbered registers. Steven, Also, Alexei, are you testing on a CONFIG_FRAME_POINTER=3Dy kernel? The ftrace code has a somewhat nasty special case to make CONFIG_FRAME_POINTER=3Dy work right, and your example trampoline does not but arguably should have exaclty the same fixup. For good performance, you should be using CONFIG_FRAME_POINTER=3Dn. Steven, with your benchmark, could you easily make your actual ftrace hook do nothing at all and get a perf report on the result (i.e. call the traced function in a loop a bunch of times under perf record -e cycles or similar)? It would be interesting to see exactly what trampoline code you're generating and just how bad it is. ISTM it should be possible to squeeze very good performance out of ftrace. I suppose you could also have a fancier mode than just "IP" that specifies that the caller knows exactly which registers are live and what they are. Then you could generate code that's exactly as good as Alexei's.