From nobody Fri Apr 26 04:56:42 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1573125441; cv=none; d=zoho.com; s=zohoarc; b=HNrTHBuxm5Bdogpcfmph4UA3OFPxadb3KW0on1i16moJmMvJl+xGLQGItio0fSrWI1gPI2hN2KrgkjGybJCqy5pWXN4J0sy6dOvz/Ai4JRRycrB+21dTKCh6uU2LqZw2y0LMpqD729IAg+JF1QeusK10N45vimwX7goERfj20jc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573125441; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lgJuRpjfnTvC03uEiQH1D42uDHrE+5EVNoiqKoKe8Bw=; b=CDhQKnVX2s7Ml6vgcy4WLTUgc5nXgbvdBHkJA1gMQUSq4lOPM9U/uLccIEnNyNB9im6OXbkCA1eBOeXaw8Z4GzM2NZ/QnFfnSih1pLehayj6+MD7a6WNqoie6niqL5i061N4woQOWb9qM7ODldPZcbRYk2npiWm75ZBdYdm+C9o= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 15731254415668.39866189622444; Thu, 7 Nov 2019 03:17:21 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iSflZ-0001WG-6k; Thu, 07 Nov 2019 11:15:53 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iSflY-0001Vq-0r for xen-devel@lists.xenproject.org; Thu, 07 Nov 2019 11:15:52 +0000 Received: from mx1.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f3cc88ac-014f-11ea-b678-bc764e2007e4; Thu, 07 Nov 2019 11:15:49 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id A1728B3C0; Thu, 7 Nov 2019 11:15:48 +0000 (UTC) X-Inumbo-ID: f3cc88ac-014f-11ea-b678-bc764e2007e4 X-Virus-Scanned: by amavisd-new at test-mx.suse.de From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Date: Thu, 7 Nov 2019 12:15:45 +0100 Message-Id: <20191107111546.26579-2-jgross@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191107111546.26579-1-jgross@suse.com> References: <20191107111546.26579-1-jgross@suse.com> Subject: [Xen-devel] [PATCH v2 1/2] xen/gntdev: replace global limit of mapped pages by limit per call X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Today there is a global limit of pages mapped via /dev/xen/gntdev set to 1 million pages per default. There is no reason why that limit is existing, as total number of grant mappings is limited by the hypervisor anyway and preferring kernel mappings over userspace ones doesn't make sense. It should be noted that the gntdev device is usable by root only. Additionally checking of that limit is fragile, as the number of pages to map via one call is specified in a 32-bit unsigned variable which isn't tested to stay within reasonable limits (the only test is the value to be <=3D zero, which basically excludes only calls without any mapping requested). So trying to map e.g. 0xffff0000 pages while already nearly 1000000 pages are mapped will effectively lower the global number of mapped pages such that a parallel call mapping a reasonable amount of pages can succeed in spite of the global limit being violated. So drop the global limit and introduce per call limit instead. This per call limit (default: 65536 grant mappings) protects against allocating insane large arrays in the kernel for doing a hypercall which will fail anyway in case a user is e.g. trying to map billions of pages. Signed-off-by: Juergen Gross Reviewed-by: Oleksandr Andrushchenko --- drivers/xen/gntdev-common.h | 2 +- drivers/xen/gntdev-dmabuf.c | 11 +++-------- drivers/xen/gntdev.c | 24 +++++++----------------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/drivers/xen/gntdev-common.h b/drivers/xen/gntdev-common.h index 2f8b949c3eeb..0e5d4660e7b8 100644 --- a/drivers/xen/gntdev-common.h +++ b/drivers/xen/gntdev-common.h @@ -87,7 +87,7 @@ void gntdev_add_map(struct gntdev_priv *priv, struct gntd= ev_grant_map *add); =20 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map= ); =20 -bool gntdev_account_mapped_pages(int count); +bool gntdev_test_page_count(unsigned int count); =20 int gntdev_map_grant_pages(struct gntdev_grant_map *map); =20 diff --git a/drivers/xen/gntdev-dmabuf.c b/drivers/xen/gntdev-dmabuf.c index 2c4f324f8626..63f0857bf62d 100644 --- a/drivers/xen/gntdev-dmabuf.c +++ b/drivers/xen/gntdev-dmabuf.c @@ -446,7 +446,7 @@ dmabuf_exp_alloc_backing_storage(struct gntdev_priv *pr= iv, int dmabuf_flags, { struct gntdev_grant_map *map; =20 - if (unlikely(count <=3D 0)) + if (unlikely(gntdev_test_page_count(count))) return ERR_PTR(-EINVAL); =20 if ((dmabuf_flags & GNTDEV_DMA_FLAG_WC) && @@ -459,11 +459,6 @@ dmabuf_exp_alloc_backing_storage(struct gntdev_priv *p= riv, int dmabuf_flags, if (!map) return ERR_PTR(-ENOMEM); =20 - if (unlikely(gntdev_account_mapped_pages(count))) { - pr_debug("can't map %d pages: over limit\n", count); - gntdev_put_map(NULL, map); - return ERR_PTR(-ENOMEM); - } return map; } =20 @@ -771,7 +766,7 @@ long gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_pr= iv *priv, int use_ptemod, if (copy_from_user(&op, u, sizeof(op)) !=3D 0) return -EFAULT; =20 - if (unlikely(op.count <=3D 0)) + if (unlikely(gntdev_test_page_count(op.count))) return -EINVAL; =20 refs =3D kcalloc(op.count, sizeof(*refs), GFP_KERNEL); @@ -818,7 +813,7 @@ long gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv= *priv, if (copy_from_user(&op, u, sizeof(op)) !=3D 0) return -EFAULT; =20 - if (unlikely(op.count <=3D 0)) + if (unlikely(gntdev_test_page_count(op.count))) return -EINVAL; =20 gntdev_dmabuf =3D dmabuf_imp_to_refs(priv->dmabuf_priv, diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index 81401f386c9c..0578d369e537 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -55,12 +55,10 @@ MODULE_AUTHOR("Derek G. Murray , " "Gerd Hoffmann "); MODULE_DESCRIPTION("User-space granted page access driver"); =20 -static int limit =3D 1024*1024; -module_param(limit, int, 0644); -MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by " - "the gntdev device"); - -static atomic_t pages_mapped =3D ATOMIC_INIT(0); +static unsigned int limit =3D 64*1024; +module_param(limit, uint, 0644); +MODULE_PARM_DESC(limit, + "Maximum number of grants that may be mapped by one mapping request"); =20 static int use_ptemod; #define populate_freeable_maps use_ptemod @@ -72,9 +70,9 @@ static struct miscdevice gntdev_miscdev; =20 /* ------------------------------------------------------------------ */ =20 -bool gntdev_account_mapped_pages(int count) +bool gntdev_test_page_count(unsigned int count) { - return atomic_add_return(count, &pages_mapped) > limit; + return !count || count > limit; } =20 static void gntdev_print_maps(struct gntdev_priv *priv, @@ -242,8 +240,6 @@ void gntdev_put_map(struct gntdev_priv *priv, struct gn= tdev_grant_map *map) if (!refcount_dec_and_test(&map->users)) return; =20 - atomic_sub(map->count, &pages_mapped); - if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { notify_remote_via_evtchn(map->notify.event); evtchn_put(map->notify.event); @@ -668,7 +664,7 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_pr= iv *priv, if (copy_from_user(&op, u, sizeof(op)) !=3D 0) return -EFAULT; pr_debug("priv %p, add %d\n", priv, op.count); - if (unlikely(op.count <=3D 0)) + if (unlikely(gntdev_test_page_count(op.count))) return -EINVAL; =20 err =3D -ENOMEM; @@ -676,12 +672,6 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_p= riv *priv, if (!map) return err; =20 - if (unlikely(gntdev_account_mapped_pages(op.count))) { - pr_debug("can't map: over limit\n"); - gntdev_put_map(NULL, map); - return err; - } - if (copy_from_user(map->grants, &u->refs, sizeof(map->grants[0]) * op.count) !=3D 0) { gntdev_put_map(NULL, map); --=20 2.16.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel From nobody Fri Apr 26 04:56:42 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1573125431; cv=none; d=zoho.com; s=zohoarc; b=kageztumhLaMtJfa39P71BNJTm1E/OyWB0GkXOJJk+IOeBsE4ZToa0hDW7T/aozQAvZq66DYQ0zv0uiNfK1f9phjJzoaVg03C08ZDEqJyEOWvtS4Lyk0R802RbBIxU8EETKQ5+cB697xpmuGfsCRMhDs+p+9Oxu9+C+nnLkz1Gg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573125431; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zajfzO9IPbHfPFHc5E5N4jnBf58oXFBz5/magDhBEeI=; b=DFytZxQsncQA+BHLATOr+JroaJQWIkvYyqtR3L1EmDl8lmBgHsCZSZqkchIdBy54Qt1ytyqdsapvqqreOL+TnVbmW4ZZL2BWYJF9Zg4YbsEQ0Sl1NYGc010j6aCVD3BDyVktGg7SfkfjCPODqSbssgeWjiWIPtGxakEOh7oAkRk= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1573125431773885.0985437616519; Thu, 7 Nov 2019 03:17:11 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iSflX-0001Vp-QU; Thu, 07 Nov 2019 11:15:51 +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.89) (envelope-from ) id 1iSflW-0001Vk-NC for xen-devel@lists.xenproject.org; Thu, 07 Nov 2019 11:15:50 +0000 Received: from mx1.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f3ccbdea-014f-11ea-a1c1-12813bfff9fa; Thu, 07 Nov 2019 11:15:49 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id BEBBDB3EB; Thu, 7 Nov 2019 11:15:48 +0000 (UTC) X-Inumbo-ID: f3ccbdea-014f-11ea-a1c1-12813bfff9fa X-Virus-Scanned: by amavisd-new at test-mx.suse.de From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Date: Thu, 7 Nov 2019 12:15:46 +0100 Message-Id: <20191107111546.26579-3-jgross@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20191107111546.26579-1-jgross@suse.com> References: <20191107111546.26579-1-jgross@suse.com> Subject: [Xen-devel] [PATCH v2 2/2] xen/gntdev: switch from kcalloc() to kvcalloc() X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" With sufficient many pages to map gntdev can reach order 9 allocation sizes. As there is no need to have physically contiguous buffers switch to kvcalloc() in order to avoid failing allocations. Signed-off-by: Juergen Gross Reviewed-by: Oleksandr Andrushchenko --- drivers/xen/gntdev.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index 0578d369e537..f1bc0d269e12 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -113,14 +113,14 @@ static void gntdev_free_map(struct gntdev_grant_map *= map) gnttab_free_pages(map->count, map->pages); =20 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC - kfree(map->frames); + kvfree(map->frames); #endif - kfree(map->pages); - kfree(map->grants); - kfree(map->map_ops); - kfree(map->unmap_ops); - kfree(map->kmap_ops); - kfree(map->kunmap_ops); + kvfree(map->pages); + kvfree(map->grants); + kvfree(map->map_ops); + kvfree(map->unmap_ops); + kvfree(map->kmap_ops); + kvfree(map->kunmap_ops); kfree(map); } =20 @@ -134,12 +134,13 @@ struct gntdev_grant_map *gntdev_alloc_map(struct gntd= ev_priv *priv, int count, if (NULL =3D=3D add) return NULL; =20 - add->grants =3D kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL); - add->map_ops =3D kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL); - add->unmap_ops =3D kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL); - add->kmap_ops =3D kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL); - add->kunmap_ops =3D kcalloc(count, sizeof(add->kunmap_ops[0]), GFP_KERNEL= ); - add->pages =3D kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); + add->grants =3D kvcalloc(count, sizeof(add->grants[0]), GFP_KERNEL); + add->map_ops =3D kvcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL); + add->unmap_ops =3D kvcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL); + add->kmap_ops =3D kvcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL); + add->kunmap_ops =3D kvcalloc(count, + sizeof(add->kunmap_ops[0]), GFP_KERNEL); + add->pages =3D kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); if (NULL =3D=3D add->grants || NULL =3D=3D add->map_ops || NULL =3D=3D add->unmap_ops || @@ -158,8 +159,8 @@ struct gntdev_grant_map *gntdev_alloc_map(struct gntdev= _priv *priv, int count, if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) { struct gnttab_dma_alloc_args args; =20 - add->frames =3D kcalloc(count, sizeof(add->frames[0]), - GFP_KERNEL); + add->frames =3D kvcalloc(count, sizeof(add->frames[0]), + GFP_KERNEL); if (!add->frames) goto err; =20 --=20 2.16.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel