From patchwork Wed Aug 17 21:04:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12946475 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 6C581C32772 for ; Wed, 17 Aug 2022 21:04:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E26D8D0008; Wed, 17 Aug 2022 17:04:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 092318D0003; Wed, 17 Aug 2022 17:04:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9C278D0008; Wed, 17 Aug 2022 17:04:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DA5918D0003 for ; Wed, 17 Aug 2022 17:04:42 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 93082C06DD for ; Wed, 17 Aug 2022 21:04:42 +0000 (UTC) X-FDA: 79810313604.30.80A14C4 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf17.hostedemail.com (Postfix) with ESMTP id 41C25401CA for ; Wed, 17 Aug 2022 21:04:42 +0000 (UTC) Received: by mail-pj1-f50.google.com with SMTP id t11-20020a17090a510b00b001fac77e9d1fso249537pjh.5 for ; Wed, 17 Aug 2022 14:04:41 -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=KAWNhWE/WD3wFZacJ2ko17zYo7gsMhvffUTmRG8how0=; b=NCslPlVANCcPFLe+NudeKJp3stSLbLNvDlVM4l6GRW31jEQunqYdwUeqVpyAZFEYUd Nsu90E90NAi+jruawjTmW3iszTS5o4+/oQRXA+EAYmMH6gMH+q6E4XaK9xCNoOE/Jlfi AtFH6utAhWuBPc8K5UIr4v/+SdWR+9gVFb9k9dah8dOC4wlzYCNQU1w10aLDysfso3Zz pT2KEyJ5Uydtip79+Zy/PgCOlmSEbMzk/b50NFLGvcFgLJ6P9DNzLuCqIeOgFtNkMbJ5 +iWKmUjxGcydPI+jGNj0PGqdzIaNM/TBGgRdACzDXrjY8dHbtYTnOzUPMA7WxV+Xzp5u E+zQ== 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=KAWNhWE/WD3wFZacJ2ko17zYo7gsMhvffUTmRG8how0=; b=T4jShPIif4jrUksYSe25S6+ZthyVCvm9W4c7pstp6Oj0IY+ErxchQV7Uat2wN3X6sx zSDgwh+Gd/BaBHOUeKEdtllByWrBn5l9do6MhDvIYKQ38uHaVElphGj0B6wMBRh2kShQ xc/IvNOiH4W+uEdOFdDvFYDg7zvG4sbGbd/0QzUyR8T95fqToQoo6FTp99ewADspp1vn /oyuoojff9KLWyVakhZn9dKgBuxh3hk0qfAZPU2lH34InqHLbq3GVdi4hoMhstzlTiSa y44NzvFl1R70px95aUI6FGqMlCRHW6BsQn/bJt1JwjgjXSWuULoXjSDrucUmuuoMV7l0 BWzg== X-Gm-Message-State: ACgBeo186WNGRg7+yU9dJuTqmiQ5Mbh9MtyqmEARTIPTZCtYyWRb6kfx VryPoWGZsLLe2pe6CRbsEbpnnPTm4Ws= X-Google-Smtp-Source: AA6agR74GmAwlmZZ2NQTJ2UBJaLITzOqdg5eTHBAJveQj8+xB4gEduTC/HaURwto3/n38jvd/RaQ8g== X-Received: by 2002:a17:90b:1c0d:b0:1f5:7bda:1447 with SMTP id oc13-20020a17090b1c0d00b001f57bda1447mr5407863pjb.88.1660770281180; Wed, 17 Aug 2022 14:04:41 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::1:ccd6]) by smtp.gmail.com with ESMTPSA id b189-20020a62cfc6000000b0052d1275a570sm10913598pfg.64.2022.08.17.14.04.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 17 Aug 2022 14:04:40 -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 v2 bpf-next 05/12] bpf: Relax the requirement to use preallocated hash maps in tracing progs. Date: Wed, 17 Aug 2022 14:04:12 -0700 Message-Id: <20220817210419.95560-6-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220817210419.95560-1-alexei.starovoitov@gmail.com> References: <20220817210419.95560-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=1660770281; 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=KAWNhWE/WD3wFZacJ2ko17zYo7gsMhvffUTmRG8how0=; b=hRPfJ4Sef180btYQsWSBbtzv9Zxn8KRGRDRO21xCMiFeFan8xqLSRJAU8playUbzx3ahrF lWYeNojmIAmpu6AQfuedYg8AUY2+vwBYkuRwVtkup3UYUGKKpcAZj9jxgi++kaEQgH8AzP CGpiWAi6+BQNDiO5HKW0U4GspMvefPE= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NCslPlVA; spf=pass (imf17.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.50 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=1660770281; a=rsa-sha256; cv=none; b=aQosTFl2VZtMLIohislq98v5nD4T1pnllX2RECYWpgnQq78hix0zaz3t33KG5IGBsSBS+X 9rFHxNuORzBIu6BlFOAjm8VGqq1oR2ifITCjuEfe7/B3rDnrIDJ/leoOeHdoa+xSWHwAGg pjVOzG04xSCduuZ7o3GlVdx3IYJAyQA= Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NCslPlVA; spf=pass (imf17.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: ccao6yy1opcqd66f7xjoxzq473us3wux X-Rspamd-Queue-Id: 41C25401CA X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1660770282-730348 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. 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 2c1f8069f7b7..d785f29047d7 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12605,10 +12605,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 @@ -12618,15 +12620,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)) {