From patchwork Fri Aug 7 11:33:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 11705583 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CBC4E722 for ; Fri, 7 Aug 2020 11:34:35 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B36AB22C9F for ; Fri, 7 Aug 2020 11:34:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B36AB22C9F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k40d5-0007WC-06; Fri, 07 Aug 2020 11:33:43 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k40d4-0007W5-DZ for xen-devel@lists.xenproject.org; Fri, 07 Aug 2020 11:33:42 +0000 X-Inumbo-ID: 73a8c4dd-169d-4f74-bf5d-9c900e1003b4 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 73a8c4dd-169d-4f74-bf5d-9c900e1003b4; Fri, 07 Aug 2020 11:33:40 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 29564AD60; Fri, 7 Aug 2020 11:33:58 +0000 (UTC) Subject: [PATCH v2 4/7] bitmap: move to/from xenctl_bitmap conversion helpers From: Jan Beulich To: "xen-devel@lists.xenproject.org" References: <3a8356a9-313c-6de8-f409-977eae1fbfa5@suse.com> Message-ID: <677805f1-c44c-ef65-8190-c4de3bdb327d@suse.com> Date: Fri, 7 Aug 2020 13:33:42 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <3a8356a9-313c-6de8-f409-977eae1fbfa5@suse.com> Content-Language: en-US X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , George Dunlap , Andrew Cooper , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" A subsequent change will exclude domctl.c from getting built for a particular configuration, yet the two functions get used from elsewhere. Signed-off-by: Jan Beulich Acked-by: Julien Grall --- v2: Move function decls to xen/bitmap.h. --- a/xen/common/bitmap.c +++ b/xen/common/bitmap.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include /* @@ -384,3 +386,87 @@ void bitmap_byte_to_long(unsigned long * } #endif + +int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, + const unsigned long *bitmap, unsigned int nbits) +{ + unsigned int guest_bytes, copy_bytes, i; + uint8_t zero = 0; + int err = 0; + uint8_t *bytemap = xmalloc_array(uint8_t, (nbits + 7) / 8); + + if ( !bytemap ) + return -ENOMEM; + + guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8; + copy_bytes = min_t(unsigned int, guest_bytes, (nbits + 7) / 8); + + bitmap_long_to_byte(bytemap, bitmap, nbits); + + if ( copy_bytes != 0 ) + if ( copy_to_guest(xenctl_bitmap->bitmap, bytemap, copy_bytes) ) + err = -EFAULT; + + for ( i = copy_bytes; !err && i < guest_bytes; i++ ) + if ( copy_to_guest_offset(xenctl_bitmap->bitmap, i, &zero, 1) ) + err = -EFAULT; + + xfree(bytemap); + + return err; +} + +int xenctl_bitmap_to_bitmap(unsigned long *bitmap, + const struct xenctl_bitmap *xenctl_bitmap, + unsigned int nbits) +{ + unsigned int guest_bytes, copy_bytes; + int err = 0; + uint8_t *bytemap = xzalloc_array(uint8_t, (nbits + 7) / 8); + + if ( !bytemap ) + return -ENOMEM; + + guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8; + copy_bytes = min_t(unsigned int, guest_bytes, (nbits + 7) / 8); + + if ( copy_bytes != 0 ) + { + if ( copy_from_guest(bytemap, xenctl_bitmap->bitmap, copy_bytes) ) + err = -EFAULT; + if ( (xenctl_bitmap->nr_bits & 7) && (guest_bytes == copy_bytes) ) + bytemap[guest_bytes-1] &= ~(0xff << (xenctl_bitmap->nr_bits & 7)); + } + + if ( !err ) + bitmap_byte_to_long(bitmap, bytemap, nbits); + + xfree(bytemap); + + return err; +} + +int cpumask_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_cpumap, + const cpumask_t *cpumask) +{ + return bitmap_to_xenctl_bitmap(xenctl_cpumap, cpumask_bits(cpumask), + nr_cpu_ids); +} + +int xenctl_bitmap_to_cpumask(cpumask_var_t *cpumask, + const struct xenctl_bitmap *xenctl_cpumap) +{ + int err = 0; + + if ( alloc_cpumask_var(cpumask) ) { + err = xenctl_bitmap_to_bitmap(cpumask_bits(*cpumask), xenctl_cpumap, + nr_cpu_ids); + /* In case of error, cleanup is up to us, as the caller won't care! */ + if ( err ) + free_cpumask_var(*cpumask); + } + else + err = -ENOMEM; + + return err; +} --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -34,91 +34,6 @@ static DEFINE_SPINLOCK(domctl_lock); -static int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, - const unsigned long *bitmap, - unsigned int nbits) -{ - unsigned int guest_bytes, copy_bytes, i; - uint8_t zero = 0; - int err = 0; - uint8_t *bytemap = xmalloc_array(uint8_t, (nbits + 7) / 8); - - if ( !bytemap ) - return -ENOMEM; - - guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8; - copy_bytes = min_t(unsigned int, guest_bytes, (nbits + 7) / 8); - - bitmap_long_to_byte(bytemap, bitmap, nbits); - - if ( copy_bytes != 0 ) - if ( copy_to_guest(xenctl_bitmap->bitmap, bytemap, copy_bytes) ) - err = -EFAULT; - - for ( i = copy_bytes; !err && i < guest_bytes; i++ ) - if ( copy_to_guest_offset(xenctl_bitmap->bitmap, i, &zero, 1) ) - err = -EFAULT; - - xfree(bytemap); - - return err; -} - -int xenctl_bitmap_to_bitmap(unsigned long *bitmap, - const struct xenctl_bitmap *xenctl_bitmap, - unsigned int nbits) -{ - unsigned int guest_bytes, copy_bytes; - int err = 0; - uint8_t *bytemap = xzalloc_array(uint8_t, (nbits + 7) / 8); - - if ( !bytemap ) - return -ENOMEM; - - guest_bytes = (xenctl_bitmap->nr_bits + 7) / 8; - copy_bytes = min_t(unsigned int, guest_bytes, (nbits + 7) / 8); - - if ( copy_bytes != 0 ) - { - if ( copy_from_guest(bytemap, xenctl_bitmap->bitmap, copy_bytes) ) - err = -EFAULT; - if ( (xenctl_bitmap->nr_bits & 7) && (guest_bytes == copy_bytes) ) - bytemap[guest_bytes-1] &= ~(0xff << (xenctl_bitmap->nr_bits & 7)); - } - - if ( !err ) - bitmap_byte_to_long(bitmap, bytemap, nbits); - - xfree(bytemap); - - return err; -} - -int cpumask_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_cpumap, - const cpumask_t *cpumask) -{ - return bitmap_to_xenctl_bitmap(xenctl_cpumap, cpumask_bits(cpumask), - nr_cpu_ids); -} - -int xenctl_bitmap_to_cpumask(cpumask_var_t *cpumask, - const struct xenctl_bitmap *xenctl_cpumap) -{ - int err = 0; - - if ( alloc_cpumask_var(cpumask) ) { - err = xenctl_bitmap_to_bitmap(cpumask_bits(*cpumask), xenctl_cpumap, - nr_cpu_ids); - /* In case of error, cleanup is up to us, as the caller won't care! */ - if ( err ) - free_cpumask_var(*cpumask); - } - else - err = -ENOMEM; - - return err; -} - static int nodemask_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_nodemap, const nodemask_t *nodemask) { --- a/xen/include/xen/bitmap.h +++ b/xen/include/xen/bitmap.h @@ -273,6 +273,13 @@ static inline void bitmap_clear(unsigned void bitmap_long_to_byte(uint8_t *bp, const unsigned long *lp, int nbits); void bitmap_byte_to_long(unsigned long *lp, const uint8_t *bp, int nbits); +struct xenctl_bitmap; +int xenctl_bitmap_to_bitmap(unsigned long *bitmap, + const struct xenctl_bitmap *xenctl_bitmap, + unsigned int nbits); +int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, + const unsigned long *bitmap, unsigned int nbits); + #endif /* __ASSEMBLY__ */ #endif /* __XEN_BITMAP_H */ --- a/xen/include/xen/domain.h +++ b/xen/include/xen/domain.h @@ -27,9 +27,6 @@ struct xen_domctl_getdomaininfo; void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info); void arch_get_domain_info(const struct domain *d, struct xen_domctl_getdomaininfo *info); -int xenctl_bitmap_to_bitmap(unsigned long *bitmap, - const struct xenctl_bitmap *xenctl_bitmap, - unsigned int nbits); /* * Arch-specifics.