From patchwork Fri Feb 9 04:05:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13550843 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 8F738C4828F for ; Fri, 9 Feb 2024 04:06:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1ED596B0080; Thu, 8 Feb 2024 23:06:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 176706B0081; Thu, 8 Feb 2024 23:06:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F31156B0082; Thu, 8 Feb 2024 23:06:24 -0500 (EST) 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 DB14A6B0080 for ; Thu, 8 Feb 2024 23:06:24 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 853791C19C5 for ; Fri, 9 Feb 2024 04:06:24 +0000 (UTC) X-FDA: 81770928288.25.F5D34CC Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf25.hostedemail.com (Postfix) with ESMTP id C44AFA0012 for ; Fri, 9 Feb 2024 04:06:22 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HwuxSRfr; spf=pass (imf25.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.177 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=1707451582; 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=RYMIDAZLgBUgMpP99zrxeTSMtNJZxQwPNyQYFWZYzso=; b=XNYaj9T+wc0VmXETsrJP4MHQnm1F/A4sNqr0AAzcHakTCuj4hnH30fwzeoi461Z2rtMUPk eB/h/Uyf+NudBAVX3WbWHtixYsOx1VcNVnw9LSgwUv1K0P3fQIG6z8lUFxl1G9NL0JKLOe bxdmw+dhle75MI2v533ZPF8lUk7pEWk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707451582; a=rsa-sha256; cv=none; b=crmhwvIgpYJi0A9Nhz6QK8KXaj/n/FEvPuaeD+25Snn7cOyt7iw2D6G2QUry1fXgXlost7 exQo60LLU2AcpKuxBPga81sXsSwuxp4kUoG+Npm5CDbL1QPuH32adOeldQonUYAV19++wH O6upJmjsAiF4IQTia+2DO4PG0y4g6Sw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HwuxSRfr; spf=pass (imf25.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1d9bd8fa49eso4309415ad.1 for ; Thu, 08 Feb 2024 20:06:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707451582; x=1708056382; 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=RYMIDAZLgBUgMpP99zrxeTSMtNJZxQwPNyQYFWZYzso=; b=HwuxSRfrEXp+kQNpXeoVKMN3yYJuenoQ8eYcnCq0ciw5I4VzrI4ujzeDG+bLaJLyb3 77GCN1hkJdE1J1H1ToXJHuXHPDil+lYK3zICyT1kwXTCE33fp7KLbMyQ0fs19vwKtcE7 7G1v2I6sGpnP1e9b/Wt8P+nBDxSKbe2FIh/vKAvAQM8WIs0210YJuA3/iASnW1aBYEIj tILl1blZzW2AzjQIxQcvpwiVD50O3mawZnzE8hyqR5z8EevIrsnRHhwGQnUlUq/aPDJF f0MCaXY2TyXqnMhs4CGkw90C5JNsXlqOIs5TlXh55zYckU/6yRZ/u1hEWMRxOHNqvRAc lcpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707451582; x=1708056382; 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=RYMIDAZLgBUgMpP99zrxeTSMtNJZxQwPNyQYFWZYzso=; b=Lbmm8N/kHMoQk8p2aics+v03K4Xgi/4cp7iwmMSOEARLndQr5gJo16Kcdsrz/WbmHk CjOgessNEwVkt5TvDymt3hM5KOKej6k7UrJmcXTFuUXMuyHSrAaArn6310wzgrjs6WH/ r9RpjzPMQHVLUT8FgiJZTW01vggvCppmHyGtJfWQiuI730W11i2rp9HlzA+PoO0nnvez hGVbFCy4XF4KObWbByYchgXsDqVX0KfOvDnfhFkBX1cdupcf1D8V0sL1NEE4MuSKS7G2 fDdqPMPZRNvFUm5DUXHmK+CEalMWeoYhGOScy7lBhWienA3ex0rynkUKFud6LbO0GIOa GG2A== X-Forwarded-Encrypted: i=1; AJvYcCWZ8MRb3PCEKrsqyS6c8OQgaJW1mBkbKwq8SDB3thv/AiimBgw5DIdHhn2+M01XycmqPhcg5NsWjUMY/ay7pScIlGs= X-Gm-Message-State: AOJu0YyjWJuKoZ1cpWsMWXEGmNGe/qyk5/xd+6BtobnELtya4dOcvhq+ kHhj31grhgADGngAz0E04lqSojEDgn/MA0AAW9NtsYnpzd65t6Vyxrljpqab X-Google-Smtp-Source: AGHT+IG7W4w6iclNa9lFghGZkrapWzAeDlADw31sucf55LWFs70wKngA8+n/+OS3WjIjVQMSK9dgJA== X-Received: by 2002:a17:902:e549:b0:1d9:6dc8:b44f with SMTP id n9-20020a170902e54900b001d96dc8b44fmr349803plf.1.1707451581624; Thu, 08 Feb 2024 20:06:21 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCU9TUB8D0L6Glwm5xlI3JvnBt7zEVn8IOywhbt58OfQBsWE4k4qMGs4CWoKHxyxuTzW1lD9onprEAHbY/BQVbHwXNg9VmhIZInBkRh8ifJLD08aWjb3T+K0tDAVFrboxdJ+d8n2YA52cdz0aPx0veAq9E4y39FA4QYAbouAb2omG9KISCOMfSfKL6a8oDHvwuifT9JSGK4q6WX0S4EqCjbHGubv+u7IqGmSdLK7SIiYC28SWmhCj/ydGdJrKud4QkLYUh1W+4dZWd/d9cSw1xwgy0LsiufRVZBXcgKL3wjIvYg5eJlRcFjB7OjTKhqUSIarEy3JRhCDPQNO/jtaRfHoaboeoMjjcVd7WbWx0EEabK2p/ysZuw== Received: from macbook-pro-49.dhcp.thefacebook.com ([2620:10d:c090:400::4:a894]) by smtp.gmail.com with ESMTPSA id ku6-20020a170903288600b001d8f82f90ccsm538134plb.199.2024.02.08.20.06.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Feb 2024 20:06:21 -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 02/20] bpf: Recognize '__map' suffix in kfunc arguments Date: Thu, 8 Feb 2024 20:05:50 -0800 Message-Id: <20240209040608.98927-3-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-Stat-Signature: w13jtyir5b5gojmxz5ytz51q8k16jruj X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C44AFA0012 X-Rspam-User: X-HE-Tag: 1707451582-700297 X-HE-Meta: U2FsdGVkX19CColYlHLLqOQwUNqQamQkJTxpvqfLCOZZ6InHOzMUhkpHIl68wKcaz1S1oJFv4Yj6WwHdodTEqJxRTNq7rIZxygsi8WfKJEAmHjlzKfNJJbt/3+lCsRUBBWAF8Fw3M6eQ6FXy80KcHDk1azdKwIBMfWaJsXPNCRASfLzTNauN+3hL+/SecHQ9vAZ0htxa0PDPfzPSfZZ03ohWqR8krjRsGSTl+/FSEAFaiF/zpMLGAWB+gs3XAh7S/WexIIJ1mhcWxIp+IRBnDbE1D5IRX6GNrqkAcaUEeNeBUcpeCzcuJGNFEcw/a1pFTEKxiCxbj/anRq6o1MS/o4wWqAZOgmhEvqJup74Ppb1rzauyQ9oPBU2GfO7+9hLAkHuqiUBHw89o0XhSSm84jigWCAqSjJGxF9dnUTDDxt2Q8b9fmM+i3vHenT+nISoC1ARfXcQvKorE1v+1c2v/IQm4SVorLJneQT59lMHighdlNJpSKmNXBHNJIVHqWHZnVL+c9GpXv1+HnGzu4kDA7Dpk8XlNy6QiHD9ZHUTosdc3M6dp4EtBZ/i+1JxpG8Aeo+UNqsSsBBnhGG8Uy+WpHUOetKemMamVkGV8fvfU86syppJwKOxof+9rIbVTedOClQAfC4t/+pbCmD7eRkbve2kqV8dHO8VXwubrLZCinBz1Ukux9VfHIyPcYcj9UARxLk/rUQ/O+0IvBsdNv1doTDek/pk9Lz1lnbBnBpUVAtqtHK6w7SkdIXb5sBCrzoJynmKyd0dtQfHDXn6h4yALEmiFQd7LkoBc4DQXRCRwqJaguhqdiMuG59lNBNOG3jTMbXRwmrbwL+cf6QOmUq/KyEuRd1zdJ00wAOv5H/98PsDoHOj12y8IVdz2mnDDFCkKZNHsFAcei1GY7IIbhJ/mozOdvneg4qGaqadGJcCHHfsemYT/iOE+Waw4wA9OJHTewUh+lQJp3z1vmvEwu43 WiLuHsmq BB6FUJ3WmCMdbNTNoej5xCxKztAdnVOi6v+/xCP8uHPeGEuvMh/FZrEjT2mkDWqPIUscshDU5TUcgXHh64ruBd4iPFMhiPRU5j1N3tnKZUGL4DyuhnYMPjY06s88RkqWRJnjnMNW3ZxEiUjuSxBRx/b51JJmuU16D0PL91/Lx2zdaDSwvgwbrfvvCps6Q8H959Sjw X-Bogosity: Ham, tests=bogofilter, spamicity=0.010018, 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 Recognize 'void *p__map' kfunc argument as 'struct bpf_map *p__map'. It allows kfunc to have 'void *' argument for maps, since bpf progs will call them as: struct { __uint(type, BPF_MAP_TYPE_ARENA); ... } arena SEC(".maps"); bpf_kfunc_with_map(... &arena ...); Underneath libbpf will load CONST_PTR_TO_MAP into the register via ld_imm64 insn. If kfunc was defined with 'struct bpf_map *' it would pass the verifier, but bpf prog would need to use '(void *)&arena'. Which is not clean. Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d9c2dbb3939f..db569ce89fb1 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10741,6 +10741,11 @@ static bool is_kfunc_arg_ignore(const struct btf *btf, const struct btf_param *a return __kfunc_param_match_suffix(btf, arg, "__ign"); } +static bool is_kfunc_arg_map(const struct btf *btf, const struct btf_param *arg) +{ + return __kfunc_param_match_suffix(btf, arg, "__map"); +} + static bool is_kfunc_arg_alloc_obj(const struct btf *btf, const struct btf_param *arg) { return __kfunc_param_match_suffix(btf, arg, "__alloc"); @@ -11064,7 +11069,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, return KF_ARG_PTR_TO_CONST_STR; if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { - if (!btf_type_is_struct(ref_t)) { + if (!btf_type_is_struct(ref_t) && !btf_type_is_void(ref_t)) { verbose(env, "kernel function %s args#%d pointer type %s %s is not supported\n", meta->func_name, argno, btf_type_str(ref_t), ref_tname); return -EINVAL; @@ -11660,6 +11665,13 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ if (kf_arg_type < 0) return kf_arg_type; + if (is_kfunc_arg_map(btf, &args[i])) { + /* If argument has '__map' suffix expect 'struct bpf_map *' */ + ref_id = *reg2btf_ids[CONST_PTR_TO_MAP]; + ref_t = btf_type_by_id(btf_vmlinux, ref_id); + ref_tname = btf_name_by_offset(btf, ref_t->name_off); + } + switch (kf_arg_type) { case KF_ARG_PTR_TO_NULL: continue;