Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1734145pxb; Thu, 16 Sep 2021 14:18:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzutwDMPyIkR/K4a8LsqNz6e1dvRw4TGDmZUzrnPxhpZGVI3lwFYWEU8m65aajpIYPkov2Y X-Received: by 2002:a05:6e02:13d4:: with SMTP id v20mr5428171ilj.247.1631827096763; Thu, 16 Sep 2021 14:18:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631827096; cv=none; d=google.com; s=arc-20160816; b=l8M7QLrzVjm4/BAfHwe8Oij0D+9Np01RAe09uL4zJJcfTg48iA9OqDDp1/NhLQ2Qt+ v8pJmIB9VsOapcsnMvU9VPSxrhVgarb8taiaLZ8UAnYzxhddMYwKT3ztjVCmLIo3UR6d Gre6I5m3sIP1l54OSDrdI23zOnFndoegPHGssK/9OgnJzEhUBnacy9NrApxA+bKGIn1p nOUZdblRT2U46+zfb0xG890fydIZnTL6FR3kaPbVH1eka08d6UIaaU9I7gk6rsAy3j7A 8J6KEeTIHmHic1ClDVLXmjEpW498Vrl0XZ7y5opEgWmAsU9mcoSvr9uEyomercipZTqt jx9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=I5fkErTvZrkiVI6NbzLcumqoZjkORUv3EO++1kQRzZs=; b=01pGG0ZkoOdely2umx6YIlxhCo2BAD/WeKuL39cjfU72DF6anQ0iFKbsKpUp5Qm0Q4 868Mxcr/oI1AE7EbZmGlnmCN4C/UWpOIrLHNKyuiiADGvX3Sn+gR94hRfvqI3PsKTqzN zKUUwr84X8wPnflL2FIzNpbogxXUSIGJnSLenBVvgRrxUL57YPdMK41CNqWRMl2yHXYC KDna9zoM/5DtGNh3iH+X5oQAOg5RCF1xbKxfPVJ+rZciM4eMtkoLsGXigaBoC2EJh4jK Sha67aLEVpHZeidcGprkt9QCYWdBBaFvkYFz8hkY/Ea1p+UWe6vMt+LPvDF6Zt8pzo4t 8QXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=D9SljK0E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u10si3508873ilm.0.2021.09.16.14.18.05; Thu, 16 Sep 2021 14:18:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=D9SljK0E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243962AbhIPQed (ORCPT + 99 others); Thu, 16 Sep 2021 12:34:33 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:43460 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241927AbhIPQ01 (ORCPT ); Thu, 16 Sep 2021 12:26:27 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 18GFgunx000473 for ; Thu, 16 Sep 2021 09:25:06 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=I5fkErTvZrkiVI6NbzLcumqoZjkORUv3EO++1kQRzZs=; b=D9SljK0EcOKqOWh0eoKaggHLnjdCdjhvLrE8T7n6JLEqNCRl2utlgWro2nOM5L3fY+54 e3EodqLr5GFMoQCXD9mUICLLn05Wf1IYYaTkvCbPIxHi8y2BSmJb58QCKtOKuBL4Gw+G iUrmfkBjvRH3o0163SLxE1P+qJxIDxK03X8= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 3b40fb3fbv-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 16 Sep 2021 09:25:06 -0700 Received: from intmgw002.48.prn1.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:21d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.14; Thu, 16 Sep 2021 09:25:04 -0700 Received: by devvm3388.prn0.facebook.com (Postfix, from userid 111017) id 3080EBE68AAC; Thu, 16 Sep 2021 09:25:02 -0700 (PDT) From: Roman Gushchin To: Peter Zijlstra , Ingo Molnar CC: Mel Gorman , , , Roman Gushchin Subject: [PATCH rfc 2/6] bpf: sched: add convenient helpers to identify sched entities Date: Thu, 16 Sep 2021 09:24:47 -0700 Message-ID: <20210916162451.709260-3-guro@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210916162451.709260-1-guro@fb.com> References: <20210915213550.3696532-1-guro@fb.com> <20210916162451.709260-1-guro@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-FB-Source: Intern X-Proofpoint-GUID: urozmMsH8JbRIdkeawhPzsEQ62mI3m4r X-Proofpoint-ORIG-GUID: urozmMsH8JbRIdkeawhPzsEQ62mI3m4r X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.790,Hydra:6.0.391,FMLib:17.0.607.475 definitions=2021-09-16_04,2021-09-16_01,2020-04-07_01 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 lowpriorityscore=0 clxscore=1015 adultscore=0 mlxlogscore=888 malwarescore=0 suspectscore=0 impostorscore=0 spamscore=0 phishscore=0 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109030001 definitions=main-2109160098 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds 3 helpers useful for dealing with sched entities: u64 bpf_sched_entity_to_tgidpid(struct sched_entity *se); u64 bpf_sched_entity_to_cgrpid(struct sched_entity *se); long bpf_sched_entity_belongs_to_cgrp(struct sched_entity *se, u64 cgrp= id); Sched entity is a basic structure used by the scheduler to represent schedulable objects: tasks and cgroups (if CONFIG_FAIR_GROUP_SCHED is enabled). It will be passed as an argument to many bpf hooks, so scheduler bpf programs need a convenient way to deal with it. bpf_sched_entity_to_tgidpid() and bpf_sched_entity_to_cgrpid() are useful to identify a sched entity in userspace terms (pid, tgid and cgroup id). bpf_sched_entity_belongs_to_cgrp() allows to check whether a sched entity belongs to sub-tree of a cgroup. It allows to write cgroup-specific scheduler policies even without enabling the cgroup cpu controller. Signed-off-by: Roman Gushchin --- include/uapi/linux/bpf.h | 23 +++++++++++ kernel/sched/bpf_sched.c | 74 ++++++++++++++++++++++++++++++++++ scripts/bpf_doc.py | 2 + tools/include/uapi/linux/bpf.h | 23 +++++++++++ 4 files changed, 122 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 6dfbebb8fc8f..199e4a92820d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4900,6 +4900,26 @@ union bpf_attr { * **-EINVAL** if *flags* is not zero. * * **-ENOENT** if architecture does not support branch records. + * + * u64 bpf_sched_entity_to_tgidpid(struct sched_entity *se) + * Description + * Return task's encoded tgid and pid if the sched entity is a task. + * Return + * Tgid and pid encoded as tgid << 32 \| pid, if *se* is a task. (u64)-= 1 otherwise. + * + * u64 bpf_sched_entity_to_cgrpid(struct sched_entity *se) + * Description + * Return cgroup id if the given sched entity is a cgroup. + * Return + * Cgroup id, if *se* is a cgroup. (u64)-1 otherwise. + * + * long bpf_sched_entity_belongs_to_cgrp(struct sched_entity *se, u64 cg= rpid) + * Description + * Checks whether the sched entity belongs to a cgroup or + * it's sub-tree. It doesn't require a cgroup CPU controller + * to be enabled. + * Return + * 1 if the sched entity belongs to a cgroup, 0 otherwise. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5079,6 +5099,9 @@ union bpf_attr { FN(get_attach_cookie), \ FN(task_pt_regs), \ FN(get_branch_snapshot), \ + FN(sched_entity_to_tgidpid), \ + FN(sched_entity_to_cgrpid), \ + FN(sched_entity_belongs_to_cgrp), \ /* */ =20 /* integer value in 'imm' field of BPF_CALL instruction selects which he= lper diff --git a/kernel/sched/bpf_sched.c b/kernel/sched/bpf_sched.c index 2f05c186cfd0..ead691dc6e85 100644 --- a/kernel/sched/bpf_sched.c +++ b/kernel/sched/bpf_sched.c @@ -42,12 +42,86 @@ int bpf_sched_verify_prog(struct bpf_verifier_log *vl= og, return 0; } =20 +BPF_CALL_1(bpf_sched_entity_to_tgidpid, struct sched_entity *, se) +{ + if (entity_is_task(se)) { + struct task_struct *task =3D task_of(se); + + return (u64) task->tgid << 32 | task->pid; + } else { + return (u64) -1; + } +} + +BPF_CALL_1(bpf_sched_entity_to_cgrpid, struct sched_entity *, se) +{ +#ifdef CONFIG_FAIR_GROUP_SCHED + if (!entity_is_task(se)) + return cgroup_id(se->cfs_rq->tg->css.cgroup); +#endif + return (u64) -1; +} + +BPF_CALL_2(bpf_sched_entity_belongs_to_cgrp, struct sched_entity *, se, + u64, cgrpid) +{ +#ifdef CONFIG_CGROUPS + struct cgroup *cgrp; + int level; + + if (entity_is_task(se)) + cgrp =3D task_dfl_cgroup(task_of(se)); +#ifdef CONFIG_FAIR_GROUP_SCHED + else + cgrp =3D se->cfs_rq->tg->css.cgroup; +#endif + + for (level =3D cgrp->level; level; level--) + if (cgrp->ancestor_ids[level] =3D=3D cgrpid) + return 1; +#endif + return 0; +} + +BTF_ID_LIST_SINGLE(btf_sched_entity_ids, struct, sched_entity) + +static const struct bpf_func_proto bpf_sched_entity_to_tgidpid_proto =3D= { + .func =3D bpf_sched_entity_to_tgidpid, + .gpl_only =3D false, + .ret_type =3D RET_INTEGER, + .arg1_type =3D ARG_PTR_TO_BTF_ID, + .arg1_btf_id =3D &btf_sched_entity_ids[0], +}; + +static const struct bpf_func_proto bpf_sched_entity_to_cgrpid_proto =3D = { + .func =3D bpf_sched_entity_to_cgrpid, + .gpl_only =3D false, + .ret_type =3D RET_INTEGER, + .arg1_type =3D ARG_PTR_TO_BTF_ID, + .arg1_btf_id =3D &btf_sched_entity_ids[0], +}; + +static const struct bpf_func_proto bpf_sched_entity_belongs_to_cgrp_prot= o =3D { + .func =3D bpf_sched_entity_belongs_to_cgrp, + .gpl_only =3D false, + .ret_type =3D RET_INTEGER, + .arg1_type =3D ARG_PTR_TO_BTF_ID, + .arg1_btf_id =3D &btf_sched_entity_ids[0], + .arg2_type =3D ARG_ANYTHING, +}; + static const struct bpf_func_proto * bpf_sched_func_proto(enum bpf_func_id func_id, const struct bpf_prog *pr= og) { switch (func_id) { case BPF_FUNC_trace_printk: return bpf_get_trace_printk_proto(); + case BPF_FUNC_sched_entity_to_tgidpid: + return &bpf_sched_entity_to_tgidpid_proto; + case BPF_FUNC_sched_entity_to_cgrpid: + return &bpf_sched_entity_to_cgrpid_proto; + case BPF_FUNC_sched_entity_belongs_to_cgrp: + return &bpf_sched_entity_belongs_to_cgrp_proto; default: return NULL; } diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py index 00ac7b79cddb..84019ba5b67b 100755 --- a/scripts/bpf_doc.py +++ b/scripts/bpf_doc.py @@ -548,6 +548,7 @@ class PrinterHelpers(Printer): 'struct socket', 'struct file', 'struct bpf_timer', + 'struct sched_entity', ] known_types =3D { '...', @@ -596,6 +597,7 @@ class PrinterHelpers(Printer): 'struct socket', 'struct file', 'struct bpf_timer', + 'struct sched_entity', } mapped_types =3D { 'u8': '__u8', diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bp= f.h index 6dfbebb8fc8f..199e4a92820d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4900,6 +4900,26 @@ union bpf_attr { * **-EINVAL** if *flags* is not zero. * * **-ENOENT** if architecture does not support branch records. + * + * u64 bpf_sched_entity_to_tgidpid(struct sched_entity *se) + * Description + * Return task's encoded tgid and pid if the sched entity is a task. + * Return + * Tgid and pid encoded as tgid << 32 \| pid, if *se* is a task. (u64)-= 1 otherwise. + * + * u64 bpf_sched_entity_to_cgrpid(struct sched_entity *se) + * Description + * Return cgroup id if the given sched entity is a cgroup. + * Return + * Cgroup id, if *se* is a cgroup. (u64)-1 otherwise. + * + * long bpf_sched_entity_belongs_to_cgrp(struct sched_entity *se, u64 cg= rpid) + * Description + * Checks whether the sched entity belongs to a cgroup or + * it's sub-tree. It doesn't require a cgroup CPU controller + * to be enabled. + * Return + * 1 if the sched entity belongs to a cgroup, 0 otherwise. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5079,6 +5099,9 @@ union bpf_attr { FN(get_attach_cookie), \ FN(task_pt_regs), \ FN(get_branch_snapshot), \ + FN(sched_entity_to_tgidpid), \ + FN(sched_entity_to_cgrpid), \ + FN(sched_entity_belongs_to_cgrp), \ /* */ =20 /* integer value in 'imm' field of BPF_CALL instruction selects which he= lper --=20 2.31.1