Received: by 2002:ab2:2997:0:b0:1ec:cbc4:63fb with SMTP id n23csp532854lqb; Thu, 29 Feb 2024 07:53:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUcWVHJsRDvcVp6YoFgHkolSPX4HU8IKsovQKMyV8CU0aWZNazYl1jdMf2/xxpM3ptDa+J6/phcN9wTyexlXqdL04fNVTQk86zQ0roKEA== X-Google-Smtp-Source: AGHT+IExwUrYqbecMMVQgtMkQQDu4uGj5PwoC+u/6maDrxABDmZTvXFAqLvNsMMlvke7ICaS7oEx X-Received: by 2002:a05:622a:189f:b0:42e:7a9a:f13b with SMTP id v31-20020a05622a189f00b0042e7a9af13bmr2984275qtc.58.1709222001168; Thu, 29 Feb 2024 07:53:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709222001; cv=pass; d=google.com; s=arc-20160816; b=NuGkmPn9jCGHaCFDB/rQaNSvPYJDqP4tuF/eZN6gWhVV2bqRc0khwcqVmtb+baUve0 sMBDRBP8ZKjPaulwww4cpI93YhT3e34dyackm3C4ENBgesYqoAO0f5dHzn8ve4hlg7XH MyJmXsaFaHrspdati7ecjjX4wn1u0Mzch7rg6oSImyzEv9cUViRi2jidt0Px+kSyXBMv DAtk6gR4LkUpDj4ETOpdltz/ONX/D6DpFsGbm48znpkdm0yQuCaWzdEt/nwQmDPeI3Vr NHcqlAyi5L1XQAjtFdibSLCZzJ8XT8l4z8boFteHc1eT/oIs5WREyialbLTQ9cXRRSIh Y9uA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=tnn4Yyd1s+CZ9bc162byRBbwwUhAqYQSoP9J3+ph3VI=; fh=/bqeTkJfK45C9AV/9D8EhlMNTevy1Cvp7UFY4IRAICA=; b=X9daKZvPNSBCFatNck885OCa6pTeeCXRnGXvGR6TNymB1vhdW7eF5ejuJMYVjeyNVB eVp6AYxoS/u/XbiFKe7fa8xk+q9P18AwezrVHSvKAlwRU9Q3teHlFbf2f87OjqX6fwvh 7o/elSBMFVreC7GqOkqr83QtVkiXFuTF7PCCNSS0pUfAroBObNAbUZLfgULDObX1l3zb KN8Q8sT5Eu7c8dmt4JlsEGKNIFJRv7FKDa3A9cXnBKBL3nNrjHDdfYpv00ur7WOkMa0r XBIB+rcQMPQhzMtkIesMS8clFVyvyf5g7b61Kc6U6od0wvu7GTQWaz2ijG8o/ipFm+1y uV+A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZaOluWqk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-86964-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86964-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o8-20020a05622a044800b0042e7765bbdasi1672988qtx.79.2024.02.29.07.53.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 07:53:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86964-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZaOluWqk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-86964-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86964-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E2EBC1C219CE for ; Thu, 29 Feb 2024 15:53:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EA99515B0FE; Thu, 29 Feb 2024 15:49:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZaOluWqk" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09E5515AADB; Thu, 29 Feb 2024 15:49:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709221761; cv=none; b=ff+rQ2XIPKXYgDb/gIfPAPAakB99JhRCrvp/+Zn6MvbvbyJG6uYA0+VNjUwACYtTYL5JPk4vp/Ul1s5cHKDbwNwgFDaCoVeMdYP7dcjvXjnUUBtwAREjHOl3Omaz6+gUFxj1ZfAbWskLkEAbajwY+BCsCT8hNqQ6NNjWyIQYpmg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709221761; c=relaxed/simple; bh=NWmTVAKYq8v6gN3EBr0vzjfVCXygas9fWX9Hme0A/jU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gCfhltyaoAEvj55StrqJ7mgQDdg5qvBsbuTD0UxkkR0Vk4ICCGa8ExPI3qjxNm5eQgZBMl1oeI4C69DzujCY5YDnnq51dfJ1YbZBTOy8AVbPXbn2bZyHZS2QY1ObOkIc91XpucQMd+gxknBER0bbtmp4mAsubCaRgCGDUiDTBhs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZaOluWqk; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D010CC43399; Thu, 29 Feb 2024 15:49:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709221760; bh=NWmTVAKYq8v6gN3EBr0vzjfVCXygas9fWX9Hme0A/jU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZaOluWqkgVgm/TjTYOjAPG/SB4fcQDqHwvykZPjOfrgnpwzTuPN9XRbqb0nIXoOHb Kxe3prJjyoTi5LuUSexePlKCCqy69dYz/awMzdQLpod2Y2aZphbJqzly0pcmw+yq8y yxisvgMPN9gE8eDZ/YyopyF6Q3ai2exS/MeFwnb79cPPAD298cVHwmLEs6uhc7APpy IVFrNDXe1akSd6QNi9wfI0ThSYr06C1BEbqppRYGjcQ//8b0Wxj6LdNd4lXfbrEn5v 5MOQKn3Bi98V68rng6ipEMa+yPmqjx3tWFkHrh6yGJpdZt5m1Q3dd2CzHbaAM+5Ki9 Vhsi0jtffWtkQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mark Brown , Guenter Roeck , Sasha Levin , gregkh@linuxfoundation.org Subject: [PATCH AUTOSEL 6.7 16/26] regmap: kunit: Ensure that changed bytes are actually different Date: Thu, 29 Feb 2024 10:48:35 -0500 Message-ID: <20240229154851.2849367-16-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240229154851.2849367-1-sashal@kernel.org> References: <20240229154851.2849367-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.7.6 Content-Transfer-Encoding: 8bit From: Mark Brown [ Upstream commit 2f0dbb24f78a333433a2b875c0b76bf55c119cd4 ] During the cache sync test we verify that values we expect to have been written only to the cache do not appear in the hardware. This works most of the time but since we randomly generate both the original and new values there is a low probability that these values may actually be the same. Wrap get_random_bytes() to ensure that the values are different, there are other tests which should have similar verification that we actually changed something. While we're at it refactor the test to use three changed values rather than attempting to use one of them twice, that just complicates checking that our new values are actually new. We use random generation to try to avoid data dependencies in the tests. Reported-by: Guenter Roeck Reviewed-by: Guenter Roeck Tested-by: Guenter Roeck Signed-off-by: Mark Brown Link: https://msgid.link/r/20240211-regmap-kunit-random-change-v3-1-e387a9ea4468@kernel.org Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/base/regmap/regmap-kunit.c | 54 +++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/drivers/base/regmap/regmap-kunit.c b/drivers/base/regmap/regmap-kunit.c index e14cc03a17f6c..4c9e38ead5c56 100644 --- a/drivers/base/regmap/regmap-kunit.c +++ b/drivers/base/regmap/regmap-kunit.c @@ -9,6 +9,23 @@ #define BLOCK_TEST_SIZE 12 +static void get_changed_bytes(void *orig, void *new, size_t size) +{ + char *o = orig; + char *n = new; + int i; + + get_random_bytes(new, size); + + /* + * This could be nicer and more efficient but we shouldn't + * super care. + */ + for (i = 0; i < size; i++) + while (n[i] == o[i]) + get_random_bytes(&n[i], 1); +} + static const struct regmap_config test_regmap_config = { .max_register = BLOCK_TEST_SIZE, .reg_stride = 1, @@ -1192,7 +1209,7 @@ static void raw_sync(struct kunit *test) struct regmap *map; struct regmap_config config; struct regmap_ram_data *data; - u16 val[2]; + u16 val[3]; u16 *hw_buf; unsigned int rval; int i; @@ -1206,17 +1223,13 @@ static void raw_sync(struct kunit *test) hw_buf = (u16 *)data->vals; - get_random_bytes(&val, sizeof(val)); + get_changed_bytes(&hw_buf[2], &val[0], sizeof(val)); /* Do a regular write and a raw write in cache only mode */ regcache_cache_only(map, true); - KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val, sizeof(val))); - if (config.val_format_endian == REGMAP_ENDIAN_BIG) - KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6, - be16_to_cpu(val[0]))); - else - KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 6, - le16_to_cpu(val[0]))); + KUNIT_EXPECT_EQ(test, 0, regmap_raw_write(map, 2, val, + sizeof(u16) * 2)); + KUNIT_EXPECT_EQ(test, 0, regmap_write(map, 4, val[2])); /* We should read back the new values, and defaults for the rest */ for (i = 0; i < config.max_register + 1; i++) { @@ -1225,24 +1238,34 @@ static void raw_sync(struct kunit *test) switch (i) { case 2: case 3: - case 6: if (config.val_format_endian == REGMAP_ENDIAN_BIG) { KUNIT_EXPECT_EQ(test, rval, - be16_to_cpu(val[i % 2])); + be16_to_cpu(val[i - 2])); } else { KUNIT_EXPECT_EQ(test, rval, - le16_to_cpu(val[i % 2])); + le16_to_cpu(val[i - 2])); } break; + case 4: + KUNIT_EXPECT_EQ(test, rval, val[i - 2]); + break; default: KUNIT_EXPECT_EQ(test, config.reg_defaults[i].def, rval); break; } } + + /* + * The value written via _write() was translated by the core, + * translate the original copy for comparison purposes. + */ + if (config.val_format_endian == REGMAP_ENDIAN_BIG) + val[2] = cpu_to_be16(val[2]); + else + val[2] = cpu_to_le16(val[2]); /* The values should not appear in the "hardware" */ - KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], val, sizeof(val)); - KUNIT_EXPECT_MEMNEQ(test, &hw_buf[6], val, sizeof(u16)); + KUNIT_EXPECT_MEMNEQ(test, &hw_buf[2], &val[0], sizeof(val)); for (i = 0; i < config.max_register + 1; i++) data->written[i] = false; @@ -1253,8 +1276,7 @@ static void raw_sync(struct kunit *test) KUNIT_EXPECT_EQ(test, 0, regcache_sync(map)); /* The values should now appear in the "hardware" */ - KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], val, sizeof(val)); - KUNIT_EXPECT_MEMEQ(test, &hw_buf[6], val, sizeof(u16)); + KUNIT_EXPECT_MEMEQ(test, &hw_buf[2], &val[0], sizeof(val)); regmap_exit(map); } -- 2.43.0