From patchwork Tue Feb 6 22:04:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547844 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 11057C4828D for ; Tue, 6 Feb 2024 22:05:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 918A06B0096; Tue, 6 Feb 2024 17:05:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A1AD6B009A; Tue, 6 Feb 2024 17:05:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F37A6B009B; Tue, 6 Feb 2024 17:05:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 54D816B0096 for ; Tue, 6 Feb 2024 17:05:24 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3038EA199F for ; Tue, 6 Feb 2024 22:05:24 +0000 (UTC) X-FDA: 81762760968.02.B05232F Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf12.hostedemail.com (Postfix) with ESMTP id 343FF40024 for ; Tue, 6 Feb 2024 22:05:21 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ifsl9gRy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707257122; 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=waJIZfpNliIHPa/9880riUne4F1M98q2cd6wla9GZjk=; b=3sfz4C9645TyHMKn9CyTxGUiiKeQXSkVI6Yxhov9PbUtluF0iGkmurk1To/Vz96+JxDLf8 cI2y/HVK/yiajn5pC9bQFCUfAoBoJCjd41AEwuxokKV3NhEaJXCFnwtuKB9za/B0nX8at4 rt6ktCoAsey6zsyCxEWpY0THfBtaPDI= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ifsl9gRy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707257122; a=rsa-sha256; cv=none; b=MlVgYL6yRf65KWqPE17bP06cbhFV5fu8OO+SugCM3lCrkMrtEEz2SO+jJzu8li0nmQ5jwT tJrxW92pwnSaCQkiE7ly2Bw820QmFoRPshI6QgdeFjZy/D6Kn9uvRKXHzYHAahoOvWoLdk hUrqaj67IEVB6FljhVkgHmBPZbcdT9E= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6da9c834646so1648b3a.3 for ; Tue, 06 Feb 2024 14:05:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257121; x=1707861921; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=waJIZfpNliIHPa/9880riUne4F1M98q2cd6wla9GZjk=; b=ifsl9gRye8t+TC6Jv5vtYFU1MxACZcjbMfxCcYDq9MH86qHx8UEmDytJWs7wzJYwyE A3I9AQtrP81iyxvmTYthuNb9htCwsDrxo87csSdiKPJa2QPXkrgow62HN+oeWtK8d1L5 h+dOqqKYVLupx/5RCkLOT/cndxIpSI2bFNq5Qw0gpu2456ZqDuFQgmctVGpXbr0Wupma GUFQXqHOfYt0upxsv03pXSET2KK7WsgWxpaBK1rA04deUZw2mivN4PGerEOwjKSxHHPL hFov5jdUW1/UA1RtDe/lSXX3m+x0vGCxWR/bUY7Op2js+jhW8745LeJpfNtJKtrciTTk v5Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257121; x=1707861921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=waJIZfpNliIHPa/9880riUne4F1M98q2cd6wla9GZjk=; b=P+N4lSzP9h0hj5VR3ttTjoJn3alXKA1V3ctyfHfavZ3XMvU1Mq3fTGo4nL4VwuTf5B leS/h43lqHu87KhlhC6tQmE96n6UynzJxqDJNtAFYJ4FG0iOvr+d9VwUi3CdDmD6UQYe GodFvclk9xgoGojPw+MH9wbaDQkUfpyDKzSk3V0OIRdCZh4sp9/0KLS1E17+mJYZ9Jvr UzWkHAAcsTX11oe8dnp+o7rpxPYDkBX1DOOHA5IWIJB9Lv/gaZf6KnIR4Q4CcLe1VNqq uo8wS+363MmGWhIgHaUD9qGilXtejgKo5cqPRljPyTM3BglefjRj3OHENo3zc1tF1FKA NEUA== X-Gm-Message-State: AOJu0Ywr1ih3kUmDSd7tEKdRP9yb87Ff0jhKWKGso1VSdkxYUGpXn//y Omn5hO1IaH/lplvwKQWP9gqlsOLMlLj7+sbpGxMZeIXbvqyoKg0F X-Google-Smtp-Source: AGHT+IGEm41W1mIMfg18EMXRi5EZlNoYfBurQNFDegQZNFeL2j9w6VizaJwjHHTqyxrGN0HSh8f/bg== X-Received: by 2002:a05:6a20:8154:b0:19e:a23f:fa73 with SMTP id u20-20020a056a20815400b0019ea23ffa73mr606797pza.5.1707257121125; Tue, 06 Feb 2024 14:05:21 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXxN8orindXUKGG1h/zyY3mQyICEUtCIntzBop61kdfolwDs7jXajQYTwYORXc3hFm7NsG4OasP13LHB6c852skW89A5Q3SEJ9IeXDoIlf8HFxDkSEQwtp0/hR6/4zyqyoEUUOyZl7qxTHsrtAYLfY96aJG11wKBcbQfUK+jAghxuHvzl5GfAONh4LbS5QeBT2B9USv2mg4iHKbjiyPWsCkS1ydEmzx+CC1iJngFwB5VSmgSP3SEUQKbB3N9kyl4hqzgAWt3xXJbEfM3pfWaP0AZ0sXpYwjQtuN Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id x17-20020a056a00271100b006e0542f9689sm2474751pfv.103.2024.02.06.14.05.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:05:20 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next 09/16] bpf: Recognize btf_decl_tag("arg:arena") as PTR_TO_ARENA. Date: Tue, 6 Feb 2024 14:04:34 -0800 Message-Id: <20240206220441.38311-10-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240206220441.38311-1-alexei.starovoitov@gmail.com> References: <20240206220441.38311-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 343FF40024 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: oacifoyrgbw3zif8tipsu3yyfa35odft X-HE-Tag: 1707257121-477960 X-HE-Meta: U2FsdGVkX1+KGn2PvIQJzbrwLTG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tgfB50GuomwESiM3QGxA4TGis9P4+C744mzc18O1nRx8pEmouf1oOjk3Ff4Nw6Gt+47Pw6la6/lLXY7kq4nr2CZk3XqYKMVR/UEcNLlX5zqHV/iUGpqbbMRHmotWKT4hqEu1S9YhnHah7RT2o6xkKK9kwhaievKEAP6e9caNutAnhgSStub8KHJN1cFv/6uyb8H0SB1sEzqE4OgEH3jtER+AfG8FH1+9t8oIlWO613/sUdf7qb6x5FJh231Vo5jYsfaQLFPDXQmNmwKyydJYEWs5Zue8HPEDEgDc6GzCBsX78195ik2lyEPEoSyxsd0zDFGamgpeOscCM5cIT7InZh0tbVliS64tKhl1JttEyGAEnuyantfuZ2eL6Hrs9IFZqbJejgys4eElBV+4PKN930Uu9H3vDQM3Mka6qjBu0AczeGMRd6UOE0SJMlXsSctcZwXy0QHBZpuMyXNJJmjoSU3Jtt1YR 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: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov In global bpf functions recognize btf_decl_tag("arg:arena") as PTR_TO_ARENA. Note, when the verifier sees: __weak void foo(struct bar *p) it recognizes 'p' as PTR_TO_MEM and 'struct bar' has to be a struct with scalars. Hence the only way to use arena pointers in global functions is to tag them with "arg:arena". Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 1 + kernel/bpf/btf.c | 19 +++++++++++++++---- kernel/bpf/verifier.c | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 82f7727e434a..401c0031090d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -715,6 +715,7 @@ enum bpf_arg_type { * on eBPF program stack */ ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */ + ARG_PTR_TO_ARENA, ARG_CONST_SIZE, /* number of bytes accessed from memory */ ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */ diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f7725cb6e564..6d2effb65943 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -7053,10 +7053,11 @@ static int btf_get_ptr_to_btf_id(struct bpf_verifier_log *log, int arg_idx, } enum btf_arg_tag { - ARG_TAG_CTX = 0x1, - ARG_TAG_NONNULL = 0x2, - ARG_TAG_TRUSTED = 0x4, - ARG_TAG_NULLABLE = 0x8, + ARG_TAG_CTX = BIT_ULL(0), + ARG_TAG_NONNULL = BIT_ULL(1), + ARG_TAG_TRUSTED = BIT_ULL(2), + ARG_TAG_NULLABLE = BIT_ULL(3), + ARG_TAG_ARENA = BIT_ULL(4), }; /* Process BTF of a function to produce high-level expectation of function @@ -7168,6 +7169,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) tags |= ARG_TAG_NONNULL; } else if (strcmp(tag, "nullable") == 0) { tags |= ARG_TAG_NULLABLE; + } else if (strcmp(tag, "arena") == 0) { + tags |= ARG_TAG_ARENA; } else { bpf_log(log, "arg#%d has unsupported set of tags\n", i); return -EOPNOTSUPP; @@ -7222,6 +7225,14 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) sub->args[i].btf_id = kern_type_id; continue; } + if (tags & ARG_TAG_ARENA) { + if (tags & ~ARG_TAG_ARENA) { + bpf_log(log, "arg#%d arena cannot be combined with any other tags\n", i); + return -EINVAL; + } + sub->args[i].arg_type = ARG_PTR_TO_ARENA; + continue; + } if (is_global) { /* generic user data pointer */ u32 mem_size; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 6bd5a0f30f72..07b8eec2f006 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -9348,6 +9348,18 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, bpf_log(log, "arg#%d is expected to be non-NULL\n", i); return -EINVAL; } + } else if (base_type(arg->arg_type) == ARG_PTR_TO_ARENA) { + /* + * Can pass any value and the kernel won't crash, but + * only PTR_TO_ARENA or SCALAR make sense. Everything + * else is a bug in the bpf program. Point it out to + * the user at the verification time instead of + * run-time debug nightmare. + */ + if (reg->type != PTR_TO_ARENA && reg->type != SCALAR_VALUE) { + bpf_log(log, "R%d is not a pointer to arena or scalar.\n", regno); + return -EINVAL; + } } else if (arg->arg_type == (ARG_PTR_TO_DYNPTR | MEM_RDONLY)) { ret = process_dynptr_func(env, regno, -1, arg->arg_type, 0); if (ret) @@ -20321,6 +20333,9 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) reg->btf = bpf_get_btf_vmlinux(); /* can't fail at this point */ reg->btf_id = arg->btf_id; reg->id = ++env->id_gen; + } else if (base_type(arg->arg_type) == ARG_PTR_TO_ARENA) { + /* caller can pass either PTR_TO_ARENA or SCALAR */ + mark_reg_unknown(env, regs, i); } else { WARN_ONCE(1, "BUG: unhandled arg#%d type %d\n", i - BPF_REG_1, arg->arg_type);