From patchwork Tue Jan 12 07:55:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 12012633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.0 required=3.0 tests=BAYES_00,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9FDE5C4360C for ; Tue, 12 Jan 2021 07:56:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 56A4F22E03 for ; Tue, 12 Jan 2021 07:56:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392153AbhALH4Q convert rfc822-to-8bit (ORCPT ); Tue, 12 Jan 2021 02:56:16 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:59226 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731058AbhALH4P (ORCPT ); Tue, 12 Jan 2021 02:56:15 -0500 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.43/8.16.0.43) with SMTP id 10C7rNY7031054 for ; Mon, 11 Jan 2021 23:55:34 -0800 Received: from mail.thefacebook.com ([163.114.132.120]) by m0001303.ppops.net with ESMTP id 35y91rv8wr-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 11 Jan 2021 23:55:34 -0800 Received: from intmgw003.08.frc2.facebook.com (2620:10d:c085:108::8) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Mon, 11 Jan 2021 23:55:32 -0800 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id 86CA92ECD646; Mon, 11 Jan 2021 23:55:23 -0800 (PST) From: Andrii Nakryiko To: , , , CC: , , Hao Luo Subject: [PATCH v3 bpf-next 0/7] Support kernel module ksym variables Date: Mon, 11 Jan 2021 23:55:13 -0800 Message-ID: <20210112075520.4103414-1-andrii@kernel.org> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343,18.0.737 definitions=2021-01-12_03:2021-01-11,2021-01-12 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 suspectscore=0 impostorscore=0 malwarescore=0 priorityscore=1501 adultscore=0 phishscore=0 lowpriorityscore=0 clxscore=1015 mlxlogscore=929 mlxscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101120042 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add support for using kernel module global variables (__ksym externs in BPF program). BPF verifier will now support ldimm64 with src_reg=BPF_PSEUDO_BTF_ID and non-zero insn[1].imm field, specifying module BTF's FD. In such case, module BTF object, similarly to BPF maps referenced from ldimm64 with src_reg=BPF_PSEUDO_MAP_FD, will be recorded in bpf_progran's auxiliary data and refcnt will be increased for both BTF object itself and its kernel module. This makes sure kernel module won't be unloaded from under active attached BPF program. These refcounts will be dropped when BPF program is unloaded. New selftest validates all this is working as intended. bpf_testmod.ko is extended with per-CPU variable. Selftests expects the latest pahole changes (soon to be released as v1.20) to generate per-CPU variable BTF info for kernel module. v2->v3: - added comments, addressed feedack (Yonghong, Hao); v1->v2: - fixed few compiler warnings, posted as separate pre-patches; rfc->v1: - use sys_membarrier(MEMBARRIER_CMD_GLOBAL) (Alexei). Cc: Hao Luo Andrii Nakryiko (7): bpf: add bpf_patch_call_args prototype to include/linux/bpf.h bpf: avoid warning when re-casting __bpf_call_base into __bpf_call_base_args bpf: declare __bpf_free_used_maps() unconditionally selftests/bpf: sync RCU before unloading bpf_testmod bpf: support BPF ksym variables in kernel modules libbpf: support kernel module ksym externs selftests/bpf: test kernel module ksym externs include/linux/bpf.h | 18 +- include/linux/bpf_verifier.h | 3 + include/linux/btf.h | 3 + include/linux/filter.h | 2 +- kernel/bpf/btf.c | 31 +++- kernel/bpf/core.c | 23 +++ kernel/bpf/verifier.c | 154 ++++++++++++++---- tools/lib/bpf/libbpf.c | 50 ++++-- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 3 + .../selftests/bpf/prog_tests/btf_map_in_map.c | 33 ---- .../selftests/bpf/prog_tests/ksyms_module.c | 31 ++++ .../selftests/bpf/progs/test_ksyms_module.c | 26 +++ tools/testing/selftests/bpf/test_progs.c | 11 ++ tools/testing/selftests/bpf/test_progs.h | 1 + 14 files changed, 305 insertions(+), 84 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module.c