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 44D82C77B7A for ; Thu, 1 Jun 2023 16:27:53 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BCEE986385; Thu, 1 Jun 2023 18:25:20 +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="AeevYSs8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8E84386377; Thu, 1 Jun 2023 18:24:47 +0200 (CEST) Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) (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 46EAD86347 for ; Thu, 1 Jun 2023 18:24:43 +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-il1-x132.google.com with SMTP id e9e14a558f8ab-33bb16ac2e9so3414215ab.3 for ; Thu, 01 Jun 2023 09:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1685636680; x=1688228680; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ng7NFxbmbklJFs0i91wGa+tO0NSwYieGmrYcbB75NUA=; b=AeevYSs8C+BiVASHwzB6i53fYCm4HyMk4l2S3hAWT7xS+Uzi1ZiaUG6gARzilnLg+I JkD2izIqEWNEv27qnGzYr2gH8X5oUwY3WYAIbU1psAXYEAYE1oRHTgo3jJJO9iraWqTC JtZCfUKnBfPqRP3JQXMIX8nhVJnLQ932ExxFc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685636680; x=1688228680; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ng7NFxbmbklJFs0i91wGa+tO0NSwYieGmrYcbB75NUA=; b=YgKH3Fn4JuvcscF+cuYbUhFeJjbyaTlZ8xsZuKrTpiq/ZIcQI+fIde4j26GkvenycD IvyX3oF/PnbfB9UsSlukqJyVqnu2Vg7zBBLRB7/ZQIdNG80SYT/E4dNKZ+N3fGLsBN+j Xy2msvqL0hKQppb3Qi3UtFuvYhokmb8xARnQcanrQRoLVMY9cIJ3+sovhsVJWdlsXysR E+x3rsaPLynWqcIPYL/iBDNMtmdpfGY7EtEjFIBdC2q2Y8MRfUlMeFXu/VSDijtT/+bB YCADM1gtVQz/Ho91AQ8s2PzCfHmui6nMV8/fNQNGav1azHm8i2jXSvfNDZbwDlt9dNah 82Gg== X-Gm-Message-State: AC+VfDwKFXDtyiZQAdTqUvqGdIfZcjmr5KQYcoMbKQh1xqbjjBEmnXz6 66trcyLGaZa+ncdb1zelP9C7NC3s5/7w72Ca9dM= X-Google-Smtp-Source: ACHHUZ72TWMWeukqLpG2eqFsQmDf7bdXyje6nw02hDRDu6yOEaIPNB6q2QLMKW5nXz4Mukcf2fug5w== X-Received: by 2002:a92:d90e:0:b0:334:c74c:43f9 with SMTP id s14-20020a92d90e000000b00334c74c43f9mr4863290iln.13.1685636679824; Thu, 01 Jun 2023 09:24:39 -0700 (PDT) Received: from sjg1.lan (c-73-14-173-85.hsd1.co.comcast.net. [73.14.173.85]) by smtp.gmail.com with ESMTPSA id l1-20020a922901000000b0033a6e7ee4e3sm3862098ilg.1.2023.06.01.09.24.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 09:24:39 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Heinrich Schuchardt , Tom Rini , Simon Glass , Jerry Van Baren Subject: [PATCH 16/40] fdt: Align the start of the livetree Date: Thu, 1 Jun 2023 10:22:40 -0600 Message-ID: <20230601162404.220488-15-sjg@chromium.org> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog In-Reply-To: <20230601162404.220488-1-sjg@chromium.org> References: <20230601162404.220488-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.8 at phobos.denx.de X-Virus-Status: Clean Ensure that the block of memory used by live tree is aligned according to the default for structures. This ensures that the root node appears at the start of the block, so it can be used with free(), rather than being 4 bytes later in some cases. This corrects a rather obscure bug in unflatten_device_tree(). Fixes: 8b50d526ea5 ("dm: Add a function to create a 'live' device tree") Signed-off-by: Simon Glass --- include/dm/of.h | 2 ++ lib/of_live.c | 5 ++++- test/dm/ofnode.c | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/dm/of.h b/include/dm/of.h index fce7cef0ff69..b1c934f610d3 100644 --- a/include/dm/of.h +++ b/include/dm/of.h @@ -63,6 +63,8 @@ struct device_node { struct device_node *sibling; }; +#define BAD_OF_ROOT 0xdead11e3 + #define OF_MAX_PHANDLE_ARGS 16 /** diff --git a/lib/of_live.c b/lib/of_live.c index 1b5964d09a9d..05588d5ed282 100644 --- a/lib/of_live.c +++ b/lib/of_live.c @@ -287,9 +287,12 @@ int unflatten_device_tree(const void *blob, struct device_node **mynodes) debug(" size is %lx, allocating...\n", size); /* Allocate memory for the expanded device tree */ - mem = malloc(size + 4); + mem = memalign(__alignof__(struct device_node), size + 4); memset(mem, '\0', size); + /* Set up value for dm_test_livetree_align() */ + *(u32 *)mem = BAD_OF_ROOT; + *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef); debug(" unflattening %p...\n", mem); diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c index 473a8cef5780..64baaf68db3a 100644 --- a/test/dm/ofnode.c +++ b/test/dm/ofnode.c @@ -1240,3 +1240,29 @@ static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); + +/* check that the livetree is aligned to a structure boundary */ +static int dm_test_livetree_align(struct unit_test_state *uts) +{ + const int align = __alignof__(struct unit_test_state); + struct device_node *node; + u32 *sentinel; + ulong start; + + start = (ulong)gd_of_root(); + ut_asserteq(start, ALIGN(start, align)); + + node = gd_of_root(); + sentinel = (void *)node - sizeof(u32); + + /* + * The sentinel should be overwritten with the root node. If it isn't, + * then the root node is not at the very start of the livetree memory + * area, and free(root) will fail to free the memory used by the + * livetree. + */ + ut_assert(*sentinel != BAD_OF_ROOT); + + return 0; +} +DM_TEST(dm_test_livetree_align, UT_TESTF_LIVE_TREE); -- 2.41.0.rc0.172.g3f132b7071-goog