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 BAEDCC43381 for ; Sat, 2 Mar 2019 01:11:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6FC4420848 for ; Sat, 2 Mar 2019 01:11:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="pQe+HhF1"; dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="KXh3U5ht" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726412AbfCBBLF (ORCPT ); Fri, 1 Mar 2019 20:11:05 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:34222 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725996AbfCBBLE (ORCPT ); Fri, 1 Mar 2019 20:11:04 -0500 Received: from pps.filterd (m0001255.ppops.net [127.0.0.1]) by mx0b-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x220voD3014001; Fri, 1 Mar 2019 17:11:03 -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=XlSn/uonOc3HQeAaA+7sifuIhts4RFVLVXGn3Fu+dLs=; b=pQe+HhF1jUKpq6ZSCy8f5bJ4cZic9tR6cuaJpSsgW4sbzpijNWLpBBN4w+NrDR3Fdglj W8q47KqzDFnEACcdzsI3lmY3NmtB/xUXMZ4l2iCbPh42th1Ov1fZM2xkb9RojIurQN2U QhHoFc+9ZBCCN1xOgFYHMpZEyxblTpwtKvQ= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 2qyebf08u8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Fri, 01 Mar 2019 17:11:03 -0800 Received: from prn-mbx03.TheFacebook.com (2620:10d:c081:6::17) by prn-hub06.TheFacebook.com (2620:10d:c081:35::130) 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 17:11:01 -0800 Received: from prn-hub03.TheFacebook.com (2620:10d:c081:35::127) by prn-mbx03.TheFacebook.com (2620:10d:c081:6::17) 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 17:11:01 -0800 Received: from NAM04-SN1-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.27) 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 17:11:01 -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=XlSn/uonOc3HQeAaA+7sifuIhts4RFVLVXGn3Fu+dLs=; b=KXh3U5htHb2mZtg09IoKF3x97WXGWRZwvNlytc52hWX3CQSfIu56rGwWOrgQt0aUGC1GR6GRJhAGUutP4W6n+Anipe7lneqXdgNKrR46l77UqM3kpOCi+VW+LRk6naCXQIxb90Rxx9u8XSo9yktYKHH40brnM3pNuvHFs00ldRs= Received: from MN2PR15MB2656.namprd15.prod.outlook.com (20.179.146.26) by MN2PR15MB3341.namprd15.prod.outlook.com (20.179.22.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1665.15; Sat, 2 Mar 2019 01:10:59 +0000 Received: from MN2PR15MB2656.namprd15.prod.outlook.com ([fe80::bcca:6d5a:ead7:9321]) by MN2PR15MB2656.namprd15.prod.outlook.com ([fe80::bcca:6d5a:ead7:9321%5]) with mapi id 15.20.1643.022; Sat, 2 Mar 2019 01:10:59 +0000 From: Javier Honduvilla Coto To: Martin Lau 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+QKX3eKkAgAAQM4A= Date: Sat, 2 Mar 2019 01:10:59 +0000 Message-ID: <20190302011053.GB25319@fb.com> References: <20190226223651.3166820-2-javierhonduco@fb.com> <20190301180614.4134278-1-javierhonduco@fb.com> <20190301180614.4134278-2-javierhonduco@fb.com> <20190302001254.5t3in6ctzkwq7hwm@kafai-mbp.dhcp.thefacebook.com> In-Reply-To: <20190302001254.5t3in6ctzkwq7hwm@kafai-mbp.dhcp.thefacebook.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: LO2P265CA0010.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:62::22) To MN2PR15MB2656.namprd15.prod.outlook.com (2603:10b6:208:128::26) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2620:10d:c092:180::1:9e23] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 992a372b-b855-4bb7-6373-08d69eabee09 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600127)(711020)(4605104)(2017052603328)(7153060)(7193020);SRVR:MN2PR15MB3341; x-ms-traffictypediagnostic: MN2PR15MB3341: x-microsoft-exchange-diagnostics: 1;MN2PR15MB3341;20:jrVZN915f1mZPt2oRgeAvhzerThCCozebEaXLy/4GMo1kY0yzOG3yRpNdAn1d91I8GZQ9FAzKxFdjrgYOlFDb0lpRfSxeAOaX6n5DPZXLOTeiDNy63Sy96FCMpGUJzRhHG3gPLyuYjRUrXY0eLcH5ppaD3ovE7PGluvJn+5T84I= x-microsoft-antispam-prvs: x-forefront-prvs: 09645BAC66 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(136003)(376002)(346002)(39860400002)(366004)(189003)(199004)(486006)(8676002)(6506007)(68736007)(2616005)(86362001)(76176011)(11346002)(81166006)(81156014)(105586002)(446003)(5660300002)(386003)(316002)(6512007)(106356001)(53936002)(6116002)(476003)(46003)(36756003)(97736004)(6436002)(7736002)(2906002)(25786009)(6246003)(37006003)(186003)(54906003)(6486002)(4326008)(6636002)(229853002)(8936002)(478600001)(93886005)(6862004)(1076003)(33656002)(256004)(102836004)(14444005)(14454004)(5024004)(71190400001)(71200400001)(52116002)(99286004)(305945005)(21314003);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR15MB3341;H:MN2PR15MB2656.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: i0AQrWle2z2E/Q5IgdqAQHK90doPSn4/K1McTVc4FLQYPa4ypC2oJawhndn/t3tPyA4KOm2FsOKfqwkBVvNvjLQHIWapnuSrV7gLUnn9juWKmasS5qea5QMHAq4RmRRtkSEse456a7BeKd9uGBUQlhs08xDALmHKCdvw2/Qfz4CnJ/nLSqe5kfO+I5lccMHVSUVplENuNnmHWXutcSOdwCQCcMQWE6ly9L5TVb5Zu4e1HOBYn2bkPvuF12vndS2tSKnUyJW5uh3tmi0ozUHE58myqapYXxuEaniEjJ/XFLcIoyPKOrKUFnYyYhb6ZlALJ9la2sBWt5iZnQFtHggzZvbQW3bolpX96gVTUOgqyj41FA9ryAqS0LoVT9g9IcQMnk3itQ1OIcXV//H6h6gdNRrsX5VkfNQDWTJTmP/pvi8= Content-Type: text/plain; charset="us-ascii" Content-ID: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 992a372b-b855-4bb7-6373-08d69eabee09 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2019 01:10:58.3568 (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: MN2PR15MB3341 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 Sat, Mar 02, 2019 at 12:12:57AM +0000, Martin Lau wrote: > On Fri, Mar 01, 2019 at 10:06:12AM -0800, Javier Honduvilla Coto wrote: > > From: Javier Honduvilla Coto > > > > This patch adds the bpf_progenyof helper which receives a PID and retur= ns > > 1 if the process currently being executed is in the process hierarchy > > including itself or 0 if not. > > > > 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: > > > > - 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 lot= s > > of instructions. That and the impossibility to jump backwards makes i= t > > really hard to be correct in really large process chains. > > > > 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(-) > > > > 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_= map_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; > > > > /* 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), > > > > /* integer value in 'imm' field of BPF_CALL instruction selects which = helper > > * 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_g= id_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; > > > > 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 > > > > /* If kernel subsystem is allowing eBPF programs to call this function= , > > * inside its own verifier_ops->get_func_proto() callback it should re= turn > > @@ -364,3 +365,31 @@ const struct bpf_func_proto bpf_get_local_storage_= proto =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. > Spot on! Just checked that we are rcu locking in include/linux/bpf.h being called kernel/trace/bpf_trace.c. Thanks!! > > + 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 = struct 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; > > -- > > 2.17.1 > >