From patchwork Tue Feb 6 22:04:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547837 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 8BFDAC48297 for ; Tue, 6 Feb 2024 22:04:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 08B0A6B0083; Tue, 6 Feb 2024 17:04:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 03A706B0085; Tue, 6 Feb 2024 17:04:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA8FC6B0087; Tue, 6 Feb 2024 17:04:56 -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 BE2066B0083 for ; Tue, 6 Feb 2024 17:04:56 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 880921408B6 for ; Tue, 6 Feb 2024 22:04:56 +0000 (UTC) X-FDA: 81762759792.02.E89691C Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf13.hostedemail.com (Postfix) with ESMTP id 9E57320014 for ; Tue, 6 Feb 2024 22:04:54 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ik2rmXIE; spf=pass (imf13.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.179 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=1707257094; 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=LK9+W+4b3xe9fbC75kRHXxIUFpMtWIXIyU/q+EZWewm8tq3r3V/kflnVGKbHTQ8umMZ8if 9+zXX9Mj/mdGTXsW+n/VBt3JbZ7FAnsG7BLsgQjcGxqvvxjEVpUypewai2foj5fEcm9b5k ozMpF4np6wCUbPBJWiOlbIy1xhuCqis= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707257094; a=rsa-sha256; cv=none; b=HID+eWJsfS8IlxYhEB6SApbS9Pq5MoY7JlmmbfLFO+ePcY9mpFwGzMmVdqVKcXx3MKSYbu F5EZbW48oLcVIsLhW6PRongaDDXiZFMAZRJmIiVQiYar1dduHWX+2cSwbn5N+yBiZmMUWg Dqrs/i5Wc5zwuSizGvpCRUGiRRPKkGA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ik2rmXIE; spf=pass (imf13.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6e055baec89so6021b3a.1 for ; Tue, 06 Feb 2024 14:04:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257093; x=1707861893; 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=Ik2rmXIEXhxqT3/BUy6tkg+7XSHUYfM2MTYdZHRU/MgO1Kvnd2hUN90kc6aLS38Y89 8tIIdHDQ9FZj59chcVtFsqjVaeMt4yPKC0fpfDrctQaLom8lYnHPjnIUlekEV+/vqs53 /gg4Zl8oVtkzmvEmL4mvjlFnCOUHnLHXB+AjlX6a1111bJxzcK/cVMcfDjc1UuZUvcn2 1oMQD4ILmDoeFQHhDAtWbSoJJd4foK9YpVgHV/IohYBAmTAiCbeRKvLSlD+mMHsXdbLv Vhc6HcXskzCxgHne/POjYMGg7IbOByJY0gr1mjyaj306zlp8fHt4OjXAOw2hPE9Z+db7 e5vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257093; x=1707861893; 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=mc+QuFJIW5mi3M3aeX2bCmuaGnRS2veX8INWo00A5J4rOTFOBorQeUcmcCiaih7c8/ k9uya9SVxYVrVS3SsKbI5T88wR4hGRCS1D9lO9gGN+M6ii5QKkoRKWiKNzKAZsV74BGq s1EsqvZM8uoq1fSLNkwqw9oCK+sPO6pwPGy0ySktd7bdFFDASdeUOwC1EWCu5dJT6C6e m6OJvAxi1Jy4xEoR4GyKXYxtLHtdbls17UanmgVF0NmCJmdJ3R/qKfaxs+Qd14G4qeD2 lcY9h5N4bh+bau8EsnMSFx1AZuH0/fcPuSqRH72oZnXg0kmDLDwaqXaPHzUV+RAuB8DW c06A== X-Forwarded-Encrypted: i=1; AJvYcCX0NZ2tVvxWFjp3x6NH0fhAG56tIK1e6rkJUHdnw+2h8SEkzINPWVBMcRJW9UiIhY2xoDQI/8UyiTpLN3+LH2LS6ic= X-Gm-Message-State: AOJu0YwatRN0199h1h6kk5RrZxT0E7lWrOvvR56yvI8nScX7BOdIhyVq /lxl2JhnwbNL9mPKO/cjvzqDLAkH2VO32YFVf1UXa3FZUgCisX1q X-Google-Smtp-Source: AGHT+IGOIOdH45J5dyDgzanegb8JLdqXenulcK+qVHNvlZfYkUWkLxixF+6eKSWhF7qb3dfASt+aRw== X-Received: by 2002:a05:6a00:b93:b0:6e0:4f30:bcfc with SMTP id g19-20020a056a000b9300b006e04f30bcfcmr1405410pfj.9.1707257093567; Tue, 06 Feb 2024 14:04:53 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWUQoVkIthQQOsy6U4eSoQ5WdjVc0nJCVsDbkA2v0JGF6Yabw2lW6vfJzu68baFahsluKlv1NXHpG3TR+PKOZ23wdIUvI68+73W6afFYOGp3111MbSF540b1PZ6MPixqg3IhHR0uNOvN8cle0tWOioXnGsAI2TDADOi5Fif9LN8YHbGgy0QINttwPB4URjcnMqY8BB11Ij1d6XI/7mcn2HIpchSHafTgbAynmFH5kUNenEDFjaaPYgtauQDqNmqat3SfznSOSAwQYvn61xLPdSXcFtvHj6P1zvY Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id y20-20020aa78554000000b006e0322f072asm2488200pfn.35.2024.02.06.14.04.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:04:53 -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 02/16] bpf: Recognize '__map' suffix in kfunc arguments Date: Tue, 6 Feb 2024 14:04:27 -0800 Message-Id: <20240206220441.38311-3-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: 9E57320014 X-Rspam-User: X-Stat-Signature: tuy4ht5uey6inxe9oupd5figgdbebnet X-Rspamd-Server: rspam03 X-HE-Tag: 1707257094-642241 X-HE-Meta: U2FsdGVkX185E1McaxdEg+ma/Hbzl2TNS/TCwzNhykouZmarElTYLBqcehCUNW5Ui7AGD/bXFG5dnM98vfFVEpA5MqlsqhntTPScJTVrOh4uLv/Q5n6jlh2/k+XtV6yPNXhwqTJywz4E05q5t73O+Ashnsul6dmILa9rtCIHcFcUg/hPv/jnRM+eZEnloggI46WuchNGtpy5ym5Ocddky/nRD7LTY4i24kDK21W0n6fD2fNGFDdv4Z6mxSj+DQ0bZLg4pC9wc+fvLg1gVY2LzZ0muQXjse5UBly7A3u7j6u0KbWj6S+jzwGafZ7VLU4az7jORneTaYAkz0hVAjD14YTDzBRzggZ83k9OIFMmVC+zNZ45+BmnHythI1pTTQNiC9gp86eJs++dcuPHQCay5jLk9kbjRo6ndjpGtM6Us3/+t7rDtFt4NgsU5r69geYF9vpWQ8++gXIcl0OJ3RBLy0wRgfyUKF59Bhc6lbQr19ptMoO3MpRppJI20HoFAEyXK9i0lczac8qzo5MQn0bIgrbTE0uRibCKsLNBzAFttV1zPAOzyGO8xwB5fNYZModWQslzfVSOEr4hsc7KgrmpRl3RxSVM65JIbvCz0OOBCO43tE5xTRagiVbQfM0E34NBEhnSG4nzdU+GrFTMRHXVzuTEDtgVwB5I2pvUwtks8eNvAGmFL0hAs9xO5JM47zI0fPYDsrydYUNa+aml0D3QyQdl/HpathIO/wJwlMEM5k3JRmmKmzZ6UpOHkut6z6+brYkzioaFfgnvqmlkTwY8JNO6JZVKF59rTJOkih3bSUNN/dZ0gpIoL6Xlg2Jh3GXLigh2eInqjibnXPFxjouAn+YnpQGao+UZnvPMFQw6QZPEP5QZ3G2BwekMV2xZvALlgKtMxdZ8jh/CNaCh70CQPa3Qh0Hjdc6IaUwIeCMFJU6PmsTkGLIoiUjFFOwKGuV/uhp3/4vxWO8QPp8KZ6N qiIS5Zus KNux7XIb7rLFbHvMe7SiC0xGjyTdfMpB/oxDjb1Qshcen8DweJnG9M5bvJ2xnamO3d+IdbGUVInrxY1mDV0j04vwAyzC/884zKcIF2a+Tn9R1z3Z1PyriSb8KKuxnCcJ0FXubsztvrj8vBuK0l4Lsp+fHYdz6qj7wElYaOShY4yUSRFy0mY3EIlELEblYzcAgwJQ1l8W1kW8I65pWgw3+/PfbuH++OIQJLPjTuv1P805kFD8toniD+It51Qjxhw9Yh4pbnQFIfBEYM54NhqDACsMyZQly3/LIWAmBn1QPYRucT4Cq0xck1Jq77guOWT+xkzLrB3avvk7q9hI+YWduySgBND3HZ39cNgRtnkGFOCF72G3l44iprrgegqyXowAPvxV1V/1BtVDIO0/+H/ydDAjQPySnCsmgCbF6ym7l9+9xp+RDLGSuX0Cs83BKfdz2B1fgbtvv8TvVCA2XEzXJB8tpwuVkaTDogMWee1wO7pT2TH88cLGiAJpAYwHtA3gk2+Il/x0/Zz8OphXpFA02Zy3Jkn2uv36E0pLBF3kNU+X6kL/wSKBbxmRJmHU9tgJO/lT9nSNOVcuarTxuTriiofchRKd1CoDRJ3VBRwRD/WFDA1lL5aakP1MaIYnZyYkSnPEvCzwrpSARTGs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.004168, 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;