diff mbox series

[v1,6/8] lmb: Remove lmb_alloc_addr_flags()

Message ID 20241211105504.453234-7-ilias.apalodimas@linaro.org
State Superseded
Headers show
Series Cleanup the LMB subsystem | expand

Commit Message

Ilias Apalodimas Dec. 11, 2024, 10:54 a.m. UTC
lmb_alloc_addr() is just calling lmb_alloc_addr_flags() with LMB_NONE
There's not much we gain from this abstraction, so let's remove the
latter, add a flags argument to lmb_alloc_addr() and make the code a
bit easier to follow.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 fs/fs.c                     |  2 +-
 include/lmb.h               | 10 ++++------
 lib/efi_loader/efi_memory.c |  2 +-
 lib/lmb.c                   | 15 ++-------------
 test/lib/lmb.c              | 34 +++++++++++++++++-----------------
 5 files changed, 25 insertions(+), 38 deletions(-)

Comments

Sam Protsenko Dec. 12, 2024, 10:34 p.m. UTC | #1
On Wed, Dec 11, 2024 at 4:55 AM Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> lmb_alloc_addr() is just calling lmb_alloc_addr_flags() with LMB_NONE
> There's not much we gain from this abstraction, so let's remove the
> latter, add a flags argument to lmb_alloc_addr() and make the code a
> bit easier to follow.
>
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> ---
>  fs/fs.c                     |  2 +-
>  include/lmb.h               | 10 ++++------
>  lib/efi_loader/efi_memory.c |  2 +-
>  lib/lmb.c                   | 15 ++-------------
>  test/lib/lmb.c              | 34 +++++++++++++++++-----------------
>  5 files changed, 25 insertions(+), 38 deletions(-)
>
> diff --git a/fs/fs.c b/fs/fs.c
> index 21a23efd932d..99ddcc5e37be 100644
> --- a/fs/fs.c
> +++ b/fs/fs.c
> @@ -554,7 +554,7 @@ static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
>
>         lmb_dump_all();
>
> -       if (lmb_alloc_addr(addr, read_len) == addr)
> +       if (lmb_alloc_addr(addr, read_len, LMB_NONE) == addr)
>                 return 0;
>
>         log_err("** Reading file would overwrite reserved memory **\n");
> diff --git a/include/lmb.h b/include/lmb.h
> index 3db35992df8d..5e59915340b7 100644
> --- a/include/lmb.h
> +++ b/include/lmb.h
> @@ -92,7 +92,6 @@ long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags);
>
>  phys_addr_t lmb_alloc(phys_size_t size, ulong align);
>  phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
> -phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
>  phys_size_t lmb_get_free_size(phys_addr_t addr);
>
>  /**
> @@ -113,8 +112,8 @@ phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
>                                  phys_addr_t max_addr, uint flags);
>
>  /**
> - * lmb_alloc_addr_flags() - Allocate specified memory address with specified
> - *                         attributes
> + * lmb_alloc_addr() - Allocate specified memory address with specified attributes

One character over 80 limit trips my OCD :/ Any chance you can move
"attributes" to the next line? It's ok to break the line in kernel-doc
function briefs.

> + *
>   * @base: Base Address requested
>   * @size: Size of the region requested
>   * @flags: Memory region attributes to be set
> @@ -125,8 +124,7 @@ phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
>   *
>   * Return: Base address on success, 0 on error.
>   */
> -phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
> -                                uint flags);
> +phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, uint flags);

Can we keep u32 for flags everywhere, for consistency?

With above nitpicks addressed, feel free to add:

Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>

