From patchwork Fri Mar 8 01:08:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13586412 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 88E95C54798 for ; Fri, 8 Mar 2024 01:08:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11F0F6B02FB; Thu, 7 Mar 2024 20:08:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D76E6B02FD; Thu, 7 Mar 2024 20:08:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E87AA6B02FE; Thu, 7 Mar 2024 20:08:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D72246B02FB for ; Thu, 7 Mar 2024 20:08:40 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BA0FF1A121C for ; Fri, 8 Mar 2024 01:08:40 +0000 (UTC) X-FDA: 81872086800.23.FAF95F4 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf28.hostedemail.com (Postfix) with ESMTP id 0B881C0018 for ; Fri, 8 Mar 2024 01:08:38 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NhAa6ZJk; spf=pass (imf28.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.174 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=1709860119; 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=u/Ky9JSsSw/fwjp77rJRTfxTbp5gTCgpDOKXnfDwZbE=; b=g52BNK3p95DHmxdfcjpx+oAwy9NOs/TsOw1aWJVm0BZYTQTUTzJL0GGq/CVkQa3ZOXikbj 2IWCgaSfsaWhn2yMLmKs0lI2j4T5IE6uLXK+4yf+f2G0zB7EOG+GjlR4KoHFwx0XMZ3bKi R8QXGaJVJAfqzcdJnfD75hUMmmT0nHA= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NhAa6ZJk; spf=pass (imf28.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.174 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=1709860119; a=rsa-sha256; cv=none; b=uwucb+DIfnKmCIIyX8sZAUkEi/jPEf5qYGPL6It2ciBsu4xEXUxYYvB8blzgabF0i5SOsv l/IEzKV3HxHOVQFRooDcZqtMsFGlswuR/yxop19z4Q/b6YZrVZP0TFmhRMHiWJZaeXSfI7 zEO34TQf7f/JELnlF7LsbadUoC3JO14= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1dc09556599so13956975ad.1 for ; Thu, 07 Mar 2024 17:08:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709860118; x=1710464918; 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=u/Ky9JSsSw/fwjp77rJRTfxTbp5gTCgpDOKXnfDwZbE=; b=NhAa6ZJkfldmKsjMZXl9YqGfkTBsao+pZ2F8nXuhz1+2KFIezwSbVJTC+ej/z3VKne jAKUFA4DbvphEec8+uHS7XbsTzZrQ6lIAUH0Dk8oRbq27bffa1/OVzsaITXD9A4FHIEj yYRy7Gg8PMzHFSMZGRThpO3fGiT4yQ74UdUjsbv/Q+lvGBUP73xAHxoWI9Nh1ibWTp27 QdWleX7JxPDatA+hCeRtDg6VhLVF5mOcghRxKoZXlUXrXrd9AMjp0tOj9NohORAzexsM QY8nGgbxXRHSQs0EAfmRFFIwllLLu/dad2r1FoYvECrlu2ndo47n2oVkwwhy/GMU69gP 1LGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709860118; x=1710464918; 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=u/Ky9JSsSw/fwjp77rJRTfxTbp5gTCgpDOKXnfDwZbE=; b=w+DWVbqeqsH4bMk6466045RfcBU9MGvkGBDPDEb/9NBaZA31EDs+kXOJYhTBPq0ePP xo4hKcA+gk35Gu8dbw4evob6z2Z83R6KX3gPYTSGMqhZtn8vOpSAPn5snbU/MDDeLQt0 g5/+ObJnnWChRceXATh5pN/H6+MYZ4w9WmU7sbfyEHbLnDaBxi+n7KVT0yKVfG+qjws5 FoN+kDscXXE1nJhg/JvMDmuG1KkxPr6La+JaEcWDYrTK1kNbGoPtQW34fYchUifmQR2z VA2CIpe2SCKvuscGt+Io42wmfcCrwyxveHtdHKYQLfx+GGeP9GLc4nzXu696wKXFGpXG jOIQ== X-Forwarded-Encrypted: i=1; AJvYcCUWwLYRd2uMGhVcMK7SCcB44C9ZASmS9ImgloGmebZzgiCEWARWQiSkY1Ln3/bbHCQWrw5teGUIx0++6y3R6D9y2RY= X-Gm-Message-State: AOJu0YwGoNjUWKeyvkze07BIgsPEET7ByzoMiaRkQDWarWGX+NLKQ5un WK494g8gfTn7H9Y/5gWBwTp4M5+7MD6qMwn/Nw+z6eS7V+rIanV4 X-Google-Smtp-Source: AGHT+IGO75KVoJnmF8Czhd7kDBIez3STkTR4G49ROAwNxjtpUbXCXj/gJTN8EOuqXfjmJ6LnuAHjUQ== X-Received: by 2002:a17:903:1248:b0:1dc:3517:1486 with SMTP id u8-20020a170903124800b001dc35171486mr11827107plh.49.1709860117510; Thu, 07 Mar 2024 17:08:37 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:428c]) by smtp.gmail.com with ESMTPSA id j15-20020a170902da8f00b001d8aa88f59esm15239094plx.110.2024.03.07.17.08.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 07 Mar 2024 17:08:37 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, torvalds@linux-foundation.org, brho@google.com, hannes@cmpxchg.org, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH v3 bpf-next 06/14] bpf: Recognize btf_decl_tag("arg:arena") as PTR_TO_ARENA. Date: Thu, 7 Mar 2024 17:08:04 -0800 Message-Id: <20240308010812.89848-7-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240308010812.89848-1-alexei.starovoitov@gmail.com> References: <20240308010812.89848-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0B881C0018 X-Rspam-User: X-Stat-Signature: j5wcsqu5yg9engtojchcjdtfn64okbdw X-Rspamd-Server: rspam01 X-HE-Tag: 1709860118-122487 X-HE-Meta: U2FsdGVkX1+vjgG3pD58swXvyTc5xIH1LiWohOHaOwQXxaNIs1O/q2ic3hpah+8WTzDylDzC4EATZ1J9ll78f0ksaYTJ5iofcSqmQ3QiTJg4Gd2FIW+RPXOJGotFPPcEJWZmHP6EewvXzh/fMIdAyJjIFAlTiqQ5eycj1eKbvr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vack.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". Acked-by: Kumar Kartikeya Dwivedi 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 d0c836ba009d..08ad265cb195 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -712,6 +712,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 170d017e8e4a..90c4a32d89ff 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -7111,10 +7111,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 @@ -7226,6 +7227,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; @@ -7280,6 +7283,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 1358e20d315a..d64f7a9b60e8 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -9379,6 +9379,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) @@ -20448,6 +20460,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);