From patchwork Fri Aug 26 02:44:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12955497 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF2F9ECAAA3 for ; Fri, 26 Aug 2022 02:44:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FAA180008; Thu, 25 Aug 2022 22:44:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A98780007; Thu, 25 Aug 2022 22:44:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FCE980008; Thu, 25 Aug 2022 22:44:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3D52980007 for ; Thu, 25 Aug 2022 22:44:55 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 19472A06C8 for ; Fri, 26 Aug 2022 02:44:55 +0000 (UTC) X-FDA: 79840201350.28.E1B8E86 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf06.hostedemail.com (Postfix) with ESMTP id BE83418000D for ; Fri, 26 Aug 2022 02:44:54 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id t11-20020a17090a510b00b001fac77e9d1fso6782066pjh.5 for ; Thu, 25 Aug 2022 19:44:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=3mqXakE7twO5rpPKR4kaQK7GjmKBp7UOh97wf3rlLLs=; b=Ts1ypdy+1bsDA841Ckzl1jFQs5Ddjh9LsLbOtpSMZAikE1vFQWCk6y8dDrk+WRnEJs ulUmiCSq/5l8XtlF24ltS4sju6xZs/vPhDxsc0yJS5UmlqJ/lMdHXjoExMcqj3LZbb8g 01MiFGQD/UEGxc22NjIWUnGH3HLTyiJOKl97ggTWdENDYGd+PRXQbNuHMlWbHvWxHLFW sz9eq4usJdVrS/bslLT4UeZ1IcUmFFYxVvhsEMwqr5ZKnqaY08m+06DnAKod2sah0h4v p8QlC7yg244hKgk8rUFgNDubkZCk8i4rWVMVHGD4tkJtWagfbZUa1zicg5C7m2MAJLhU iNBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=3mqXakE7twO5rpPKR4kaQK7GjmKBp7UOh97wf3rlLLs=; b=n2nQFXNSv5JuJL13SoC7xHETT1R5DFjQmpsPEQqELf8ug2clW0koMA05XVodYYWQWY tc5sv2ygKa6Gxz+8TZG5EhLxUpwqS9NdZgR0ddLPulBDr2tpc78c04qBOsb6MUZBD+Po 5g62r6YZnGyUMNEuywnUoKP9VsLIbONG4X0txyKqHpgiTyLNOe8GIe6y/f/2sMeSAfv1 eYS+eAWq+RBd/5iUuyczg1NY995U8PFoWM3hCpwIA2bTnPC8oWR4BvjN6AxcqaWXEUBO LK8TJuimlgL5YRkPdyk0hxQsVQGYGoSTtH+EIwP6YkOH5Yh//IrPVhTxqAUNGM/pobNR Pg3w== X-Gm-Message-State: ACgBeo0mX9/bWJTPjPATP2aFlenQS/aTaTqtnnRYWbWN/rqYT2rO5DvS X0PBlz/HJ+uyLC9eI9n9eFE= X-Google-Smtp-Source: AA6agR4vvT5Bvx0/kBrb3s1GNaOacjAGPqdnwmEadcnCadwXfyraXlXYt6utigpRN4EkUigFdsAhyQ== X-Received: by 2002:a17:902:a411:b0:172:766e:7f3d with SMTP id p17-20020a170902a41100b00172766e7f3dmr1714520plq.24.1661481893768; Thu, 25 Aug 2022 19:44:53 -0700 (PDT) Received: from macbook-pro-3.dhcp.thefacebook.com ([2620:10d:c090:400::5:15dc]) by smtp.gmail.com with ESMTPSA id i14-20020a17090a2a0e00b001f3e643ebbfsm463922pjd.0.2022.08.25.19.44.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 25 Aug 2022 19:44:53 -0700 (PDT) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, andrii@kernel.org, tj@kernel.org, memxor@gmail.com, delyank@fb.com, linux-mm@kvack.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v4 bpf-next 05/15] bpf: Relax the requirement to use preallocated hash maps in tracing progs. Date: Thu, 25 Aug 2022 19:44:20 -0700 Message-Id: <20220826024430.84565-6-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220826024430.84565-1-alexei.starovoitov@gmail.com> References: <20220826024430.84565-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661481894; a=rsa-sha256; cv=none; b=ilqrSNw9fGxG1noRRZNiQcbJ80REkEInjDMNMFSQSu5euK9N5UrdxxvANoveIuXItBizn8 nrBKbtjI+5H5KtHw0V7ZG4pSNPU4PMNLI0ns6Ov8M7xEhAmcC8DXDk5r62iMoOhT30EQiP fJbrvrnk/lQ+6oXYFuPmdozT7WEYz6U= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Ts1ypdy+; spf=pass (imf06.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661481894; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3mqXakE7twO5rpPKR4kaQK7GjmKBp7UOh97wf3rlLLs=; b=E5m2XOPZpIisI425Jrq5Gf8PZfq+j6gxlznn+j3Ajnh08whfK7bDIt6jj41k2H2I0FHLyo uwJWXi74ddiAdgYw7v+60OHDsBf2y5NvgKZrRuJFIpvQ4kWovIRZGm9etEWUng+eRNe2tp jxFtwX2/8XsXKuRcIW8VkhpW2dUMxuU= Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Ts1ypdy+; spf=pass (imf06.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: bwaxw66ubcdei9tfd1zdqeasapjdwkpj X-Rspamd-Queue-Id: BE83418000D X-HE-Tag: 1661481894-583005 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Alexei Starovoitov Since bpf hash map was converted to use bpf_mem_alloc it is safe to use from tracing programs and in RT kernels. But per-cpu hash map is still using dynamic allocation for per-cpu map values, hence keep the warning for this map type. In the future alloc_percpu_gfp can be front-end-ed with bpf_mem_cache and this restriction will be completely lifted. perf_event (NMI) bpf programs have to use preallocated hash maps, because free_htab_elem() is using call_rcu which might crash if re-entered. Sleepable bpf programs have to use preallocated hash maps, because life time of the map elements is not protected by rcu_read_lock/unlock. This restriction can be lifted in the future as well. Acked-by: Kumar Kartikeya Dwivedi Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 0194a36d0b36..3dce3166855f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12629,10 +12629,12 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, * For programs attached to PERF events this is mandatory as the * perf NMI can hit any arbitrary code sequence. * - * All other trace types using preallocated hash maps are unsafe as - * well because tracepoint or kprobes can be inside locked regions - * of the memory allocator or at a place where a recursion into the - * memory allocator would see inconsistent state. + * All other trace types using non-preallocated per-cpu hash maps are + * unsafe as well because tracepoint or kprobes can be inside locked + * regions of the per-cpu memory allocator or at a place where a + * recursion into the per-cpu memory allocator would see inconsistent + * state. Non per-cpu hash maps are using bpf_mem_alloc-tor which is + * safe to use from kprobe/fentry and in RT. * * On RT enabled kernels run-time allocation of all trace type * programs is strictly prohibited due to lock type constraints. On @@ -12642,15 +12644,26 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, */ if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { + /* perf_event bpf progs have to use preallocated hash maps + * because non-prealloc is still relying on call_rcu to free + * elements. + */ verbose(env, "perf_event programs can only use preallocated hash map\n"); return -EINVAL; } - if (IS_ENABLED(CONFIG_PREEMPT_RT)) { - verbose(env, "trace type programs can only use preallocated hash map\n"); - return -EINVAL; + if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || + (map->inner_map_meta && + map->inner_map_meta->map_type == BPF_MAP_TYPE_PERCPU_HASH)) { + if (IS_ENABLED(CONFIG_PREEMPT_RT)) { + verbose(env, + "trace type programs can only use preallocated per-cpu hash map\n"); + return -EINVAL; + } + WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); + verbose(env, + "trace type programs with run-time allocated per-cpu hash maps are unsafe." + " Switch to preallocated hash maps.\n"); } - WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); - verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); } if (map_value_has_spin_lock(map)) {