linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matt Fleming <matt@codeblueprint.co.uk>
To: Ingo Molnar <mingo@kernel.org>
Cc: Scott Ashcroft <scott.ashcroft@talk21.com>,
	"H . Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Matthew Garrett <mjg59@srcf.ucam.org>,
	Borislav Petkov <bp@alien8.de>,
	Ben Hutchings <ben@decadent.org.uk>,
	Raphael Hertzog <hertzog@debian.org>,
	Roger Shimizu <rogershimizu@gmail.com>,
	Alexis Murzeau <amurzeau@gmail.com>,
	Julia Lawall <julia.lawall@lip6.fr>
Subject: Re: [PATCH] x86/efi: Always map boot service regions into new EFI page tables
Date: Mon, 14 Mar 2016 14:27:34 +0000	[thread overview]
Message-ID: <20160314142734.GE2619@codeblueprint.co.uk> (raw)
In-Reply-To: <20160314120502.GA17388@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5314 bytes --]

On Mon, 14 Mar, at 01:05:02PM, Ingo Molnar wrote:
> 
> There's a number of such occurences that look suspicious:
> 
>  triton:~/tip> git grep 'cpa->pfn.*<<.*PAGE_SHIFT' arch/x86/
>  arch/x86/mm/pageattr.c:         set_pmd(pmd, __pmd(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
>  arch/x86/mm/pageattr.c:         set_pud(pud, __pud(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
>  arch/x86/mm/pageattr.c: unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
>  arch/x86/mm/pageattr.c:         unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +
> 
> are you sure none of the code runs on 32-bit?

The following occurrences do not run on 32-bit,

>  arch/x86/mm/pageattr.c:         set_pmd(pmd, __pmd(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |
>  arch/x86/mm/pageattr.c:         set_pud(pud, __pud(cpa->pfn << PAGE_SHIFT | _PAGE_PSE |

but these might,

>  arch/x86/mm/pageattr.c: unsigned long laddr = (unsigned long)__va(cpa->pfn << PAGE_SHIFT);
>  arch/x86/mm/pageattr.c:         unsigned long temp_cpa_vaddr = (cpa->pfn << PAGE_SHIFT) +

> All this got introduced with:
> 
> | commit edc3b9129cecd0f0857112136f5b8b1bc1d45918
> | Author: Matt Fleming <matt@codeblueprint.co.uk>
> | Date:   Fri Nov 27 21:09:31 2015 +0000
> |
> |    x86/mm/pat: Ensure cpa->pfn only contains page frame numbers
> 
> AFAICS.
 
Kinda. The bugs that do not run on 32-bit were introduced with this
commit. The ones that will run on 32-bit existed before this.

Running the attached semantic patch across arch/x86/mm yields a few
more places where we get the data type wrong for PAE,
 
* file: arch/x86/mm/mmap.c:43 shifting int '( ( - 1UL ) & STACK_RND_MASK )' by PAGE_SHIFT is truncated to 32-bits
* file: arch/x86/mm/pageattr-test.c:57 shifting long 'i' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr-test.c:138 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/gup.c:422 shifting int 'nr' by PAGE_SHIFT is truncated to 32-bits
* file: arch/x86/mm/gup.c:303 shifting unsigned long '( unsigned long ) nr_pages' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/gup.c:370 shifting unsigned long '( unsigned long ) nr_pages' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pat.c:751 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr-test.c:57 shifting long 'i' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:947 shifting unsigned 'num_pages' by PAGE_SHIFT is truncated to 32-bits
* file: arch/x86/mm/pageattr.c:1995 shifting unsigned 'numpages' by PAGE_SHIFT is truncated to 32-bits
* file: arch/x86/mm/pageattr-test.c:138 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:1117 shifting unsigned long 'cpa -> numpages' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:1017 shifting unsigned long 'cpa -> numpages' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:1277 shifting unsigned long 'cpa -> pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:1318 shifting unsigned long 'cpa -> pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:986 shifting unsigned long 'cpa -> pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/pageattr.c:1059 shifting unsigned long 'cpa -> pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:197 shifting unsigned long 'end_pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:100 shifting unsigned long 'min_pfn_mapped' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:641 shifting unsigned long 'pagenr' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:111 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:121 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:111 shifting unsigned long __initdata 'pgt_buf_end' by PAGE_SHIFT is truncated to 32-bits
* file: arch/x86/mm/init.c:196 shifting unsigned long 'start_pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:91 shifting unsigned long '( unsigned long ) num' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init.c:117 shifting unsigned long '( pfn + i )' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init_32.c:293 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init_32.c:301 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init_32.c:344 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init_32.c:361 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE
* file: arch/x86/mm/init_32.c:471 shifting unsigned long 'pfn' by PAGE_SHIFT is truncated to 32-bits for PAE

The coccinelle script isn't perfect, and there are a number of false
positives. For example, the first hit is bogus and looks like a
coccinelle bug, but the results do show some things that need to be
investigated.

Running it over arch/x86/ turns up 66 potential issues.

[-- Attachment #2: page-shift.cocci --]
[-- Type: text/plain, Size: 902 bytes --]

// A variable declared as a 32-bit data type should not be shifted by
// PAGE_SHIFT because the result may not fit in sizeof(T) for large
// numbers of pages, or high page frame numbers, e.g. both of these
// are potentially bogus:
//
//	unsigned long pfn;
//	unsigned int numpages;
//	...
//	unsigned long pa = pfn << PAGE_SHIFT;
//	unsigned long size = numpages << PAGE_SHIFT;
//
// Options: --no-includes --include-headers

virtual context
virtual report
virtual org

@inttrunc@
{int, unsigned int, size_t, signed int, long, unsigned long, signed long} i;
position p;
type tt; tt i1;
@@

 \(i@p\&i1\) << PAGE_SHIFT

@script:python@
p << inttrunc.p;
i << inttrunc.i;
tt << inttrunc.tt;
@@
pae_types = ["unsigned long", "signed long", "long"]

print "* file: %s:%s shifting %s '%s' by PAGE_SHIFT is truncated to 32-bits%s" % \
	(p[0].file, p[0].line, tt, i, \
		" for PAE" if tt in pae_types else "")

  reply	other threads:[~2016-03-14 14:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-11 11:19 [GIT PULL] EFI urgent fix for v4.6 queue Matt Fleming
2016-03-11 11:19 ` [PATCH] x86/efi: Always map boot service regions into new EFI page tables Matt Fleming
2016-03-12 18:57   ` [tip:x86/urgent] x86/efi: Fix boot crash by always mapping " tip-bot for Matt Fleming
2016-03-12 23:02     ` Matt Fleming
2016-03-13 17:56       ` Ingo Molnar
2016-03-13 17:09   ` [PATCH] x86/efi: Always map " Scott Ashcroft
2016-03-13 21:58     ` Matt Fleming
2016-03-13 23:07       ` Matt Fleming
2016-03-13 23:44         ` Scott Ashcroft
2016-03-14  1:09         ` Scott Ashcroft
2016-03-14 10:30         ` Ingo Molnar
2016-03-14 11:35           ` Matt Fleming
2016-03-14 12:05             ` Ingo Molnar
2016-03-14 14:27               ` Matt Fleming [this message]
2016-03-14 16:47                 ` Ingo Molnar
2016-03-15 15:54                   ` Matt Fleming
2016-03-15 16:05                     ` Borislav Petkov
2016-03-15 16:25                     ` Julia Lawall

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=20160314142734.GE2619@codeblueprint.co.uk \
    --to=matt@codeblueprint.co.uk \
    --cc=amurzeau@gmail.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=ben@decadent.org.uk \
    --cc=bp@alien8.de \
    --cc=hertzog@debian.org \
    --cc=hpa@zytor.com \
    --cc=julia.lawall@lip6.fr \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mingo@kernel.org \
    --cc=mjg59@srcf.ucam.org \
    --cc=rogershimizu@gmail.com \
    --cc=scott.ashcroft@talk21.com \
    --cc=tglx@linutronix.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 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).