[snip]
Ilias Apalodimas Dec. 13, 2024, 5:39 a.m. UTC | #2
On Fri, 13 Dec 2024 at 00:34, Sam Protsenko <semen.protsenko@linaro.org> wrote:
>
> On Wed, Dec 11, 2024 at 4:55 AM Ilias Apalodimas
> <ilias.apalodimas@linaro.org> wrote:
> >
> > lmb_alloc_addr() is just calling lmb_alloc_addr_flags() with LMB_NONE
> > There's not much we gain from this abstraction, so let's remove the
> > latter, add a flags argument to lmb_alloc_addr() and make the code a
> > bit easier to follow.
> >
> > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > ---
> >  fs/fs.c                     |  2 +-
> >  include/lmb.h               | 10 ++++------
> >  lib/efi_loader/efi_memory.c |  2 +-
> >  lib/lmb.c                   | 15 ++-------------
> >  test/lib/lmb.c              | 34 +++++++++++++++++-----------------
> >  5 files changed, 25 insertions(+), 38 deletions(-)
> >
> > diff --git a/fs/fs.c b/fs/fs.c
> > index 21a23efd932d..99ddcc5e37be 100644
> > --- a/fs/fs.c
> > +++ b/fs/fs.c
> > @@ -554,7 +554,7 @@ static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
> >
> >         lmb_dump_all();
> >
> > -       if (lmb_alloc_addr(addr, read_len) == addr)
> > +       if (lmb_alloc_addr(addr, read_len, LMB_NONE) == addr)
> >                 return 0;
> >
> >         log_err("** Reading file would overwrite reserved memory **\n");
> > diff --git a/include/lmb.h b/include/lmb.h
> > index 3db35992df8d..5e59915340b7 100644
> > --- a/include/lmb.h
> > +++ b/include/lmb.h
> > @@ -92,7 +92,6 @@ long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags);
> >
> >  phys_addr_t lmb_alloc(phys_size_t size, ulong align);
> >  phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
> > -phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
> >  phys_size_t lmb_get_free_size(phys_addr_t addr);
> >
> >  /**
> > @@ -113,8 +112,8 @@ phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
> >                                  phys_addr_t max_addr, uint flags);
> >
> >  /**
> > - * lmb_alloc_addr_flags() - Allocate specified memory address with specified
> > - *                         attributes
> > + * lmb_alloc_addr() - Allocate specified memory address with specified attributes
>
> One character over 80 limit trips my OCD :/ Any chance you can move
> "attributes" to the next line? It's ok to break the line in kernel-doc
> function briefs.
>
> > + *
> >   * @base: Base Address requested
> >   * @size: Size of the region requested
> >   * @flags: Memory region attributes to be set
> > @@ -125,8 +124,7 @@ phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
> >   *
> >   * Return: Base address on success, 0 on error.
> >   */
> > -phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
> > -                                uint flags);
> > +phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, uint flags);
>
> Can we keep u32 for flags everywhere, for consistency?

We are, I thought it would make the patches easier to review if I only
changed the enum here.
This turns to a u32 in the last patch


>
> With above nitpicks addressed, feel free to add:
>
> Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>

Thanks!
/Ilias

>
> [snip]
diff mbox series

Patch

diff --git a/fs/fs.c b/fs/fs.c
index 21a23efd932d..99ddcc5e37be 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -554,7 +554,7 @@  static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
 
 	lmb_dump_all();
 
-	if (lmb_alloc_addr(addr, read_len) == addr)
+	if (lmb_alloc_addr(addr, read_len, LMB_NONE) == addr)
 		return 0;
 
 	log_err("** Reading file would overwrite reserved memory **\n");
diff --git a/include/lmb.h b/include/lmb.h
index 3db35992df8d..5e59915340b7 100644
--- a/include/lmb.h
+++ b/include/lmb.h
@@ -92,7 +92,6 @@  long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags);
 
 phys_addr_t lmb_alloc(phys_size_t size, ulong align);
 phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr);
-phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size);
 phys_size_t lmb_get_free_size(phys_addr_t addr);
 
 /**
@@ -113,8 +112,8 @@  phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
 				 phys_addr_t max_addr, uint flags);
 
 /**
- * lmb_alloc_addr_flags() - Allocate specified memory address with specified
- *			    attributes
+ * lmb_alloc_addr() - Allocate specified memory address with specified attributes
+ *
  * @base: Base Address requested
  * @size: Size of the region requested
  * @flags: Memory region attributes to be set
@@ -125,8 +124,7 @@  phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
  *
  * Return: Base address on success, 0 on error.
  */
-phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
-				 uint flags);
+phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, uint flags);
 
 /**
  * lmb_is_reserved_flags() - Test if address is in reserved region with flag
@@ -164,7 +162,7 @@  void lmb_pop(struct lmb *store);
 
 static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
 {
-	return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
+	return lmb_alloc_addr(addr, len, LMB_NONE) == addr ? 0 : -1;
 }
 
 /**
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index edd7da7d8c6e..34e2b9e18ef0 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -490,7 +490,7 @@  efi_status_t efi_allocate_pages(enum efi_allocate_type type,
 			return EFI_NOT_FOUND;
 
 		addr = map_to_sysmem((void *)(uintptr_t)*memory);
-		addr = (u64)lmb_alloc_addr_flags(addr, len, flags);
+		addr = (u64)lmb_alloc_addr(addr, len, flags);
 		if (!addr)
 			return EFI_NOT_FOUND;
 		break;
diff --git a/lib/lmb.c b/lib/lmb.c
index 659581f13f20..ffad7ec12eb5 100644
--- a/lib/lmb.c
+++ b/lib/lmb.c
@@ -761,8 +761,7 @@  phys_addr_t lmb_alloc_base_flags(phys_size_t size, ulong align,
 	return alloc;
 }
 
-static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
-				   u32 flags)
+static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags)
 {
 	long rgn;
 	struct lmb_region *lmb_memory = lmb.available_mem.data;
@@ -786,17 +785,7 @@  static phys_addr_t _lmb_alloc_addr(phys_addr_t base, phys_size_t size,
 	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);
-}
-
-phys_addr_t lmb_alloc_addr_flags(phys_addr_t base, phys_size_t size,
-				 uint flags)
+phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, uint flags)
 {
 	return _lmb_alloc_addr(base, size, flags);
 }
diff --git a/test/lib/lmb.c b/test/lib/lmb.c
index 6e870274fedb..971614fd8314 100644
--- a/test/lib/lmb.c
+++ b/test/lib/lmb.c
@@ -530,21 +530,21 @@  static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
 	ut_asserteq(ret, 0);
 
 	/* Try to allocate a page twice */
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x1000, LMB_NONE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
 	ut_asserteq(b, alloc_addr_a);
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
 	ut_asserteq(b, 0);
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x1000, LMB_NONE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
 	ut_asserteq(b, alloc_addr_a);
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x2000, LMB_NONE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x2000, LMB_NONE);
 	ut_asserteq(b, alloc_addr_a);
 	ret = lmb_free(alloc_addr_a, 0x2000);
 	ut_asserteq(ret, 0);
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
 	ut_asserteq(b, alloc_addr_a);
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x1000, LMB_NONE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NONE);
 	ut_asserteq(b, 0);
