From patchwork Thu Sep 1 16:15:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12962888 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 B8025ECAAD8 for ; Thu, 1 Sep 2022 16:16:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51BC880007; Thu, 1 Sep 2022 12:16:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A3DE6B009E; Thu, 1 Sep 2022 12:16:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31E2F80007; Thu, 1 Sep 2022 12:16:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 174D96B009D for ; Thu, 1 Sep 2022 12:16:12 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DA30A140362 for ; Thu, 1 Sep 2022 16:16:11 +0000 (UTC) X-FDA: 79864018542.05.29AA909 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf22.hostedemail.com (Postfix) with ESMTP id 5CF51C0060 for ; Thu, 1 Sep 2022 16:16:11 +0000 (UTC) Received: by mail-pf1-f173.google.com with SMTP id w139so1819453pfc.13 for ; Thu, 01 Sep 2022 09:16:11 -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=J/UPgVAZJ6tCW63aRjPvtUFbVFMqtQ38ez6bXN9pU3M=; b=IjJOZXlwXAdP31mjg7782uhU4/jcLGpev5FwbYyLhgz8B3Zyrk4J8mVjMSBWXWA4bI RrzZpEIwc0YP+o9onLWQaX5ooaOkQouh8CYdWyrvbzI+EMPUGTivQ5WkqlFhAODmhltO jgF8SWSqZeqj+a87lrKgwL7ZowGWROIsXchk69atLmOeiLLT8zne4zXYWaie77A5Wtc2 Y481EEM1zTnfk0387pksfhRPshOc9invTfAvcs/p81XSnKJyNPXvy6Dszik+X3hI7DN9 7Qr0fb9T91xvc5QFwMditIqFHhfJkvXDNi5dkEuHpmp0or4w2iLyXz/lQEDgEtjrreBR w6GQ== 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=J/UPgVAZJ6tCW63aRjPvtUFbVFMqtQ38ez6bXN9pU3M=; b=CIJ9iWVL8agciIB02HPXoXAlxWMzl6pl+GpsASkPLgqDWx6dlv2wALl9DmzD1MH0NA tjhlCW0cO4x0hzyyemnU0o0UnPafj2QA3wjr4kgb4ERiwXjH/Fp1w2v/Wga0kdrPjJDR OkzuiquYZV2mXQA3xwLD5FCLuq09DP1sK0LZz3i4FCmzuxB2jf4QtL59jbmpllNCSq8P SjAOCSaQSMUQirVxsuTS7onudPQr/JxfiKNEYFbo8ACr3QTmhEs27HuXDuVHMVnPHxID m0Cl98flQPx4ueq20xIGoIU6NJlkx/gkqfKTrei7E6X2uhUGvHFEuAwudhOpCyumY5eM IuBA== X-Gm-Message-State: ACgBeo0NoPX/Vzol4oadXnLf/0ad+1vzLbO5HQjFcV/RjxxL6m5+PJ2y nfKZMqUM8bDizUteeW9vFGo= X-Google-Smtp-Source: AA6agR6tHH4CQrxuMQdew+YzVDFHkU2FlaZGbV8ylgjbpuxZs8+gpgFwqfa6KHB8piTEvus91KL2Ag== X-Received: by 2002:a63:450f:0:b0:430:491e:ed5c with SMTP id s15-20020a63450f000000b00430491eed5cmr5762528pga.330.1662048970311; Thu, 01 Sep 2022 09:16:10 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::3:4dc5]) by smtp.gmail.com with ESMTPSA id z16-20020aa79490000000b00538147b1a38sm9804924pfk.160.2022.09.01.09.16.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 01 Sep 2022 09:16:09 -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 v5 bpf-next 05/15] bpf: Relax the requirement to use preallocated hash maps in tracing progs. Date: Thu, 1 Sep 2022 09:15:37 -0700 Message-Id: <20220901161547.57722-6-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220901161547.57722-1-alexei.starovoitov@gmail.com> References: <20220901161547.57722-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662048971; 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=J/UPgVAZJ6tCW63aRjPvtUFbVFMqtQ38ez6bXN9pU3M=; b=nHQHtuf/nk34aB2qCBM4V+9MLGOhAwaMAq7lhhXQiP2xXm/ONjwEugwGYchIgB5q41O2BG NEsr/O9wq0aP5S5A4pfq4way4Mdj4wjc/10IVYjKY2zy10g8KF6WbdvS32fsPpeH9S9HKu 7lreMhERZuDXSQg4wzXf4Kw/bhT8A/0= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IjJOZXlw; spf=pass (imf22.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662048971; a=rsa-sha256; cv=none; b=GgVjU0eCvtzGq+I4LxMbzclsMygOrcuahZn2AHz7U7aEDfI+xEZiAtOwlCFhW9c2SeelxB GeLD/gb7Dmk1xA06p/aGTMYsNUX9PlPYqI8qq9FPurjQFLbjQGDwtCSE1gmKutUWHjTjfq fs6On+8Bg/LbY77GnqISq/qYwCq/ivQ= X-Stat-Signature: 9hcda7qs4nyxk9feyd9gdrh5xd7nzrwk X-Rspamd-Queue-Id: 5CF51C0060 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IjJOZXlw; spf=pass (imf22.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam12 X-Rspam-User: X-HE-Tag: 1662048971-384906 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 Acked-by: Andrii Nakryiko 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)) {