From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3C6FC43381 for ; Sat, 2 Mar 2019 00:13:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 939722083D for ; Sat, 2 Mar 2019 00:13:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="LWLbKLfz"; dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="grgs0B5F" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726232AbfCBAND (ORCPT ); Fri, 1 Mar 2019 19:13:03 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:38378 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725966AbfCBAND (ORCPT ); Fri, 1 Mar 2019 19:13:03 -0500 Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x2205h4d030093; Fri, 1 Mar 2019 16:13:00 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=facebook; bh=2H5SV79lnF/DjZBJuG9aLG9YKOJ/Z1EjBNEOC6auNkc=; b=LWLbKLfzZTddm3Jhi8ANiTI5Wmd1QVN/L1jvAV/Ogl3S4Yo/CfgyMLefdL9L+MOCSH9M EgDp55gMq3/+xZN6/1b6h1GS0bHKwfIPSZe1UVXqhRsmOuWpPXnnpMwnM7bmPgBZ+2xd SUGI2/UCKSHviqag5T0ZpKIeTFVznHk6dwk= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2qycv50e0p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Fri, 01 Mar 2019 16:13:00 -0800 Received: from prn-mbx02.TheFacebook.com (2620:10d:c081:6::16) by prn-hub04.TheFacebook.com (2620:10d:c081:35::128) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.1.1713.5; Fri, 1 Mar 2019 16:12:59 -0800 Received: from prn-hub04.TheFacebook.com (2620:10d:c081:35::128) by prn-mbx02.TheFacebook.com (2620:10d:c081:6::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.1.1713.5; Fri, 1 Mar 2019 16:12:59 -0800 Received: from NAM04-BN3-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.1.1713.5 via Frontend Transport; Fri, 1 Mar 2019 16:12:59 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2H5SV79lnF/DjZBJuG9aLG9YKOJ/Z1EjBNEOC6auNkc=; b=grgs0B5Fx4YI/OegQcIeYz2eUHABKZ08lq5Y2cW8sujIkLABuhIRQX1VoLZx+Npk/HIMk7EoDt7fhh/UrT30mHMUqDlkAJVIeZ0uQWe3SN752OiWaKE38YX229cXpr46pkqg965oORJpX2zW3ZwU0LgUxhUqOyeLKQhe+ckcnSY= Received: from DM5PR15MB1788.namprd15.prod.outlook.com (10.174.246.146) by DM5PR15MB1483.namprd15.prod.outlook.com (10.173.225.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1643.18; Sat, 2 Mar 2019 00:12:57 +0000 Received: from DM5PR15MB1788.namprd15.prod.outlook.com ([fe80::9de3:2a66:ff8a:997c]) by DM5PR15MB1788.namprd15.prod.outlook.com ([fe80::9de3:2a66:ff8a:997c%4]) with mapi id 15.20.1643.019; Sat, 2 Mar 2019 00:12:57 +0000 From: Martin Lau To: Javier Honduvilla Coto CC: "netdev@vger.kernel.org" , Yonghong Song , Kernel Team , Javier Honduvilla Coto Subject: Re: [PATCH v2 bpf-next 1/3] bpf: add bpf_progenyof helper Thread-Topic: [PATCH v2 bpf-next 1/3] bpf: add bpf_progenyof helper Thread-Index: AQHU0FmfAabuD/Gm8k6FdUSidw8+QKX3eKkA Date: Sat, 2 Mar 2019 00:12:57 +0000 Message-ID: <20190302001254.5t3in6ctzkwq7hwm@kafai-mbp.dhcp.thefacebook.com> References: <20190226223651.3166820-2-javierhonduco@fb.com> <20190301180614.4134278-1-javierhonduco@fb.com> <20190301180614.4134278-2-javierhonduco@fb.com> In-Reply-To: <20190301180614.4134278-2-javierhonduco@fb.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BYAPR04CA0027.namprd04.prod.outlook.com (2603:10b6:a03:40::40) To DM5PR15MB1788.namprd15.prod.outlook.com (2603:10b6:4:4d::18) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2620:10d:c090:200::2:9b14] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 0bedfa87-12df-43bb-e9c4-08d69ea3d2b1 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600127)(711020)(4605104)(2017052603328)(7153060)(7193020);SRVR:DM5PR15MB1483; x-ms-traffictypediagnostic: DM5PR15MB1483: x-microsoft-exchange-diagnostics: 1;DM5PR15MB1483;20:BX7Kun9LivXF1l2cWaOBJt2leke3LxYjJV5MxFfJL18/1r5iJsoGqW8uzYelkLWu23RRfM/gAwcIPWNshPc6qCqTcaZfWWfWag4YjhNPsOAMfyneYGkhmfXFENvWLlP6OwOiOj/kI/W176yhMGfpvRByKY0ColFYkHFAeZdCxsQ= x-microsoft-antispam-prvs: x-forefront-prvs: 09645BAC66 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(376002)(39860400002)(366004)(346002)(136003)(189003)(199004)(46003)(105586002)(476003)(6636002)(4326008)(5024004)(6506007)(14444005)(386003)(9686003)(6512007)(25786009)(53936002)(305945005)(99286004)(486006)(256004)(71190400001)(8936002)(97736004)(229853002)(102836004)(54906003)(14454004)(11346002)(6246003)(76176011)(186003)(6116002)(2906002)(86362001)(68736007)(7736002)(5660300002)(6862004)(71200400001)(446003)(1076003)(6486002)(52116002)(316002)(6436002)(106356001)(478600001)(8676002)(81166006)(81156014)(21314003);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR15MB1483;H:DM5PR15MB1788.namprd15.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: fb.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: dMXla4MD6x8V043GnslHHBu7cLThndwf7ISNSQcz2VWXOpKoHQtLEDXJD+3pUCtKiC4TobkFxFXRkMiPPuU/KMobEmh0Q48rP8i7B3JOcSamLZwXezisJgC4vZE123sAjuU27PWFCHUExCav3V0Jne4N25a6ZwWkNq/Y7NWGGzwP5V6NU/aEg1DMwz1Pam84LB3Px2RJmUzSRLz00zBHupNdxg206PbcCWcz4RloIwT1gX9+N2DvT75+YKbOmcWgrfSqIOFYHlE6shUuwagkhU5sQVSEGodLzQaoSdPqOF+qxuRnu1Zb9HRF5XLDlhyIiPtFR4RPGZhg0TKsA13w+eyXO4RN/+ad8IxMenKQmdcRrJCL1OB7qOnHVphjyWklfa1w2Vsuf0Mh1TPicymg0Xt2vNnfCvoIh5uJEJjrWxQ= Content-Type: text/plain; charset="us-ascii" Content-ID: <6246C11A43067741A4BD21AB071D4980@namprd15.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 0bedfa87-12df-43bb-e9c4-08d69ea3d2b1 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2019 00:12:56.4901 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR15MB1483 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-03-01_16:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Fri, Mar 01, 2019 at 10:06:12AM -0800, Javier Honduvilla Coto wrote: > From: Javier Honduvilla Coto >=20 > This patch adds the bpf_progenyof helper which receives a PID and returns > 1 if the process currently being executed is in the process hierarchy > including itself or 0 if not. >=20 > This is very useful in tracing programs when we want to filter by a > given PID and all the children it might spawn. The current workarounds > most people implement for this purpose have issues: >=20 > - Attaching to process spawning syscalls and dynamically add those PIDs > to some bpf map that would be used to filter is cumbersome and > potentially racy. > - Unrolling some loop to perform what this helper is doing consumes lots > of instructions. That and the impossibility to jump backwards makes it > really hard to be correct in really large process chains. >=20 > Signed-off-by: Javier Honduvilla Coto > --- > include/linux/bpf.h | 1 + > include/uapi/linux/bpf.h | 11 ++++++++++- > kernel/bpf/core.c | 1 + > kernel/bpf/helpers.c | 29 +++++++++++++++++++++++++++++ > kernel/trace/bpf_trace.c | 2 ++ > 5 files changed, 43 insertions(+), 1 deletion(-) >=20 > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > index de18227b3d95..447395ba202b 100644 > --- a/include/linux/bpf.h > +++ b/include/linux/bpf.h > @@ -921,6 +921,7 @@ extern const struct bpf_func_proto bpf_sk_redirect_ma= p_proto; > extern const struct bpf_func_proto bpf_spin_lock_proto; > extern const struct bpf_func_proto bpf_spin_unlock_proto; > extern const struct bpf_func_proto bpf_get_local_storage_proto; > +extern const struct bpf_func_proto bpf_progenyof_proto; > =20 > /* Shared helpers among cBPF and eBPF. */ > void bpf_user_rnd_init_once(void); > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index bcdd2474eee7..354ec295864c 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -2359,6 +2359,14 @@ union bpf_attr { > * Return > * A **struct bpf_tcp_sock** pointer on success, or NULL in > * case of failure. > + * > + * int bpf_progenyof(int pid) > + * Description > + * This helper is useful in programs that want to filter events > + * happening to a pid of any of its descendants. > + * Return > + * 1 if the currently executing process' pid is in the process > + * hierarchy of the passed pid. 0 Otherwise. > */ > #define __BPF_FUNC_MAPPER(FN) \ > FN(unspec), \ > @@ -2457,7 +2465,8 @@ union bpf_attr { > FN(spin_lock), \ > FN(spin_unlock), \ > FN(sk_fullsock), \ > - FN(tcp_sock), > + FN(tcp_sock), \ > + FN(progenyof), > =20 > /* integer value in 'imm' field of BPF_CALL instruction selects which he= lper > * function eBPF program intends to call > diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c > index ef88b167959d..69e209fbd128 100644 > --- a/kernel/bpf/core.c > +++ b/kernel/bpf/core.c > @@ -2015,6 +2015,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid= _proto __weak; > const struct bpf_func_proto bpf_get_current_comm_proto __weak; > const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak; > const struct bpf_func_proto bpf_get_local_storage_proto __weak; > +const struct bpf_func_proto bpf_progenyof_proto __weak; > =20 > const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void) > { > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index a411fc17d265..3899787e8dbf 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -18,6 +18,7 @@ > #include > #include > #include > +#include > =20 > /* If kernel subsystem is allowing eBPF programs to call this function, > * inside its own verifier_ops->get_func_proto() callback it should retu= rn > @@ -364,3 +365,31 @@ const struct bpf_func_proto bpf_get_local_storage_pr= oto =3D { > }; > #endif > #endif > + > +BPF_CALL_1(bpf_progenyof, int, pid) > +{ > + int result =3D 0; > + struct task_struct *task =3D current; > + > + if (unlikely(!task)) > + return -EINVAL; > + > + rcu_read_lock(); I don't think it is needed. It should already be under rcu_read_lock(). You may add 'WARN_ON_ONCE(!rcu_read_lock_held());' to be safe, like other "bpf_map_xxx_elem()" helpers in the same file. > + while (task !=3D &init_task) { > + if (task->pid =3D=3D pid) { > + result =3D 1; > + break; > + } > + task =3D rcu_dereference(task->real_parent); > + } > + rcu_read_unlock(); > + > + return result; > +} > + > +const struct bpf_func_proto bpf_progenyof_proto =3D { > + .func =3D bpf_progenyof, > + .gpl_only =3D false, > + .ret_type =3D RET_INTEGER, > + .arg1_type =3D ARG_ANYTHING, > +}; > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index f1a86a0d881d..8602ae83c799 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -600,6 +600,8 @@ tracing_func_proto(enum bpf_func_id func_id, const st= ruct bpf_prog *prog) > return &bpf_get_prandom_u32_proto; > case BPF_FUNC_probe_read_str: > return &bpf_probe_read_str_proto; > + case BPF_FUNC_progenyof: > + return &bpf_progenyof_proto; > #ifdef CONFIG_CGROUPS > case BPF_FUNC_get_current_cgroup_id: > return &bpf_get_current_cgroup_id_proto; > --=20 > 2.17.1 >=20