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 1418EC43381 for ; Fri, 1 Mar 2019 17:29:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C7A5420818 for ; Fri, 1 Mar 2019 17:29:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="awOBRJ0S"; dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="bpujuvmZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388433AbfCAR3Z (ORCPT ); Fri, 1 Mar 2019 12:29:25 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:45356 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727951AbfCAR3Z (ORCPT ); Fri, 1 Mar 2019 12:29:25 -0500 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x21HPCVR029559 for ; Fri, 1 Mar 2019 09:29:24 -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=j5KroyBFE4eAXZ2LzJbTnXET4ZFDl4VCWTLQBBDx2Uc=; b=awOBRJ0SyYHhR25tBj6RISdhFsulDSSXtBgcehTtcz/xPQb1sjIkNBnWpjEAzcZDuBJ+ 1dPTlAkjO+/WujieDcVdqoD36ZQX0i33o08y/IOjdzEzgDVFs4tnlIlPp57aPjVPqxQC MeSiyhyYQrqdBUoS7qNKzte4yXjHue3kes8= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2qy80w893r-7 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 01 Mar 2019 09:29:23 -0800 Received: from frc-hub05.TheFacebook.com (2620:10d:c021:18::175) by frc-hub06.TheFacebook.com (2620:10d:c021:18::176) 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 09:28:40 -0800 Received: from NAM01-SN1-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.75) 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 09:28:40 -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=j5KroyBFE4eAXZ2LzJbTnXET4ZFDl4VCWTLQBBDx2Uc=; b=bpujuvmZ+3qizPomoN5vyn/aRMmeturMu6UnERLNtB35VKH7Mq23bTaAyiv/utq7uVsfQKNlb9B68zw/aKWlwkGgm7Cy4rh5uFxt72cAYW1Z7cQd04jbCYWxvtX1r6tzqb+qlFGhPmzAKmmdaf3LM35EoGuy0a85g2k+sFmtbSw= Received: from MN2PR15MB2656.namprd15.prod.outlook.com (20.179.146.26) by MN2PR15MB2942.namprd15.prod.outlook.com (20.178.251.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1643.21; Fri, 1 Mar 2019 17:28:39 +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; Fri, 1 Mar 2019 17:28:39 +0000 From: Javier Honduvilla Coto To: Martin Lau 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+WKXzLoOAgAPdkAA= Date: Fri, 1 Mar 2019 17:28:39 +0000 Message-ID: <20190301172826.GA99700@fb.com> References: <20190226223651.3166820-1-javierhonduco@fb.com> <20190226223651.3166820-2-javierhonduco@fb.com> <20190227062639.jpzwmm63iygad5f7@kafai-mbp.dhcp.thefacebook.com> In-Reply-To: <20190227062639.jpzwmm63iygad5f7@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: LO2P265CA0247.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:8a::19) To MN2PR15MB2656.namprd15.prod.outlook.com (2603:10b6:208:128::26) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2620:10d:c092:200::1:de4a] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: e175de99-f6a3-4c2f-2a4a-08d69e6b57a9 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600127)(711020)(4605104)(2017052603328)(7153060)(7193020);SRVR:MN2PR15MB2942; x-ms-traffictypediagnostic: MN2PR15MB2942: x-microsoft-exchange-diagnostics: 1;MN2PR15MB2942;20:cJZDee9peivVbHgWGbLKQwcLyYYy904Uk7Eitzjc8Wvi0wYih3j8+0aCAHhInY7zOjxOJpom4NzH71sSBtxSQ9jLUoA8SyLN+xiahlGum9btg72w5A4I2f5WrDlgodFfPtkBPi4dZ/wa4TGx316Q4LJeFZSZpjLdnw1vs/1mSKA= x-microsoft-antispam-prvs: x-forefront-prvs: 09634B1196 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(366004)(376002)(39860400002)(396003)(136003)(346002)(199004)(189003)(14444005)(97736004)(8936002)(5660300002)(478600001)(6486002)(6436002)(86362001)(76176011)(486006)(386003)(106356001)(256004)(6506007)(316002)(105586002)(46003)(6636002)(5024004)(52116002)(71200400001)(6246003)(186003)(102836004)(71190400001)(6116002)(68736007)(2616005)(37006003)(54906003)(476003)(11346002)(2906002)(53936002)(446003)(36756003)(33656002)(81156014)(229853002)(4326008)(81166006)(6512007)(305945005)(8676002)(25786009)(1076003)(7736002)(6862004)(14454004)(99286004);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR15MB2942;H:MN2PR15MB2656.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: ugh0DPBGSvmCUkhdmFxWtDMe90uVnDvSQQ3j0DNkLUsWo8v4NNDfJfHhFTs/ByPRDXdwqZdFKSyTwp+VIctODMUYe2PPGQNKcfr8nhiOypw80FplqC2QSDRGYoUIajOOdpXL7217+F2ZOC/li5ELeAFP1VUx2bFaALJ61CFoY4DvTT18LJaD4HpN7cGeqKA/L6xFypLXkqCWWnE1s8fVhAoGrSSO+NW0EXz5VRPQCLJD9Er7kRO+6uJydIRwOKztJuJ5NxdGDkkMtP1slWPZLHJ6E7+Mu5rH754IWgPjMVJent5QHnTPezUQGF2eMV6ygokjkUFwO331NoDEgGHesUr/F5OVPSlmy12PhMMaov/vTxtcxWg7l+lXgxJu9TlN2zTDVXKWcr3kCVLA6bVR4gfBzMtMWR/CQA6ma1XrT30= 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: e175de99-f6a3-4c2f-2a4a-08d69e6b57a9 X-MS-Exchange-CrossTenant-originalarrivaltime: 01 Mar 2019 17:28:38.3953 (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: MN2PR15MB2942 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-03-01_12:,, 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 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 retur= ns > 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 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 | 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_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; > 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. Oops, good catch, thanks! Will send v2 soon!! > > > > > /* 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)) > hmm.... Could current be NULL? 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 Al= exei about this and he told me this is probably not necessary anymore, but I guess it doesn't hurt leaving it? > > > + 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"? > 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) :) bpf_progenyof with 0 or 1 will always return 1 > > + 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 > >