From patchwork Fri Feb 9 04:05:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13550851 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 05304C4828F for ; Fri, 9 Feb 2024 04:06:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8562A8D0007; Thu, 8 Feb 2024 23:06:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8043E8D0001; Thu, 8 Feb 2024 23:06:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67FB18D0007; Thu, 8 Feb 2024 23:06:58 -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 503958D0001 for ; Thu, 8 Feb 2024 23:06:58 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 28743120E3A for ; Fri, 9 Feb 2024 04:06:58 +0000 (UTC) X-FDA: 81770929716.28.0494956 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf02.hostedemail.com (Postfix) with ESMTP id 4848A80006 for ; Fri, 9 Feb 2024 04:06:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Nn31RBqR; spf=pass (imf02.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.170 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=1707451616; 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=XZ9Snvkhjzxk5f60xE38IZQNIMkdSN5DBO4L+T7puE0=; b=YDrgiw0LemH/mlFCYgliIkF1CCMy6wqERADqFXASC5O1aM+V+AI40AnTPfrLu+KMNw/PC2 tFIwtudFTpbPJ7DLZiwZ0kqYO5E5AW2GthuVfYOnUUw5jcgagj55ZePbzf+DKFeiiIjcDV 3W4QUW3sfDYTNReZ1YM63PfUCaB86DU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707451616; a=rsa-sha256; cv=none; b=vnCQ12yPtKadajmaCg0WyJ2hEuyHP+xynVHEOacg5lb1+QYjiTvi3Mhi573ROkZEI0sjkU u0B0/YEr/IFI2/KifOfZJrFp3Rfi56O9XUfOoGlggx0m1neKr76y1jfKdiO2zHjxeRIatF vIJl7paZmVB9Q86jcqkO4q9746jueRo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Nn31RBqR; spf=pass (imf02.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1d7354ba334so4476065ad.1 for ; Thu, 08 Feb 2024 20:06:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707451615; x=1708056415; 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=XZ9Snvkhjzxk5f60xE38IZQNIMkdSN5DBO4L+T7puE0=; b=Nn31RBqR873Jx8ukrPZVsfVGtDXH+cR44s3OxG1TvMHZ2Nnmc5Ah7IztAGLBgqlr3O 24x+Yg+aCou2BnREvpjaPxInSq4j7menFyEVrnT3kza/AVOaFNHdHAbzXfb7RRgmSxjJ iIe7RSL/A0S2flwtZ1wf5EAottEE2QGvVwMxDK71FfTwb++sEmL/V6qL82c1cKNaxCEg hEqOSvkPfr9XDnRm+iMMWo1gTNXeqYwHuW9h0JIQcZqe6Q/ptiuTLUdEczyVScp2L87o DhA8Af232J7bfYEB32M0m+RGKKXSdueQnXJLab+zl2892TBNs9jqbthpIRLFW6H39U4j PZ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707451615; x=1708056415; 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=XZ9Snvkhjzxk5f60xE38IZQNIMkdSN5DBO4L+T7puE0=; b=pklb3X0zr1pe4VPiT1DiD/ku6MKk7YFzQFH2HrWlB7vYVh4BMWJ0QH4v+0//4RyHJr deL9BBRlL64O0fau0GwLxJFsVp446cN8pn2Zzps3tLlr8TVULNvbE5JHkJkc3liZQqAJ cOk+2miNSrv4YTTeC4H4v77QlV7nJmK2q8T+A+gEt8YQbYXqV6U5rPnUFYcSkH1jLPOC f74i14XsmdXiWhWoHzBXv2GjQFXyGHneqABU0JfNNoBI2xnjZLRQjHaw+FN8vvaNXyzQ YPcoC09zMFhNk0YgIMFYNQQMgKxzFcrp97l/Fi13RVnsnpYnia+5INYOpn2/sIV0Nt2o ul6g== X-Gm-Message-State: AOJu0YzbU0jVSc97Ghf8Bxokoc7OI9cdB3OPgqTx580UukZ8jJnBh1I5 bD4KSNgnyEtETH8Cw8RuuwDJRE/ZplN5wdETWDGUx7m1J6vLCTxF X-Google-Smtp-Source: AGHT+IGdKvGloD2x7T0TvljdUW1/xtPfwtVY4eu3Wz+SojJrERvkkNs762n7o/a8ViAyTbYVnq16Ig== X-Received: by 2002:a17:902:c403:b0:1d9:bbc2:87e7 with SMTP id k3-20020a170902c40300b001d9bbc287e7mr500885plk.36.1707451615176; Thu, 08 Feb 2024 20:06:55 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCU/x2cbpnz0BUBrwD3oynKGkbQWd49A9Q51EP6K/5fCnnF23ZUpv0qRDmHTlrR6yA0AtXPMi3FuK5K+KFjNWQZB5mJntsOxS8Pm1u0s8ziWoeQw2r37+Me/HIxl3XgxR4kF3y3DhvCArqcZd+TXsi4bpWVUbTSne3QN9pquK2NsflaDJazTaGUXoTDw5FqEjjU2zuYKegJHRBDw6WC7STY0O4c1TLAlrG4mfSz/8Qw8kYBzblb0HSXdBLKCCxn0P8d6aqm9/j/gIVQKJRkMiVlEC3V3y4n8PzaD9mBz4h5KNSVPY3TwerwIV4dK9wOn2J489jvaUMgPG6ksWBRa0Vjh8eFWxGtM4aKjqcZVj6h9pFRxYkRbAg== Received: from macbook-pro-49.dhcp.thefacebook.com ([2620:10d:c090:400::4:a894]) by smtp.gmail.com with ESMTPSA id kw13-20020a170902f90d00b001d752c4f180sm560989plb.94.2024.02.08.20.06.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Feb 2024 20:06:54 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, lstoakes@gmail.com, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 10/20] bpf: Recognize btf_decl_tag("arg:arena") as PTR_TO_ARENA. Date: Thu, 8 Feb 2024 20:05:58 -0800 Message-Id: <20240209040608.98927-11-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240209040608.98927-1-alexei.starovoitov@gmail.com> References: <20240209040608.98927-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4848A80006 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 47uw9ueacot8xuuku5qfrc4rgfien1br X-HE-Tag: 1707451616-253340 X-HE-Meta: U2FsdGVkX19qlBM2T1OQIZgZM1xc/kpESDIPpDaQvakOm0DzE8tMRYlcoBvFPQ+kBOxDLffWTu8iAmeDiwEFwSR/OTJZcpgAX4DutMXzKgdzacQnQWS1o32cPUzo/+Z8UghRN8oK+t/bwLDb6SvmQo5EoMNjPuGbl8Vara5awm9+Y+oTiwf+rwxmQxrIMywi9bRwqNEXxEcsgRhmrfhaawnXAuxKaWlCyId7UE9N2Hc5jEpmhl0bqVhq+G/zXixQkcv/8b3K5OBtUFkmAcHIBHfTMVllhBgQB/6X+PNRPTatI5aoxxfojUyhjoQTppIH66oiYWlUNW/8rUpnSpUMEQo7HJpP2Mpt/ajE4UwHDNNM3iiKKOEdfO7+nu3WHcPKOXa/ztLjAihY576xpKXgDCmPlONCzhiS3pn1F0WXWqLjeeDVvN8Mq2wdR30bqMuArx59O6fX6zDZm2ULa53BF/1aRleD2pATuOTtxl0Izeazk/C0rR7hxQHE0qCiDAgEY59zVjoaYa026KZdh4GUWqDmqP6H2uttweDwgvL9R+kY0+oasF2SXN7cxAww7Ha1S3fBokQ98w0VKnWUVoFiyMedhRPbBIxaH7CgVPM+nd9dRoQ+r8UDnUERmNbwZP+zDJPV8jQFY1UFitwQj59QyE2TIK1u2qwy18L2SKzm0+bCE1nQsri2ySaB7IIay433QnzzRNwi1utC0hdkIIBc2xp4cvIR+gjgD9M1bRYY+FKxZBoxFIgb/PpX4C1hIxMaanJcWVrM82TYdWR208vPR7y2g1TuqDeW7TvbHvDsjHMwJ5xSLCCnQJPBsfbdvZ4T2GKZyB+Kfon3Uy9U3tZFGaiTY2vnzv9ZtpE8DVBg5nQOrh/25giepKgSqJpE7Od/dDmK4QXFM4FgcXpqVEgWyBhOhcUguujj86xpUFucJ14zad+20LzR/SZ+bOkic8X/KwUUTDfflm9afI2jYuT MS54eq// ReQey9ZIcKMY1J4Uixcdr6i/a0vWMD3SnMEGWFO/Uu09yPXNlKLlRdAuYy1Gnf9M285Vu32nP1gdynP2B6MWoovGg2JCS6zSg1+he+NqZaPDCQbduUNXAaKgIUyZPb4NpRHLopQUE7xeqGRf4ram5WjANW9ceqhvtPM+osLRKdIyoAPbbJf22xXaH/ytdLbpEcHxFtXmAeDfpOnnnujf3KODEUtVdlCQJFlMeIyDrSbhSDhaIF2eSMLdPtl9/JzoQy9NK/ZsHAC55ecmgJuwolvy+J+g2IW1VmY01l647FjWqGndd4uFCcqPlepVwEQhCUqNWoLPhAheFpD3xicu8cQ9Yp0KrjD54vII24q32nHJrNHY/tubuwPCreFaremEah1oxAfR6Z1ZqS18IjpdaXGz8TC9LkJbJ17iUzVNkxbg9SbFQ5jFERAtF/ieRapWZpFUxDdC9uZ0ef2fIKRZIGwktuOZ/o1tYoE7SURPnM5M5fKX0DsUs29fBVWhlDdyvFElwt/nrBVytdU04O5wCjgzOIuWdePjpm5qj0Gb/gm5oPpm4OxeWcQ+KOComE3se4FKp 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 70d5351427e6..46a92e41b9d5 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -718,6 +718,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 8e06d29961f1..857059c8d56c 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 5eeb9bf7e324..fa49602194d5 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) @@ -20329,6 +20341,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);