Skip to contentSkip to conversation

efi: Fix truncation of constant value

PATCH New Eugeniu Rosca 5 participants 16 messages Jul 14, 2018 – Aug 20, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Starting with commit 867a6ac86dd8 ("efi: Add start-up library code"), sparse constantly complains about truncated constant value in efi.h:

include/efi.h:176:35: warning: cast truncates bits from constant value (8000000000000000 becomes 0)

This can get quite noisy, preventing real issues to be noticed:

$ make defconfig
*** Default configuration is based on 'sandbox_defconfig'
$ make C=2 -j12 2>&1 | grep truncates | wc -l
441

After the patch is applied:

$ make C=2 -j12 2>&1 | grep truncates | wc -l
0
$ sparse --version
v0.5.2

Following the suggestion of Heinrich Schuchardt, instead of only fixing the root-cause, I replaced the whole enum of _SHIFT values by ULL defines. This matches both the UEFI 2.7 spec and the Linux kernel implementation.

Some ELF size comparison before and after the patch (gcc 7.3.0):

efi-x86_payload64_defconfig:

text    data    bss     dec       hex   filename
407174  29432   278676  715282    aea12 u-boot.old
407152  29464   278676  715292    aea1c u-boot.new
-22     +32     0       +10

efi-x86_payload32_defconfig:

text    data    bss     dec       hex   filename
447075  30308   280076  757459    b8ed3 u-boot.old
447053  30340   280076  757469    b8edd u-boot.new
-22     +32     0       +10
Fixes 867a6ac86dd8 ("efi: Add start-up library code")

---

v2:
- Replace _SHIFT enum values by ULL defines to match UEFI 2.7 spec
- Add ELF size comparison to patch description

 cmd/efi.c                   | 22 +++++++++++-----------
 include/efi.h               | 24 ++++++++++--------------
 lib/efi_loader/efi_memory.c |  7 +++----
 3 files changed, 24 insertions(+), 29 deletions(-)
cmd/efi.c +11 -11
index 6c1eb88424be..92a565f71373 100644
@@ -28,18 +28,18 @@ static const char *const type_name[] = {
 };
 
 static struct attr_info {
-	int shift;
+	u64 val;
 	const char *name;
 } mem_attr[] = {
-	{ EFI_MEMORY_UC_SHIFT, "uncached" },
-	{ EFI_MEMORY_WC_SHIFT, "write-coalescing" },
-	{ EFI_MEMORY_WT_SHIFT, "write-through" },
-	{ EFI_MEMORY_WB_SHIFT, "write-back" },
-	{ EFI_MEMORY_UCE_SHIFT, "uncached & exported" },
-	{ EFI_MEMORY_WP_SHIFT, "write-protect" },
-	{ EFI_MEMORY_RP_SHIFT, "read-protect" },
-	{ EFI_MEMORY_XP_SHIFT, "execute-protect" },
-	{ EFI_MEMORY_RUNTIME_SHIFT, "needs runtime mapping" }
+	{ EFI_MEMORY_UC, "uncached" },
+	{ EFI_MEMORY_WC, "write-coalescing" },
+	{ EFI_MEMORY_WT, "write-through" },
+	{ EFI_MEMORY_WB, "write-back" },
+	{ EFI_MEMORY_UCE, "uncached & exported" },
+	{ EFI_MEMORY_WP, "write-protect" },
+	{ EFI_MEMORY_RP, "read-protect" },
+	{ EFI_MEMORY_XP, "execute-protect" },
+	{ EFI_MEMORY_RUNTIME, "needs runtime mapping" }
 };
 
 /* Maximum different attribute values we can track */
