Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8657FC433DF for ; Fri, 7 Aug 2020 17:51:17 +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 4FC7C2086A for ; Fri, 7 Aug 2020 17:51:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="ihfffQgD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4FC7C2086A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.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 1k46Vs-0000l5-1f; Fri, 07 Aug 2020 17:50:40 +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 1k46Vq-0000l0-Nn for xen-devel@lists.xenproject.org; Fri, 07 Aug 2020 17:50:38 +0000 X-Inumbo-ID: d5ad2b37-00c7-46cf-940e-f048e993cece Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id d5ad2b37-00c7-46cf-940e-f048e993cece; Fri, 07 Aug 2020 17:50:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1596822636; h=subject:to:cc:references:from:message-id:date: mime-version:in-reply-to:content-transfer-encoding; bh=wwCZLKDhb5/YHhej40g9Rn2xcYtbyTNw7MdBP6jan68=; b=ihfffQgDex21wl4lLunsxYKBS0kNghNDffsk7YBiBV9L4LC/YleBzxKQ w/Iuu8WPZmSQT2TVubXnBoBD2zrsyNZkvxYLFQvDuuwez+og8P1QJmj+1 ipPyQX+XpTnpxxrFFZ7AJDz6lXBVhZbXdKcOMHBY3jKkEsP7KeyqeX0SG M=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: 95vaTNz1rDw8oL3yK908yQAuC+KF38oWZ9SCRarG7Ppc57YIt+MjzVY6128t47a/nJt1VcyH5I WkMdJKbXOOns/87G16HF2FQnx1d0CK1/7MgRkM68/cqkBbNSIud8nFkh20wKUMl4YR4kzGav/Z Yy0mLqgiq+6N3ppxYFKJiAu84HRVamHvYT1ocNfQr3HJcEVH0xcdexQANKw+8RY03qhpsvcInq sCOdWqe8Io5Mtr9AhFTuzlQ2Vv9wmwu37BEqjrmbtvQDH4I1EYpJE7KCLXVjTmnZTuGyHQBzgP /jU= X-SBRS: 2.7 X-MesageID: 24071940 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.75,446,1589256000"; d="scan'208";a="24071940" Subject: Re: [PATCH v2 4/7] bitmap: move to/from xenctl_bitmap conversion helpers To: Jan Beulich , "xen-devel@lists.xenproject.org" References: <3a8356a9-313c-6de8-f409-977eae1fbfa5@suse.com> <677805f1-c44c-ef65-8190-c4de3bdb327d@suse.com> From: Andrew Cooper Message-ID: Date: Fri, 7 Aug 2020 18:50:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <677805f1-c44c-ef65-8190-c4de3bdb327d@suse.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Content-Language: en-GB X-ClientProxiedBy: AMSPEX02CAS02.citrite.net (10.69.22.113) To AMSPEX02CL02.citrite.net (10.69.22.126) 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: George Dunlap , Ian Jackson , Stefano Stabellini , Julien Grall , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" On 07/08/2020 12:33, Jan Beulich wrote: > --- a/xen/common/bitmap.c > +++ b/xen/common/bitmap.c > @@ -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) ) { At a minimum, please fix this style during the move. However, the more I look at this code, the more concerned I get. There is absolutely no need to allocate(/doubly allocate) memory or double/triple bounce buffer the data.  All that is necessary is some careful handling of the copy length, and trailing zeroing. The cpumask variants should be trivial static inline wrapper.  The fact that they're not suggests an API error. In reality, these are just data-shuffling helpers, and would probably live better in a guest-access.c if we had a suitable one to hand, but I guess bitmap.c will have to do for now. ~Andrew