diff mbox

kvm: x86: mmu: Add cast to negated bitmasks in update_permission_bitmask()

Message ID CAKwvOdnhPCDRFMhYiAr2kSCDWrL_gaY8c6XHJ96PupcN-wcDpQ@mail.gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Nick Desaulniers June 19, 2018, 5:08 p.m. UTC
On Tue, Jun 19, 2018 at 8:19 AM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> On 15/06/2018 20:45, Nick Desaulniers wrote:
> >>
> >>> In any case I think it it preferable to fix the code over disabling
> >>> the warning, unless the warning is bogus or there are just too many
> >>> occurrences.
> >> Maybe.
> > Spurious warning today, actual bug tomorrow?  I prefer to not to
> > disable warnings wholesale.  They don't need to find actual bugs to be
> > useful.  Flagging code that can be further specified does not hurt.
> > Part of the effort to compile the kernel with different compilers is
> > to add warning coverage, not remove it.  That said, there may be
> > warnings that are never useful (or at least due to some invariant that
> > only affects the kernel).  I cant think of any off the top of my head,
> > but I'm also not sure this is one.
>
> This one really makes the code uglier though, so I'm not really inclined
> to applying the patch.

Note that of the three variables (w, u, x), only u is used later on.
What about declaring them as negated with the cast, that way there's
no cast in a ternary?

Ex:

        bool cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP) != 0;
@@ -4278,11 +4279,11 @@ static void update_permission_bitmask(struct
kvm_vcpu *vcpu,
                 */

                /* Faults from writes to non-writable pages */
-               u8 wf = (pfec & PFERR_WRITE_MASK) ? ~w : 0;
+               u8 wf = (pfec & PFERR_WRITE_MASK) ? w_not : 0;
                /* Faults from user mode accesses to supervisor pages */
-               u8 uf = (pfec & PFERR_USER_MASK) ? ~u : 0;
+               u8 uf = (pfec & PFERR_USER_MASK) ? u_not : 0;
                /* Faults from fetches of non-executable pages*/
-               u8 ff = (pfec & PFERR_FETCH_MASK) ? ~x : 0;
+               u8 ff = (pfec & PFERR_FETCH_MASK) ? x_not : 0;
                /* Faults from kernel mode fetches of user pages */
                u8 smepf = 0;
                /* Faults from kernel mode accesses of user pages */


Maybe you have a better naming scheme than *_not ? What do you think?

Comments

Paolo Bonzini June 19, 2018, 5:13 p.m. UTC | #1
On 19/06/2018 19:08, Nick Desaulniers wrote:
>> This one really makes the code uglier though, so I'm not really inclined
>> to applying the patch.
> Note that of the three variables (w, u, x), only u is used later on.
> What about declaring them as negated with the cast, that way there's
> no cast in a ternary?

I still find it inferior, but I guess it's at least acceptable.  I
prefer not_{x,w,u} though. :)

Paolo
Matthias Kaehlcke June 19, 2018, 6:38 p.m. UTC | #2
On Tue, Jun 19, 2018 at 07:13:41PM +0200, Paolo Bonzini wrote:
> On 19/06/2018 19:08, Nick Desaulniers wrote:
> >> This one really makes the code uglier though, so I'm not really inclined
> >> to applying the patch.
> > Note that of the three variables (w, u, x), only u is used later on.
> > What about declaring them as negated with the cast, that way there's
> > no cast in a ternary?
> 
> I still find it inferior, but I guess it's at least acceptable.  I
> prefer not_{x,w,u} though. :)

Thanks Nick and Paolo for the suggestions, I'll sent an updated
version soon.
diff mbox

Patch

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index d594690d8b95..53673ad4b295 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -4261,8 +4261,9 @@  static void update_permission_bitmask(struct
kvm_vcpu *vcpu,
 {
        unsigned byte;

-       const u8 x = BYTE_MASK(ACC_EXEC_MASK);
-       const u8 w = BYTE_MASK(ACC_WRITE_MASK);
+       const u8 x_not = (u8)~BYTE_MASK(ACC_EXEC_MASK);
+       const u8 w_not = (u8)~BYTE_MASK(ACC_WRITE_MASK);
+       const u8 u_not = (u8)~BYTE_MASK(ACC_USER_MASK);
        const u8 u = BYTE_MASK(ACC_USER_MASK);