mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Jeremy Fitzhardinge <jeremy@goop.org>
Cc: Hugh Dickins <hugh@veritas.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Ingo Molnar <mingo@elte.hu>, LKML <linux-kernel@vger.kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Theodore Tso <tytso@mit.edu>,
	Gabriel C <nix.or.die@googlemail.com>,
	Keith Packard <keithp@keithp.com>,
	"Pallipadi, Venkatesh" <venkatesh.pallipadi@intel.com>,
	Eric Anholt <eric@anholt.net>,
	"Siddha, Suresh B" <suresh.b.siddha@intel.com>,
	airlied@linux.ie, "Barnes, Jesse" <jesse.barnes@intel.com>,
	"Rafael J. Wysocki" <rjw@sisk.pl>
Subject: Re: [PATCH 0 of 8] x86: use PTE_MASK consistently
Date: Tue, 20 May 2008 11:34:25 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.1.10.0805201116590.3081@woody.linux-foundation.org> (raw)
In-Reply-To: <4832D65C.2030108@goop.org>



On Tue, 20 May 2008, Jeremy Fitzhardinge wrote:

> Hugh Dickins wrote:
> > I'll leave it to you and Linus whether your way of defining PTE_MASK is
> > satisfactory as is, or needs to be improved to his way.  I've not tried
> > his suggestion of doing the _PAGE_BIT definitions: certainly it's
> > seemed odd to me that they were defined with L, but I've little
> > appetite to mess around with them now myself.
> 
> Yes, well, that was me too, with the intention of making ~_PAGE_FOO generate
> an appropriately sized mask.  I guess it would be better to use pteval_t these
> days.

The thing is, making _PAGE_FOO be a signed long in no way guarantees any 
appropriately sized masks.

Why? Because 'long' mixes with 'unsigned long' and in the C type system, 
unsigned is stronger than signed.

As a result, if you depend on sign-extension of things like a binary 'not' 
operation, you're always going to have cases where it simply doesn't work, 
just because you happen to mix it up with other things that may have type 
'unsigned long' (or, on 32-bit, 'unsigned int' which has the same size: 
the 'unsigned' is so strong that it will convert to 'long' because it has 
the same size.

Try this one in a 32-bit environment:

	/*
	 * 'A' is the low bits mask - and explicitly signed so
	 * that you can do '~A' to get the high bits of a PTE
	 */
	#define A 0xfffl

	/*
	 * Some random value that just happens to be unsigned for other 
	 * reasons - perhaps it's a sizeof expression or whatever.
	 */
	#define B 5U

	int main(int argc, char **argv)
	{
	        unsigned long long pte;

	        pte = B | ~A;
	        printf("%llx\n", pte);

	        pte = ~A;
	        pte |= B;
	        printf("%llx\n", pte);

	        pte = B;
	        pte |= ~A;
	        printf("%llx\n", pte);
	}

and see what it generates. It does the EXACT SAME thing in all three 
cases: the expression is "B | ~A" in various guises, but they are not the 
same values because the order of the type expansion matters. Note how in 
one case the sign bits of the binary not did *not* percolate up to the 
upper 32 bits.

So making something signed and just assuming that it means that when it is 
expanded to a bigger type the bits will always be set is simply not true. 
It's *often* true, but it's untrue in many trivial cases. 

If you want the upper bits to behave reliably in a bigger type, then you 
have to actually make the values *be* of that type.

Or do people really want to live with a kernel where 

	pte = B | ~A;

is different from

	pte = B;
	pte |= ~A;

and making the "obvious" simplification will just generate a totaly 
different result?

I don't think we want to continue to rely on that kind of subtle C type 
tricks, especially since we know they haven't worked (ie we had this 
*exact* issue with mixing in PAGE_MASK that happened to be unsigned, and 
changed all the arithmetic for the signed longs that depended on sign 
extensions to bigger types).

Obviously, we could just choose to make *everything* be of a signed type, 
but we know that doesn't work very well, and it interacts badly with 
things like right-shifting (which we do quite a lot on the things 
involved: we extract fields like the PFN with a right shift and a mask).

			Linus

  reply	other threads:[~2008-05-20 18:36 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-20  7:26 Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 1 of 8] x86: define PTE_MASK in a universally useful way Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 2 of 8] x86: fix warning on 32-bit non-PAE Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 3 of 8] x86: rearrange __(VIRTUAL|PHYSICAL)_MASK Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 4 of 8] x86: use PTE_MASK in 32-bit PAE Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 5 of 8] x86: use PTE_MASK in pgtable_32.h Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 6 of 8] x86: clarify use of _PAGE_CHG_MASK Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 7 of 8] x86: use PTE_MASK rather than ad-hoc mask Jeremy Fitzhardinge
2008-05-20  7:26 ` [PATCH 8 of 8] xen: use PTE_MASK in pte_mfn() Jeremy Fitzhardinge
2008-05-20 12:57 ` [PATCH 0 of 8] x86: use PTE_MASK consistently Hugh Dickins
2008-05-20 12:59   ` [PATCH] x86: strengthen 64-bit p?d_bad() Hugh Dickins
2008-05-20 13:47   ` [PATCH 0 of 8] x86: use PTE_MASK consistently Jeremy Fitzhardinge
2008-05-20 18:34     ` Linus Torvalds [this message]
2008-05-20 19:51 ` Ingo Molnar
2008-05-20 19:55   ` Ingo Molnar
2008-05-20 20:02   ` Linus Torvalds
  -- strict thread matches above, loose matches on Subject: below --
2008-05-09 11:02 Jeremy Fitzhardinge
2008-05-13 10:11 ` 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=alpine.LFD.1.10.0805201116590.3081@woody.linux-foundation.org \
    --to=torvalds@linux-foundation.org \
    --cc=airlied@linux.ie \
    --cc=akpm@linux-foundation.org \
    --cc=eric@anholt.net \
    --cc=hugh@veritas.com \
    --cc=jeremy@goop.org \
    --cc=jesse.barnes@intel.com \
    --cc=keithp@keithp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=nix.or.die@googlemail.com \
    --cc=rjw@sisk.pl \
    --cc=suresh.b.siddha@intel.com \
    --cc=tglx@linutronix.de \
    --cc=tytso@mit.edu \
    --cc=venkatesh.pallipadi@intel.com \
    /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

all inboxes | Powered by JetHome®