From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D6A3C4332F for ; Wed, 21 Dec 2022 01:22:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234214AbiLUBWS (ORCPT ); Tue, 20 Dec 2022 20:22:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbiLUBWR (ORCPT ); Tue, 20 Dec 2022 20:22:17 -0500 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 903241EED3 for ; Tue, 20 Dec 2022 17:22:15 -0800 (PST) Message-ID: <1d04bc36-38f8-cda7-1ed9-ef0be31197e5@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1671585734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ymJyc/UBqKzR3dT/5GvSIPNBG1uC3OMmBS23Z32ShQs=; b=gTDs2I+CLWJdtWhlOKU3NK5ur+RqsDKsIxYX2RmnjwjJ1fuRzHl0mf/QUoVLmNS6sAXNi0 RbyFIfoT+6Q8K6BgPyimgImXZFrBByGBuhaOihpRseFXJ+igtjBGLnrDNVXpwAlQEIPVd1 TxPuozY97bfEyKY+pirJ00rZ+XxzeDU= Date: Tue, 20 Dec 2022 17:22:11 -0800 MIME-Version: 1.0 Subject: Re: [PATCH v2 bpf-next] bpf: Reduce smap->elem_size Content-Language: en-US To: Andrii Nakryiko Cc: bpf@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@meta.com, Yonghong Song References: <20221221011538.3263935-1-martin.lau@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On 12/20/22 5:17 PM, Andrii Nakryiko wrote: > On Tue, Dec 20, 2022 at 5:15 PM Martin KaFai Lau wrote: >> >> From: Martin KaFai Lau >> >> 'struct bpf_local_storage_elem' has an unused 56 byte padding at the >> end due to struct's cache-line alignment requirement. This padding >> space is overlapped by storage value contents, so if we use sizeof() >> to calculate the total size, we overinflate it by 56 bytes. Use >> offsetofend() instead to calculate more exact memory use. >> >> Acked-by: Yonghong Song >> Signed-off-by: Martin KaFai Lau >> --- >> v2: >> Rephrase the commit message (Andrii and Yonghong) >> Use offsetofend instead of offsetof (Andrii) >> >> kernel/bpf/bpf_local_storage.c | 4 ++-- >> 1 file changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c >> index b39a46e8fb08..e73fc70071b0 100644 >> --- a/kernel/bpf/bpf_local_storage.c >> +++ b/kernel/bpf/bpf_local_storage.c >> @@ -580,8 +580,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_att >> raw_spin_lock_init(&smap->buckets[i].lock); >> } >> >> - smap->elem_size = >> - sizeof(struct bpf_local_storage_elem) + attr->value_size; >> + smap->elem_size = offsetofend(struct bpf_local_storage_elem, sdata) + >> + attr->value_size; > > Heh, we raced down to a minute. Copy/pasting my latest reply from > original thread. lol. email is more parallel than most people thought :) > > it just occurred to me > that your change can be written equivalently (but now I do think it's > cleaner) as: > > smap->elem_size = offsetof(struct bpf_local_storage_elem, > sdata.data[attr->value_size]); Sure. will post v3. > > > sdata is embedded struct, no pointer dereference, so single offsetof() > should be enough to peer inside it > > > Whichever you prefer, both versions work for me: > > Acked-by: Andrii Nakryiko Thanks for the review.