linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ingo Molnar <mingo@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Lutomirski <luto@amacapital.net>,
	Borislav Petkov <bp@alien8.de>, "H . Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Yinghai Lu <yinghai@kernel.org>
Subject: Re: [PATCH 37/50] x86/boot/e820: Use 'enum e820_type' in 'struct e820_entry'
Date: Sun, 29 Jan 2017 10:19:12 +0100	[thread overview]
Message-ID: <20170129091912.GA26237@gmail.com> (raw)
In-Reply-To: <CA+55aFxSgQZVmefbzWgNa6Af3+HDN=zZQnV_OactcJRD1YMHKA@mail.gmail.com>


* Linus Torvalds <torvalds@linux-foundation.org> wrote:

> On Sat, Jan 28, 2017 at 2:11 PM, Ingo Molnar <mingo@kernel.org> wrote:
> > Use a stricter type for struct e820_entry. Add a build-time check to make
> > sure the compiler won't ever pack the enum into a field smaller than
> > 'int'.
> 
> I'm not sure this is a good idea. In fact, I'm pretty sure it's a horrible idea.
> 
> The compiler that *users* use might decide that the "enum" fits in a
> 8-bit unsigned char, and decide to use that. The kernel build won't
> notice and the BUG_ON() won't help, because we use a different
> compiler.

Hm, good point, have not considered that.

> (Or even if it's the same compiler you can have build flags - the size
> of an enum very much depends on various compiler options, particularly
> "--short-enums" for gcc).
> 
> Basically, we should not use "enum"s in types exported to user space.
> The size just isn't sufficiently well defined, and it's a maintenance
> nightmare.
> 
> Use explicitly sized members only, please. No "enum".

Ok.

Would it be acceptable to use enum for the kernel internal representation (our 
e820_table structures never actually comes directly, we construct it ourselves), 
and maintain the very explicitly sized ABI type for the boot protocol only (i.e. 
uapi/asm/bootparam.h)?

( I actually partially implemented that originally, but chickened out after 
  running into header dependency hell - but I think it could be tackled with a bit 
  more effort. )

The vagueness of the C spec about enum size and the resulting enum auto-packing 
efforts of compilers is annoying and a real problem for ABIs, but the reason I'm 
pushing them in this case is that enums also have some advantages within the 
kernel:

 1) they are pretty good self-documenting types

 2) GCC at least has some useful enum warnings for switch() statements, one of 
    which we use in the kernel today:

       -Wswitch

           Warn whenever a "switch" statement has an index of enumerated type and 
           lacks a "case" for one or more of the named codes of that enumeration.  
           (The presence of a "default" label prevents this warning.)  "case" 
           labels outside the enumeration range also provoke warnings when this 
           option is used (even if there is a "default" label).
           This warning is enabled by -Wall.

    There are also more intrusive warnings which found/prevented quite a few bugs
    in various user-space efforts I've been involved with (such as tools/perf):

       -Wswitch-default

           Warn whenever a "switch" statement does not have a "default" case.

       -Wswitch-enum

           Warn whenever a "switch" statement has an index of enumerated type and 
           lacks a "case" for one or more of the named codes of that enumeration.  
           "case" labels outside the enumeration range also provoke warnings when 
           this option is used.  The only difference between -Wswitch and this 
           option is that this option gives a warning about an omitted enumeration 
           code even if there is a "default" label.

