Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp791702lql; Mon, 11 Mar 2024 19:06:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVBRmlsB2frsBPuBis8li4zVRliqejTjJE4LLvxYyZ2Tl9a0ekD7IjmzzO4wlGMBIzRIEHCgR4EAT28kP42Ga9S3SjmY7v0NACPaU3UZw== X-Google-Smtp-Source: AGHT+IGa7j60l4fJnkZd1Y9i1bxNlDLRhUOEhPcebhQhw85EofSoiOVhkO7YHWeE8l6nBWOvNpxJ X-Received: by 2002:a17:902:d2c8:b0:1dd:66d1:a62b with SMTP id n8-20020a170902d2c800b001dd66d1a62bmr9084162plc.5.1710209168298; Mon, 11 Mar 2024 19:06:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710209168; cv=pass; d=google.com; s=arc-20160816; b=jZ/RwwfPhzXjlxItsAlWSo7JcoarYpcSnVj11BPAZ1PcNvjozwr8eQtahNMWXoS1uZ PIqOao+RR1F9f8Qo+n8TXfaBc+J9LHJvZVdslFoD4Dg7tNkub95fzFwJhU/kAIoBqZM1 L9/NVBUyArnS61otGPQREvrla4SYVE/0ZQHk+ORczScOP3JYOpisPDPZZkVIto+3b8id 4TqlZt6a6es2gZ1mPXA1whYomnKErn2LXE55bjtfdsjVJZgT5ZQWE2CM8CKoWxtTuJns EwjkAsEIPSh1XJIstOef61eujUsPnFj2isfhFymapW1vj5XryzQytT5c8GxkX7uH6vEV IbLw== 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=nuFUN/hOP8k7Y+FzC49ZRi5HcBAhtMCwCDKyd6VJ7Rk=; fh=2YKi3G73U6inopib7GIEGufOTr0GrSaaako5TRVVWl8=; b=vzn+QQQg7yy5yQrTwKZxQKpzdjoY5DM+tOL+czb14EPZdIyEZ21aZYp0EHsvR7gPHh fVEmzTqm/HI54a8mvM4rHXp5Xq3p0pZAz/L4/LZtXtP5SroaQ6Hh2YWOI9F5e7uqjOw0 zhc8wcwzz4MeJfhvDFCzH/9ClGanPzSJrIVkn0qE0pw97M4cVDfr9t8RlatJIUdrdadR WLDjiyOYVvilxyGJk1GsIr7xaL7w0ru92iRZnRPITZCBULDEDi/4JE5tyPGIU1UXm9H7 VVDAc76nMFijIyU3+Sv6T8wUHpe/vLigDbFZeKH/RYPl7mvjsc8ffWw5lCcg1Ns7hWuP pcvg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=cXcf1zwv; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-99754-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99754-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id u14-20020a170902e5ce00b001dd60cbd537si5967859plf.61.2024.03.11.19.06.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 19:06:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99754-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=cXcf1zwv; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-99754-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99754-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C917EB2113E for ; Tue, 12 Mar 2024 02:06:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 36D3979D2; Tue, 12 Mar 2024 02:05:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="cXcf1zwv" Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 3FD741C2E for ; Tue, 12 Mar 2024 02:05:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710209149; cv=none; b=APP5z7iCwCwH9alVJS38+7BEYOFqKAEy/OS59V9smC6oeXVUZW/ZGJ3wiW8kazdQ77XnOUqZxeIlCvc/aXEHOVmMIRBbghfQx48BAE3BzL6Ruw7ykct0OJN9jMphW5B8gxpcxd/CPTenU1h2UfnOxGjiuXlvHJvcz0FNLDcLY1g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710209149; c=relaxed/simple; bh=u+cghnX8IkuhizDm1mtP5lcke/YetyNGJY33fxG2Q9s=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=LqjUFaRF3Qc8dOKqtJSCJesdVQ0R0zo3L8+UWocrpSs36vy8U68TzHVjkmHjf2JEIggUuvyCTQs7nY6YO9eNPjuDMFDhAMY4ICrHOQxhDZ/I6mNWLPeuvDU1jmIixxPHjCrEMHXerEmECKIha0H/YqcJ7FwfvgR9+BlLHNiMu/U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=cXcf1zwv; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-29b7b9a4908so2232693a91.1 for ; Mon, 11 Mar 2024 19:05:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1710209146; x=1710813946; 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=nuFUN/hOP8k7Y+FzC49ZRi5HcBAhtMCwCDKyd6VJ7Rk=; b=cXcf1zwveq2BZWd/hk/hHFZ2bcBAjvdrll3461JJbiXfEV9/3pMGWlPuG/pNBmFeWn efLes1S0DvG6ddCay1haGI+KNrUIGrvHtaGEQn2QwONhSa59Vtp7qwluT7qkTkskd1bt ZEVE1tLa/71CjFipBFpNl4IIiO1ydlP+FnQGuct76Bj2d7vsiG2rcaoD+Yac0BDlhTmC jOVjwt/UyyYGtGxS+6Ipa4w1EyBEixkiT8ksDCT7lM/4fPpepGC7JuMD+KQLUGUN5CFr yL4YcFEjbwIvbbEI22rhvTobOMG7tWg+ce70c1y+mivokvwmGYcIurpPA9a2Hg/LdMC5 E81g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710209146; x=1710813946; 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=nuFUN/hOP8k7Y+FzC49ZRi5HcBAhtMCwCDKyd6VJ7Rk=; b=vNReiJw54zXgKmnkjhywRJYP3r06WnppTe8kHRH9iOqEcBWWHzayQQsU8276FvzcPP u+WI0ojNukp3Y4HvAAjsWbDhsqDTPOfL9a36skZKitoxPz8YRdxldyle3cXelvrtEtTk OEB9HoRdVjOlX7PTrpT77A+JLzbgu+oNl9Ug7fREX2mTFpZj0tut3EAwywpzGPPBAYic 4i8GBC1OQ/pxrCeLY5hIX/++ZslcXR71neDbacae03DP2r+dQ4qqi30OmDl3WHpYqz+U o1QL36HROTXHLGjyKBj6MjOwEVWW9XbnRQlAmzBj5z9Q5VJOAr7eb6Db4Nr9xcuzeSW2 sypg== X-Forwarded-Encrypted: i=1; AJvYcCXxWxh3udpSJToO4VgM/lDZcM3C9R0EGPFRLkDPal8AOeB/68FVzfS5E4p4VSoUHSJKiKP/XzpLkzO48ANoz8SM3eaHpQNWSVbiLgwT X-Gm-Message-State: AOJu0YySZOajI4mpcVN10ktTJIIAq4tL+tUbiCGWzJDV+teNYdaQ76tr WTm4IevZYihc5L0ftMp5yKqOK5QNaxTUg28y2MW6Njam7T40Vj/BxckkuWecZ2MGwW7a7CHjmO9 ddsHCLZL+nk7MLFhPTqTbOCNc+DMorT67I9plsw== X-Received: by 2002:a17:90b:46d3:b0:29b:ae33:6ef2 with SMTP id jx19-20020a17090b46d300b0029bae336ef2mr6174052pjb.38.1710209146474; Mon, 11 Mar 2024 19:05:46 -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: From: =?UTF-8?B?5qKm6b6Z6JGj?= Date: Tue, 12 Mar 2024 10:05:35 +0800 Message-ID: Subject: Re: [External] Re: [PATCH bpf-next v2 7/9] libbpf: don't free btf if program of multi-link tracing existing To: Alexei Starovoitov 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 Tue, Mar 12, 2024 at 9:55=E2=80=AFAM Alexei Starovoitov wrote: > > 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 fre= e > > 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 = bpf_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_= MULTI || > > + prog->expected_attach_type =3D=3D BPF_TRACE_FEXIT_M= ULTI || > > + prog->expected_attach_type =3D=3D BPF_MODIFY_RETURN= _MULTI) > > + 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 *ob= j, 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. Yes, libbpf will clean up the btfs when bpf_object is freed in this commit. And I'm trying to offer a way to early free the btfs by the users manual to reduce the memory usage. Or, the btfs that we opened will keep existing until we close the bpf_object. This is optional, I can remove it if you prefer. Thanks! Menglong Dong