[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 01/16] x86/shadow: fix and improve sh_page_has_multiple_shadows()


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Wed, 22 Mar 2023 10:29:49 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=S4LWW46rODj4dfG2ddfXP6R321VQnvo+mPhbiTw0dTo=; b=G9G47C2NYomvuPvJDEPCAPLwugsj4ZMw/pizXaondFXntH5F7/S/RKopRulVtctgW3V2gUndlulu81fJBAn6pBQ6nj4qw9vowaabhBNM3MMG5XlCyiyCut9pc0HIRVD1/YC57eT5DxoE3OiPavRavrAXaOofcfS5WY7xscM0caPjPFlvMfdfet2jCcFu2r+HFhHm2l34C9reFv/FFwg6uQL6Ymgoec26nrFkifV/T/XUknrphPB8DmFqdLINa5iIZ5aDLPDiafx7srJEiTmbrNud9xIn1QfHJHgGdK9/i7sufatVFQlQ9HXW0bJ4yxqQP4QbA2TelM7C9m5spRTSjA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Np8B34yAAi+uCh28eL8aUv+zYOLC4DV203h6mIYXSuXbZT3b+5sAs3Cndcf6tpvKH5SKZAtx45+ec5vUxeOKNh/XQMB/EBEo+zVg0brQBsHVgsz0CJxboqYoWVifCzEJ2i+f5Zqseizs5fzcWjVMY4rv3szybc+w0TJFlb6SGU33zYT7yfiah4KE6b8h/ZOGDV10ALNJkCEiL+IBYHjE19WNUpFxWtZiH4htfEYScZB8yQhQDtZtQp6zBC4OQi3vOAPdLB9sMLBvfExt0fTbnOfKnusb7tT09+ACHTcUSbKy+T0elYO7jri7PeD9/ea1e5UGRBpn3dZYQKaPwpxxuA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Tim Deegan <tim@xxxxxxx>
  • Delivery-date: Wed, 22 Mar 2023 09:30:05 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

While no caller currently invokes the function without first making sure
there is at least one shadow [1], we'd better eliminate UB here:
find_first_set_bit() requires input to be non-zero to return a well-
defined result.

Further, using find_first_set_bit() isn't very efficient in the first
place for the intended purpose.

Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>

[1] The function has exactly two uses, and both are from OOS code, which
    is HVM-only. For HVM (but not for PV) sh_mfn_is_a_page_table(),
    guarding the call to sh_unsync(), guarantees at least one shadow.
    Hence even if sh_page_has_multiple_shadows() returned a bogus value
    when invoked for a PV domain, the subsequent is_hvm_vcpu() and
    oos_active checks (the former being redundant with the latter) will
    compensate. (Arguably that oos_active check should come first, for
    both clarity and efficiency reasons.)
---
Considering present uses, ASSERT(shadows) might be an option as well,
instead of making the null check part of the return value expression.

--- a/xen/arch/x86/mm/shadow/private.h
+++ b/xen/arch/x86/mm/shadow/private.h
@@ -332,7 +332,7 @@ static inline int sh_page_has_multiple_s
         return 0;
     shadows = pg->shadow_flags & SHF_page_type_mask;
     /* More than one type bit set in shadow-flags? */
-    return ( (shadows & ~(1UL << find_first_set_bit(shadows))) != 0 );
+    return shadows && (shadows & (shadows - 1));
 }
 
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.