linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: Wei Yang <richard.weiyang@gmail.com>
Cc: akpm@linux-foundation.org, linux-mm@kvack.org
Subject: Re: [Patch v2 2/8] memblock tests: add memblock_reserve_many_may_conflict_check()
Date: Sun, 28 Apr 2024 09:40:33 +0300	[thread overview]
Message-ID: <Zi3vYV8U-upXwo50@kernel.org> (raw)
In-Reply-To: <20240425071929.18004-3-richard.weiyang@gmail.com>

On Thu, Apr 25, 2024 at 07:19:23AM +0000, Wei Yang wrote:
> This may trigger the case fixed by commit 48c3b583bbdd ("mm/memblock:
> fix overlapping allocation when doubling reserved array").
> 
> This is done by adding the 129th reserve region into memblock.memory. If
> memblock_double_array() use this reserve region as new array, it fails.
> 
> Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
> ---
>  tools/testing/memblock/tests/basic_api.c | 123 +++++++++++++++++++++++
>  tools/testing/memblock/tests/common.c    |   4 +-
>  tools/testing/memblock/tests/common.h    |   1 +
>  3 files changed, 126 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/memblock/tests/basic_api.c b/tools/testing/memblock/tests/basic_api.c
> index 1ae62272867a..748950e02589 100644
> --- a/tools/testing/memblock/tests/basic_api.c
> +++ b/tools/testing/memblock/tests/basic_api.c
> @@ -991,6 +991,128 @@ static int memblock_reserve_many_check(void)
>  	return 0;
>  }
>  
> +/* Keep the gap so these memory region will not be merged. */

The gap where? What regions should not be merged?
Also please add a comment with the test description

