[PATCH 06/17] dm: test: Clear the block cache after running a test

Simon Glass sjg at chromium.org
Sun Oct 30 02:47:08 CET 2022


Some tests access data in block devices and so cause the cache to fill
up. This results in memory being allocated.

Some tests check the malloc usage at the beginning and then again at the
end, to ensure there is no memory leak caused by the test. The block cache
makes this difficult, since the any test may cause entries to be allocated
or even freed, if the cache becomes full.

It is simpler to clear the block cache after each test. This ensures that
it will not introduce noise in tests which check malloc usage.

Add the logic to clear the cache, using the existing blkcache_invalidate()
function. Drop the duplicate code at the same time.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 drivers/block/blkcache.c | 23 ++++++++++-------------
 include/blk.h            |  9 +++++++--
 test/test-main.c         |  3 +++
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/drivers/block/blkcache.c b/drivers/block/blkcache.c
index b53420a3a88..f99465aa479 100644
--- a/drivers/block/blkcache.c
+++ b/drivers/block/blkcache.c
@@ -150,8 +150,8 @@ void blkcache_invalidate(int iftype, int devnum)
 
 	list_for_each_safe(entry, n, &block_cache) {
 		node = (struct block_cache_node *)entry;
-		if ((node->iftype == iftype) &&
-		    (node->devnum == devnum)) {
+		if (iftype == -1 ||
+		    (node->iftype == iftype && node->devnum == devnum)) {
 			list_del(entry);
 			free(node->cache);
 			free(node);
@@ -162,18 +162,10 @@ void blkcache_invalidate(int iftype, int devnum)
 
 void blkcache_configure(unsigned blocks, unsigned entries)
 {
-	struct block_cache_node *node;
+	/* invalidate cache if there is a change */
 	if ((blocks != _stats.max_blocks_per_entry) ||
-	    (entries != _stats.max_entries)) {
-		/* invalidate cache */
-		while (!list_empty(&block_cache)) {
-			node = (struct block_cache_node *)block_cache.next;
-			list_del(&node->lh);
-			free(node->cache);
-			free(node);
-		}
-		_stats.entries = 0;
-	}
+	    (entries != _stats.max_entries))
+		blkcache_invalidate(-1, 0);
 
 	_stats.max_blocks_per_entry = blocks;
 	_stats.max_entries = entries;
@@ -188,3 +180,8 @@ void blkcache_stats(struct block_cache_stats *stats)
 	_stats.hits = 0;
 	_stats.misses = 0;
 }
+
+void blkcache_free(void)
+{
+	blkcache_invalidate(-1, 0);
+}
diff --git a/include/blk.h b/include/blk.h
index d3ab9a10b96..58322c39eec 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -147,8 +147,8 @@ void blkcache_fill(int iftype, int dev,
  * blkcache_invalidate() - discard the cache for a set of blocks
  * because of a write or device (re)initialization.
  *
- * @param iftype - uclass_id_x for type of device
- * @param dev - device index of particular type
+ * @iftype - UCLASS_ID_ for type of device, or -1 for any
+ * @dev - device index of particular type, if @iftype is not -1
  */
 void blkcache_invalidate(int iftype, int dev);
 
@@ -178,6 +178,9 @@ struct block_cache_stats {
  */
 void blkcache_stats(struct block_cache_stats *stats);
 
+/** blkcache_free() - free all memory allocated to the block cache */
+void blkcache_free(void);
+
 #else
 
 static inline int blkcache_read(int iftype, int dev,
@@ -193,6 +196,8 @@ static inline void blkcache_fill(int iftype, int dev,
 
 static inline void blkcache_invalidate(int iftype, int dev) {}
 
+static inline void blkcache_free(void) {}
+
 #endif
 
 #if CONFIG_IS_ENABLED(BLK)
diff --git a/test/test-main.c b/test/test-main.c
index ddfd89c089a..954a280119e 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -5,6 +5,7 @@
  */
 
 #include <common.h>
+#include <blk.h>
 #include <console.h>
 #include <cyclic.h>
 #include <dm.h>
@@ -353,6 +354,8 @@ static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
 	free(uts->of_other);
 	uts->of_other = NULL;
 
+	blkcache_free();
+
 	return 0;
 }
 
-- 
2.38.1.273.g43a17bfeac-goog



More information about the U-Boot mailing list