Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3696119pxb; Mon, 24 Jan 2022 15:37:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJzAatKZA+JY5tDshRDUAHSHFrDZD9wH4gFDuQgEzNOGgo/+ISD3++4S/gsNcCw1VJ6S8FJQ X-Received: by 2002:a17:902:b781:b0:14b:68ed:cf6 with SMTP id e1-20020a170902b78100b0014b68ed0cf6mr2662851pls.79.1643067444885; Mon, 24 Jan 2022 15:37:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643067444; cv=none; d=google.com; s=arc-20160816; b=VjUyK3+UZSisP5XVuM7bcEmhshkkBdwvPnPGN7TECy8508uyzTGdg8L+afdAljeN7s rGA+OVqzxQ1QIrswFzOPdZf5CktahAQ/G68Su4qqX3BI5S0Dd/y76sGs+r2gjLdhnzVI kU6gCRZLHx/Qk3BVtypOicX6o8atf3TqdQEdlCh1mxkOmVDoNAM9nNj9nwgKsJxw/F1o Mw37o4Aj9d/CudCzmBHVe2iTz3dRomgYrOVMmLNxplV3u6f8YY68uM06yCJ2uTSuiwTG IINW1HPpgEcOKUJNn7GQLCCrudob3TxALkwfrHpViGElEvD5Ma9P1Yp/128TzVv9oQU+ cV4w== 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=FOw6lI35D9SkB4kPvUUN839s8iRA7otw5I6zs+Xe9RU=; b=GkB+cKvVZBxQ6fL6YJL832aTE+Z2i9ssRtr2IbfSafCyDJ6aIz0giqb096iE7btlLP 9JxxEI0iZfj/15v6IzmXGXhmDClYiejLaqxduTXfbAlRSGhTKUCv3Tl471G+yiXGC8fU imF6fS+SyxS7+mmcVtKB0nwK1K52vR/mmgLp9BhrOEr3/aQT9toO4WpRw+qmg/ItHIb0 IXOMz5d/iBsPxtKZrkZmgmAOxuy3hhUUROPsrYGXvs+y2jGxBL0FK/96/n/ociI9ewpD sUFSuc8GrCNey/KbtDRVer5mtNJ51Qi8IIBDnGdtU0qAgBKFijRBt4JeSu4IgKZJfNyG /avA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0ltuypwT; 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 t16si6837530plg.315.2022.01.24.15.37.11; Mon, 24 Jan 2022 15:37:24 -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=0ltuypwT; 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 S1851063AbiAXXbu (ORCPT + 99 others); Mon, 24 Jan 2022 18:31:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1836489AbiAXWjg (ORCPT ); Mon, 24 Jan 2022 17:39:36 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C51BC054871; Mon, 24 Jan 2022 13:01:03 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3434AB81233; Mon, 24 Jan 2022 21:01:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51387C340E5; Mon, 24 Jan 2022 21:01:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058060; bh=Ix2i5yklliOEQD/vcWRxVdDk4r7GXMXZENMCJQoSCpY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0ltuypwTLpxBDaFSCin5BANZZOpkI3H9HFoGL0BoxhJwf3S1QYPRDfcTXerJeJO6/ ltSCw7ztBP3S6YQ1r/E/Qfs3JzOqPGeLsub/KXRxnUqZOpAKbE7P/IV59FVL2jMaGx Oh+94zrK04agrR3FwpEq2C0LNNZSBHYu++ibuw3g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Daniel Borkmann , Song Liu , Sasha Levin Subject: [PATCH 5.16 0169/1039] libbpf: Load global data maps lazily on legacy kernels Date: Mon, 24 Jan 2022 19:32:38 +0100 Message-Id: <20220124184130.907300076@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@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: Andrii Nakryiko [ Upstream commit 16e0c35c6f7a2e90d52f3035ecf942af21417b7b ] Load global data maps lazily, if kernel is too old to support global data. Make sure that programs are still correct by detecting if any of the to-be-loaded programs have relocation against any of such maps. This allows to solve the issue ([0]) with bpf_printk() and Clang generating unnecessary and unreferenced .rodata.strX.Y sections, but it also goes further along the CO-RE lines, allowing to have a BPF object in which some code can work on very old kernels and relies only on BPF maps explicitly, while other BPF programs might enjoy global variable support. If such programs are correctly set to not load at runtime on old kernels, bpf_object will load and function correctly now. [0] https://lore.kernel.org/bpf/CAK-59YFPU3qO+_pXWOH+c1LSA=8WA1yabJZfREjOEXNHAqgXNg@mail.gmail.com/ Fixes: aed659170a31 ("libbpf: Support multiple .rodata.* and .data.* BPF maps") Signed-off-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Acked-by: Song Liu Link: https://lore.kernel.org/bpf/20211123200105.387855-1-andrii@kernel.org Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 509f3719409bb..4050a0f8dad66 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -4988,6 +4988,24 @@ bpf_object__create_maps(struct bpf_object *obj) for (i = 0; i < obj->nr_maps; i++) { map = &obj->maps[i]; + /* To support old kernels, we skip creating global data maps + * (.rodata, .data, .kconfig, etc); later on, during program + * loading, if we detect that at least one of the to-be-loaded + * programs is referencing any global data map, we'll error + * out with program name and relocation index logged. + * This approach allows to accommodate Clang emitting + * unnecessary .rodata.str1.1 sections for string literals, + * but also it allows to have CO-RE applications that use + * global variables in some of BPF programs, but not others. + * If those global variable-using programs are not loaded at + * runtime due to bpf_program__set_autoload(prog, false), + * bpf_object loading will succeed just fine even on old + * kernels. + */ + if (bpf_map__is_internal(map) && + !kernel_supports(obj, FEAT_GLOBAL_DATA)) + continue; + retried = false; retry: if (map->pin_path) { @@ -5587,6 +5605,14 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE; insn[0].imm = relo->map_idx; } else { + const struct bpf_map *map = &obj->maps[relo->map_idx]; + + if (bpf_map__is_internal(map) && + !kernel_supports(obj, FEAT_GLOBAL_DATA)) { + pr_warn("prog '%s': relo #%d: kernel doesn't support global data\n", + prog->name, i); + return -ENOTSUP; + } insn[0].src_reg = BPF_PSEUDO_MAP_VALUE; insn[0].imm = obj->maps[relo->map_idx].fd; } @@ -6121,6 +6147,8 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) */ if (prog_is_subprog(obj, prog)) continue; + if (!prog->load) + continue; err = bpf_object__relocate_calls(obj, prog); if (err) { @@ -6134,6 +6162,8 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) prog = &obj->programs[i]; if (prog_is_subprog(obj, prog)) continue; + if (!prog->load) + continue; err = bpf_object__relocate_data(obj, prog); if (err) { pr_warn("prog '%s': failed to relocate data references: %d\n", @@ -6915,10 +6945,6 @@ static int bpf_object__sanitize_maps(struct bpf_object *obj) bpf_object__for_each_map(m, obj) { if (!bpf_map__is_internal(m)) continue; - if (!kernel_supports(obj, FEAT_GLOBAL_DATA)) { - pr_warn("kernel doesn't support global data\n"); - return -ENOTSUP; - } if (!kernel_supports(obj, FEAT_ARRAY_MMAP)) m->def.map_flags ^= BPF_F_MMAPABLE; } -- 2.34.1