Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp788161lql; Mon, 11 Mar 2024 18:56:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXkzNE9PzSEUysbiH4/LBQMMrt64mk9S1pQlz3eXVM/sFEO7oR41x6Hkuc4EyGy0jZkLV9M8j0CEEtfFQjnp+uugM3rdWQeJHHdRVe7og== X-Google-Smtp-Source: AGHT+IHloQystx5kFdlzp1RvE2eesZYf++NE42yb08pGPpkF9kaek3tWNyaC3NoMGYZoKCVQtPQA X-Received: by 2002:a81:4913:0:b0:607:9d64:d68e with SMTP id w19-20020a814913000000b006079d64d68emr8167058ywa.26.1710208560114; Mon, 11 Mar 2024 18:56:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710208560; cv=pass; d=google.com; s=arc-20160816; b=xaWSBOksBVHaleh0OFAKe9+gIXC6qC3AK0xWXh7pR/g+qxVYW05hVeWe7H/aL+9Gww khFf3fRYmxcXcglJumAJz5SgZ6Hi1rhKMlbKmRO5nZqA8I06xeVuFF5FOIzVFn7ftOj/ /4UtXSzsGStbVN09wD99DFi0Ks3ZloDC+LJPI6eX8T64+zs84x0/i+QqfcOXKclMFIa5 OSIswOGdLIc+s49e1drznzFqzfmngSAPDhoesEYFf+4rXCo6o+g+sEfZCTA2iHBV1Trn 9+U2U3FVJDO4gIlnGe4z/sQ+CoT7MVJChA9h66AZzMBnY3cVC6cdYrNPE8eoCGU7xFzs 6AEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=qUQTgV/c1LY6TxFowY3rUQdPlQN/M6/pFPRnz+ClL7I=; fh=25znkJgNDdi8+dwi6viTFj/isiDqwfKPSOwRTI5ocMw=; b=pXAMJe1MtIsVwc3lrLKTA0vZrF6OKtwfUuLN7PR1m0rAnpLtEhqSYA/BFZPjg+v4bA VKug6QTFQiBU2aKCECEvxM47PSaDZpI8OcOL+agvf2xiH5tRU+8z9JI2EgSF4tmf9weF cqe/+PgROVcuFFUeYcAWDwfmcJ4pHWI26Hu4KQvvHiiFprZ5kIytoMN6bW6w3Zrz27QP A107f1hSKwL9gwt56Fet0Wfmto+JfY7M0wBySVmApYi+rePTchtEi1IDARNxaKqSkKik uY223Y3kwJuX2F7KHeOjJoeUbtdwNvsKRSABf57nQjuslabqkf2ygfY0ti5IjuboOI25 5HVQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XUhqZm9T; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-99749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99749-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c10-20020ac85a8a000000b0042dbe70836dsi6884730qtc.489.2024.03.11.18.55.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 18:56:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XUhqZm9T; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-99749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99749-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C43CA1C2115D for ; Tue, 12 Mar 2024 01:55:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0C24B6FC5; Tue, 12 Mar 2024 01:55:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XUhqZm9T" Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AE8215BF; Tue, 12 Mar 2024 01:55:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710208547; cv=none; b=RTtF7QFrsxIi34zqmdAwM5iw6niv5MhhshqeCYuvOEEYAaA5yzOvbeYK2Ctt1BqML15fVZaWsnrctAK3moM2mIXOlRhEJ0zpfPM/lI3KPZRe7fj2ebmRpOi9+61TsL6sJZeG0V0uQ/lhSXaI44VYeq5OTCvtSa4Ldt/CPRzxUYQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710208547; c=relaxed/simple; bh=zshZgIMKPMwYAHrRgT1ICEsjyoVZMcPQc3ur1N+Oe4Y=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=qz8M+N0mklDlU9KkAQ98LT4iuzyMhXVetSNt3jbUxYiQFvTISwdNi7aPmHVmAODUSJZGcrQar4bWwQKDM6w9zggw12brNDxprWc4vTOQhX5HOwGyhf0FsfVYa7aiDLUEXyLbADI6g1Pc/FyVAO8ouhvzBILtrdPU0BGzQvJ2jas= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XUhqZm9T; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-33e959d8bc0so1541033f8f.1; Mon, 11 Mar 2024 18:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710208543; x=1710813343; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=qUQTgV/c1LY6TxFowY3rUQdPlQN/M6/pFPRnz+ClL7I=; b=XUhqZm9TlkZPioqABRqiq039TeH1jzD3vYOdz1n0kYugPuTlsMQ2J4mPd1oOcKvQgA Iy1Ta6Wejb1yIxbVOtqiyvyRg5v3lSUShVjYcKz48BDR5urY0AG/CoUVzGfPOePnKQoQ pJ0UzOjcBqv4wRnwyrJRius+tjaBydacWFvw2dYdp2gXx8XrD/YWSo+rPMlMT9DC5/0j cwqpqavJohYoF4xGXbUWGvTALrUIx2zceA5ctfO/wKj9mloyZLot12+cPp0iLm0KONx9 tWGupsb23beyQgARD0oY85c+pJcNHJNfBqkZRKCe318n5Inyx942F99YuzGTa1eHhjHP ymUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710208543; x=1710813343; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qUQTgV/c1LY6TxFowY3rUQdPlQN/M6/pFPRnz+ClL7I=; b=bC/755AlKCrcGKXTfwntJdvqB5kbWk7+vcVjzxypn8JwE5oHkaXzO9RjJnexjfgrgR 3c14VJL+Xq6o+bOFWdngOqkQLJzMepX7JllBCifX1PPAen1cz12Q+b45JvIW59cGwf7v eb2VmTKWaDqEmXjVtZy0f4CqANO5j9SALmmDZoFMBlsLQaaVYezPmRUYM0r4PAsLGOKr ltHGZVOOKE9N9sb0CApMqudF/LQZ3BOirfLRupSVLJaNMp+n7zrcg7qTUhioZvlWkxmr uxgDxRYhhU4kS6ADRcmsJE6ghUGvX2RAJRTRb3+O1WnbE6/Emm5/UlARaVc29FPJiz+H IO1w== X-Forwarded-Encrypted: i=1; AJvYcCUP9C+Dq5rVZtfw5eOZm4zC6gZc+Yy1oEzFXMFgfb7V95vyVjFrOGSVlNCCMOegOqCv6PEvDQafR31jleqq7/ntZV12ah6XvtNFvlix4zqeJj4I3RT3ljhexi//uUvF4MSJEy5jQ+zysdfjJhpeE73JVgYBsvWuBnHOt9TqqYn3rxg3zhplaTH9mBQ6uflaB+jCUoQu7xVyBcTGshum8moLxnwSeQkJev/qImNWjDiBEqSQ11enKWse/zG9hsx8b1GolmMupcFZh+UJxICu/6GeuvKqlqcYg70dYg== X-Gm-Message-State: AOJu0Yyq04wMSJHx7NBO0FtZ2ytz837FXkSsxMl/QsCukO+GT/D+WOZs MFH4f5WXelTVfO7hWbqSSohVWadkwwEIJhdCJ+vo4NLq3DB7GaZqYizRmkdK4rrNekGU77sF6YU 4cfLjLfKipJaub1eAdifJvs5io8E= X-Received: by 2002:adf:ec8c:0:b0:33e:1ee1:ef92 with SMTP id z12-20020adfec8c000000b0033e1ee1ef92mr6682508wrn.67.1710208543538; Mon, 11 Mar 2024 18:55:43 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240311093526.1010158-1-dongmenglong.8@bytedance.com> <20240311093526.1010158-8-dongmenglong.8@bytedance.com> In-Reply-To: <20240311093526.1010158-8-dongmenglong.8@bytedance.com> From: Alexei Starovoitov Date: Mon, 11 Mar 2024 18:55:32 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 7/9] libbpf: don't free btf if program of multi-link tracing existing To: Menglong Dong Cc: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Eddy Z , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , David Ahern , Dave Hansen , X86 ML , Steven Rostedt , Mathieu Desnoyers , Quentin Monnet , bpf , linux-arm-kernel , LKML , linux-riscv , linux-s390 , Network Development , linux-trace-kernel@vger.kernel.org, "open list:KERNEL SELFTEST FRAMEWORK" , linux-stm32@st-md-mailman.stormreply.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 11, 2024 at 2:35=E2=80=AFAM Menglong Dong wrote: > > By default, the kernel btf that we load during loading program will be > freed after the programs are loaded in bpf_object_load(). However, we > still need to use these btf for tracing of multi-link during attaching. > Therefore, we don't free the btfs until the bpf object is closed if any > bpf programs of the type multi-link tracing exist. > > Meanwhile, introduce the new api bpf_object__free_btf() to manually free > the btfs after attaching. > > Signed-off-by: Menglong Dong > --- > tools/lib/bpf/libbpf.c | 47 ++++++++++++++++++++++++++++++---------- > tools/lib/bpf/libbpf.h | 2 ++ > tools/lib/bpf/libbpf.map | 1 + > 3 files changed, 38 insertions(+), 12 deletions(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index 567ad367e7aa..fd5428494a7e 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -8267,6 +8267,39 @@ static int bpf_object_prepare_struct_ops(struct bp= f_object *obj) > return 0; > } > > +void bpf_object__free_btfs(struct bpf_object *obj) > +{ > + int i; > + > + /* clean up module BTFs */ > + for (i =3D 0; i < obj->btf_module_cnt; i++) { > + close(obj->btf_modules[i].fd); > + btf__free(obj->btf_modules[i].btf); > + free(obj->btf_modules[i].name); > + } > + free(obj->btf_modules); > + obj->btf_modules =3D NULL; > + obj->btf_module_cnt =3D 0; > + > + /* clean up vmlinux BTF */ > + btf__free(obj->btf_vmlinux); > + obj->btf_vmlinux =3D NULL; > +} > + > +static void bpf_object_early_free_btf(struct bpf_object *obj) > +{ > + struct bpf_program *prog; > + > + bpf_object__for_each_program(prog, obj) { > + if (prog->expected_attach_type =3D=3D BPF_TRACE_FENTRY_MU= LTI || > + prog->expected_attach_type =3D=3D BPF_TRACE_FEXIT_MUL= TI || > + prog->expected_attach_type =3D=3D BPF_MODIFY_RETURN_M= ULTI) > + return; > + } > + > + bpf_object__free_btfs(obj); > +} > + > static int bpf_object_load(struct bpf_object *obj, int extra_log_level, = const char *target_btf_path) > { > int err, i; > @@ -8307,18 +8340,7 @@ static int bpf_object_load(struct bpf_object *obj,= int extra_log_level, const ch > /* clean up fd_array */ > zfree(&obj->fd_array); > > - /* clean up module BTFs */ > - for (i =3D 0; i < obj->btf_module_cnt; i++) { > - close(obj->btf_modules[i].fd); > - btf__free(obj->btf_modules[i].btf); > - free(obj->btf_modules[i].name); > - } > - free(obj->btf_modules); > - > - /* clean up vmlinux BTF */ > - btf__free(obj->btf_vmlinux); > - obj->btf_vmlinux =3D NULL; > - > + bpf_object_early_free_btf(obj); > obj->loaded =3D true; /* doesn't matter if successfully or not */ > > if (err) > @@ -8791,6 +8813,7 @@ void bpf_object__close(struct bpf_object *obj) > usdt_manager_free(obj->usdt_man); > obj->usdt_man =3D NULL; > > + bpf_object__free_btfs(obj); > bpf_gen__free(obj->gen_loader); > bpf_object__elf_finish(obj); > bpf_object_unload(obj); > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h > index 5723cbbfcc41..c41a909ea4c1 100644 > --- a/tools/lib/bpf/libbpf.h > +++ b/tools/lib/bpf/libbpf.h > @@ -299,6 +299,8 @@ LIBBPF_API struct bpf_program * > bpf_object__find_program_by_name(const struct bpf_object *obj, > const char *name); > > +LIBBPF_API void bpf_object__free_btfs(struct bpf_object *obj); > + It shouldn't be exported. libbpf should clean it up when bpf_object is freed.