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,URIBL_BLOCKED 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 2DE13C43381 for ; Sat, 2 Mar 2019 01:08:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CE0D12083E for ; Sat, 2 Mar 2019 01:08:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="a/JiNmPb"; dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="N2oE4GiS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726778AbfCBBIM (ORCPT ); Fri, 1 Mar 2019 20:08:12 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:51590 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725996AbfCBBIM (ORCPT ); Fri, 1 Mar 2019 20:08:12 -0500 Received: from pps.filterd (m0044008.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x220wfhG023726 for ; Fri, 1 Mar 2019 17:08:10 -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=l4gWNnOYr7QL4kD5ttEE5F7zQSjX4U5wSlCzdUbR2mU=; b=a/JiNmPbf1wPCGG3At5OENY1tO3FX1A8zlDmpMfkQi2mek0ZbBn1MJB+90FA0cNirO+F g0bct6z3aTOVahn1XHVbkqu7rGumZ8RLqMpsTudCMA++LvPV5Qen3P/+ezsvGRL0XHCe XqkqGjgqMXMwV0hQ/T3NjJq94jc+B9dLxXk= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2qye5v09hd-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 01 Mar 2019 17:08:10 -0800 Received: from prn-mbx06.TheFacebook.com (2620:10d:c081:6::20) by prn-hub02.TheFacebook.com (2620:10d:c081:35::126) 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:08:09 -0800 Received: from prn-hub03.TheFacebook.com (2620:10d:c081:35::127) by prn-mbx06.TheFacebook.com (2620:10d:c081:6::20) 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:08:09 -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:08:09 -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=l4gWNnOYr7QL4kD5ttEE5F7zQSjX4U5wSlCzdUbR2mU=; b=N2oE4GiSjuslxeZ3llfP56t7ujt5D8bQkv98tn/cgDwI2jkM7tgJWnTVRmHANYPGAk2Nvq9NTxkkQOCKxJDFeRoyPhJ5Qnh1NmeRzgXRqa9V6msLCznz2Lg6ppXd7UUqJG2Z0igaXI+A7FhXjOW+xNrfYIKKzv1M+4vMONG9KJY= 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:08:08 +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:08:08 +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+WKXzLoOAgAPdkACAAG27gIAAEqkA Date: Sat, 2 Mar 2019 01:08:07 +0000 Message-ID: <20190302010758.GA25319@fb.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> <20190302000111.xkkpxa3r36qy62rm@kafai-mbp.dhcp.thefacebook.com> In-Reply-To: <20190302000111.xkkpxa3r36qy62rm@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: LO2P265CA0101.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:c::17) 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: 75e14d31-44d6-45ea-8bf5-08d69eab87dc 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:QuyYES7Em0f1bjj34u/DJDmw+xfx/UigweBg1eu38yNR6aqlRKFouj+bFV7lWKelk2g1U2hqiicKWIBKRzcbR18IEz+ykuA2A1naCiiO1wcj3DiHWR5/nz1aLHTWQR1xdlfAaW5iiOUTMD4O6rg81gdSo22nNSo23WKOVQt74X8= x-microsoft-antispam-prvs: x-forefront-prvs: 09645BAC66 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(366004)(39860400002)(346002)(376002)(136003)(396003)(199004)(189003)(1076003)(93886005)(6862004)(229853002)(6636002)(4326008)(6486002)(54906003)(8936002)(478600001)(99286004)(52116002)(71190400001)(71200400001)(305945005)(5024004)(102836004)(14444005)(33656002)(256004)(14454004)(76176011)(2616005)(68736007)(86362001)(386003)(316002)(5660300002)(81166006)(81156014)(11346002)(446003)(105586002)(486006)(6506007)(8676002)(25786009)(2906002)(97736004)(6436002)(7736002)(6246003)(37006003)(186003)(53936002)(6116002)(6512007)(106356001)(476003)(36756003)(46003);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR15MB3341;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: ZbeI51DctLrsZ18nxCtYcCJmnHnadue3r9YojLruI4TJKFWzXtPBI/jW8Tno1L4FxCCebIe2gXYvIJsEc/YYCJ9655AA92RV6zzWfaM6+8vxsdGxNEHKMTJ1JgqF6NPQziabOrLmUGnLtMOBHzuTHQp8aTmV4b/KzOvMm0DnFsRJtyK06RouDQhCYQ92LCXky+kVv4ur4+au4G72k6xZhUzBK8lHybTAzeEmj/N6CzloaWv/6ZxeNJQGoA9F21oj14NF/fVvK0hWArSF7slZGDjmNtRcQXrUPbv+q47Z3sKRNHzcDVLx76PWgirvI+RwicAOLzdmtESnhNKjA8q6sUT+Vy7DlgXGdT0A+Lm+ABP3LHFscnQNJIcCg9xBxsohFCeADptLXlFzxIbJBeGGvK2f3fzSgAHE3LAt/u/n2bA= Content-Type: text/plain; charset="us-ascii" Content-ID: <4942D89B36F7E64FA3FD110876C6C468@namprd15.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 75e14d31-44d6-45ea-8bf5-08d69eab87dc X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Mar 2019 01:08:06.9974 (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:01:14AM +0000, Martin Lau wrote: > 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 wrot= e: > > > > This patch adds the bpf_progenyof helper which receives a PID and r= eturns > > > What is progenof? > > > > > > > 1 if the process currently being executed is in the process hierarc= hy > > > > 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 workarou= nds > > > > 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= lots > > > > of instructions. That and the impossibility to jump backwards mak= es 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_redir= ect_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 wh= ich 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_u= id_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(vo= id) > > > > { > > > > 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 func= tion, > > > > * inside its own verifier_ops->get_func_proto() callback it shoul= d return > > > > @@ -364,3 +365,31 @@ const struct bpf_func_proto bpf_get_local_stor= age_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. Texte= d Alexei > > 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 > 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. > My bad, that was a typo. bpf_progenyof(1) returns 1, and bpf_progenyof(0) returns 0. Sorry for the confusion. This is a good point! I chose to return 0 for this case because of init_task being an implementation detail and PID 0 not having much meaning for most users, but you are right that I should document it as an exception if we keep it as is That being said I think changing the behaviour to make progenyof(0) return = 1 makes more sense from a semantics perspective! > > > > > > + 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, co= nst 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 > > > >