Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1591376pxb; Wed, 10 Feb 2021 11:56:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJxBHRAsFbeh3kIxXO7ako7kTCQ6P1RgSiDzdAQefF1U7LdG8BAs4K739aCOSjxMU6rtpqY9 X-Received: by 2002:a17:906:fc5:: with SMTP id c5mr4486935ejk.538.1612986983414; Wed, 10 Feb 2021 11:56:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612986983; cv=none; d=google.com; s=arc-20160816; b=fXzNm8Mjl0/DK9nrhXmQZL+KK1E4xaP04QYZskyuaITjZvz2AHh9KJrzMEjLBYY6Nn lOCLqFtGvebJ2+7RDAihiyTM9vM5zr+QTVuR8wSVHUTisLkPODQMvI9LUCLx/fFJNOFu +qGVqGX0Ve1O7Fx++bWut/7TD8qy4LO28Hp0tw0WhLye5VgBF8uwjnPMICPVzuUTAV1J TUIhWf/pVAWgS2ne5N4WzN19OjHmkPLBrzoTsndkKFpvcf4piDzdMnAI/Dj4R7b7w0ZO BMNjs+lrEhSu4ew2RUgG4YCQ1FcdTMhBlJiR8ZXdDqOHmSRX7cPaXtdNbQ9t7UOcMuND j9bg== 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=s4RdDmXskdI19A4WAwFntV8dNBLK9hF7TdOLyP5FEbc=; b=TWXc5kMaZ97oDB4bpKfpR0Isvm65JEwCY5opB6TszwXCdMfgXvO3+oAULTwUWDEt9C TvWprujz0Z4fdHTSUt80bZAZ/Z5tXMJyyYkxuW2PqwuJEbQLamIg90R9/Wm/d73aix6q IEZW0vfArSFiX7zeYzwb0REbhj2QfsjuVA4HhGwyDZOJqWVaYbYA+wVkg4upgOa4PGaM R4pe5i/ZPkPk+CUukiuqow70eXuOKs9vamICdWe7nuO0sGS3ZiTPPgQI5+LUTYe7rzuy Fdbk6mdZznA19XTkS3VwfdELkLU3UTNxGiZZOu2B0XdeV+EIOAg059PSKr1J37qnuVKG YwPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=a2QbyP+x; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m9si1977164edj.103.2021.02.10.11.55.59; Wed, 10 Feb 2021 11:56:23 -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=@gmail.com header.s=20161025 header.b=a2QbyP+x; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232884AbhBJTw7 (ORCPT + 99 others); Wed, 10 Feb 2021 14:52:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232361AbhBJTwn (ORCPT ); Wed, 10 Feb 2021 14:52:43 -0500 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76AE8C061786; Wed, 10 Feb 2021 11:52:03 -0800 (PST) Received: by mail-yb1-xb33.google.com with SMTP id l8so3198500ybe.12; Wed, 10 Feb 2021 11:52:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=s4RdDmXskdI19A4WAwFntV8dNBLK9hF7TdOLyP5FEbc=; b=a2QbyP+xPg/eXRgAepxs4yYO+n7tR5Gc6rLZOQeXxIXU0zNdnJCz//6bCvewiCejso Hvfx66Mbfu5Cwy1Og8EpFX9uOlnIdHwkO8aa4JYgWzhfs2TBHfqfNMW2BYRdj//+V88b gKYKhDJMoSrzaXuMCK4qusugPvtIbskOC6TSjjbcwz6QgN6DxXehKIrP0lyeR6Om0KQb eBAXuClRVHkLEo5g6y30e2/1bVkkP0nyHDDDtk4MdlZ2TGB65qOorF2vBr92V8yYQ81a 0HrJcGGT/kYIaonFe5HquC906gb/ZQqImygDZw/VdNWtjhIq2izPRZ7XGpFd5DeeBc2y c1zQ== 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; bh=s4RdDmXskdI19A4WAwFntV8dNBLK9hF7TdOLyP5FEbc=; b=Ys2GeopME5APUaMVkXP/UJA5k+EMtkDlINSHLG4iS6lqk8cYx/LbfcJWD1NwJNSiiy YoFb0YN2KeAUCCtLeLGz8wzbqGtqeKFe6yxnJNU9VPQz6Frlr4IQlWEn8PMb7YHO2RgS cq32UOk8BFcXdhbm0xWyYMnR7ON3vKKUC2aJKNp+gv1ODDgrRQK7TX47vn//WFiVOALA c9PbgYSBn1PnhSggTwPsWWncnxI5SlrCqisKu/0yi9RIekPpICa1mQI9MZCVRp3PiMja wYlSKy7mMyj7Lnlpg/Dcj9VcPtPJW7RuVM4cGrKo9ZZNCB/Wgrk+rFzRDBizv4fueiRV ywWw== X-Gm-Message-State: AOAM530rz6aygxNlvEv4WDJ2ecPNmJtqXmTLub+bpzJaQ1tOMQd41dJK phkSf/rKmu+nxDftC53l32dL7Mmb3o76rj94zhCO01D3k/VhzQ== X-Received: by 2002:a25:a183:: with SMTP id a3mr6592411ybi.459.1612986722643; Wed, 10 Feb 2021 11:52:02 -0800 (PST) MIME-Version: 1.0 References: <20210210111406.785541-1-revest@chromium.org> <20210210111406.785541-2-revest@chromium.org> In-Reply-To: <20210210111406.785541-2-revest@chromium.org> From: Andrii Nakryiko Date: Wed, 10 Feb 2021 11:51:51 -0800 Message-ID: Subject: Re: [PATCH bpf-next v7 2/5] bpf: Expose bpf_get_socket_cookie to tracing programs To: Florent Revest Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , KP Singh , Florent Revest , Brendan Jackman , open list , KP Singh Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 10, 2021 at 3:14 AM Florent Revest wrote: > > This needs a new helper that: > - can work in a sleepable context (using sock_gen_cookie) > - takes a struct sock pointer and checks that it's not NULL > > Signed-off-by: Florent Revest > Acked-by: KP Singh > --- It's customary to send cover letter with patch sets of 2 or more related patches. It's a good place to explain the motivation of a patch set. And a good place to ack all patches in one go ;) Acked-by: Andrii Nakryiko > include/linux/bpf.h | 1 + > include/uapi/linux/bpf.h | 8 ++++++++ > kernel/trace/bpf_trace.c | 2 ++ > net/core/filter.c | 12 ++++++++++++ > tools/include/uapi/linux/bpf.h | 8 ++++++++ > 5 files changed, 31 insertions(+) > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > index 321966fc35db..d212ae7d9731 100644 > --- a/include/linux/bpf.h > +++ b/include/linux/bpf.h > @@ -1888,6 +1888,7 @@ extern const struct bpf_func_proto bpf_per_cpu_ptr_proto; > extern const struct bpf_func_proto bpf_this_cpu_ptr_proto; > extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto; > extern const struct bpf_func_proto bpf_sock_from_file_proto; > +extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto; > > const struct bpf_func_proto *bpf_tracing_func_proto( > enum bpf_func_id func_id, const struct bpf_prog *prog); > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index 0b735c2729b2..a8d9ad543300 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -1673,6 +1673,14 @@ union bpf_attr { > * Return > * A 8-byte long unique number. > * > + * u64 bpf_get_socket_cookie(struct sock *sk) > + * Description > + * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts > + * *sk*, but gets socket from a BTF **struct sock**. This helper > + * also works for sleepable programs. > + * Return > + * A 8-byte long unique number or 0 if *sk* is NULL. > + * > * u32 bpf_get_socket_uid(struct sk_buff *skb) > * Return > * The owner UID of the socket associated to *skb*. If the socket > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index 6c0018abe68a..845b2168e006 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -1760,6 +1760,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) > return &bpf_sk_storage_delete_tracing_proto; > case BPF_FUNC_sock_from_file: > return &bpf_sock_from_file_proto; > + case BPF_FUNC_get_socket_cookie: > + return &bpf_get_socket_ptr_cookie_proto; > #endif > case BPF_FUNC_seq_printf: > return prog->expected_attach_type == BPF_TRACE_ITER ? > diff --git a/net/core/filter.c b/net/core/filter.c > index e15d4741719a..57aaed478362 100644 > --- a/net/core/filter.c > +++ b/net/core/filter.c > @@ -4631,6 +4631,18 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = { > .arg1_type = ARG_PTR_TO_CTX, > }; > > +BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk) > +{ > + return sk ? sock_gen_cookie(sk) : 0; > +} > + > +const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = { > + .func = bpf_get_socket_ptr_cookie, > + .gpl_only = false, > + .ret_type = RET_INTEGER, > + .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON, > +}; > + > BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx) > { > return __sock_gen_cookie(ctx->sk); > diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h > index 0b735c2729b2..a8d9ad543300 100644 > --- a/tools/include/uapi/linux/bpf.h > +++ b/tools/include/uapi/linux/bpf.h > @@ -1673,6 +1673,14 @@ union bpf_attr { > * Return > * A 8-byte long unique number. > * > + * u64 bpf_get_socket_cookie(struct sock *sk) > + * Description > + * Equivalent to **bpf_get_socket_cookie**\ () helper that accepts > + * *sk*, but gets socket from a BTF **struct sock**. This helper > + * also works for sleepable programs. > + * Return > + * A 8-byte long unique number or 0 if *sk* is NULL. > + * > * u32 bpf_get_socket_uid(struct sk_buff *skb) > * Return > * The owner UID of the socket associated to *skb*. If the socket > -- > 2.30.0.478.g8a0d178c01-goog >