-	b = lmb_alloc_addr_flags(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
+	b = lmb_alloc_addr(alloc_addr_a, 0x1000, LMB_NOOVERWRITE);
 	ut_asserteq(b, 0);
 	ret = lmb_free(alloc_addr_a, 0x1000);
 	ut_asserteq(ret, 0);
@@ -560,22 +560,22 @@  static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
 		   alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
 
 	/* allocate blocks */
-	a = lmb_alloc_addr(ram, alloc_addr_a - ram);
+	a = lmb_alloc_addr(ram, alloc_addr_a - ram, LMB_NONE);
 	ut_asserteq(a, ram);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 3, ram, 0x8010000,
 		   alloc_addr_b, 0x10000, alloc_addr_c, 0x10000);
 	b = lmb_alloc_addr(alloc_addr_a + 0x10000,
-			   alloc_addr_b - alloc_addr_a - 0x10000);
+			   alloc_addr_b - alloc_addr_a - 0x10000, LMB_NONE);
 	ut_asserteq(b, alloc_addr_a + 0x10000);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x10010000,
 		   alloc_addr_c, 0x10000, 0, 0);
 	c = lmb_alloc_addr(alloc_addr_b + 0x10000,
-			   alloc_addr_c - alloc_addr_b - 0x10000);
+			   alloc_addr_c - alloc_addr_b - 0x10000, LMB_NONE);
 	ut_asserteq(c, alloc_addr_b + 0x10000);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
 		   0, 0, 0, 0);
 	d = lmb_alloc_addr(alloc_addr_c + 0x10000,
-			   ram_end - alloc_addr_c - 0x10000);
+			   ram_end - alloc_addr_c - 0x10000, LMB_NONE);
 	ut_asserteq(d, alloc_addr_c + 0x10000);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, ram_size,
 		   0, 0, 0, 0);
@@ -591,7 +591,7 @@  static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
 
 	/* allocate at 3 points in free range */
 
-	d = lmb_alloc_addr(ram_end - 4, 4);
+	d = lmb_alloc_addr(ram_end - 4, 4, LMB_NONE);
 	ut_asserteq(d, ram_end - 4);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
 		   d, 4, 0, 0);
@@ -600,7 +600,7 @@  static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
 		   0, 0, 0, 0);
 
-	d = lmb_alloc_addr(ram_end - 128, 4);
+	d = lmb_alloc_addr(ram_end - 128, 4, LMB_NONE);
 	ut_asserteq(d, ram_end - 128);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, ram, 0x18010000,
 		   d, 4, 0, 0);
@@ -609,7 +609,7 @@  static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010000,
 		   0, 0, 0, 0);
 
-	d = lmb_alloc_addr(alloc_addr_c + 0x10000, 4);
+	d = lmb_alloc_addr(alloc_addr_c + 0x10000, 4, LMB_NONE);
 	ut_asserteq(d, alloc_addr_c + 0x10000);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram, 0x18010004,
 		   0, 0, 0, 0);
@@ -624,18 +624,18 @@  static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram)
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, ram + 0x8000000,
 		   0x10010000, 0, 0, 0, 0);
 
-	d = lmb_alloc_addr(ram, 4);
+	d = lmb_alloc_addr(ram, 4, LMB_NONE);
 	ut_asserteq(d, ram);
 	ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 2, d, 4,
 		   ram + 0x8000000, 0x10010000, 0, 0);
 
 	/* check that allocating outside memory fails */
 	if (ram_end != 0) {
-		ret = lmb_alloc_addr(ram_end, 1);
+		ret = lmb_alloc_addr(ram_end, 1, LMB_NONE);
 		ut_asserteq(ret, 0);
 	}
 	if (ram != 0) {
-		ret = lmb_alloc_addr(ram - 1, 1);
+		ret = lmb_alloc_addr(ram - 1, 1, LMB_NONE);
 		ut_asserteq(ret, 0);
 	}