From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 E369D279DCD for ; Wed, 5 Nov 2025 19:23:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762370636; cv=none; b=do9/M9nag4XK9Mu3FbuTyiVzO9AVRu1mENJMqNSGT9vgqwTrfdVWNvxDhA+ms31LtX8kMaFdfvJGZ7V1FvbMGMnIOiV0Y9f8hTPia8JjBzB998mQ5PldmFK34tyKYRDGqAem5WSBZqJp75J5tv4VsZDy1DFZpmLgEj6Bw9v/fqM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762370636; c=relaxed/simple; bh=xumo1NJjI1tSArbHaxlVI/vTMt0zSW0N/aOr2WFfe5c=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=UWYOuxkVtHCegnw5Z12dwSFTcr8TrX1cn8Wd3TnDUVmX1GLoK8pFsQid3M+USz9OMmk2yPkhj2/Q+GVPSijWggLnYxJwoYDDzz3mdHeoXkNKjqBCg8j2/XTFKKDrJPmTnTaefnu+/fAlcmxcLeZaU6HtewxCXjcOPAAztW4ij2k= 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=XC+A+6ae; arc=none smtp.client-ip=209.85.210.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XC+A+6ae" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7a1603a098eso153586b3a.1 for ; Wed, 05 Nov 2025 11:23:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762370634; x=1762975434; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=RREx40hV5tyq0dYa5rX5oC5Z4Ah1UHImj4yHSS+0TPM=; b=XC+A+6aeXtaikgEIpbmvDPTvFpCq7olfeVKY6QWw6ZOD/x6WXKvWRCt8C3sOSf5RQN QWXairb9JaU7Gpgx93CCihV1/aW194smY2aRvujhTin+dTR8PORPiyOPM5hkk6D3FjZc QjAq5Ll1fNNwUJ8zQPADxTOouQ0yfELWUdzfuRFw0oNICrwl6VzINLOG020DNwd1nYo5 RVylP6NGmRRXrYSOQphlJUoUYJ20ejY96ppEInqIvAmATOnzPIj9VT2JerdArG0oNJ2L juM5y86njPE7l6v5ZVvmve0Xmdzt+vZuYu60W+xRLpl5orDv5TTc0VPsMH31631U7mvM 6XAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762370634; x=1762975434; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=RREx40hV5tyq0dYa5rX5oC5Z4Ah1UHImj4yHSS+0TPM=; b=oSIernwdIeGkFtZutIaHyh1erE3qpreGiOMtzEDOy7uKxTcyWLd8+pUnBL7eZOg/Ca JH6YUbUybqM1+2RUhh4B8kndBw4JPH+eRb1Zp9pWU7yukD5w4mloXrKofiyVkIfXkajW pkdUOo3sCQJQ4F7r8jUa7sfdJG2/sCiVoB/kmH1wu41L/W3sV4tNGDjn5JafV5dw+x8T ApeAB0UbfA/UTxkpWEk6xN2axEkKk9Zg/zLr7nSbzvnYoTYE9jX1LfFKPRbCx2oTy3wN HffmO+pSJ20TP97Ge2FhlxRZuxTTvbHxT6DL4iFYFjj+VGiZ9Obsml+Xgh2lHztYgUvx M48Q== X-Forwarded-Encrypted: i=1; AJvYcCXtZMIs51arQ6xXVma4n507k4gkftUKNROWycT5TSPNTa8OqeasFp19VvNfBikPl9iNFRyYJnda53it7nk=@vger.kernel.org X-Gm-Message-State: AOJu0YyYpRubGso3r7cYVhQU8kYW8TIHKyrWZIqIV7P9PGVZnXONH89k 6UMODoafdfEYupBDF5BsXKRPgACXpF05H0hqvT/Wkyg4uN16BDYI3jEt X-Gm-Gg: ASbGncuuNFpbGRNbu/z0a3cZHZn2SnFebhWsk7i7yj8WS4dUZSe8K96RsNiVdGJbEVE 2XdTqdW6w4i+poL4F0Wsh7wwxDUVasCrusC+wgt8T/NIHi43l26b3ZCIIveytcJmRUnaeBLeVg/ Cf6/4Dsuw8fu+UBPyR8pIaUiXDCTJ3Z2knyBKWxPC9pQ+FjwSszpOYMYqfKB/z3BUEgKYIq4f32 aJIN2EUe7V3IyzPG9CJO+mTguPZwvy53NoICGRRXrbzqFSs7ks1+gRkgtpvlkYlOOj/aG8LgRIT 67zuaK/aCViqKhW8yAHiEI3DQnadq707tHLTkqDIirFFUtUo9rq59r854rZvK8RfQ74jP2yQ98H YiXFM4JAhIEDjqtVNhK/6x/YbH3rF6ROSb6DJnL9kxqa8Z0Rj9pidklaQIuILBVveAibUcz3xJJ Z2XvEhD8QVSaCsA84rNjcVYL1W4anzxdXHeVM= X-Google-Smtp-Source: AGHT+IEF1f46SRcectCdqICEP1eNEqVAI9FbwInYUxhpGA0Pisokd6E6G3WgcwMjG2n50Dr7nHIlyg== X-Received: by 2002:a05:6a00:2e11:b0:7a2:73a9:97e with SMTP id d2e1a72fcca58-7ae1f5937c6mr4969623b3a.26.1762370634009; Wed, 05 Nov 2025 11:23:54 -0800 (PST) Received: from ?IPv6:2a03:83e0:115c:1:cdf2:29c1:f331:3e1? ([2620:10d:c090:500::6:8aee]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7af82204713sm187026b3a.36.2025.11.05.11.23.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Nov 2025 11:23:53 -0800 (PST) Message-ID: <2ba0561a653254254a0fa1709bffb3704488f33b.camel@gmail.com> Subject: Re: [RFC PATCH v4 2/7] libbpf: Add BTF permutation support for type reordering From: Eduard Zingerman To: Andrii Nakryiko Cc: Donglin Peng , ast@kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire , Song Liu , pengdonglin Date: Wed, 05 Nov 2025 11:23:52 -0800 In-Reply-To: References: <20251104134033.344807-1-dolinux.peng@gmail.com> <20251104134033.344807-3-dolinux.peng@gmail.com> <3986a6b863be2ec62820ea5d2cf471f7e233fac0.camel@gmail.com> <627795f165b1e66500b9f032ed7474125938f33a.camel@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-1.fc42) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Wed, 2025-11-05 at 10:23 -0800, Andrii Nakryiko wrote: > On Tue, Nov 4, 2025 at 5:20=E2=80=AFPM Eduard Zingerman wrote: > >=20 > > On Tue, 2025-11-04 at 17:04 -0800, Andrii Nakryiko wrote: > > > On Tue, Nov 4, 2025 at 4:16=E2=80=AFPM Eduard Zingerman wrote: > > > >=20 > > > > On Tue, 2025-11-04 at 16:11 -0800, Andrii Nakryiko wrote: > > > >=20 > > > > [...] > > > >=20 > > > > > > +static int btf_permute_remap_type_id(__u32 *type_id, void *ctx= ) > > > > > > +{ > > > > > > + struct btf_permute *p =3D ctx; > > > > > > + __u32 new_type_id =3D *type_id; > > > > > > + > > > > > > + /* skip references that point into the base BTF */ > > > > > > + if (new_type_id < p->btf->start_id) > > > > > > + return 0; > > > > > > + > > > > > > + new_type_id =3D p->map[*type_id - p->btf->start_id]; > > > > >=20 > > > > > I'm actually confused, I thought p->ids would be the mapping from > > > > > original type ID (minus start_id, of course) to a new desired ID,= but > > > > > it looks to be the other way? ids is a desired resulting *sequenc= e* of > > > > > types identified by their original ID. I find it quite confusing.= I > > > > > think about permutation as a mapping from original type ID to a n= ew > > > > > type ID, am I confused? > > > >=20 > > > > Yes, it is a desired sequence, not mapping. > > > > I guess its a bit simpler to use for sorting use-case, as you can j= ust > > > > swap ids while sorting. > > >=20 > > > The question is really what makes most sense as an interface. Because > > > for sorting cases it's just the matter of a two-line for() loop to > > > create ID mapping once types are sorted. > > >=20 > > > I have slight preference for id_map approach because it is easy to > > > extend to the case of selectively dropping some types. We can just > > > define that such IDs should be mapped to zero. This will work as a > > > natural extension. With the desired end sequence of IDs, it's less > > > natural and will require more work to determine which IDs are missing > > > from the sequence. > > >=20 > > > So unless there is some really good and strong reason, shall we go > > > with the ID mapping approach? > >=20 > > If the interface is extended with types_cnt, as you suggest, deleting > > types is trivial with sequence interface as well. At-least the way it > > is implemented by this patch, you just copy elements from 'ids' one by > > one. >=20 > But it is way less explicit and obvious way to delete element. With ID > map it is obvious, that type will be mapped to zero. With list of IDs, > you effectively search for elements that are missing, which IMO is way > less optimal an interface. >=20 > So I still favor the ID map approach. You don't need to search for deleted elements with current implementation (assuming the ids_cnt parameter is added). Suppose there are 4 types + void in BTF and the 'ids' sequence looks as follows: {1, 3, 4}, current implementation will: - iterate over 'ids': - copy 1 to new_types, remember to remap 1 to 1 - copy 3 to new_types, remember to remap 3 to 2 - copy 4 to new_types, remember to remap 4 to 3 - do the remapping. Consider the sorting use-case: - If 'ids' is the desired final order of types, libbpf needs to allocate the mapping from old id to new id, as described above. - If 'ids' is a map from old id to new id: - libbpf will have to allocate a temporary array to hold the desired id sequence, to know in which order to copy the types; - user will have to allocate the array for mapping. So, for id map approach it is one more allocation for no benefit.