Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp189687lqh; Mon, 6 May 2024 16:10:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVHNX4OcYRcHeBOe9KkDSYMqSOKqSWSXcdZ7pTbRxb0gKW+CvnFykElMhWHWvmYofv2MEAkR4W9m3OLocvZHu/SClBX+hM1u1P+CeeIeg== X-Google-Smtp-Source: AGHT+IHSy2HTGNJWdKnvhYD+UyljT4spVYS/mpEaOoknpeHDBYoSmaA30lCOLAmJsReFFN1p2caS X-Received: by 2002:a50:c00f:0:b0:571:fc02:1ce with SMTP id r15-20020a50c00f000000b00571fc0201cemr6400796edb.38.1715037016863; Mon, 06 May 2024 16:10:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715037016; cv=pass; d=google.com; s=arc-20160816; b=drKB+mKyAQL22QAXL6TTLVKL2euHid59KakBDKUmmyxQ8ZF/zsDiI6gt5Tv9EYY+P6 HrONzTmmnlznTAasAO87xwT/FzNNsrfMF4Bdk2u4Z4hLPVtAHoBwbfH1ZxgyXpkFfr5q 43cxDzGsFUbKrN0RBrjLefNVQZ4OUnKa26QvySWQUPCH6UyU+9hc1IcpqUwuJUhIyQpT IGu0qgAGKEwxboxnq/4TKJ/0UWwb1v+bk8pKqk7lsLIXTfXJjH1wn2thSbGpDfI0eK3v mpj3Ehy+PQCi0HH1IMEN5XWq3BJoDIZW8laSUADep/IIz5ZwL++8euNxrpJxwUBnVq9r HGmQ== 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=2djqDQE+yQ6VgjclssSW7VHfPd80JOhsReXmW/fEqXY=; fh=23HgZxb48qQ9xZgSlVifkHmfUsVta7r3Tz4ZFzKC5kM=; b=YropQQKU7GsUjypjv/206jZc9Csrc/ZrK2+rfbN445qCc1sefTc/vZCifPOcTmXaVx 9AdxUnF6aDwXRdfGEODEih+Wd3G3ZRqnZg0sJ8NirPiDKUraaayWPgTZKMC3P5rl91CI eee73Uoa7R1q/n7Ucv8xTAN/+CyeEYmY0cELiPfiIZgogI9WqEDPzipe+j5+IMjBv34Z 3U2DNMyoLmo3v2GkdO9iOk/GDr63uFczEz3+OzsIZ1D6sp2kOVKo3p76f1kqUDXI0xDV 6UBh3L4fz0OlFY3EYxandRj9XsyksiJL2cX9tgnTleTfTWoM6mX/pSgO3fHhcsTmKMOJ ONIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="nFF/q6E/"; 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-170494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170494-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 k9-20020aa7d8c9000000b0056fde9c51easi5260778eds.459.2024.05.06.16.10.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 16:10:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170494-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="nFF/q6E/"; 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-170494-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170494-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 705711F21A43 for ; Mon, 6 May 2024 23:10:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1C8B15D5A3; Mon, 6 May 2024 23:10:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nFF/q6E/" Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 5F1F51E492; Mon, 6 May 2024 23:10:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715037003; cv=none; b=p3PvvQdHYLfBGQ4RK8RDYMRSGf9KY8v8p+dercix1asilymhWj/C0g795D85t8dNFJtmlTQygwCeMM40oxzCo8JQKBuZX0FJWt6No1KnueafZKIDK+0XCFLyuCEzm758AFR7PUVK3Y19Om4uifmD1lGZ5K+L1ZHe3KrECvjNPZ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715037003; c=relaxed/simple; bh=jb6AoaZ4+1JJOP5y0uiexhZgPhIBpaMIIaYEadS16Q4=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=dq7d/ztGS7bwT+R6gunl+rpBaUKKlGmSa6F4pTPuShoYV5XwtRNedSihydA/WtCkmPNKQUxWdsw/Cdxj/MhRNrMMwUhpWJwvnlRQNa6LKl+NfBOhYrXmFbEfXaV9sPvCdGSyx1EELzeYyysKInyuw5C79161SC/c8tGZNsBhOLI= 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=nFF/q6E/; arc=none smtp.client-ip=209.85.221.42 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-f42.google.com with SMTP id ffacd0b85a97d-34db6a29a1eso1571042f8f.1; Mon, 06 May 2024 16:10:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715037000; x=1715641800; 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=2djqDQE+yQ6VgjclssSW7VHfPd80JOhsReXmW/fEqXY=; b=nFF/q6E/ncV3MYvnMC7xKGOyT/arhdI5FKJ2nO17qmvlhvec75Ip4UFkbelMBZm7u+ PBybAqvSwcZ9oDJ8hZ65h3OUtU6K6Tuy4OkIGMJU59xY9PCjEXwEAGoDPocnQ+odxFom N4Er+uN2wFHGu3eg6asD6OXFTuW6yF4gNJU933BhbVd4uV4tZ6+URa8Jwqd66UlaKEXz FEA2YE5NWTnaVcLhc7lKdA+UZIO5JJc7OE0mGh0wB5dw8+AOkZN8ljykJ0/x5DDTqv8I +nfXwAZgQyqRLuTHKeUPhB9EQwjDvTRuBnfJV5zlxfON94q4okZL4aKm22KMg0a5rCoU 77jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715037000; x=1715641800; 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=2djqDQE+yQ6VgjclssSW7VHfPd80JOhsReXmW/fEqXY=; b=GaQdPszgEpeawo3JT8tP1hTAO04LdGbkLuwmnMF5k9+FiNe9ikF2g8dgHY/iTVaqn2 QExx9KJgqi2XrNYK6XUhBmP06jo4TO/obfKkG5vH2uOOp2yVDE2RseuD7jJ3P38Kfo12 i0dQiiYiCDYH3GOwV1MxmcUr5OTn9d/3fSRb6HrLwZOGIPj1NIBG4iys3BKAKJRNQPkc Y39/1DsJlK+uTbiDY8KvJ6UBRY0qCl7H2VFQ78lnM1gP/R7I0Zybzh7ZdWbUIw/Z1JkM /vRIsPAzucIrNmB+6lWg8cHZxeu5uZN7vE/mYLjODOoLbA++Vl7U2FmCkbw2/QTmOAJr ZZ8g== X-Forwarded-Encrypted: i=1; AJvYcCUaK882yAs7A/4I5W3SbVLAoLMrcy0nHYyak7mj5O7c+Hbrqn7BR12EfPEQSsWKRxjwIMCvyCHopGjpmcLmkC9+xbXZAYCJ3bMDumqOZVVj10hfF8NzN6R3fnBS4w1/r1ghl/lQ0j3Hhzfzd0zYYg0lMoT0GILyGM4p X-Gm-Message-State: AOJu0YwXiLNQZKp11BEH1vJXozsm/o7CPlhnvv2FoznjxEQjEfl/rvpx mvMI2t9aD2zKDuZ1LTi82ZFnvMkPgmXNk4+reoPZJUOCLjlrRmLpeynVvEPL3K2WgGrClApeLjU 9nEhgpMBIt8Ib/U5eE2pKEC6VAY4= X-Received: by 2002:adf:e60c:0:b0:34d:b993:fe6e with SMTP id p12-20020adfe60c000000b0034db993fe6emr8100162wrm.0.1715036999414; Mon, 06 May 2024 16:09:59 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240503182957.1042122-1-bigeasy@linutronix.de> <20240503182957.1042122-15-bigeasy@linutronix.de> <87y18mohhp.fsf@toke.dk> In-Reply-To: <87y18mohhp.fsf@toke.dk> From: Alexei Starovoitov Date: Mon, 6 May 2024 16:09:47 -0700 Message-ID: Subject: Re: [PATCH net-next 14/15] net: Reference bpf_redirect_info via task_struct on PREEMPT_RT. To: =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= Cc: Sebastian Andrzej Siewior , LKML , Network Development , "David S. Miller" , Boqun Feng , Daniel Borkmann , Eric Dumazet , Frederic Weisbecker , Ingo Molnar , Jakub Kicinski , Paolo Abeni , Peter Zijlstra , Thomas Gleixner , Waiman Long , Will Deacon , Alexei Starovoitov , Andrii Nakryiko , Eduard Zingerman , Hao Luo , Jesper Dangaard Brouer , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song , bpf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, May 6, 2024 at 12:41=E2=80=AFPM Toke H=C3=B8iland-J=C3=B8rgensen wrote: > > Sebastian Andrzej Siewior writes: > > > The XDP redirect process is two staged: > > - bpf_prog_run_xdp() is invoked to run a eBPF program which inspects th= e > > packet and makes decisions. While doing that, the per-CPU variable > > bpf_redirect_info is used. > > > > - Afterwards xdp_do_redirect() is invoked and accesses bpf_redirect_inf= o > > and it may also access other per-CPU variables like xskmap_flush_list= . > > > > At the very end of the NAPI callback, xdp_do_flush() is invoked which > > does not access bpf_redirect_info but will touch the individual per-CPU > > lists. > > > > The per-CPU variables are only used in the NAPI callback hence disablin= g > > bottom halves is the only protection mechanism. Users from preemptible > > context (like cpu_map_kthread_run()) explicitly disable bottom halves > > for protections reasons. > > Without locking in local_bh_disable() on PREEMPT_RT this data structure > > requires explicit locking. > > > > PREEMPT_RT has forced-threaded interrupts enabled and every > > NAPI-callback runs in a thread. If each thread has its own data > > structure then locking can be avoided. > > > > Create a struct bpf_net_context which contains struct bpf_redirect_info= . > > Define the variable on stack, use bpf_net_ctx_set() to save a pointer t= o > > it. Use the __free() annotation to automatically reset the pointer once > > function returns. > > The bpf_net_ctx_set() may nest. For instance a function can be used fro= m > > within NET_RX_SOFTIRQ/ net_rx_action which uses bpf_net_ctx_set() and > > NET_TX_SOFTIRQ which does not. Therefore only the first invocations > > updates the pointer. > > Use bpf_net_ctx_get_ri() as a wrapper to retrieve the current struct > > bpf_redirect_info. > > > > On PREEMPT_RT the pointer to bpf_net_context is saved task's > > task_struct. On non-PREEMPT_RT builds the pointer saved in a per-CPU > > variable (which is always NODE-local memory). Using always the > > bpf_net_context approach has the advantage that there is almost zero > > differences between PREEMPT_RT and non-PREEMPT_RT builds. > > Did you ever manage to get any performance data to see if this has an > impact? > > [...] > > > +static inline struct bpf_net_context *bpf_net_ctx_get(void) > > +{ > > + struct bpf_net_context *bpf_net_ctx =3D this_cpu_read(bpf_net_con= text); > > + > > + WARN_ON_ONCE(!bpf_net_ctx); > > If we have this WARN... > > > +static inline struct bpf_redirect_info *bpf_net_ctx_get_ri(void) > > +{ > > + struct bpf_net_context *bpf_net_ctx =3D bpf_net_ctx_get(); > > + > > + if (!bpf_net_ctx) > > + return NULL; > > ... do we really need all the NULL checks? Indeed. Let's drop all NULL checks, since they definitely add overhead. I'd also remove ifdef CONFIG_PREEMPT_RT and converge on single implementati= on: static inline struct bpf_net_context * bpf_net_ctx_get(void) { return current->bpf_net_context; }