All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: Sughosh Ganu <sughosh.ganu@linaro.org>
Cc: Tom Rini <trini@konsulko.com>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Simon Glass <sjg@chromium.org>, Marek Vasut <marex@denx.de>,
	Mark Kettenis <mark.kettenis@xs4all.nl>,
	Fabio Estevam <festevam@gmail.com>,
	u-boot@lists.denx.de
Subject: Re: [RFC PATCH 09/31] lmb: allow for resizing lmb regions
Date: Tue, 11 Jun 2024 11:17:08 +0200	[thread overview]
Message-ID: <5d133571-ba43-40c7-bd19-0c0a4e857034@gmx.de> (raw)
In-Reply-To: <20240607185240.1892031-10-sughosh.ganu@linaro.org>

On 07.06.24 20:52, Sughosh Ganu wrote:
> Allow for resizing of LMB regions if the region attributes match. The
> current code returns a failure status on detecting an overlapping
> address. This worked up until now since the LMB calls were not
> persistent and global -- the LMB memory map was specific and private
> to a given caller of the LMB API's.
>
> With the change in the LMB code to make the LMB reservations
> persistent, there needs to be a check on whether the memory region can
> be resized, and then do it if so. To distinguish between memory that
> cannot be resized, add a new flag, LMB_NOOVERWRITE. Reserving a region
> of memory with this attribute would indicate that the region cannot be
> resized.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>   include/lmb.h |   1 +
>   lib/lmb.c     | 120 ++++++++++++++++++++++++++++++++++++++++++++------
>   2 files changed, 107 insertions(+), 14 deletions(-)
>
> diff --git a/include/lmb.h b/include/lmb.h
> index 03bce2a50c..1d4cd255d2 100644
> --- a/include/lmb.h
> +++ b/include/lmb.h
> @@ -20,6 +20,7 @@
>   enum lmb_flags {
>   	LMB_NONE		= 0x0,
>   	LMB_NOMAP		= 0x4,
> +	LMB_NOOVERWRITE		= 0x8,

Please, add the missing description for the new value. Using the first
available bit (0x01) would be expected.

Using the BIT macro would make it clearer that these are bits of a bitmap:

enum lmb_flags {
	LMB_NONE		= BIT(0),
	LMB_NOOVERWRITE		= BIT(1),
};

Best regards

Heinrich


>   };
>
>   /**
> diff --git a/lib/lmb.c b/lib/lmb.c
> index de5a2cf23b..0a4f3d5bcd 100644
> --- a/lib/lmb.c
> +++ b/lib/lmb.c
> @@ -260,12 +260,88 @@ void lmb_add_memory(struct bd_info *bd)
>   	}
>   }
>
> +static bool lmb_region_flags_match(struct lmb_region *rgn, unsigned long r1,
> +				   enum lmb_flags flags)
> +{
> +	return rgn->region[r1].flags == flags;
> +}
> +
> +static long lmb_merge_overlap_regions(struct lmb_region *rgn, unsigned long i,
> +				      phys_addr_t base, phys_size_t size,
> +				      enum lmb_flags flags)
> +{
> +	phys_size_t rgnsize;
> +	unsigned long rgn_cnt, idx;
> +	phys_addr_t rgnbase, rgnend;
> +	phys_addr_t mergebase, mergeend;
> +
> +	rgn_cnt = 0;
> +	idx = i;
> +	/*
> +	 * First thing to do is to identify how many regions does
> +	 * the requested region overlap.
> +	 * If the flags match, combine all these overlapping
> +	 * regions into a single region, and remove the merged
> +	 * regions.
> +	 */
> +	while (idx < rgn->cnt - 1) {
> +		rgnbase = rgn->region[idx].base;
> +		rgnsize = rgn->region[idx].size;
> +
> +		if (lmb_addrs_overlap(base, size, rgnbase,
> +				      rgnsize)) {
> +			if (!lmb_region_flags_match(rgn, idx, flags))
> +				return -1;
> +			rgn_cnt++;
> +			idx++;
> +		}
> +	}
> +
> +	/* The merged region's base and size */
> +	rgnbase = rgn->region[i].base;
> +	mergebase = min(base, rgnbase);
> +	rgnend = rgn->region[idx].base + rgn->region[idx].size;
> +	mergeend = max(rgnend, (base + size));
> +
> +	rgn->region[i].base = mergebase;
> +	rgn->region[i].size = mergeend - mergebase;
> +
> +	/* Now remove the merged regions */
> +	while (--rgn_cnt)
> +		lmb_remove_region(rgn, i + 1);
> +
> +	return 0;
> +}
> +
> +static long lmb_resize_regions(struct lmb_region *rgn, unsigned long i,
> +			       phys_addr_t base, phys_size_t size,
> +			       enum lmb_flags flags)
> +{
> +	long ret = 0;
> +	phys_addr_t rgnend;
> +
> +	if (i == rgn->cnt - 1 ||
> +		base + size < rgn->region[i + 1].base) {
> +		if (!lmb_region_flags_match(rgn, i, flags))
> +			return -1;
> +
> +		rgnend = rgn->region[i].base + rgn->region[i].size;
> +		rgn->region[i].base = min(base, rgn->region[i].base);
> +		rgnend = max(base + size, rgnend);
> +		rgn->region[i].size = rgnend - rgn->region[i].base;
> +	} else {
> +		ret = lmb_merge_overlap_regions(rgn, i, base, size, flags);
> +	}
> +
> +	return ret;
> +}
> +
>   /* This routine called with relocation disabled. */
>   static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
>   				 phys_size_t size, enum lmb_flags flags)
>   {
>   	unsigned long coalesced = 0;
> -	long adjacent, i;
> +	long ret, i;
>
>   	if (rgn->cnt == 0) {
>   		rgn->region[0].base = base;
> @@ -290,23 +366,32 @@ static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
>   				return -1; /* regions with new flags */
>   		}
>
> -		adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
> -		if (adjacent > 0) {
> +		ret = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
> +		if (ret > 0) {
>   			if (flags != rgnflags)
>   				break;
>   			rgn->region[i].base -= size;
>   			rgn->region[i].size += size;
>   			coalesced++;
>   			break;
> -		} else if (adjacent < 0) {
> +		} else if (ret < 0) {
>   			if (flags != rgnflags)
>   				break;
>   			rgn->region[i].size += size;
>   			coalesced++;
>   			break;
>   		} else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) {
> -			/* regions overlap */
> -			return -1;
> +			if (flags == LMB_NONE) {
> +				ret = lmb_resize_regions(rgn, i, base, size,
> +							 flags);
> +				if (ret < 0)
> +					return -1;
> +
> +				coalesced++;
> +				break;
> +			} else {
> +				return -1;
> +			}
>   		}
>   	}
>
> @@ -448,7 +533,7 @@ static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size)
>   }
>
>   static phys_addr_t __lmb_alloc_base(phys_size_t size, ulong align,
> -				    phys_addr_t max_addr)
> +				    phys_addr_t max_addr, enum lmb_flags flags)
>   {
>   	long i, rgn;
>   	phys_addr_t base = 0;
> @@ -498,7 +583,7 @@ phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
>   {
>   	phys_addr_t alloc;
>
> -	alloc = __lmb_alloc_base(size, align, max_addr);
> +	alloc = __lmb_alloc_base(size, align, max_addr, LMB_NONE);
>
>   	if (alloc == 0)
>   		printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n",
> @@ -507,11 +592,8 @@ phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr)
>   	return alloc;
>   }
>
> -/*
> - * Try to allocate a specific address range: must be in defined memory but not
> - * reserved
> - */
> -phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
> +static phys_addr_t __lmb_alloc_addr(phys_addr_t base, phys_size_t size,
> +				    enum lmb_flags flags)
>   {
>   	long rgn;
>
> @@ -526,13 +608,23 @@ phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
>   				      lmb.memory.region[rgn].size,
>   				      base + size - 1, 1)) {
>   			/* ok, reserve the memory */
> -			if (lmb_reserve(base, size) >= 0)
> +			if (lmb_reserve_flags(base, size, flags) >= 0)
>   				return base;
>   		}
>   	}
> +
>   	return 0;
>   }
>
> +/*
> + * Try to allocate a specific address range: must be in defined memory but not
> + * reserved
> + */
> +phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size)
> +{
> +	return __lmb_alloc_addr(base, size, LMB_NONE);
> +}
> +
>   /* Return number of bytes from a given address that are free */
>   phys_size_t lmb_get_free_size(phys_addr_t addr)
>   {


  parent reply	other threads:[~2024-06-11  9:17 UTC|newest]

Thread overview: 127+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07 18:52 [RFC PATCH 00/31] Make U-Boot memory reservations coherent Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 01/31] lmb: remove the unused lmb_is_reserved() function Sughosh Ganu
2024-06-10  9:33   ` Ilias Apalodimas
2024-06-07 18:52 ` [RFC PATCH 02/31] lmb: staticize __lmb_alloc_base() Sughosh Ganu
2024-06-10  9:37   ` Ilias Apalodimas
2024-06-07 18:52 ` [RFC PATCH 03/31] lmb: make the lmb reservations persistent Sughosh Ganu
2024-06-10 21:17   ` Ilias Apalodimas
2024-06-10 11:23     ` Heinrich Schuchardt
2024-06-10 16:55       ` Tom Rini
2024-06-11 18:52   ` Simon Glass
2024-06-07 18:52 ` [RFC PATCH 04/31] lmb: remove local instances of the lmb structure variable Sughosh Ganu
2024-06-11 18:52   ` Simon Glass
2024-06-11 21:01     ` Tom Rini
2024-06-11 22:08       ` Simon Glass
2024-06-11 22:55         ` Tom Rini
2024-06-12  2:41           ` Simon Glass
2024-06-12  5:41             ` Ilias Apalodimas
2024-06-12  6:13             ` Heinrich Schuchardt
2024-06-12 17:22             ` Tom Rini
2024-06-12 20:24               ` Simon Glass
2024-06-12 21:40                 ` Tom Rini
2024-06-13 15:22                   ` Simon Glass
2024-06-13 15:42                     ` Tom Rini
2024-06-13 16:59                       ` Simon Glass
2024-06-13 17:27                         ` Heinrich Schuchardt
2024-06-13 18:17                           ` Sughosh Ganu
2024-06-13 19:06                             ` Simon Glass
2024-06-13 19:05                           ` Simon Glass
2024-06-13 20:11                             ` Heinrich Schuchardt
2024-06-14  5:58                               ` Ilias Apalodimas
2024-06-19  3:01                                 ` Simon Glass
2024-06-19  3:03                               ` Simon Glass
2024-06-13 20:06                         ` Tom Rini
2024-06-07 18:52 ` [RFC PATCH 05/31] lmb: pass a flag to image_setup_libfdt() for lmb reservations Sughosh Ganu
2024-06-10 17:12   ` Tom Rini
2024-06-07 18:52 ` [RFC PATCH 06/31] lmb: reserve and add common memory regions post relocation Sughosh Ganu
2024-06-10 17:30   ` Tom Rini
2024-06-07 18:52 ` [RFC PATCH 07/31] lmb: remove lmb_init_and_reserve_range() function Sughosh Ganu
2024-06-10 17:30   ` Tom Rini
2024-06-10 21:42   ` Ilias Apalodimas
2024-06-07 18:52 ` [RFC PATCH 08/31] lmb: replcace the lmb_init_and_reserve() function Sughosh Ganu
2024-06-10 17:31   ` Tom Rini
2024-06-11  8:50     ` Sughosh Ganu
2024-06-11 13:57       ` Tom Rini
2024-06-07 18:52 ` [RFC PATCH 09/31] lmb: allow for resizing lmb regions Sughosh Ganu
2024-06-10 12:03   ` Ilias Apalodimas
2024-06-10 12:20     ` Sughosh Ganu
2024-06-10 12:47       ` Ilias Apalodimas
2024-06-10 12:57         ` Sughosh Ganu
2024-06-10 14:21           ` Ilias Apalodimas
2024-06-10 14:33             ` Sughosh Ganu
2024-06-10 12:54       ` Heinrich Schuchardt
2024-06-10 13:01         ` Sughosh Ganu
2024-06-11  9:17   ` Heinrich Schuchardt [this message]
2024-06-11  9:50     ` Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 10/31] event: add events to notify memory map changes Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 11/31] lib: Kconfig: add a config symbol for getting memory map updates Sughosh Ganu
2024-06-08  3:53   ` Heinrich Schuchardt
2024-06-08  4:34     ` Heinrich Schuchardt
2024-06-10 11:44   ` Ilias Apalodimas
2024-06-10 11:47     ` Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 12/31] add a function to check if an address is in RAM memory Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 13/31] efi_memory: notify of any changes to the EFI memory map Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 14/31] lmb: notify of any changes to the LMB " Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 15/31] efi_memory: add an event handler to update " Sughosh Ganu
2024-06-10 12:09   ` Ilias Apalodimas
2024-06-10 12:25     ` Sughosh Ganu
2024-06-10 14:17       ` Ilias Apalodimas
2024-06-10 14:52         ` Sughosh Ganu
2024-06-10 14:54           ` Sughosh Ganu
2024-06-11  6:19           ` Ilias Apalodimas
2024-06-10 15:12   ` Heinrich Schuchardt
2024-06-10 15:42     ` Sughosh Ganu
2024-06-10 15:54       ` Simon Glass
2024-06-12  6:45         ` Ilias Apalodimas
2024-06-12  7:11           ` Sughosh Ganu
2024-06-11 10:17   ` Heinrich Schuchardt
2024-06-11 10:27     ` Sughosh Ganu
2024-06-11 14:36     ` Tom Rini
2024-06-11 18:52       ` Simon Glass
2024-06-11 21:01         ` Tom Rini
2024-06-11 22:22           ` Simon Glass
2024-06-11 22:54             ` Tom Rini
2024-06-12  2:42               ` Simon Glass
2024-06-12  5:48                 ` Ilias Apalodimas
2024-06-12  6:20                   ` Sughosh Ganu
2024-06-12 20:24                   ` Simon Glass
2024-06-12  6:06                 ` Heinrich Schuchardt
2024-06-12 20:24                   ` Simon Glass
2024-06-07 18:52 ` [RFC PATCH 16/31] lmb: " Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 17/31] lmb: remove call to efi_lmb_reserve() Sughosh Ganu
2024-06-10 11:46   ` Ilias Apalodimas
2024-06-11  9:11   ` Heinrich Schuchardt
2024-06-11  9:49     ` Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 18/31] sandbox: iommu: remove lmb allocation in the driver Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 19/31] zynq: lmb: do not add to lmb map before relocation Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 20/31] test: cedit: use allocated address for reading file Sughosh Ganu
2024-06-11 18:52   ` Simon Glass
2024-06-07 18:52 ` [RFC PATCH 21/31] test: event: update the expected event dump output Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 22/31] test: lmb: run the LMB tests only on sandbox Sughosh Ganu
2024-06-10 17:44   ` Tom Rini
2024-06-11  8:55     ` Sughosh Ganu
2024-06-11  9:56       ` Heinrich Schuchardt
2024-06-11 10:09         ` Sughosh Ganu
2024-06-11 14:05       ` Tom Rini
2024-06-11 14:06         ` Ilias Apalodimas
2024-06-07 18:52 ` [RFC PATCH 23/31] test: lmb: initialise the lmb structure before tests Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 24/31] test: lmb: add a test case for checking overlapping region add Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 25/31] test: lmb: adjust the test case to handle overlapping regions Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 26/31] test: lmb: run lmb tests only manually Sughosh Ganu
2024-06-08  4:39   ` Heinrich Schuchardt
2024-06-10  6:22     ` Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 27/31] test: bdinfo: dump the global LMB memory map Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 28/31] cmd: bdinfo: only dump the current LMB memory Sughosh Ganu
2024-06-08  3:59   ` Heinrich Schuchardt
2024-06-10 11:42     ` Ilias Apalodimas
2024-06-07 18:52 ` [RFC PATCH 29/31] temp: mx6sabresd: bump up the size limit of the board Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 30/31] temp: cmd: efi_mem: add a command to test efi alloc/free Sughosh Ganu
2024-06-08  3:37   ` Heinrich Schuchardt
2024-06-10  6:44     ` Sughosh Ganu
2024-06-07 18:52 ` [RFC PATCH 31/31] temp: cmd: efi: add a command to dump EFI memory map Sughosh Ganu
2024-06-08  3:28   ` Heinrich Schuchardt
2024-06-10  6:45     ` Sughosh Ganu
2024-06-10 21:05 ` [RFC PATCH 00/31] Make U-Boot memory reservations coherent Tom Rini
2024-06-11  9:01   ` Sughosh Ganu
2024-06-11 14:39     ` Tom Rini
2024-06-11 18:52 ` Simon Glass

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=5d133571-ba43-40c7-bd19-0c0a4e857034@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=festevam@gmail.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=marex@denx.de \
    --cc=mark.kettenis@xs4all.nl \
    --cc=sjg@chromium.org \
    --cc=sughosh.ganu@linaro.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.