From patchwork Wed Dec 21 01:15:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin KaFai Lau X-Patchwork-Id: 13078320 X-Patchwork-Delegate: bpf@iogearbox.net 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 996C9C4332F for ; Wed, 21 Dec 2022 01:15:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234268AbiLUBPx (ORCPT ); Tue, 20 Dec 2022 20:15:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234192AbiLUBPv (ORCPT ); Tue, 20 Dec 2022 20:15:51 -0500 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 648A5C09 for ; Tue, 20 Dec 2022 17:15:47 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1671585345; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Z0+lcomwj/it/3Y7HQ65P1R/9Jh8Tj+AEluAVKDgE3E=; b=aqRfiCBPBhbAjlp9KgzWWWgaGNCJV9KWMuHzjJcnjea4tu5p7cELA3S2sVJpYW9XEeHbD/ GRcYsi7OLubkeY8dodS62C88d1EhL3rpCN3rQE6Fs3Rs1SIy3jUoZDdMwAuAMNUZhjhtgy E8kBpm+DEJjikwiEqTUmkGsanVdjj0M= From: Martin KaFai Lau To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@meta.com, Yonghong Song Subject: [PATCH v2 bpf-next] bpf: Reduce smap->elem_size Date: Tue, 20 Dec 2022 17:15:38 -0800 Message-Id: <20221221011538.3263935-1-martin.lau@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 Acked-by: Andrii Nakryiko --- 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; return smap; }