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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2163EC433F5 for ; Sun, 27 Mar 2022 20:28:32 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 95827839E7; Sun, 27 Mar 2022 22:27:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="VPEeDErO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 53C7483AD0; Sun, 27 Mar 2022 22:27:31 +0200 (CEST) Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B7C0D83A5B for ; Sun, 27 Mar 2022 22:27:18 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-pl1-x629.google.com with SMTP id e5so13130277pls.4 for ; Sun, 27 Mar 2022 13:27:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tL1VUDRxz8hdXs7hLG0AMzE8m1LYd62C0bYmcB4X9cM=; b=VPEeDErORbMeX9eU6GN0k12UaybYied+PXwk27L9VwHVrVz2/b0ZzgIzqNUKjjkuTp vvoZ7AQV6xMj85Z7Hkw8b9WJvLWbxBWz8mcMTR9Hs3APm2qp/SXbOoHsgnepsmv9ICch t7cB6O4HlUtqe8bsJaiwrXu62v/Q5Am9ngs38= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tL1VUDRxz8hdXs7hLG0AMzE8m1LYd62C0bYmcB4X9cM=; b=DCr9UpfCkGn7V9Gm52sX3f6JF3/Z+xntFeVC+VtDcb2XYj/eX4HPlIFid3XMDLFecV XzlhdfR0yCH/pCLk4lQxp3gJCWSFNgcSMD8siO1v+FxH6QXeBkoFSvuOeOTfqBA8x4i4 tT1PUfNd0VqJIQu1ugcmKeMxgXVMKCIG+vr50ETXSivhLr5scEIAOqTAqODGuYEc8+Uf czXrjnawnr/Df5Sdz8BDhRF2hchfrBkjT6whDR/XJBLRs+9FE88P/+di/kTRY9LpS3Rw 13iPr3amxjkXne3T+mpA6Ka+svDcOu4UBYxWYEilwOHiKWnHdH+QdWoAwKrJCHz4Y7oy WQIw== X-Gm-Message-State: AOAM533QxqmYpq4BFvMBwHQRiz0uMA9ixWG66adHkVA0KVmVLYIYQFSd x+S9AI+IfPmrhG9bN+lOei1ujQOvigHu8g== X-Google-Smtp-Source: ABdhPJy6XtgMXW78qXw2SeECwesEJ4b+XOE7EyddJbfCAovKiYX+6qJA+mFuTDrZYZMBBsb/AKDsAw== X-Received: by 2002:a17:902:cec2:b0:154:6df6:1e6a with SMTP id d2-20020a170902cec200b001546df61e6amr23093613plg.58.1648412837043; Sun, 27 Mar 2022 13:27:17 -0700 (PDT) Received: from localhost.localdomain (118-93-105-181.dsl.dyn.ihug.co.nz. [118.93.105.181]) by smtp.gmail.com with ESMTPSA id k3-20020a056a00168300b004f7e60da26csm13915466pfc.182.2022.03.27.13.27.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Mar 2022 13:27:16 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Heinrich Schuchardt , Sean Anderson , =?UTF-8?q?Marek=20Beh=C3=BAn?= , U-Boot Custodians , Tom Rini , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH 08/10] dm: core: Deal with a wrinkle with linker lists Date: Sun, 27 Mar 2022 14:26:20 -0600 Message-Id: <20220327202622.3438333-7-sjg@chromium.org> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog In-Reply-To: <20220327202622.3438333-1-sjg@chromium.org> References: <20220327202622.3438333-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean When every member of a linker list is aligned by the compiler, we can no longer rely on the sizeof of the struct to determine the number of entries. For example, if the struct size is 0x90 but every entry is aligned to 0xa0 by the compiler, the linker list entries takes more space in memory and the calculation of the number of entries is incorrect. For example, we may see 0x12 entries when there are only 0x11. This is a real problem. There may be a general solution, although I cannot currently think of one. So far it only bites with OF_PLATDATA_RT which creates a pointer to each entry of the 'struct udevice' linker_list. This does not happen without that option, so it only affects SPL. Work around it by manually calculating the aligned size of struct udevice, then using that for the n_ent calculation. Note: the alignment fix to linker list was here: 0b2fa98aa5e linker_lists: Fix alignment issue Signed-off-by: Simon Glass --- drivers/core/device.c | 3 ++- drivers/core/root.c | 8 +++++++- include/dm/device.h | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/drivers/core/device.c b/drivers/core/device.c index b7ce8544140..3ab2583df38 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -1186,7 +1186,8 @@ int dev_enable_by_path(const char *path) static struct udevice_rt *dev_get_rt(const struct udevice *dev) { struct udevice *base = ll_entry_start(struct udevice, udevice); - int idx = dev - base; + uint each_size = dm_udevice_size(); + int idx = ((void *)dev - (void *)base) / each_size; struct udevice_rt *urt = gd_dm_udevice_rt() + idx; diff --git a/drivers/core/root.c b/drivers/core/root.c index 8efb4256b27..6d1a4097e14 100644 --- a/drivers/core/root.c +++ b/drivers/core/root.c @@ -136,12 +136,18 @@ static int dm_setup_inst(void) if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) { struct udevice_rt *urt; + void *start, *end; + int each_size; void *base; int n_ents; uint size; /* Allocate the udevice_rt table */ - n_ents = ll_entry_count(struct udevice, udevice); + each_size = dm_udevice_size(); + start = ll_entry_start(struct udevice, udevice); + end = ll_entry_end(struct udevice, udevice); + size = end - start; + n_ents = size / each_size; urt = calloc(n_ents, sizeof(struct udevice_rt)); if (!urt) return log_msg_ret("urt", -ENOMEM); diff --git a/include/dm/device.h b/include/dm/device.h index 3d8961f9ac6..e0f86f5df9f 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -192,6 +192,14 @@ struct udevice { #endif }; +static inline int dm_udevice_size(void) +{ + if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) + return ALIGN(sizeof(struct udevice), CONFIG_LINKER_LIST_ALIGN); + + return sizeof(struct udevice); +} + /** * struct udevice_rt - runtime information set up by U-Boot * -- 2.35.1.1021.g381101b075-goog