Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1544382pxb; Thu, 16 Sep 2021 09:36:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjhttu7Le1exgaaCS34arpmsJICLLl2AjubNPgZR5/ufce50Igpg7boSFNX70d5HV/WXUO X-Received: by 2002:a5e:a813:: with SMTP id c19mr5133615ioa.199.1631810206797; Thu, 16 Sep 2021 09:36:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631810206; cv=none; d=google.com; s=arc-20160816; b=RLftSTJfYFHBt7885teD8MFrEPx6eAB0CJAosbHw6RmexWUUKZ9DUEYTW6CgEa3VHW O8mQwakNuH4KgWWkTpfchfsJRVVm5yeeOuTmp7lHGKENoqI1F0fctUK/VkvSc+rHCkaO bOBqY9S+HI+cVJCd7Guq+OYB7SVk5tvOMd3kRpFroskcrYBHcjITT+DLxvD3PDdSsMJV anspTnPAu/xLNnWNebxixfqYE8DXTkW+7Xk1qb4Qbwbzn0GrsWsD0XfREEv/CGKnf7ob Pdqxy4KlqQ8YCU1AJpo/EzGvUDTarcfoMO0JHQ9VRF46PpPHjLuqxrgku3f9hXcc+cPU VD8w== 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=FvkZsdRnuDFdsFs6RIuDJK0lxpqem6zpUwmYQiIpIG4=; b=XPfToeuErXJx3mbqtzyhXfBfKRUA9bsu3GgLO5N5rESbpae2ZJmfxQnLsSIsGMMi8E tv33pz+fHeX/Mwdgrc227+159QlTI/fspDw+XmlmeNbXkcGKZ11KWHw3yC2Q2eBW7Bek f9/u38WCaGs9JC8Qp5fK7a6T/q920AMl1zqREi7HoUJD2CHgQTwQarDfJiJOJcsfKWaw smmRjacj/UWlSJQE2a8gULu+01iEbVLlKM4guVaMTs2VnJO2ELOJD+Zs+c5Y1nyzBVyR c/LfSRumUnwTSPE8hj23zV6ls4IJcBvEIIREb5FVIo98H2VCitBM134dDP4Q6qRFM0wT aNKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="A+pK/+rZ"; 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 w8si3005925ilv.158.2021.09.16.09.36.29; Thu, 16 Sep 2021 09:36:46 -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="A+pK/+rZ"; 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 S244263AbhIPQfD (ORCPT + 99 others); Thu, 16 Sep 2021 12:35:03 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:15754 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241938AbhIPQ02 (ORCPT ); Thu, 16 Sep 2021 12:26:28 -0400 Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 18GFguOn013873 for ; Thu, 16 Sep 2021 09:25:07 -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=FvkZsdRnuDFdsFs6RIuDJK0lxpqem6zpUwmYQiIpIG4=; b=A+pK/+rZLVyk3N245w0cDN5aG0VOxd+gBaLAQqwoiLk7nrCCQsqw9vd12Lc3/zjUlkfb OnpuSAqHGioIdNO65LTJY/iG2rj8AbBNj3NvrJfRdzRUqpRfQVq14RqyxPFhW1LOKPXw BHU+uKzXLBbs76Xsp1h3kv4NL0b2YxbrDfU= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 3b47j40x4p-13 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 16 Sep 2021 09:25:07 -0700 Received: from intmgw002.46.prn1.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:21d::6) 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 36BF6BE68AAE; 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 3/6] bpf: sched: introduce bpf_sched_enable() Date: Thu, 16 Sep 2021 09:24:48 -0700 Message-ID: <20210916162451.709260-4-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-ORIG-GUID: bgoBr8-8SqZVrkno6Y8nM81ns3-XggVX X-Proofpoint-GUID: bgoBr8-8SqZVrkno6Y8nM81ns3-XggVX 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 phishscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 mlxscore=0 spamscore=0 priorityscore=1501 clxscore=1015 lowpriorityscore=0 impostorscore=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 Introduce a dedicated static key and the bpf_sched_enabled() wrapper to guard all invocations of bpf programs in the scheduler code. It will help to avoid any potential performance regression in a case when no scheduler bpf programs are attached. Signed-off-by: Roman Gushchin --- include/linux/bpf_sched.h | 24 ++++++++++++++++++++++++ kernel/bpf/syscall.c | 7 +++++++ kernel/sched/bpf_sched.c | 2 ++ 3 files changed, 33 insertions(+) diff --git a/include/linux/bpf_sched.h b/include/linux/bpf_sched.h index 0f8d3dae53df..6e773aecdff7 100644 --- a/include/linux/bpf_sched.h +++ b/include/linux/bpf_sched.h @@ -6,6 +6,8 @@ =20 #ifdef CONFIG_BPF_SYSCALL =20 +#include + #define BPF_SCHED_HOOK(RET, DEFAULT, NAME, ...) \ RET bpf_sched_##NAME(__VA_ARGS__); #include @@ -14,6 +16,23 @@ int bpf_sched_verify_prog(struct bpf_verifier_log *vlog, const struct bpf_prog *prog); =20 +DECLARE_STATIC_KEY_FALSE(bpf_sched_enabled_key); + +static inline bool bpf_sched_enabled(void) +{ + return static_branch_unlikely(&bpf_sched_enabled_key); +} + +static inline void bpf_sched_inc(void) +{ + static_branch_inc(&bpf_sched_enabled_key); +} + +static inline void bpf_sched_dec(void) +{ + static_branch_dec(&bpf_sched_enabled_key); +} + #else /* CONFIG_BPF_SYSCALL */ =20 #define BPF_SCHED_HOOK(RET, DEFAULT, NAME, ...) \ @@ -23,6 +42,11 @@ static inline RET bpf_sched_##NAME(__VA_ARGS__) \ } #undef BPF_SCHED_HOOK =20 +static inline bool bpf_sched_enabled(void) +{ + return false; +} + #endif /* CONFIG_BPF_SYSCALL */ =20 #endif /* _BPF_CGROUP_H */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 67e062376f22..aa5565110498 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -31,6 +31,7 @@ #include #include #include +#include =20 #define IS_FD_ARRAY(map) ((map)->map_type =3D=3D BPF_MAP_TYPE_PERF_EVENT= _ARRAY || \ (map)->map_type =3D=3D BPF_MAP_TYPE_CGROUP_ARRAY || \ @@ -2602,6 +2603,9 @@ static void bpf_tracing_link_release(struct bpf_lin= k *link) struct bpf_tracing_link *tr_link =3D container_of(link, struct bpf_tracing_link, link); =20 + if (link->prog->type =3D=3D BPF_PROG_TYPE_SCHED) + bpf_sched_dec(); + WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog, tr_link->trampoline)); =20 @@ -2804,6 +2808,9 @@ static int bpf_tracing_prog_attach(struct bpf_prog = *prog, goto out_unlock; } =20 + if (prog->type =3D=3D BPF_PROG_TYPE_SCHED) + bpf_sched_inc(); + link->tgt_prog =3D tgt_prog; link->trampoline =3D tr; =20 diff --git a/kernel/sched/bpf_sched.c b/kernel/sched/bpf_sched.c index ead691dc6e85..bf92cfb5ecf4 100644 --- a/kernel/sched/bpf_sched.c +++ b/kernel/sched/bpf_sched.c @@ -6,6 +6,8 @@ #include #include "sched.h" =20 +DEFINE_STATIC_KEY_FALSE(bpf_sched_enabled_key); + /* * For every hook declare a nop function where a BPF program can be atta= ched. */ --=20 2.31.1