From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32A81219E7 for ; Tue, 14 May 2024 11:54:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715687694; cv=none; b=cMnLbymfLs/szwj8Wg532tFvaBXwC4MYMymlvCyjFPgf5cvHEJaOQbcnEPOzehKm1XnsjPY5VffW64uSdsgW/KG6VVFLZS1t+Nv39LM6kC844Wwv4j1AovyaYce2qD+Iquab3gfYsugGa7NqzVbwxHu7rHeNbWxdwGQIaGuvRFQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715687694; c=relaxed/simple; bh=nnJX6P3EkP1dyPUxV/tx9bKRjnghvW7CbEGyGDRSCGA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=Na9EQSgr8Y+cAj7FsqRaOb22o/xGUGC8olVvYc6VJ7OKWSq3pys6cvEpaGaMmLV1XwRGPGqXUi9BDAL3Gv34xsozNVuN7ew4485+Vk7YY6tuCIj2RxUw2lo/QQX9deDcHemABDy+Y3JYs/ntbOoiBKW7w9uAiEYD0J4j2C9dJhk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WdfMcH24; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WdfMcH24" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715687692; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZmVpbn0AeBjsEQpU86fpJTQEpZN0TMYC3p/NPKB9WEU=; b=WdfMcH24QMlZ4rxKdjm7hA5zeyke9zvcfZyFBNU+FcVqLeObd6gTCM7hacnzP/aGcDBQKb vU+LT4DyXn115yT8W5bxOoATdVqs7CjYwzMNvmMEq3gwe0rtDmSD/qaNUDIV3QFOC9dMDI 8r2jBt7uD3Q0jjqWflh8ZL8c7T4cAQ0= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-527-Y0fHUBu2N02rjgnPvN0hiA-1; Tue, 14 May 2024 07:54:45 -0400 X-MC-Unique: Y0fHUBu2N02rjgnPvN0hiA-1 Received: by mail-ed1-f70.google.com with SMTP id 4fb4d7f45d1cf-558aafe9bf2so659665a12.1 for ; Tue, 14 May 2024 04:54:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715687684; x=1716292484; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZmVpbn0AeBjsEQpU86fpJTQEpZN0TMYC3p/NPKB9WEU=; b=UaSZRrB6gOVBwqmKEEbMdOvoBF+SIrS+EYsBKM573O/sEAMv+jO+dbjogqWNpxVmT9 v0CWMQ+f+6pkWS4Y2poMNdfjQoVKFLGV0xXISxjdWkbJEq9fH4ozjAw9yF0ElFIiq/97 i6SlMuaEcXUxUjn61jEKYQZvnDyU8F+KBKkvg3HI+brCDGp88ZtStS8x7OdyPSRd0qpc tWfJOeXohAi+HGe8Ql5kuxJm2dLgHBCltSts13Qd9DzoM9ZnIYVt9TzwVJJHUc8f3v6N IPdJUi3vk1ysonGCXlbP/PYe/YaNETqnVJQJPWPtiOY/sazrW6u4mhPg9WPXRrBDAZEg RR9A== X-Forwarded-Encrypted: i=1; AJvYcCU6sjF4r4yot29MyGhpJnu5nUbl7+4GotQMJMKud9SLpc2pgY0mZYW4cJ7KWLJnEUMQDsHBU54TJTCQIUpL/bjf7HW26k0y X-Gm-Message-State: AOJu0YwlD7dVYtWL/7uyc+NsPBkvkc3V3ZXZIGEEyyydrReR1K4BBqq4 K47SjUrXVrq+uyjByJJVd5dZXgbMk6OG1CZV5zZvK/b0lkgs2QI0SUa7jDYMNU6qKjg3JDmWMN4 FQYft98X+gNzEVeOjBkem28UrYUXkOg43mvC844qx+XNJwnCSf6Yx4A== X-Received: by 2002:a17:906:f1cd:b0:a59:af54:1651 with SMTP id a640c23a62f3a-a5a2d641977mr770408066b.57.1715687684485; Tue, 14 May 2024 04:54:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDjLdVyLdxEuxHEe+hgxcQWw/qN8QsStaUE90DA4xueTt2WHN4fQhw5JMZD6CMDc+VjV8qZA== X-Received: by 2002:a17:906:f1cd:b0:a59:af54:1651 with SMTP id a640c23a62f3a-a5a2d641977mr770406366b.57.1715687683960; Tue, 14 May 2024 04:54:43 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5a8883344bsm49361266b.9.2024.05.14.04.54.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 04:54:43 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 0834F12F64B5; Tue, 14 May 2024 13:54:43 +0200 (CEST) From: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= To: Sebastian Andrzej Siewior , Jesper Dangaard Brouer Cc: Alexei Starovoitov , 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 , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song , bpf Subject: Re: [PATCH net-next 14/15 v2] net: Reference bpf_redirect_info via task_struct on PREEMPT_RT. In-Reply-To: <20240510162121.f-tvqcyf@linutronix.de> References: <20240503182957.1042122-1-bigeasy@linutronix.de> <20240503182957.1042122-15-bigeasy@linutronix.de> <87y18mohhp.fsf@toke.dk> <20240507123636.cTnT7TvU@linutronix.de> <93062ce7-8dfa-48a9-a4ad-24c5a3993b41@kernel.org> <20240510162121.f-tvqcyf@linutronix.de> X-Clacks-Overhead: GNU Terry Pratchett Date: Tue, 14 May 2024 13:54:43 +0200 Message-ID: <87le4cd2ws.fsf@toke.dk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Sebastian Andrzej Siewior writes: > The XDP redirect process is two staged: > - bpf_prog_run_xdp() is invoked to run a eBPF program which inspects the > 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_info > 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 disabling > 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 to > 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 from > 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. > > Cc: Alexei Starovoitov > Cc: Andrii Nakryiko > Cc: Eduard Zingerman > Cc: Hao Luo > Cc: Jesper Dangaard Brouer > Cc: Jiri Olsa > Cc: John Fastabend > Cc: KP Singh > Cc: Martin KaFai Lau > Cc: Song Liu > Cc: Stanislav Fomichev > Cc: Toke H=C3=B8iland-J=C3=B8rgensen > Cc: Yonghong Song > Cc: bpf@vger.kernel.org > Signed-off-by: Sebastian Andrzej Siewior > --- > include/linux/filter.h | 42 ++++++++++++++++++++++++++++++++----- > include/linux/sched.h | 3 +++ > kernel/bpf/cpumap.c | 3 +++ > kernel/fork.c | 1 + > net/bpf/test_run.c | 11 +++++++++- > net/core/dev.c | 19 ++++++++++++++++- > net/core/filter.c | 47 +++++++++++++++++++----------------------- > net/core/lwt_bpf.c | 3 +++ > 8 files changed, 96 insertions(+), 33 deletions(-) > > diff --git a/include/linux/filter.h b/include/linux/filter.h > index d5fea03cb6e61..6db5a68db6ee1 100644 > --- a/include/linux/filter.h > +++ b/include/linux/filter.h > @@ -744,7 +744,39 @@ struct bpf_redirect_info { > struct bpf_nh_params nh; > }; >=20=20 > -DECLARE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info); > +struct bpf_net_context { > + struct bpf_redirect_info ri; > +}; > + > +static inline struct bpf_net_context *bpf_net_ctx_set(struct bpf_net_con= text *bpf_net_ctx) > +{ > + struct task_struct *tsk =3D current; > + > + if (tsk->bpf_net_context !=3D NULL) > + return NULL; > + tsk->bpf_net_context =3D bpf_net_ctx; > + return bpf_net_ctx; > +} > + > +static inline void bpf_net_ctx_clear(struct bpf_net_context *bpf_net_ctx) > +{ > + if (bpf_net_ctx) > + current->bpf_net_context =3D NULL; > +} > + > +static inline struct bpf_net_context *bpf_net_ctx_get(void) > +{ > + return current->bpf_net_context; > +} > + > +static inline struct bpf_redirect_info *bpf_net_ctx_get_ri(void) > +{ > + struct bpf_net_context *bpf_net_ctx =3D bpf_net_ctx_get(); > + > + return &bpf_net_ctx->ri; > +} > + > +DEFINE_FREE(bpf_net_ctx_clear, struct bpf_net_context *, bpf_net_ctx_cle= ar(_T)); >=20=20 > /* flags for bpf_redirect_info kern_flags */ > #define BPF_RI_F_RF_NO_DIRECT BIT(0) /* no napi_direct on return_frame */ > @@ -1021,21 +1053,21 @@ void bpf_clear_redirect_map(struct bpf_map *map); >=20=20 > static inline bool xdp_return_frame_no_direct(void) > { > - struct bpf_redirect_info *ri =3D this_cpu_ptr(&bpf_redirect_info); > + struct bpf_redirect_info *ri =3D bpf_net_ctx_get_ri(); >=20=20 > return ri->kern_flags & BPF_RI_F_RF_NO_DIRECT; > } >=20=20 > static inline void xdp_set_return_frame_no_direct(void) > { > - struct bpf_redirect_info *ri =3D this_cpu_ptr(&bpf_redirect_info); > + struct bpf_redirect_info *ri =3D bpf_net_ctx_get_ri(); >=20=20 > ri->kern_flags |=3D BPF_RI_F_RF_NO_DIRECT; > } >=20=20 > static inline void xdp_clear_return_frame_no_direct(void) > { > - struct bpf_redirect_info *ri =3D this_cpu_ptr(&bpf_redirect_info); > + struct bpf_redirect_info *ri =3D bpf_net_ctx_get_ri(); >=20=20 > ri->kern_flags &=3D ~BPF_RI_F_RF_NO_DIRECT; > } > @@ -1591,7 +1623,7 @@ static __always_inline long __bpf_xdp_redirect_map(= struct bpf_map *map, u64 inde > u64 flags, const u64 flag_mask, > void *lookup_elem(struct bpf_map *map, u32 key)) > { > - struct bpf_redirect_info *ri =3D this_cpu_ptr(&bpf_redirect_info); > + struct bpf_redirect_info *ri =3D bpf_net_ctx_get_ri(); > const u64 action_mask =3D XDP_ABORTED | XDP_DROP | XDP_PASS | XDP_TX; >=20=20 > /* Lower bits of the flags are used as return code on lookup failure */ > diff --git a/include/linux/sched.h b/include/linux/sched.h > index 6779d3b8f2578..cc9be45de6606 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -53,6 +53,7 @@ struct bio_list; > struct blk_plug; > struct bpf_local_storage; > struct bpf_run_ctx; > +struct bpf_net_context; > struct capture_control; > struct cfs_rq; > struct fs_struct; > @@ -1504,6 +1505,8 @@ struct task_struct { > /* Used for BPF run context */ > struct bpf_run_ctx *bpf_ctx; > #endif > + /* Used by BPF for per-TASK xdp storage */ > + struct bpf_net_context *bpf_net_context; Okay, so if we are going the route of always putting this in 'current', why not just embed the whole struct bpf_net_context inside task_struct, instead of mucking about with the stack-allocated structures and setting/clearing of pointers? -Toke