Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1601703lqz; Mon, 1 Apr 2024 11:03:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX3Y/hPMdxdVfNgNIxdX6nUqIO6QKwKJWVGnJIkqE4ew7pDzvtUpdDDgHIQYvoiafJZ6SCmqGHPlJZbDMTvOP6G6Op2yAcIhTCCAWmLDw== X-Google-Smtp-Source: AGHT+IEa8r5P8CLGYbQ/fMag13h7tOPpwPOY9BZQx7f98xELuEiVBHx5ObOZiuFOcVbOPKnacdnL X-Received: by 2002:a17:906:b80b:b0:a4e:8044:231e with SMTP id dv11-20020a170906b80b00b00a4e8044231emr343386ejb.42.1711994608011; Mon, 01 Apr 2024 11:03:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711994608; cv=pass; d=google.com; s=arc-20160816; b=nFqTYx5UTz33xYquxp7zJHtW6IDf4Jme1GdqvFI3919sFdsRnlmWtXn5meY7h58/8m ERTVDYtsPO8bXolw+2WJ9F1PPmB/qxunM+tW/aKn3SMtSJci+umuGJmWU6tgM6+MeZ/I wSzIwE/mlVJPOEPTq55KTXIvFQrAfjxgclk3QyxXgxuVjwdp/Dh3NV/R75Y6Hh983NBg nYPNY5hjvwpBKUVTVh1CBoKVdhPM9LiJkxC5LAdcGUlYv4qDN1b8lZaGK7AHDtjxAPAE b89l9m4WUKrBQr4WOeKXS8ob4WZt9Js8PBA1eP3Uxo4UG2kLcLFIyUk6FUeF5a9wdQzd F8MA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=6JaPPNMEgkwh4HfN+ukbZ/toOf6Bi9mBKsKcLnPIOUU=; fh=bU10qsJbGXcHbBYep9e+LFTD9Qoy6tVaMMNruQEWSBM=; b=hBK7c/YMjq98AlMuNEr6+33iXvMdPPsTXPFdAULL8so4vgGkCzmNweoCyGAHLOw4FZ SxYpJj6L14Ef5zxBzsoUe+zZqYqSV0l4vdg4RSrHCaMzRqJkN/kjCv6TabmdZPmwIksJ Mwrh07iVJG+OsRO6IN4cV+bhVDreT1YVRGlLA4iuGKdsZWEenGKMbLmpvm/v1a7JIZjV f0hHDkJfec6RdI4NFm0YFVDn+xyfu9BvtKhblDH2kwNu3EfUSCf4bLAPPS1+QIPtKyjU ibeos6cU4FQthFyC1+2sG27AUgqsOciJ13Y+7Je/SdGQQNwuBjyocFKtKd5bdT1XHwpz nYKg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TCqP7Tus; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-126931-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126931-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id lf12-20020a170907174c00b00a4a367a17f2si4725649ejc.230.2024.04.01.11.03.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 11:03:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-126931-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TCqP7Tus; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-126931-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126931-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9373A1F226F1 for ; Mon, 1 Apr 2024 18:03:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C1B924F217; Mon, 1 Apr 2024 18:03:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TCqP7Tus" Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4318E481C6; Mon, 1 Apr 2024 18:03:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711994594; cv=none; b=DVti2xeus4xPqCoYrgnTrNS5fpLfHcBdK/CdVmtj27KY+0sjWcLc7IHX9n19aAlYaDY3DX5+BtAtS0TT/Dgdov8dIZ9ZSBPAmZG1x8EsN2v3O2hzd77wosHLdXUoVC4G1Gy4IM6vm+FuZwHrTSWCNJ8yqCDxqAWTY8zSqQUEcBM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711994594; c=relaxed/simple; bh=dJpGXu0C9rO7fmzbAKI1TioiM08l2gECNbt1RRXQXIM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=hd44rhaSBd5HkmWo85CIztYJX49fD3HUaCcfBz2xVN+Q+lHGn7ZlUYXpz1FQt0ytDXqdIQletN2/bsSs0f7H4XxRLiQEG+UtHDxGRndZJRn44uUroN/WnzYdTjFIOovLDgXzPtM8l4fYPMkNtqA5nudWawE3zYrpjo0mB6KOIDQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TCqP7Tus; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3434c6e1941so465941f8f.0; Mon, 01 Apr 2024 11:03:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711994591; x=1712599391; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=6JaPPNMEgkwh4HfN+ukbZ/toOf6Bi9mBKsKcLnPIOUU=; b=TCqP7TusWZgzdoKmAF6d+BtqpVpiTtv4KhGRoHG0I8NxP5B3gjXLFHUifuCdk/nBj1 KWpKAw/fqZMRfsMDJzR09CQ55bWFYsqh6xm8m/zteMhKtRXi66L39BVpaTRWFxnRXV9o ZYWX6MCHQwe5yyw5mF4jFLd4LNAKNWU69zn7Lf74e2CMrsP12ircEvhXeMxymPCgPVVi XZO4Gl66FU3UhHOky1pYI7FwHczQgigdNfmqr4sEIEVPR3SwTTz1Ot1HDFRfUCK8Xej/ 00j6ysC7aTiupRZI2y9JzoEpFGGbQoNGcA/x8SBU2Iwo69/yqeW0ChpsEe+qfWfOSl5Y NZQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711994591; x=1712599391; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6JaPPNMEgkwh4HfN+ukbZ/toOf6Bi9mBKsKcLnPIOUU=; b=awAf5iIab2UnWSHquR32lLKDXkQ1mYIj/cQGZm2pD96m+Mke1pLpTGX0fiHNe+RsmM 3Kn6D/sTRAa9D55KJs8UA+ut5p7Dc4v63Sv/oT1sA3UhhA8fY7gYnV7q7aiF/tkqqvSy /7A5HjKSiylZzZPIizb1AAR1PZMEFKsWuOBdeh5qaqHmaTdE33TNsGfG0GdtFaN6f7jm VEbFV2n+xPRv5lCgRQEihEccBxK5GkjnjJT9i8lUJghWgY53sGL3gig456R2nKxGVuyj K4jiMv0IUNDs7iiYc7zV96AvlcAfbKQKjDdraZIsvtaqHR8pMk7tVzmqrml3ac8H7o8p 5BcA== X-Forwarded-Encrypted: i=1; AJvYcCU+oyODtERkwcb2ESwsHFz8eIoGM5+h4UCGNNfRdmYGStRPbdz1Cw3UaVuw4QPXkvuwJc89PJzbPoXV1zSUgIqXmb+GFhwKrd0cKcJX/Ck+yfAK+evH1/QrlUAwlq0xOuRSNHRHYaoQaJB/Oc2Sr+EjMTtwUyENRMyD X-Gm-Message-State: AOJu0YzRLaoeZRcF0j6fDCx3foBwgMdT4fWYGz5vNoF7EukWiD2uL0cZ HQxwNDTwihKUSd1eaoHYhLPiSbIdx/nz8E2chc8LKYtHAoDU4L2vT3ANmI8DAOLbCTsd4zNA18r i2YRcLAMol0M962+p1RijHjjurC0= X-Received: by 2002:adf:edcc:0:b0:33e:40a3:22c8 with SMTP id v12-20020adfedcc000000b0033e40a322c8mr6490749wro.33.1711994591321; Mon, 01 Apr 2024 11:03:11 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240329094906.18147-1-ubizjak@gmail.com> <20240329094906.18147-3-ubizjak@gmail.com> In-Reply-To: From: Alexei Starovoitov Date: Mon, 1 Apr 2024 11:02:59 -0700 Message-ID: Subject: Re: [PATCH RESEND bpf 2/2] x86/bpf: Fix IP for relocating call depth accounting To: Uros Bizjak Cc: X86 ML , bpf , Network Development , LKML , =?UTF-8?Q?Joan_Bruguera_Mic=C3=B3?= , Ingo Molnar , Alexei Starovoitov , Daniel Borkmann Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Mar 30, 2024 at 2:01=E2=80=AFAM Uros Bizjak wro= te: > > On Fri, Mar 29, 2024 at 10:53=E2=80=AFPM Alexei Starovoitov > wrote: > > > > On Fri, Mar 29, 2024 at 2:49=E2=80=AFAM Uros Bizjak = wrote: > > > > > > From: Joan Bruguera Mic=C3=B3 > > > > > > The recently introduced support for %rip-relative relocations in the > > > call thunk template assumes that the code is being patched in-place, > > > so the destination of the relocation matches the address of the code. > > > This is not true for the call depth accounting emitted by the BPF JIT= , > > > so the calculated address is wrong and usually causes a page fault. > > > > Could you share the link to what this 'rip-relative' relocation is ? > > Please see the "RIP relative addressing" section in [1]. > > [1] https://compas.cs.stonybrook.edu/~nhonarmand/courses/sp17/cse506/ref/= assembly.html > > In our case: > > The callthunks patching creates a call thunk template in the .rodata > section (please see arch/x86/kernel/callthunks.c) that is later > copied to the .text section at the correct place. The template uses > X86_call_depth in the pcpu_hot structure. Previously, the template > used absolute location for X86_call_depth and the linker resolved the > address in the template to this absolute location. There is no issue > when this template is copied to the various places in the .text > section. > > When we want to use PC relative relocations (to reduce the code size), > then the linker calculates the address of the variable in the template > according to the PC in the .rodata section. If we want to copy the > template to its final location, then the address of X86_call_depth, > relative to the PC, has to be adjusted, as explained in > arch/x86/kernel/alternative.c, in the comment above apply_reloc_n > macro. I didn't mean to ask for info about the definition of rip-relative, but how it's used in this case and what you've been trying to achieve with commit 17bce3b2ae2d that broke call depth accounting. And the whole sequence of events that caused this breakage. Something like: commit 59bec00ace28 ("x86/percpu: Introduce %rip-relative addressing to PER_CPU_VAR()") made PER_CPU_VAR() to use rip-relative addressing, hence INCREMENT_CALL_DEPTH macro and skl_call_thunk_template got rip-relative asm code inside of it. Hence x86_call_depth_emit_accounting() was changed in commit 17bce3b2ae2d ("x86/callthunks: Handle %rip-relative relocations in call thunk template") to use apply_relocation(), but it was mistakenly made to use *pprog as dest ip, so jit-ed bpf progs on kernels with call depth tracking got broken. Such details should be in the commit log.