Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp574680pxb; Wed, 3 Mar 2021 09:52:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJw1on+WLtVMwJdELpeu3hSvfiZ5qIKjYK3tgj7ux5Z/Oz1+Py6XmKDk2uAkv+JWY0t5g/of X-Received: by 2002:a05:6402:30b9:: with SMTP id df25mr506160edb.136.1614793920227; Wed, 03 Mar 2021 09:52:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614793920; cv=none; d=google.com; s=arc-20160816; b=a2X2JmjtcvUuAnoLQR3PUUsQgmb6aG+McGR1xB3RposM7eqrqKRn/NKXKysFF4Y2TC NBQ7vAfMAf+UoC2A7XLcDVJBz7JImUf7xQtLl5jMk5Fd1DRQLVn85D7K95JMQiNTfUBC hdPp8yPezoXxZCJ4t7TQa9EpFdBls7O2wNwBysbR0QIDQ9xd7FT/E/Vkgxw57GDdso0L U2KzFhhtKyGWAhZf8Iss9hORrmJLFWumfT1QgrilfuymHm2qxGlQFIPmt0dIiyYuHKzm c8K4rVYXF5BJRjGfW3trqOwBlCfiT67I9IGG+8BIzJBQZGYPvmG7CFCGKhLfCFUpy2ew 6WfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xu3BL3u7IJPg6jrYc3Ot5Go8F3dakXs2YWF3o/GOClw=; b=qX2KoUUhtXK/lz87HSwPSd2ZKBQZD/8E/qmi0Fr8Hfuf8rEhXWQB2+of2pU5Hz0TcQ fCEJ/gonfiB/E1X7U5oTPA2rSVVKUETko9QKTuU3nQti9NmdmryDc4ylyBNuJigvxqLc VtkNBcuRL3sj1mTMlzrx5dB5Bf2DRbc8Qx5ZvAqGDtQ9VowjDdFNMuGA8+Ksy6Z8JHZG vHfSJMBdFWUmcZ5zPPSYRNhs4lsdOHPa+pVXr085ydjGTmfXRdIcSxU4WJvDdKYjCFWs YVVTAVUZEbVwx8WNcSzm9bI+KZmoqY0Z1oa0gr7wawnsyLZ5A0BT4Vm99OYRIprP9NST vZfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=n24clUJ0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t13si14926568eje.700.2021.03.03.09.50.37; Wed, 03 Mar 2021 09:52:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=n24clUJ0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240431AbhCBA7k (ORCPT + 99 others); Mon, 1 Mar 2021 19:59:40 -0500 Received: from mail.kernel.org ([198.145.29.99]:54534 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238088AbhCAStl (ORCPT ); Mon, 1 Mar 2021 13:49:41 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8AF0264F7E; Mon, 1 Mar 2021 17:08:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614618517; bh=s/KjInfOI6Bcqyc8z6zHefJ0Y+U7aME1kP5vyxxf3/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n24clUJ0/mrFEshbEJ3bZBsBwR6RqY+yRjAWt5UUADul7U9uicJJ0QQ4KesjZkaxx H/22tFQ94t2uEohLsiye8iGmyLB0CcOHZcJ/GMt6KEMSqT4xiVvtrZFtX9hSDkU1PV 0KA3RaXIUxET+ArD6stuAwWFaGE584uw7S9U4cQ0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Martin KaFai Lau , Andrii Nakryiko , Sasha Levin Subject: [PATCH 5.10 115/663] libbpf: Ignore non function pointer member in struct_ops Date: Mon, 1 Mar 2021 17:06:03 +0100 Message-Id: <20210301161147.432354479@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Martin KaFai Lau [ Upstream commit d2836dddc95d5dd82c7cb23726c97d8c9147f050 ] When libbpf initializes the kernel's struct_ops in "bpf_map__init_kern_struct_ops()", it enforces all pointer types must be a function pointer and rejects others. It turns out to be too strict. For example, when directly using "struct tcp_congestion_ops" from vmlinux.h, it has a "struct module *owner" member and it is set to NULL in a bpf_tcp_cc.o. Instead, it only needs to ensure the member is a function pointer if it has been set (relocated) to a bpf-prog. This patch moves the "btf_is_func_proto(kern_mtype)" check after the existing "if (!prog) { continue; }". The original debug message in "if (!prog) { continue; }" is also removed since it is no longer valid. Beside, there is a later debug message to tell which function pointer is set. The "btf_is_func_proto(mtype)" has already been guaranteed in "bpf_object__collect_st_ops_relos()" which has been run before "bpf_map__init_kern_struct_ops()". Thus, this check is removed. v2: - Remove outdated debug message (Andrii) Remove because there is a later debug message to tell which function pointer is set. - Following mtype->type is no longer needed. Remove: "skip_mods_and_typedefs(btf, mtype->type, &mtype_id)" - Do "if (!prog)" test before skip_mods_and_typedefs. Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support") Signed-off-by: Martin KaFai Lau Signed-off-by: Andrii Nakryiko Acked-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20210212021030.266932-1-kafai@fb.com Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ad165e6e74bc0..b954db52bb807 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -865,24 +865,24 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map, if (btf_is_ptr(mtype)) { struct bpf_program *prog; - mtype = skip_mods_and_typedefs(btf, mtype->type, &mtype_id); + prog = st_ops->progs[i]; + if (!prog) + continue; + kern_mtype = skip_mods_and_typedefs(kern_btf, kern_mtype->type, &kern_mtype_id); - if (!btf_is_func_proto(mtype) || - !btf_is_func_proto(kern_mtype)) { - pr_warn("struct_ops init_kern %s: non func ptr %s is not supported\n", + + /* mtype->type must be a func_proto which was + * guaranteed in bpf_object__collect_st_ops_relos(), + * so only check kern_mtype for func_proto here. + */ + if (!btf_is_func_proto(kern_mtype)) { + pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n", map->name, mname); return -ENOTSUP; } - prog = st_ops->progs[i]; - if (!prog) { - pr_debug("struct_ops init_kern %s: func ptr %s is not set\n", - map->name, mname); - continue; - } - prog->attach_btf_id = kern_type_id; prog->expected_attach_type = kern_member_idx; -- 2.27.0