@@ -173,7 +173,7 @@ static void efi_print_mem_table(struct efi_entry_memmap *map,
 		printf("%c%llx: ", attr & EFI_MEMORY_RUNTIME ? 'r' : ' ',
 		       attr & ~EFI_MEMORY_RUNTIME);
 		for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
-			if (attr & (1ULL << mem_attr[j].shift)) {
+			if (attr & mem_attr[j].val) {
 				if (first)
 					first = false;
 				else
include/efi.h +10 -14
index 0fe15e65c06c..eb2a569fe010 100644
@@ -162,20 +162,16 @@ enum efi_mem_type {
 };
 
 /* Attribute values */
-enum {
-	EFI_MEMORY_UC_SHIFT	= 0,	/* uncached */
-	EFI_MEMORY_WC_SHIFT	= 1,	/* write-coalescing */
-	EFI_MEMORY_WT_SHIFT	= 2,	/* write-through */
-	EFI_MEMORY_WB_SHIFT	= 3,	/* write-back */
-	EFI_MEMORY_UCE_SHIFT	= 4,	/* uncached, exported */
-	EFI_MEMORY_WP_SHIFT	= 12,	/* write-protect */
-	EFI_MEMORY_RP_SHIFT	= 13,	/* read-protect */
-	EFI_MEMORY_XP_SHIFT	= 14,	/* execute-protect */
-	EFI_MEMORY_RUNTIME_SHIFT = 63,	/* range requires runtime mapping */
-
-	EFI_MEMORY_RUNTIME = 1ULL << EFI_MEMORY_RUNTIME_SHIFT,
-	EFI_MEM_DESC_VERSION	= 1,
-};
+#define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
+#define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
+#define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
+#define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
+#define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
+#define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
+#define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
+#define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
+#define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
+#define EFI_MEM_DESC_VERSION	1
 
 #define EFI_PAGE_SHIFT		12
 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
lib/efi_loader/efi_memory.c +3 -4
index ec66af98ea8f..233333bf6b18 100644
@@ -171,14 +171,13 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
 	switch (memory_type) {
 	case EFI_RUNTIME_SERVICES_CODE:
 	case EFI_RUNTIME_SERVICES_DATA:
-		newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
-					  (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
+		newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
 		break;
 	case EFI_MMAP_IO:
-		newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
+		newlist->desc.attribute = EFI_MEMORY_RUNTIME;
 		break;
 	default:
-		newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
+		newlist->desc.attribute = EFI_MEMORY_WB;
 		break;
 	}
 
Signature · 1 line
2.18.0
#1 lore raw

PATCH v2 2/3 efi: Add EFI_MEMORY_{NV, MORE_RELIABLE, RO} attributes

With this update, the memory attributes are in sync with Linux kernel v4.18-rc4. They also match page 190 of UEFI 2.7 spec [1].

[1] http://www.uefi.org/sites/default/files/resources/UEFI_Spec_2_7.pdf

---

v2:
- Newly added

 cmd/efi.c     | 3 +++
 include/efi.h | 4 ++++
 2 files changed, 7 insertions(+)
cmd/efi.c +3 -0
index 92a565f71373..366a79a96488 100644
@@ -39,6 +39,9 @@ static struct attr_info {
 	{ EFI_MEMORY_WP, "write-protect" },
 	{ EFI_MEMORY_RP, "read-protect" },
 	{ EFI_MEMORY_XP, "execute-protect" },
+	{ EFI_MEMORY_NV, "non-volatile" },
+	{ EFI_MEMORY_MORE_RELIABLE, "higher reliability" },
+	{ EFI_MEMORY_RO, "read-only" },
 	{ EFI_MEMORY_RUNTIME, "needs runtime mapping" }
 };
 
include/efi.h +4 -0
index eb2a569fe010..046decba3d58 100644
@@ -170,6 +170,10 @@ enum efi_mem_type {
 #define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
 #define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
 #define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
+#define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
+#define EFI_MEMORY_MORE_RELIABLE \
+				((u64)0x0000000000010000ULL)	/* higher reliability */
+#define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
 #define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
 #define EFI_MEM_DESC_VERSION	1
 
Signature · 1 line
2.18.0
#2 lore raw

PATCH v2 2/3 efi: Add EFI_MEMORY_{NV, MORE_RELIABLE, RO} attributes

Quoted 9 lines · Eugeniu Rosca

With this update, the memory attributes are in sync with Linux kernel v4.18-rc4. They also match page 190 of UEFI 2.7 spec [1].

[1] http://www.uefi.org/sites/default/files/resources/UEFI_Spec_2_7.pdf

---

#3 lore raw

PATCH v2 2/3 efi: Add EFI_MEMORY_{NV, MORE_RELIABLE, RO} attributes

In reply to Eugeniu Rosca
Quoted 8 lines

With this update, the memory attributes are in sync with Linux kernel v4.18-rc4. They also match page 190 of UEFI 2.7 spec [1].

[1] http://www.uefi.org/sites/default/files/resources/UEFI_Spec_2_7.pdf

Thanks, applied to efi-2018.09

Alex

#4 lore raw

PATCH v2 3/3 cmd: efi: Clarify calculation precedence for '&' and '?'

In reply to Eugeniu Rosca

Fix cppcheck complaint:
[cmd/efi.c:173]: (style) Clarify calculation precedence for '&' and '?'.

Fixes f1a0bafb5802 ("efi: Add a command to display the memory map")

---

v2:
- newly added

 cmd/efi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/efi.c +1 -1
index 366a79a96488..919cb2fcfd55 100644
@@ -173,7 +173,7 @@ static void efi_print_mem_table(struct efi_entry_memmap *map,
 		bool first;
 		int j;
 
-		printf("%c%llx: ", attr & EFI_MEMORY_RUNTIME ? 'r' : ' ',
+		printf("%c%llx: ", (attr & EFI_MEMORY_RUNTIME) ? 'r' : ' ',
 		       attr & ~EFI_MEMORY_RUNTIME);
 		for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
 			if (attr & mem_attr[j].val) {
Signature · 1 line
2.18.0
#7 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

In reply to Eugeniu Rosca
Quoted 106 lines · Eugeniu Rosca

Starting with commit 867a6ac86dd8 ("efi: Add start-up library code"), sparse constantly complains about truncated constant value in efi.h:

include/efi.h:176:35: warning: cast truncates bits from constant value (8000000000000000 becomes 0)

This can get quite noisy, preventing real issues to be noticed:

$ make defconfig
*** Default configuration is based on 'sandbox_defconfig'
$ make C=2 -j12 2>&1 | grep truncates | wc -l
441

After the patch is applied:

$ make C=2 -j12 2>&1 | grep truncates | wc -l
0
$ sparse --version
v0.5.2

Following the suggestion of Heinrich Schuchardt, instead of only fixing the root-cause, I replaced the whole enum of _SHIFT values by ULL defines. This matches both the UEFI 2.7 spec and the Linux kernel implementation.

Some ELF size comparison before and after the patch (gcc 7.3.0):

efi-x86_payload64_defconfig:

text    data    bss     dec       hex   filename
407174  29432   278676  715282    aea12 u-boot.old
407152  29464   278676  715292    aea1c u-boot.new
-22     +32     0       +10

efi-x86_payload32_defconfig:

text    data    bss     dec       hex   filename
447075  30308   280076  757459    b8ed3 u-boot.old
447053  30340   280076  757469    b8edd u-boot.new
-22     +32     0       +10
Fixes 867a6ac86dd8 ("efi: Add start-up library code")

---

v2:
- Replace _SHIFT enum values by ULL defines to match UEFI 2.7 spec
- Add ELF size comparison to patch description

 cmd/efi.c                   | 22 +++++++++++-----------
 include/efi.h               | 24 ++++++++++--------------
 lib/efi_loader/efi_memory.c |  7 +++----
 3 files changed, 24 insertions(+), 29 deletions(-)
cmd/efi.c +11 -11
index 6c1eb88424be..92a565f71373 100644
@@ -28,18 +28,18 @@ static const char *const type_name[] = {
 };
 
 static struct attr_info {
-	int shift;
+	u64 val;
 	const char *name;
 } mem_attr[] = {
-	{ EFI_MEMORY_UC_SHIFT, "uncached" },
-	{ EFI_MEMORY_WC_SHIFT, "write-coalescing" },
-	{ EFI_MEMORY_WT_SHIFT, "write-through" },
-	{ EFI_MEMORY_WB_SHIFT, "write-back" },
-	{ EFI_MEMORY_UCE_SHIFT, "uncached & exported" },
-	{ EFI_MEMORY_WP_SHIFT, "write-protect" },
-	{ EFI_MEMORY_RP_SHIFT, "read-protect" },
-	{ EFI_MEMORY_XP_SHIFT, "execute-protect" },
-	{ EFI_MEMORY_RUNTIME_SHIFT, "needs runtime mapping" }
+	{ EFI_MEMORY_UC, "uncached" },
+	{ EFI_MEMORY_WC, "write-coalescing" },
+	{ EFI_MEMORY_WT, "write-through" },
+	{ EFI_MEMORY_WB, "write-back" },
+	{ EFI_MEMORY_UCE, "uncached & exported" },
+	{ EFI_MEMORY_WP, "write-protect" },
+	{ EFI_MEMORY_RP, "read-protect" },
+	{ EFI_MEMORY_XP, "execute-protect" },
+	{ EFI_MEMORY_RUNTIME, "needs runtime mapping" }
 };
 
 /* Maximum different attribute values we can track */
@@ -173,7 +173,7 @@ static void efi_print_mem_table(struct efi_entry_memmap *map,
 		printf("%c%llx: ", attr & EFI_MEMORY_RUNTIME ? 'r' : ' ',
 		       attr & ~EFI_MEMORY_RUNTIME);
 		for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) {
-			if (attr & (1ULL << mem_attr[j].shift)) {
+			if (attr & mem_attr[j].val) {
 				if (first)
 					first = false;
 				else
include/efi.h +0 -5
index 0fe15e65c06c..eb2a569fe010 100644
@@ -162,20 +162,16 @@ enum efi_mem_type {
 };
 
 /* Attribute values */
-enum {
-	EFI_MEMORY_UC_SHIFT	= 0,	/* uncached */
-	EFI_MEMORY_WC_SHIFT	= 1,	/* write-coalescing */
-	EFI_MEMORY_WT_SHIFT	= 2,	/* write-through */
-	EFI_MEMORY_WB_SHIFT	= 3,	/* write-back */

include/efi.h enum efi_mem_type { · line 165

Quoted 10 lines
include/efi.h +1 -9
-	EFI_MEMORY_UCE_SHIFT	= 4,	/* uncached, exported */
-	EFI_MEMORY_WP_SHIFT	= 12,	/* write-protect */
-	EFI_MEMORY_RP_SHIFT	= 13,	/* read-protect */
-	EFI_MEMORY_XP_SHIFT	= 14,	/* execute-protect */
-	EFI_MEMORY_RUNTIME_SHIFT = 63,	/* range requires runtime mapping */
-
-	EFI_MEMORY_RUNTIME = 1ULL << EFI_MEMORY_RUNTIME_SHIFT,
-	EFI_MEM_DESC_VERSION	= 1,
-};
+#define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */

Unsigned long long int (ULL) is at least 64bit wide and unsigned (cf.
https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html). Why do you introduce the (u64) conversion?

Otherwise

Quoted 35 lines
include/efi.h +9 -0
+#define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
+#define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
+#define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
+#define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
+#define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
+#define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
+#define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
+#define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
+#define EFI_MEM_DESC_VERSION	1
 
 #define EFI_PAGE_SHIFT		12
 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
lib/efi_loader/efi_memory.c +3 -4
index ec66af98ea8f..233333bf6b18 100644
@@ -171,14 +171,13 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
 	switch (memory_type) {
 	case EFI_RUNTIME_SERVICES_CODE:
 	case EFI_RUNTIME_SERVICES_DATA:
-		newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
-					  (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
+		newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
 		break;
 	case EFI_MMAP_IO:
-		newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
+		newlist->desc.attribute = EFI_MEMORY_RUNTIME;
 		break;
 	default:
-		newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
+		newlist->desc.attribute = EFI_MEMORY_WB;
 		break;
 	}
 
#8 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Hi Heinrich,

Thanks for your review comments. See my reply below.

On Mon, Jul 16, 2018 at 07:52:20AM +0200, Heinrich Schuchardt wrote:
[--snip--]

Quoted 19 lines
Quoted 19 lines
include/efi.h +0 -11
index 0fe15e65c06c..eb2a569fe010 100644
@@ -162,20 +162,16 @@ enum efi_mem_type {
 };
 
 /* Attribute values */
-enum {
-	EFI_MEMORY_UC_SHIFT	= 0,	/* uncached */
-	EFI_MEMORY_WC_SHIFT	= 1,	/* write-coalescing */
-	EFI_MEMORY_WT_SHIFT	= 2,	/* write-through */
-	EFI_MEMORY_WB_SHIFT	= 3,	/* write-back */
-	EFI_MEMORY_UCE_SHIFT	= 4,	/* uncached, exported */
-	EFI_MEMORY_WP_SHIFT	= 12,	/* write-protect */
-	EFI_MEMORY_RP_SHIFT	= 13,	/* read-protect */
-	EFI_MEMORY_XP_SHIFT	= 14,	/* execute-protect */
-	EFI_MEMORY_RUNTIME_SHIFT = 63,	/* range requires runtime mapping */
-
Quoted 8 lines

include/efi.h enum efi_mem_type { · line 165

Quoted 4 lines
include/efi.h +1 -3
-	EFI_MEMORY_RUNTIME = 1ULL << EFI_MEMORY_RUNTIME_SHIFT,
-	EFI_MEM_DESC_VERSION	= 1,
-};
+#define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */

Unsigned long long int (ULL) is at least 64bit wide and unsigned (cf.
https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html). Why do you introduce the (u64) conversion?

Because this is how it appears in Linux kernel:

$ git blame ./include/linux/efi.h | grep 'define EFI_MEMORY_.*u64'
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   90) #define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   91) #define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   92) #define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   93) #define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
9c97e0bdd4b4a (Laszlo Ersek        2014-09-03 13:32:19 +0200   94) #define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   95) #define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   96) #define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   97) #define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
c016ca08f89c6 (Robert Elliott      2016-02-01 22:07:06 +0000   98) #define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
87db73aebf555 (Ard Biesheuvel      2015-08-07 09:36:54 +0100  101) #define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  102) #define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */

Unless we see a potential issue with that (and I don't see), IMO we should stick to kernel sources as much as possible, since this makes integration/re-sync process easier.

Quoted 15 lines

Otherwise

Quoted 10 lines
include/efi.h +9 -0
+#define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
+#define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
+#define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
+#define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
+#define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
+#define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
+#define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
+#define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
+#define EFI_MEM_DESC_VERSION	1
 
Quoted 2 lines

include/efi.h enum efi_mem_type { · line 177

Quoted 2 lines
include/efi.h +0 -0
 #define EFI_PAGE_SHIFT		12
 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)

Best regards,
Eugeniu.

#9 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Quoted 49 lines · Eugeniu Rosca

Hi Heinrich,

Thanks for your review comments. See my reply below.

On Mon, Jul 16, 2018 at 07:52:20AM +0200, Heinrich Schuchardt wrote:
[--snip--]

Quoted 19 lines
Quoted 19 lines
include/efi.h +0 -11
index 0fe15e65c06c..eb2a569fe010 100644
@@ -162,20 +162,16 @@ enum efi_mem_type {
  };
 
  /* Attribute values */
-enum {
-	EFI_MEMORY_UC_SHIFT	= 0,	/* uncached */
-	EFI_MEMORY_WC_SHIFT	= 1,	/* write-coalescing */
-	EFI_MEMORY_WT_SHIFT	= 2,	/* write-through */
-	EFI_MEMORY_WB_SHIFT	= 3,	/* write-back */
-	EFI_MEMORY_UCE_SHIFT	= 4,	/* uncached, exported */
-	EFI_MEMORY_WP_SHIFT	= 12,	/* write-protect */
-	EFI_MEMORY_RP_SHIFT	= 13,	/* read-protect */
-	EFI_MEMORY_XP_SHIFT	= 14,	/* execute-protect */
-	EFI_MEMORY_RUNTIME_SHIFT = 63,	/* range requires runtime mapping */
-
Quoted 8 lines

include/efi.h enum efi_mem_type { · line 165

Quoted 4 lines
include/efi.h +1 -3
-	EFI_MEMORY_RUNTIME = 1ULL << EFI_MEMORY_RUNTIME_SHIFT,
-	EFI_MEM_DESC_VERSION	= 1,
-};
+#define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */

Unsigned long long int (ULL) is at least 64bit wide and unsigned (cf.
https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html). Why do you introduce the (u64) conversion?

Because this is how it appears in Linux kernel:

$ git blame ./include/linux/efi.h | grep 'define EFI_MEMORY_.*u64'
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   90) #define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   91) #define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   92) #define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   93) #define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
9c97e0bdd4b4a (Laszlo Ersek        2014-09-03 13:32:19 +0200   94) #define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   95) #define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   96) #define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   97) #define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
c016ca08f89c6 (Robert Elliott      2016-02-01 22:07:06 +0000   98) #define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
87db73aebf555 (Ard Biesheuvel      2015-08-07 09:36:54 +0100  101) #define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  102) #define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
Quoted 4 lines

Unless we see a potential issue with that (and I don't see), IMO we should stick to kernel sources as much as possible, since this makes integration/re-sync process easier.

#10 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Hello Alexander,

Heinrich was kind to have a look at [1] and already provided his Reviewed-by. Could you please state your further expectations to accept the patches?

[1] https://patchwork.ozlabs.org/patch/944004/

Thanks,
Eugeniu.

#11 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Hello Tom, Simon, Alexander, Heinrich,

Quoted 10 lines · Eugeniu Rosca

Hello Alexander,

Heinrich was kind to have a look at [1] and already provided his Reviewed-by. Could you please state your further expectations to accept the patches?

[1] https://patchwork.ozlabs.org/patch/944004/

Thanks,
Eugeniu.

Any idea how to move forward with these patches?

Best regards,
Eugeniu.

#12 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Hello Tom, Alexander,

Quoted 9 lines · Eugeniu Rosca

Hello Tom, Simon, Alexander, Heinrich,

Quoted 6 lines · Eugeniu Rosca

Hello Alexander,

Heinrich was kind to have a look at [1] and already provided his Reviewed-by. Could you please state your further expectations to accept the patches?

Quoted 6 lines
Quoted 4 lines

[1] https://patchwork.ozlabs.org/patch/944004/

Thanks,
Eugeniu.

Any idea how to move forward with these patches?

I apologize for this second reminder. Could you please suggest the next steps for this simple set of three fixes? Thank you.

Quoted 3 lines

Best regards,
Eugeniu.

#13 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Hi,

Quoted 14 lines · Eugeniu Rosca

Hello Tom, Alexander,

Quoted 11 lines · Eugeniu Rosca

Hello Tom, Simon, Alexander, Heinrich,

Quoted 8 lines · Eugeniu Rosca

Hello Alexander,

Heinrich was kind to have a look at [1] and already provided his Reviewed-by. Could you please state your further expectations to accept the patches?

[1] https://patchwork.ozlabs.org/patch/944004/

Quoted 11 lines
Quoted 4 lines
Quoted 2 lines

Thanks,
Eugeniu.

Any idea how to move forward with these patches?

I apologize for this second reminder. Could you please suggest the next steps for this simple set of three fixes? Thank you.

Quoted 3 lines

Best regards,
Eugeniu.

I believe Alex should pick these up.

Regards,
Simon

#14 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

Quoted 4 lines · Simon Glass

I believe Alex should pick these up.

Regards,
Simon

Thanks, Simon!
I then look forward for some feedback from Alex.

Best regards,
Eugeniu.

#15 lore raw

PATCH v2 1/3 efi: Fix truncation of constant value

In reply to Eugeniu Rosca
Quoted 37 lines

Starting with commit 867a6ac86dd8 ("efi: Add start-up library code"), sparse constantly complains about truncated constant value in efi.h:

include/efi.h:176:35: warning: cast truncates bits from constant value (8000000000000000 becomes 0)

This can get quite noisy, preventing real issues to be noticed:

$ make defconfig
*** Default configuration is based on 'sandbox_defconfig'
$ make C=2 -j12 2>&1 | grep truncates | wc -l
441

After the patch is applied:

$ make C=2 -j12 2>&1 | grep truncates | wc -l
0
$ sparse --version
v0.5.2

Following the suggestion of Heinrich Schuchardt, instead of only fixing the root-cause, I replaced the whole enum of _SHIFT values by ULL defines. This matches both the UEFI 2.7 spec and the Linux kernel implementation.

Some ELF size comparison before and after the patch (gcc 7.3.0):

efi-x86_payload64_defconfig:

text    data    bss     dec       hex   filename
407174  29432   278676  715282    aea12 u-boot.old
407152  29464   278676  715292    aea1c u-boot.new
-22     +32     0       +10

efi-x86_payload32_defconfig:

text    data    bss     dec       hex   filename
447075  30308   280076  757459    b8ed3 u-boot.old
447053  30340   280076  757469    b8edd u-boot.new
-22     +32     0       +10
Quoted 5 lines
Fixes 867a6ac86dd8 ("efi: Add start-up library code")

Thanks, applied to efi-2018.09

Alex