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 909B1C43381 for ; Sat, 2 Mar 2019 00:01:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 411D920823 for ; Sat, 2 Mar 2019 00:01:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="HsulIxa0"; dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="G5KLXq4T" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726982AbfCBABS (ORCPT ); Fri, 1 Mar 2019 19:01:18 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:33324 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725934AbfCBABS (ORCPT ); Fri, 1 Mar 2019 19:01:18 -0500 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.27/8.16.0.27) with SMTP id x21NuROK004942 for ; Fri, 1 Mar 2019 16:01:17 -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=c0XKMH4nF1Y5FwQgq3HTW6f065fWBsJMAhrjfwTLrfQ=; b=HsulIxa03SoSVOtuVbbnpIt0foYCF2u0+45FeeA/HHbXq8rxWcFEYEejm75A1Em8H1zc H3xlDaB2IR37ejvBsRvFZKe/LC7EYyQH5E3iRlIBiOSwhq8bQ+Mw0sX0BH0faP2jYPCC /RJqGcPMYd4xgyE+v+7Pi0vlCGmvO4v4G6c= Received: from mail.thefacebook.com ([199.201.64.23]) by m0001303.ppops.net with ESMTP id 2qyefh02j4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 01 Mar 2019 16:01:17 -0800 Received: from prn-hub02.TheFacebook.com (2620:10d:c081:35::126) by prn-hub01.TheFacebook.com (2620:10d:c081:35::125) 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:01:15 -0800 Received: from NAM03-CO1-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.26) 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:01:15 -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=c0XKMH4nF1Y5FwQgq3HTW6f065fWBsJMAhrjfwTLrfQ=; b=G5KLXq4TKUw4qxT0zMq0weqOQaV6TBf10aN9mFv9cFuYyQDoJytc2uZfa8mapJy499A1WMzT2SDiWD5D78Az9B/77zqyWyYE7WArsMirl8hqdkGgU4xXfQlSOCGaLpfCnsmLIJGZ8YUEKLJwqoQssZfx5IeiOEOVRPAYBF+T3jk= Received: from DM5PR15MB1788.namprd15.prod.outlook.com (10.174.246.146) by DM5PR15MB1946.namprd15.prod.outlook.com (10.173.215.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1665.16; Sat, 2 Mar 2019 00:01:14 +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:01:14 +0000 From: Martin Lau To: Javier Honduvilla Coto CC: "netdev@vger.kernel.org" , Yonghong Song , Kernel Team Subject: Re: [PATCH bpf-next 1/3] bpf: add bpf_progenyof helper Thread-Topic: [PATCH bpf-next 1/3] bpf: add bpf_progenyof helper Thread-Index: AQHUziPfqF8M0tD5ZkW/n/TDI83+WKXzLoOAgAPdkACAAG27gA== Date: Sat, 2 Mar 2019 00:01:14 +0000 Message-ID: <20190302000111.xkkpxa3r36qy62rm@kafai-mbp.dhcp.thefacebook.com> References: <20190226223651.3166820-1-javierhonduco@fb.com> <20190226223651.3166820-2-javierhonduco@fb.com> <20190227062639.jpzwmm63iygad5f7@kafai-mbp.dhcp.thefacebook.com> <20190301172826.GA99700@fb.com> In-Reply-To: <20190301172826.GA99700@fb.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MWHPR0201CA0008.namprd02.prod.outlook.com (2603:10b6:301:74::21) 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: 6d2c5293-dfe8-4137-8ffe-08d69ea22f80 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600127)(711020)(4605104)(2017052603328)(7153060)(7193020);SRVR:DM5PR15MB1946; x-ms-traffictypediagnostic: DM5PR15MB1946: x-microsoft-exchange-diagnostics: 1;DM5PR15MB1946;20:4cn6RCnR24WVvfri0n+nH//SklQvz66JBASFEKLzjqtuzope2Xs2sfOIQLm+Tf9ykYcigX+3nJFgBqI0UDoHIXdWLCdAfFtJV/kuTj3LXIjtnOCDfM/aqumkUuz7Tg4Rsu45zW7m5crGn/nbbMhO2k5q2aM7JYwfTiaB4HuwPGU= x-microsoft-antispam-prvs: x-forefront-prvs: 09645BAC66 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(39860400002)(376002)(366004)(136003)(346002)(199004)(189003)(8936002)(46003)(97736004)(102836004)(6486002)(9686003)(6436002)(76176011)(229853002)(2906002)(7736002)(386003)(6506007)(52116002)(71190400001)(71200400001)(6512007)(316002)(68736007)(54906003)(8676002)(81156014)(105586002)(106356001)(6636002)(81166006)(6116002)(11346002)(14444005)(5024004)(93886005)(6862004)(14454004)(446003)(6246003)(1076003)(256004)(186003)(476003)(486006)(25786009)(478600001)(305945005)(5660300002)(86362001)(53936002)(4326008)(99286004);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR15MB1946;H:DM5PR15MB1788.namprd15.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX: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: ybfhVn8V6pA4zBSU9zE/mDyxtUvYnrb0U/e3wgWkoflo/nM66yyqxMJLsK4+6yq+FK7ll0k1nvyivnkcaQpza15I2Mtos2DfV2dQ9JcoxMOzRtnNAclXWbbCrXuogSrCV7YZ+vezVz/iKryGddCPQtaWcA3xzvIh74Bq6oy03mWFM6goG/tLfO4Wlv5a5pD2YGQMFrRuy0dft0/pIQnf7zRtnASeA+mzk3nI029Qi/AoV/FsRzCITngKOxMXPVV7ZI+F429yRyu9JDw+XJbnSWCKYNzs1+v6VNV77kIKT0QRfN/ZhYDYkdJx7ucdU68r8kFwRJ9ymp2hB89AMhgdD2Bh8VHOLxM6LLxeKTYo9zVSwrjczUIL+gX2RBUG2w6BKjOuSpIjMuoHkubZO4FMiqGsnJN08ydHMBgEp1DQ/AQ= Content-Type: text/plain; charset="us-ascii" Content-ID: <64A66A31644BF84EAB330E912143852D@namprd15.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 6d2c5293-dfe8-4137-8ffe-08d69ea22f80 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2019 00:01:13.4465 (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: DM5PR15MB1946 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 09:28:39AM -0800, Javier Honduvilla Coto wrote: > On Wed, Feb 27, 2019 at 06:26:41AM +0000, Martin Lau wrote: > > On Tue, Feb 26, 2019 at 02:36:49PM -0800, Javier Honduvilla Coto wrote: > > > This patch adds the bpf_progenyof helper which receives a PID and ret= urns > > What is progenof? > > > > > 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 workaround= s > > > most people implement for this purpose have issues: > > > > > > - Attaching to process spawning syscalls and dynamically add those PI= Ds > > > 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 l= ots > > > of instructions. That and the impossibility to jump backwards makes= it > > > 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 | 3 ++- > > > kernel/bpf/core.c | 1 + > > > kernel/bpf/helpers.c | 29 +++++++++++++++++++++++++++++ > > > kernel/trace/bpf_trace.c | 2 ++ > > > 5 files changed, 35 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_redirec= t_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; > > It seems only used in bpf_trace.c. Does it have to be here? > > > > > > > > /* 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..804e4218eb28 100644 > > > --- a/include/uapi/linux/bpf.h > > > +++ b/include/uapi/linux/bpf.h > > > @@ -2457,7 +2457,8 @@ union bpf_attr { > > > FN(spin_lock), \ > > > FN(spin_unlock), \ > > > FN(sk_fullsock), \ > > > - FN(tcp_sock), > > > + FN(tcp_sock), \ > > > + FN(progenyof), > > Please add doc like other helpers do. >=20 > Oops, good catch, thanks! Will send v2 soon!! >=20 > > > > > > > > /* integer value in 'imm' field of BPF_CALL instruction selects whic= h 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= _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; > > > > > > 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 functi= on, > > > * inside its own verifier_ops->get_func_proto() callback it should = return > > > @@ -364,3 +365,31 @@ const struct bpf_func_proto bpf_get_local_storag= e_proto =3D { > > > }; > > > #endif > > > #endif > > > + > > > +BPF_CALL_1(bpf_progenyof, int, pid) > > > +{ > > > + int result =3D 0; > > > + struct task_struct *task =3D current; > > > + > > > + if (unlikely(!task)) > > hmm.... Could current be NULL? >=20 > Wasn't sure about this but added as bpf_get_current_pid_tgid, > bpf_get_current_uid_gid, and bpf_get_current_comm check for this. Texted = Alexei > about this and he told me this is probably not necessary anymore, but I > guess it doesn't hurt leaving it? >=20 > > > > > + return -EINVAL; > > > + > > > + rcu_read_lock(); > > > + while (task !=3D &init_task) { > > I don't know the details of init_task, so qq: > > Could the passed in "pid" be the init_task->pid? > > If possible, what is the expected "result"? > > >=20 > Yep! init_task doesn't set a pid for what I could see, so I guess it > will be PID=3D0. The test in the last patch check bpf_progenyof(0) :) >=20 > bpf_progenyof with 0 or 1 will always return 1 the test in patch 3 commit message has this though: "- progenyof(0) =3D=3D 0" so the intention for progenyof(0) is to always return 0 or 1? A random ps output from my vm: [root@arch-fb-vm1 bpf]# ps -eaf | head -3 UID PID PPID C STIME TTY TIME CMD root 1 0 0 11:45 ? 00:00:12 /sbin/init root 2 0 0 11:45 ? 00:00:00 [kthreadd] I was asking because, after reading the loop, it seems all tasks tracing back to init_task. so my intuitive thinking is progenyof(init_task.pid) should always return 1. If it is otherwise, some comments and doc would be useful to explain why treating init_task.pid differently. >=20 > > > + 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, cons= t 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 > > >