From patchwork Sun Sep 20 00:49:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1367763 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=c/jy9Jdt; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Bv8B03dFWz9sSC for ; Sun, 20 Sep 2020 10:50:16 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id F12F98252D; Sun, 20 Sep 2020 02:49:49 +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="c/jy9Jdt"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6B66A8252D; Sun, 20 Sep 2020 02:49:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ot1-x342.google.com (mail-ot1-x342.google.com [IPv6:2607:f8b0:4864:20::342]) (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 40A6A82528 for ; Sun, 20 Sep 2020 02:49:39 +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-ot1-x342.google.com with SMTP id n61so9049892ota.10 for ; Sat, 19 Sep 2020 17:49:39 -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=pxVlLw4xE7UQFQCjtkg6ZZKx754Ei02oIRLCCoTHx4Y=; b=c/jy9Jdt6A50blws7cY0kv2boB/vb87eRrlRviedM9tN8tLPiVwmW4nWDlQMjAxqyC dHQbkqyKUOnWmuZutjT5e6lBSxQbAw08hZllVvqUGa2C42GSAmjTzvUCXjnUYPfUZQAP EffeK06vWwz3+/1YiM7J9EctuzKKNrU+akFTw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pxVlLw4xE7UQFQCjtkg6ZZKx754Ei02oIRLCCoTHx4Y=; b=CIS4f4kdNgSerWiu2ImoO+rUVSnSffjT6qbhTXcgn+Jv5m5EWIUwki6jdxG2I4O8/+ ZgfRWaGAxQbEseq+BBfH328wswpi1242zcgqmxGjqoFLy2vRimQp5yRkBvgMKkr6X1md r9YRmxkeZAT92fxikPYqnFOhz4Yds535d1JvJq8CIk7v5t6JmZ19MX+mQc8prVgzc4GG f9QHZkQ5YzWbymt+G7GT2XkNhvoC2EyLGLNdRIaBHWXYJe98hre+ITBwHnjW9vyJ1n+6 8guhXgC4TvCiTApKR7uNUvAd8TF5a3QMINVL7Jc4tZKYvlk1FeW6V0j6CJDxuvf8kabY RTrg== X-Gm-Message-State: AOAM530colhz6kDe0QJqwwaaCpYg7Aw3lvQoaa644zm0pwa+IDx+9JPL b8JQP9uClzAFb6d5y2aDHVh7qWQKK4b7IKoT X-Google-Smtp-Source: ABdhPJwENZ5pMLjOqLD8zXVlccblKptdFMxxcoeTJp3x5c3+GKgKeggV/Udtty7q4FbrrmYRd073xA== X-Received: by 2002:a9d:7616:: with SMTP id k22mr26191439otl.87.1600562977899; Sat, 19 Sep 2020 17:49:37 -0700 (PDT) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id w12sm6234168oow.22.2020.09.19.17.49.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Sep 2020 17:49:37 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass , Philippe Reynes Subject: [PATCH 3/5] bloblist: Tidy up the data alignment Date: Sat, 19 Sep 2020 18:49:28 -0600 Message-Id: <20200920004930.2108887-4-sjg@chromium.org> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog In-Reply-To: <20200920004930.2108887-1-sjg@chromium.org> References: <20200920004930.2108887-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.3 at phobos.denx.de X-Virus-Status: Clean The intention which bloblists is that each blob's data is aligned in memory. At present it is only the headers that are aligned. Update the code to correct this and add a little more documentation. Signed-off-by: Simon Glass Signed-off-by: Simon Glass --- common/bloblist.c | 32 ++++++++++++++++++++++++++------ test/bloblist.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/common/bloblist.c b/common/bloblist.c index c86ea029c8d..173f28d8ec9 100644 --- a/common/bloblist.c +++ b/common/bloblist.c @@ -11,6 +11,20 @@ #include #include +/* + * A bloblist is a single contiguous chunk of memory with a header + * (struct bloblist_hdr) and a number of blobs in it. + * + * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the + * bloblist and consists of a struct bloblist_rec, some padding to the required + * alignment for the blog and then the actual data. The padding ensures that the + * start address of the data in each blob is aligned as required. Note that + * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment + * of the bloblist itself or the blob header. + * + * So far, only BLOBLIST_ALIGN alignment is supported. + */ + DECLARE_GLOBAL_DATA_PTR; static const char *const tag_name[] = { @@ -73,9 +87,14 @@ static int bloblist_addrec(uint tag, int size, struct bloblist_rec **recp) { struct bloblist_hdr *hdr = gd->bloblist; struct bloblist_rec *rec; - int new_alloced; + int data_start, new_alloced; + + /* Figure out where the new data will start */ + data_start = hdr->alloced + sizeof(*rec); + data_start = ALIGN(data_start, BLOBLIST_ALIGN); - new_alloced = hdr->alloced + sizeof(*rec) + ALIGN(size, BLOBLIST_ALIGN); + /* Calculate the new allocated total */ + new_alloced = data_start + ALIGN(size, BLOBLIST_ALIGN); if (new_alloced >= hdr->size) { log(LOGC_BLOBLIST, LOGL_ERR, "Failed to allocate %x bytes size=%x, need size=%x\n", @@ -83,15 +102,16 @@ static int bloblist_addrec(uint tag, int size, struct bloblist_rec **recp) return log_msg_ret("bloblist add", -ENOSPC); } rec = (void *)hdr + hdr->alloced; - hdr->alloced = new_alloced; rec->tag = tag; - rec->hdr_size = sizeof(*rec); + rec->hdr_size = data_start - hdr->alloced; rec->size = size; rec->spare = 0; /* Zero the record data */ - memset(rec + 1, '\0', rec->size); + memset((void *)rec + rec->hdr_size, '\0', rec->size); + + hdr->alloced = new_alloced; *recp = rec; return 0; @@ -139,7 +159,7 @@ void *bloblist_add(uint tag, int size) if (bloblist_addrec(tag, size, &rec)) return NULL; - return rec + 1; + return (void *)rec + rec->hdr_size; } int bloblist_ensure_size(uint tag, int size, void **blobp) diff --git a/test/bloblist.c b/test/bloblist.c index 271fe9f5d7f..3493e681f39 100644 --- a/test/bloblist.c +++ b/test/bloblist.c @@ -29,6 +29,8 @@ enum { TEST_ADDR = CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE = 0x100, + + ERASE_BYTE = '\xff', }; static struct bloblist_hdr *clear_bloblist(void) @@ -41,7 +43,7 @@ static struct bloblist_hdr *clear_bloblist(void) * to 0xff for testing purposes. */ hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE); - memset(hdr, '\xff', TEST_BLOBLIST_SIZE); + memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE); memset(hdr, '\0', sizeof(*hdr)); return hdr; @@ -292,6 +294,40 @@ static int bloblist_test_cmd_list(struct unit_test_state *uts) } BLOBLIST_TEST(bloblist_test_cmd_list, 0); +/* Test alignment of bloblist blobs */ +static int bloblist_test_align(struct unit_test_state *uts) +{ + struct bloblist_hdr *hdr; + int i; + + /* At the start there should be no records */ + hdr = clear_bloblist(); + ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0)); + ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE)); + + /* Check the alignment */ + for (i = 0; i < 3; i++) { + int size = i * 3; + ulong addr; + char *data; + int j; + + data = bloblist_add(i, size); + ut_assertnonnull(data); + addr = map_to_sysmem(data); + ut_asserteq(0, addr & (BLOBLIST_ALIGN - 1)); + + /* Only the bytes in the blob data should be zeroed */ + for (j = 0; j < size; j++) + ut_asserteq(0, data[j]); + for (; j < BLOBLIST_ALIGN; j++) + ut_asserteq(ERASE_BYTE, data[j]); + } + + return 0; +} +BLOBLIST_TEST(bloblist_test_align, 0); + int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) {