So if say one of the e820 functions didn't handle E820_TYPE_PMEM, we'd today 
generate this warning in the kernel build:

  arch/x86/kernel/e820.c: In function ‘e820_print_type’:
  arch/x86/kernel/e820.c:143:2: warning: enumeration value ‘E820_TYPE_PMEM’ not 
  handled in switch [-Wswitch]
    switch (type) {
    ^
  arch/x86/kernel/e820.c:143:2: warning: enumeration value ‘E820_TYPE_PRAM’ not 
  handled in switch [-Wswitch]

And in fact, we could go further than that - the kernel does not enable 
-Wswitch-enum right now (because there are valid patterns that it warns about, 
such as enums with extensive list of values where listing all the values would 
make thecode worse), but when enabled for e820.c it generates this warning:

  arch/x86/kernel/e820.c: In function ‘e820_type_to_string’:
  arch/x86/kernel/e820.c:965:2: warning: enumeration value ‘E820_TYPE_RESERVED’ not handled in switch [-Wswitch-enum]
    switch (entry->type) {
    ^

  arch/x86/kernel/e820.c: In function ‘e820_type_to_iomem_type’:
  arch/x86/kernel/e820.c:979:2: warning: enumeration value ‘E820_TYPE_RESERVED’ not handled in switch [-Wswitch-enum]
    switch (entry->type) {
    ^

  arch/x86/kernel/e820.c: In function ‘e820_type_to_iores_desc’:
  arch/x86/kernel/e820.c:993:2: warning: enumeration value ‘E820_TYPE_RESERVED’ not handled in switch [-Wswitch-enum]
    switch (entry->type) {
    ^

  arch/x86/kernel/e820.c: In function ‘do_mark_busy’:
  arch/x86/kernel/e820.c:1015:2: warning: enumeration value ‘E820_TYPE_RAM’ not handled in switch [-Wswitch-enum]
    switch (type) {
	    ^

All four warnings are interesting IMHO:

The one in e820_type_to_string() should be fixed this way I think:

diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index d2c6468a8d38..20834a81854e 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -970,7 +970,8 @@ static const char *__init e820_type_to_string(struct e820_entry *entry)
 	case E820_TYPE_UNUSABLE:	return "Unusable memory";
 	case E820_TYPE_PRAM:		return "Persistent Memory (legacy)";
 	case E820_TYPE_PMEM:		return "Persistent Memory";
-	default:			return "Reserved";
+	case E820_TYPE_RESERVED:	return "Reserved";
+	default:			return "Unknown E820 type";
 	}
 }

As it's useful to have differentiation in output between a known-reserved E820 
range and a totally unknown type range (firmware special or new feature).

The ones in e820_type_to_iomem_type() and e820_type_to_iores_desc() look OK but 
interesting nevertheless, and should probably be fixed this way:

diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index d2c6468a8d38..20834a81854e 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -984,6 +985,7 @@ static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
 	case E820_TYPE_UNUSABLE:	/* Fall-through: */
 	case E820_TYPE_PRAM:		/* Fall-through: */
 	case E820_TYPE_PMEM:		/* Fall-through: */
+	case E820_TYPE_RESERVED:	/* Fall-through: */
 	default:			return IORESOURCE_MEM;
 	}
 }
@@ -998,6 +1000,7 @@ static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
 	case E820_TYPE_RESERVED_KERN:	/* Fall-through: */
 	case E820_TYPE_RAM:		/* Fall-through: */
 	case E820_TYPE_UNUSABLE:	/* Fall-through: */
+	case E820_TYPE_RESERVED:	/* Fall-through: */
 	default:			return IORES_DESC_NONE;
 	}
 }

I think it's worth a mention that E820_TYPE_RESERVED is handled differently from 
E820_TYPE_RESERVED_KERN in these two functions: the reason is that 
E820_TYPE_RESERVED_KERN is really a special RAM area and probably somewhat of a 
misnomer - E820_TYPE_RAM_RESERVED might be better.

The one in do_mark_busy() is IMHO interesting as well:

diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index d2c6468a8d38..20834a81854e 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -998,7 +1000,7 @@ static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
 	}
 }
 
-static bool __init do_mark_busy(u32 type, struct resource *res)
+static bool __init do_mark_busy(enum e820_type type, struct resource *res)
 {
 	/* this is the legacy bios/dos rom-shadow + mmio region */
 	if (res->start < (1ULL<<20))
@@ -1017,6 +1020,11 @@ static bool __init do_mark_busy(u32 type, struct resource *res)
 	case E820_TYPE_PRAM:
 	case E820_TYPE_PMEM:
 		return false;
+	case E820_TYPE_RESERVED_KERN:
+	case E820_TYPE_RAM:
+	case E820_TYPE_ACPI:
+	case E820_TYPE_NVS:
+	case E820_TYPE_UNUSABLE:
 	default:
 		return true;
 	}

(this patch keeps/documents the status quo.)

As it raises the question of why E820_TYPE_RESERVED is handled differently from 
E820_TYPE_ACPI and E820_TYPE_NVS. Shouldn't E820_TYPE_RESERVED be marked busy as 
well, or do sometimes PCI devices hide there? Even if the code is correct this 
would merit a line of documentation as well, IMHO.

( There's also the -Wswitch-default warning which is not enabled by the kernel 
  build, and which even if enabled does not trigger for e820.o but it's useful as 
  well. )

