From patchwork Wed Jul 15 10:40:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 11664751 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 6CA29913 for ; Wed, 15 Jul 2020 10:41:23 +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 52D3C20656 for ; Wed, 15 Jul 2020 10:41:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 52D3C20656 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 1jvepu-0001yt-Ez; Wed, 15 Jul 2020 10:40:26 +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 1jveps-0001yn-OI for xen-devel@lists.xenproject.org; Wed, 15 Jul 2020 10:40:24 +0000 X-Inumbo-ID: 95feb65c-c687-11ea-93b5-12813bfff9fa Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 95feb65c-c687-11ea-93b5-12813bfff9fa; Wed, 15 Jul 2020 10:40:23 +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 97A66B1F7; Wed, 15 Jul 2020 10:40:25 +0000 (UTC) Subject: [PATCH 5/8] bitmap: move to/from xenctl_bitmap conversion helpers From: Jan Beulich To: "xen-devel@lists.xenproject.org" References: <3375cacd-d3b7-9f06-44a7-4b684b6a77d6@suse.com> Message-ID: <5835147f-8428-1d74-7d6e-bbb5522289c7@suse.com> Date: Wed, 15 Jul 2020 12:40:23 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <3375cacd-d3b7-9f06-44a7-4b684b6a77d6@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 --- a/xen/common/bitmap.c +++ b/xen/common/bitmap.c @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include #include /* @@ -384,3 +387,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/domain.h +++ b/xen/include/xen/domain.h @@ -30,6 +30,8 @@ void arch_get_domain_info(const struct d 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); /* * Arch-specifics.