Re: [PATCH 6/5] x86/fault: Clean up the page fault oops decoder a bit

From: Sean Christopherson
Date: Wed Dec 05 2018 - 10:23:39 EST


On Tue, Dec 04, 2018 at 11:22:25AM -0800, Andy Lutomirski wrote:
> On Tue, Nov 27, 2018 at 7:32 AM Sean Christopherson
> <sean.j.christopherson@xxxxxxxxx> wrote:
> > arch/x86/mm/fault.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
> > index 2ff25ad33233..510e263c256b 100644
> > --- a/arch/x86/mm/fault.c
> > +++ b/arch/x86/mm/fault.c
> > @@ -660,8 +660,10 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad
> > err_str_append(error_code, err_txt, X86_PF_RSVD, "[RSVD]" );
> > err_str_append(error_code, err_txt, X86_PF_INSTR, "[INSTR]");
> > err_str_append(error_code, err_txt, X86_PF_PK, "[PK]" );
> > -
> > - pr_alert("#PF error: %s\n", error_code ? err_txt : "[normal kernel read fault]");
> > + err_str_append(~error_code, err_txt, X86_PF_USER, "[KERNEL]");
> > + err_str_append(~error_code, err_txt, X86_PF_WRITE | X86_PF_INSTR,
> > + "[READ]");
> > + pr_alert("#PF error code: %s\n", err_txt);
> >
>
> Seems generally nice, but I would suggest making the bit-not-set name
> be another parameter to err_str_append().

I didn't recall why I chose to negate error_code until I revisited the
actual code. The "READ" case is a combination of !WRITE && !USER, i.e.
doesn't fit into an existing err_str_append() call. So we'd end up with
an extra err_str_append() call that would also have a null message for
the positive test, which seemed unnecessarily complex and more convoluted
than simply negating error_code.

E.g.:

diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index 2ff25ad33233..48b420621825 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -607,12 +607,17 @@ static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index)
* This helper function transforms the #PF error_code bits into
* "[PROT] [USER]" type of descriptive, almost human-readable error strings:
*/
-static void err_str_append(unsigned long error_code, char *buf, unsigned long mask, const char *txt)
+static void err_str_append(unsigned long error_code, char *buf, unsigned long mask,
+ const char *pos, const char *neg)
{
- if (error_code & mask) {
+ if ((error_code & mask) == mask && pos) {
if (buf[0])
strcat(buf, " ");
- strcat(buf, txt);
+ strcat(buf, pos);
+ } else if (!(error_code & mask) && neg) {
+ if (buf[0])
+ strcat(buf, " ");
+ strcat(buf, neg);
}
}

@@ -654,14 +659,15 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad
* Note: length of these appended strings including the separation space and the
* zero delimiter must fit into err_txt[].
*/
- err_str_append(error_code, err_txt, X86_PF_PROT, "[PROT]" );
- err_str_append(error_code, err_txt, X86_PF_WRITE, "[WRITE]");
- err_str_append(error_code, err_txt, X86_PF_USER, "[USER]" );
- err_str_append(error_code, err_txt, X86_PF_RSVD, "[RSVD]" );
- err_str_append(error_code, err_txt, X86_PF_INSTR, "[INSTR]");
- err_str_append(error_code, err_txt, X86_PF_PK, "[PK]" );
-
- pr_alert("#PF error: %s\n", error_code ? err_txt : "[normal kernel read fault]");
+ err_str_append(error_code, err_txt, X86_PF_PROT, "[PROT]" , NULL);
+ err_str_append(error_code, err_txt, X86_PF_WRITE, "[WRITE]", NULL);
+ err_str_append(error_code, err_txt, X86_PF_USER, "[USER]" , "[SUPERVISOR]");
+ err_str_append(error_code, err_txt, X86_PF_RSVD, "[RSVD]" , NULL);
+ err_str_append(error_code, err_txt, X86_PF_INSTR, "[INSTR]", NULL);
+ err_str_append(error_code, err_txt, X86_PF_PK, "[PK]" , NULL);
+ err_str_append(error_code, err_txt, X86_PF_WRITE | X86_PF_INSTR, NULL,
+ "[READ]");
+ pr_alert("#PF error code: %s\n", err_txt);

if (!(error_code & X86_PF_USER) && user_mode(regs)) {
struct desc_ptr idt, gdt;