> +#define MEMORY_BASE_OFFSET(idx, offset) ((offset) + (MEM_SIZE * 2) * (idx))
> +static int memblock_reserve_many_may_conflict_check(void)
> +{
> +	int i, skip;
> +	void *orig_region;
> +	struct region r = {
> +		.base = SZ_16K,
> +		.size = SZ_16K,
> +	};
> +	phys_addr_t new_reserved_regions_size;
> +
> +	/*
> +	 *  0        1          129
> +	 *  +---+    +---+      +---+
> +	 *  |32K|    |32K|  ..  |32K|
> +	 *  +---+    +---+      +---+
> +	 *
> +	 * Pre-allocate the range for 129 memory block + one range for double
> +	 * memblock.reserved.regions at idx 0.
> +	 * See commit 48c3b583bbdd ("mm/memblock: fix overlapping allocation
> +	 * when doubling reserved array")

Sorry, but I'm failing to parse it

> +	 */
> +	dummy_physical_memory_init();
> +	phys_addr_t memory_base = dummy_physical_memory_base();
> +	phys_addr_t offset = PAGE_ALIGN(memory_base);
> +
> +	PREFIX_PUSH();
> +
> +	/* Reserve the 129th memory block for all possible positions*/
> +	for (skip = 1; skip <= INIT_MEMBLOCK_REGIONS + 1; skip++) {
> +		reset_memblock_regions();
> +		memblock_allow_resize();
> +
> +		reset_memblock_attributes();
> +		/* Add a valid memory region used by double_array(). */
> +		memblock_add(MEMORY_BASE_OFFSET(0, offset), MEM_SIZE);
> +		/*
> +		 * Add a memory region which will be reserved as 129th memory
> +		 * region. This is not expected to be used by double_array().
> +		 */
> +		memblock_add(MEMORY_BASE_OFFSET(skip, offset), MEM_SIZE);
> +
> +		for (i = 1; i <= INIT_MEMBLOCK_REGIONS + 1; i++) {
> +			if (i == skip)
> +				continue;
> +
> +			/* Reserve some fakes memory region to fulfill the memblock. */
> +			memblock_reserve(MEMORY_BASE_OFFSET(i, offset), MEM_SIZE);
> +
> +			if (i < skip) {
> +				ASSERT_EQ(memblock.reserved.cnt, i);
> +				ASSERT_EQ(memblock.reserved.total_size, i * MEM_SIZE);
> +			} else {
> +				ASSERT_EQ(memblock.reserved.cnt, i - 1);
> +				ASSERT_EQ(memblock.reserved.total_size, (i - 1) * MEM_SIZE);
> +			}
> +		}
> +
> +		orig_region = memblock.reserved.regions;
> +
> +		/* This reserve the 129 memory_region, and makes it double array. */
> +		memblock_reserve(MEMORY_BASE_OFFSET(skip, offset), MEM_SIZE);
> +
> +		/*
> +		 * This is the memory region size used by the doubled reserved.regions,
> +		 * and it has been reserved due to it has been used. The size is used to
> +		 * calculate the total_size that the memblock.reserved have now.
> +		 */
> +		new_reserved_regions_size = PAGE_ALIGN((INIT_MEMBLOCK_REGIONS * 2) *
> +						sizeof(struct memblock_region));
> +		/*
> +		 * The double_array() will find a free memory region as the new
> +		 * reserved.regions, and the used memory region will be reserved, so
> +		 * there will be one more region exist in the reserved memblock. And the
> +		 * one more reserved region's size is new_reserved_regions_size.
> +		 */
> +		ASSERT_EQ(memblock.reserved.cnt, INIT_MEMBLOCK_REGIONS + 2);
> +		ASSERT_EQ(memblock.reserved.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE +
> +							new_reserved_regions_size);
> +		ASSERT_EQ(memblock.reserved.max, INIT_MEMBLOCK_REGIONS * 2);
> +
> +		/*
> +		 * The first reserved region is allocated for double array
> +		 * with the size of new_reserved_regions_size and the base to be
> +		 * MEMORY_BASE_OFFSET(0, offset) + SZ_32K - new_reserved_regions_size
> +		 */
> +		ASSERT_EQ(memblock.reserved.regions[0].base + memblock.reserved.regions[0].size,
> +			  MEMORY_BASE_OFFSET(0, offset) + SZ_32K);
> +		ASSERT_EQ(memblock.reserved.regions[0].size, new_reserved_regions_size);
> +
> +		/*
> +		 * Now memblock_double_array() works fine. Let's check after the
> +		 * double_array(), the memblock_reserve() still works as normal.
> +		 */
> +		memblock_reserve(r.base, r.size);
> +		ASSERT_EQ(memblock.reserved.regions[0].base, r.base);
> +		ASSERT_EQ(memblock.reserved.regions[0].size, r.size);
> +
> +		ASSERT_EQ(memblock.reserved.cnt, INIT_MEMBLOCK_REGIONS + 3);
> +		ASSERT_EQ(memblock.reserved.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE +
> +							new_reserved_regions_size +
> +							r.size);
> +		ASSERT_EQ(memblock.reserved.max, INIT_MEMBLOCK_REGIONS * 2);
> +
> +		/*
> +		 * The current reserved.regions is occupying a range of memory that
> +		 * allocated from dummy_physical_memory_init(). After free the memory,
> +		 * we must not use it. So restore the origin memory region to make sure
> +		 * the tests can run as normal and not affected by the double array.
> +		 */
> +		memblock.reserved.regions = orig_region;
> +		memblock.reserved.cnt = INIT_MEMBLOCK_RESERVED_REGIONS;
> +	}
> +
> +	dummy_physical_memory_cleanup();
> +
> +	test_pass_pop();
> +
> +	return 0;
> +}
> +
>  static int memblock_reserve_checks(void)
>  {
>  	prefix_reset();
> @@ -1006,6 +1128,7 @@ static int memblock_reserve_checks(void)
>  	memblock_reserve_between_check();
>  	memblock_reserve_near_max_check();
>  	memblock_reserve_many_check();
> +	memblock_reserve_many_may_conflict_check();
>  
>  	prefix_pop();
>  
> diff --git a/tools/testing/memblock/tests/common.c b/tools/testing/memblock/tests/common.c
> index c2c569f12178..5633ffc5aaa7 100644
> --- a/tools/testing/memblock/tests/common.c
> +++ b/tools/testing/memblock/tests/common.c
> @@ -61,7 +61,7 @@ void reset_memblock_attributes(void)
>  
>  static inline void fill_memblock(void)
>  {
> -	memset(memory_block.base, 1, MEM_SIZE);
> +	memset(memory_block.base, 1, MEM_ALLOC_SIZE);

I believe PHYS_MEM_SIZE is a better name.

>  }
>  
>  void setup_memblock(void)
> @@ -103,7 +103,7 @@ void setup_numa_memblock(const unsigned int node_fracs[])
>  
>  void dummy_physical_memory_init(void)
>  {
> -	memory_block.base = malloc(MEM_SIZE);
> +	memory_block.base = malloc(MEM_ALLOC_SIZE);
>  	assert(memory_block.base);
>  	fill_memblock();
>  }
> diff --git a/tools/testing/memblock/tests/common.h b/tools/testing/memblock/tests/common.h
> index b5ec59aa62d7..741d57315ba6 100644
> --- a/tools/testing/memblock/tests/common.h
> +++ b/tools/testing/memblock/tests/common.h
> @@ -12,6 +12,7 @@
>  #include <../selftests/kselftest.h>
>  
>  #define MEM_SIZE		SZ_32K
> +#define MEM_ALLOC_SIZE		SZ_16M

Do we really need 16M? Wouldn't one or two suffice?

>  #define NUMA_NODES		8
>  
>  #define INIT_MEMBLOCK_REGIONS			128
> -- 
> 2.34.1
> 

-- 
Sincerely yours,
Mike.


  parent reply	other threads:[~2024-04-28  6:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-25  7:19 [Patch v2 0/8] memblock: clenup Wei Yang
2024-04-25  7:19 ` [Patch v2 1/8] memblock tests: reserve the 129th memory block at all possible position Wei Yang
2024-04-28  6:35   ` Mike Rapoport
2024-04-28 12:22     ` Wei Yang
2024-04-29 14:17       ` Mike Rapoport
2024-04-30  0:12         ` Wei Yang
2024-04-25  7:19 ` [Patch v2 4/8] mm/memblock: remove consecutive regions at once Wei Yang
2024-04-28  6:44   ` Mike Rapoport
2024-04-28 12:37     ` Wei Yang
2024-04-25  7:19 ` [Patch v2 5/8] memblock tests: add memblock_overlaps_region_checks Wei Yang
2024-04-25  7:19 ` [Patch v2 6/8] mm/memblock: return true directly on finding overlap region Wei Yang
2024-04-25  7:19 ` [Patch v2 7/8] mm/memblock: use PAGE_ALIGN_DOWN to get pgend in free_memmap Wei Yang
2024-04-25  7:19 ` [Patch v2 8/8] mm/memblock: default region's nid may be MAX_NUMNODES Wei Yang
     [not found] ` <20240425071929.18004-3-richard.weiyang@gmail.com>
2024-04-28  6:40   ` Mike Rapoport [this message]
2024-04-28 12:36     ` [Patch v2 2/8] memblock tests: add memblock_reserve_many_may_conflict_check() Wei Yang
2024-04-30  1:49     ` Wei Yang
2024-05-01  8:44       ` Mike Rapoport
     [not found] ` <20240425071929.18004-4-richard.weiyang@gmail.com>
2024-04-28  6:43   ` [Patch v2 3/8] mm/memblock: fix comment for memblock_isolate_range() Mike Rapoport
2024-04-28 13:07     ` Wei Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zi3vYV8U-upXwo50@kernel.org \
    --to=rppt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-mm@kvack.org \
    --cc=richard.weiyang@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).