I.e. I think enum e820_type is a case where very explicit listing of all the 
values in switch() statements actively improves the code, almost unconditionally: 
it resulted in a fix, three improvements in code quality and a couple of questions 
that might result in improved comments.

None of this is possible in such an active way with CPP #defines.

So if you think isolating the enum from the ABI interface would be an acceptable 
solution to the problem we could still use the enum - eat and have the cake and 
such?

Thanks,

	Ingo

  reply	other threads:[~2017-01-29  9:29 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-28 22:11 [PATCH 00/50] x86: Clean up and reorganize the E820 table handling code Ingo Molnar
2017-01-28 22:11 ` [PATCH 01/50] x86/boot/e820: Introduce arch/x86/include/asm/e820/types.h Ingo Molnar
2017-01-29 17:13   ` Sam Ravnborg
2017-01-30  7:58     ` Ingo Molnar
2017-01-31  5:41       ` Sam Ravnborg
2017-01-31 16:35         ` Ingo Molnar
2017-01-31 17:22           ` Sam Ravnborg
2017-01-31 18:00             ` Ingo Molnar
2017-01-31 18:04               ` Joe Perches
2017-01-31 19:17               ` Sam Ravnborg
2017-02-01  8:56                 ` Ingo Molnar
2017-01-28 22:11 ` [PATCH 02/50] x86/boot/e820: Clean up and improve comments in asm/e820/types.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 03/50] x86/boot/e820: Move asm/e820.h to asm/e820/api.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 04/50] x86/boot/e820: Split minimal UAPI types out into uapi/asm/e820/types.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 05/50] x86/boot/e820: Clean up the E820_X_MAX definition Ingo Molnar
2017-01-28 22:11 ` [PATCH 06/50] x86/boot/e820: Remove spurious asm/e820/api.h inclusions Ingo Molnar
2017-01-28 22:11 ` [PATCH 07/50] x86/boot/e820: Remove assembly guard from asm/e820/types.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 08/50] x86/boot/e820: Clean up asm/e820/api.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 09/50] x86/boot/e820: Remove unnecessary __ASSEMBLY__ guard Ingo Molnar
2017-01-28 22:11 ` [PATCH 10/50] x86/boot/e820: Move HIGH_MEMORY define to asm/e820/types.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 11/50] x86/boot/e820: Rename the basic e820 data types to 'struct e820_entry' and 'struct e820_array' Ingo Molnar
2017-01-28 22:11 ` [PATCH 12/50] x86/boot/e820: Remove unnecessary #include <linux/ioport.h> from asm/e820/api.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 13/50] x86/boot/e820: Remove e820_mark_nosave_regions() definition uglies Ingo Molnar
2017-01-28 22:11 ` [PATCH 14/50] x86/boot/e820: Rename 'e820_map' variables to 'e820_array' Ingo Molnar
2017-01-28 22:11 ` [PATCH 15/50] x86/boot/e820: Rename everything to e820_table Ingo Molnar
2017-01-28 22:11 ` [PATCH 16/50] x86/boot/e820: Harmonize the 'struct e820_table' fields Ingo Molnar
2017-01-28 22:11 ` [PATCH 17/50] x86/boot/e820: Rename default_machine_specific_memory_setup() to e820__memory_setup_default() Ingo Molnar
2017-01-28 22:11 ` [PATCH 18/50] x86/boot/e820: Rename e820_table_saved to e820_table_firmware and improve the description Ingo Molnar
2017-01-28 22:11 ` [PATCH 19/50] x86/boot/e820: Basic cleanup of e820.c Ingo Molnar
2017-01-28 22:11 ` [PATCH 20/50] x86/boot/e820: Rename memblock_x86_fill() to e820__memblock_setup() and improve the explanations Ingo Molnar
2017-01-28 22:11 ` [PATCH 21/50] x86/boot/e820: Consolidate 'struct e820_entry *entry' local variable names Ingo Molnar
2017-01-28 22:11 ` [PATCH 22/50] x86/boot/e820: Convert printk(KERN_* ...) to pr_*() Ingo Molnar
2017-01-28 22:59   ` Joe Perches
2017-01-28 22:11 ` [PATCH 23/50] x86/boot/e820: Move the memblock_find_dma_reserve() function and rename it to memblock_set_dma_reserve() Ingo Molnar
2017-01-28 22:11 ` [PATCH 24/50] x86/boot/e820: Rename parse_e820_ext() to e820__memory_setup_extended() Ingo Molnar
2017-01-28 22:11 ` [PATCH 25/50] x86/boot/e820: Move e820_reserve_setup_data() to e820.c Ingo Molnar
2017-01-28 22:11 ` [PATCH 26/50] x86/boot/e820: Clarify the role of finish_e820_parsing() and rename it to e820__finish_early_params() Ingo Molnar
2017-01-28 22:11 ` [PATCH 27/50] x86/boot/e820: Rename early_reserve_e820() to e820__memblock_alloc() and document it Ingo Molnar
2017-01-28 22:11 ` [PATCH 28/50] x86/boot/e820: Rename update_e820() to e820__update_table() Ingo Molnar
2017-01-28 22:11 ` [PATCH 29/50] x86/boot/e820: Rename sanitize_e820_table() " Ingo Molnar
2017-01-28 22:11 ` [PATCH 30/50] x86/boot/e820: Rename e820_any_mapped()/e820_all_mapped() to e820__mapped_any()/e820__mapped_all() Ingo Molnar
2017-01-28 22:11 ` [PATCH 31/50] x86/boot/e820: Rename e820_setup_gap() to e820__setup_pci_gap() Ingo Molnar
2017-01-28 22:11 ` [PATCH 32/50] x86/boot/e820: Create coherent API function names for E820 range operations Ingo Molnar
2017-01-28 22:11 ` [PATCH 33/50] x86/boot/e820: Rename e820_print_map() to e820__print_table() Ingo Molnar
2017-01-28 22:11 ` [PATCH 34/50] x86/boot/e820: Reorder the function prototypes in api.h Ingo Molnar
2017-01-28 22:11 ` [PATCH 35/50] x86/boot/e820: Simplify e820_reserve_resources() Ingo Molnar
2017-01-28 22:11 ` [PATCH 36/50] x86/boot/e820: Introduce 'enum e820_type' Ingo Molnar
2017-01-28 22:11 ` [PATCH 37/50] x86/boot/e820: Use 'enum e820_type' in 'struct e820_entry' Ingo Molnar
2017-01-28 23:07   ` Linus Torvalds
2017-01-29  9:19     ` Ingo Molnar [this message]
2017-01-29 12:38       ` [PATCH] x86/boot/e820: Separate the E820 ABI structures from the in-kernel structures Ingo Molnar
2017-01-29 18:53       ` [PATCH 37/50] x86/boot/e820: Use 'enum e820_type' in 'struct e820_entry' Linus Torvalds
2017-01-28 22:11 ` [PATCH 38/50] x86/boot/e820: Use 'enum e820_type' when handling the e820 region type Ingo Molnar
2017-01-28 22:12 ` [PATCH 39/50] x86/boot/e820: Prefix the E820_* type names with "E820_TYPE_" Ingo Molnar
2017-01-28 22:12 ` [PATCH 40/50] x86/boot/e820: Clean up the E820 table size define names Ingo Molnar
2017-01-28 22:12 ` [PATCH 41/50] x86/boot/e820: Clean up and standardize sizeof() uses Ingo Molnar
2017-01-28 22:12 ` [PATCH 42/50] xen, x86/boot/e820: Simplify Xen's xen_e820_table construct Ingo Molnar
2017-01-28 22:12 ` [PATCH 43/50] x86/boot/e820: Simplify the e820__update_table() interface Ingo Molnar
2017-01-28 22:12 ` [PATCH 44/50] x86/boot/e820: Clean up __e820__update_table() et al Ingo Molnar
2017-01-28 22:12 ` [PATCH 45/50] x86/boot/e820: Document e820__reserve_setup_data() Ingo Molnar
2017-01-28 22:12 ` [PATCH 46/50] x86/boot/e820: Use bool in query APIs Ingo Molnar
2017-01-28 22:12 ` [PATCH 47/50] x86/boot/e820: Rename e820_reserve_resources*() to e820__reserve_resources*() Ingo Molnar
2017-01-28 22:12 ` [PATCH 48/50] x86/boot/e820: Rename e820_mark_nosave_regions() to e820__register_nosave_regions() Ingo Molnar
2017-01-28 22:12 ` [PATCH 49/50] x86/boot/e820: Remove unnecessary #include's Ingo Molnar
2017-01-28 22:12 ` [PATCH 50/50] x86/boot/e820: Rename the remaining E820 APIs to the e820__*() prefix Ingo Molnar

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=20170129091912.GA26237@gmail.com \
    --to=mingo@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=yinghai@kernel.org \
    /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).