From nobody Fri May 3 03:44:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1589182342; cv=none; d=zohomail.com; s=zohoarc; b=ZWJjUibqvJOGFb7/Ffq85YS6G/a3DMhXZk0Iyq1EntBJTdPv6F9jygKlc0SoBoCfIXnx8huN4wWVIi7ExzfBsRS46RJGO3V1CsZj7o1SEWJ7R1c18Av5d1OjwA5jsVA6BTiApym+qtvntklS+ls5eAfYjsbyIAkNfWATcO1vH+o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589182342; h=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=RyylGemaEplkGEP+pwKTTqxFnacnTinPGDCoNHSzxys=; b=FsqtV+U6NAsrBvULgboMbI49fGD1qg2iqkF9UWOq6KJU82DUAEkqK7tIpTFpWZDKxGmiGJmS6A0K4KcCLcqVQrHgdqdGjXYzbF0rroXXoz6iGaYEl4Zw/OUeDSDDK9w5Zjw/EuAUSxSTXSWj9Cnuh6B0T2KV9ix7+x8HOgW4Trg= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) 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 1589182342720154.30338864629698; Mon, 11 May 2020 00:32:22 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jY2v1-0004M4-1u; Mon, 11 May 2020 07:32:07 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jY2uz-0004Le-KL for xen-devel@lists.xenproject.org; Mon, 11 May 2020 07:32:05 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7d0814a6-9359-11ea-9887-bc764e2007e4; Mon, 11 May 2020 07:31:55 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id D0575AC5B; Mon, 11 May 2020 07:31:56 +0000 (UTC) X-Inumbo-ID: 7d0814a6-9359-11ea-9887-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, clang-built-linux@googlegroups.com Subject: [PATCH 1/2] xen/xenbus: avoid large structs and arrays on the stack Date: Mon, 11 May 2020 09:31:50 +0200 Message-Id: <20200511073151.19043-2-jgross@suse.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200511073151.19043-1-jgross@suse.com> References: <20200511073151.19043-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , Arnd Bergmann Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Content-Type: text/plain; charset="utf-8" xenbus_map_ring_valloc() and its sub-functions are putting quite large structs and arrays on the stack. This is problematic at runtime, but might also result in build failures (e.g. with clang due to the option -Werror,-Wframe-larger-than=3D... used). Fix that by moving most of the data from the stack into a dynamically allocated struct. Performance is no issue here, as xenbus_map_ring_valloc() is used only when adding a new PV device to a backend driver. While at it move some duplicated code from pv/hvm specific mapping functions to the single caller. Reported-by: Arnd Bergmann Signed-off-by: Juergen Gross --- drivers/xen/xenbus/xenbus_client.c | 127 +++++++++++++++-------------- 1 file changed, 66 insertions(+), 61 deletions(-) diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus= _client.c index 040d2a43e8e3..d8e5c5e4fa67 100644 --- a/drivers/xen/xenbus/xenbus_client.c +++ b/drivers/xen/xenbus/xenbus_client.c @@ -69,11 +69,27 @@ struct xenbus_map_node { unsigned int nr_handles; }; =20 +struct map_ring_valloc { + struct xenbus_map_node *node; + + /* Why do we need two arrays? See comment of __xenbus_map_ring */ + union { + unsigned long addrs[XENBUS_MAX_RING_GRANTS]; + pte_t *ptes[XENBUS_MAX_RING_GRANTS]; + }; + phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS]; + + struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS]; + struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS]; + + unsigned int idx; /* HVM only. */ +}; + static DEFINE_SPINLOCK(xenbus_valloc_lock); static LIST_HEAD(xenbus_valloc_pages); =20 struct xenbus_ring_ops { - int (*map)(struct xenbus_device *dev, + int (*map)(struct xenbus_device *dev, struct map_ring_valloc *info, grant_ref_t *gnt_refs, unsigned int nr_grefs, void **vaddr); int (*unmap)(struct xenbus_device *dev, void *vaddr); @@ -449,12 +465,32 @@ int xenbus_map_ring_valloc(struct xenbus_device *dev,= grant_ref_t *gnt_refs, unsigned int nr_grefs, void **vaddr) { int err; + struct map_ring_valloc *info; + + *vaddr =3D NULL; + + if (nr_grefs > XENBUS_MAX_RING_GRANTS) + return -EINVAL; + + info =3D kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + info->node =3D kzalloc(sizeof(*info->node), GFP_KERNEL); + if (!info->node) { + err =3D -ENOMEM; + goto out; + } + + err =3D ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr); =20 - err =3D ring_ops->map(dev, gnt_refs, nr_grefs, vaddr); /* Some hypervisors are buggy and can return 1. */ if (err > 0) err =3D GNTST_general_error; =20 + out: + kfree(info->node); + kfree(info); return err; } EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc); @@ -466,12 +502,10 @@ static int __xenbus_map_ring(struct xenbus_device *de= v, grant_ref_t *gnt_refs, unsigned int nr_grefs, grant_handle_t *handles, - phys_addr_t *addrs, + struct map_ring_valloc *info, unsigned int flags, bool *leaked) { - struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS]; - struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS]; int i, j; int err =3D GNTST_okay; =20 @@ -479,23 +513,22 @@ static int __xenbus_map_ring(struct xenbus_device *de= v, return -EINVAL; =20 for (i =3D 0; i < nr_grefs; i++) { - memset(&map[i], 0, sizeof(map[i])); - gnttab_set_map_op(&map[i], addrs[i], flags, gnt_refs[i], - dev->otherend_id); + gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags, + gnt_refs[i], dev->otherend_id); handles[i] =3D INVALID_GRANT_HANDLE; } =20 - gnttab_batch_map(map, i); + gnttab_batch_map(info->map, i); =20 for (i =3D 0; i < nr_grefs; i++) { - if (map[i].status !=3D GNTST_okay) { - err =3D map[i].status; - xenbus_dev_fatal(dev, map[i].status, + if (info->map[i].status !=3D GNTST_okay) { + err =3D info->map[i].status; + xenbus_dev_fatal(dev, info->map[i].status, "mapping in shared page %d from domain %d", gnt_refs[i], dev->otherend_id); goto fail; } else - handles[i] =3D map[i].handle; + handles[i] =3D info->map[i].handle; } =20 return GNTST_okay; @@ -503,19 +536,19 @@ static int __xenbus_map_ring(struct xenbus_device *de= v, fail: for (i =3D j =3D 0; i < nr_grefs; i++) { if (handles[i] !=3D INVALID_GRANT_HANDLE) { - memset(&unmap[j], 0, sizeof(unmap[j])); - gnttab_set_unmap_op(&unmap[j], (phys_addr_t)addrs[i], + gnttab_set_unmap_op(&info->unmap[j], + info->phys_addrs[i], GNTMAP_host_map, handles[i]); j++; } } =20 - if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, j)) + if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, info->unmap, j)) BUG(); =20 *leaked =3D false; for (i =3D 0; i < j; i++) { - if (unmap[i].status !=3D GNTST_okay) { + if (info->unmap[i].status !=3D GNTST_okay) { *leaked =3D true; break; } @@ -566,21 +599,12 @@ static int xenbus_unmap_ring(struct xenbus_device *de= v, grant_handle_t *handles, return err; } =20 -struct map_ring_valloc_hvm -{ - unsigned int idx; - - /* Why do we need two arrays? See comment of __xenbus_map_ring */ - phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS]; - unsigned long addrs[XENBUS_MAX_RING_GRANTS]; -}; - static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn, unsigned int goffset, unsigned int len, void *data) { - struct map_ring_valloc_hvm *info =3D data; + struct map_ring_valloc *info =3D data; unsigned long vaddr =3D (unsigned long)gfn_to_virt(gfn); =20 info->phys_addrs[info->idx] =3D vaddr; @@ -590,38 +614,27 @@ static void xenbus_map_ring_setup_grant_hvm(unsigned = long gfn, } =20 static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev, + struct map_ring_valloc *info, grant_ref_t *gnt_ref, unsigned int nr_grefs, void **vaddr) { - struct xenbus_map_node *node; + struct xenbus_map_node *node =3D info->node; int err; void *addr; bool leaked =3D false; - struct map_ring_valloc_hvm info =3D { - .idx =3D 0, - }; unsigned int nr_pages =3D XENBUS_PAGES(nr_grefs); =20 - if (nr_grefs > XENBUS_MAX_RING_GRANTS) - return -EINVAL; - - *vaddr =3D NULL; - - node =3D kzalloc(sizeof(*node), GFP_KERNEL); - if (!node) - return -ENOMEM; - err =3D alloc_xenballooned_pages(nr_pages, node->hvm.pages); if (err) goto out_err; =20 gnttab_foreach_grant(node->hvm.pages, nr_grefs, xenbus_map_ring_setup_grant_hvm, - &info); + info); =20 err =3D __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles, - info.phys_addrs, GNTMAP_host_map, &leaked); + info, GNTMAP_host_map, &leaked); node->nr_handles =3D nr_grefs; =20 if (err) @@ -641,11 +654,13 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_d= evice *dev, spin_unlock(&xenbus_valloc_lock); =20 *vaddr =3D addr; + info->node =3D NULL; + return 0; =20 out_xenbus_unmap_ring: if (!leaked) - xenbus_unmap_ring(dev, node->handles, nr_grefs, info.addrs); + xenbus_unmap_ring(dev, node->handles, nr_grefs, info->addrs); else pr_alert("leaking %p size %u page(s)", addr, nr_pages); @@ -653,7 +668,6 @@ static int xenbus_map_ring_valloc_hvm(struct xenbus_dev= ice *dev, if (!leaked) free_xenballooned_pages(nr_pages, node->hvm.pages); out_err: - kfree(node); return err; } =20 @@ -677,39 +691,29 @@ EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree); =20 #ifdef CONFIG_XEN_PV static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev, + struct map_ring_valloc *info, grant_ref_t *gnt_refs, unsigned int nr_grefs, void **vaddr) { - struct xenbus_map_node *node; + struct xenbus_map_node *node =3D info->node; struct vm_struct *area; - pte_t *ptes[XENBUS_MAX_RING_GRANTS]; - phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS]; int err =3D GNTST_okay; int i; bool leaked; =20 - *vaddr =3D NULL; - - if (nr_grefs > XENBUS_MAX_RING_GRANTS) - return -EINVAL; - - node =3D kzalloc(sizeof(*node), GFP_KERNEL); - if (!node) - return -ENOMEM; - - area =3D alloc_vm_area(XEN_PAGE_SIZE * nr_grefs, ptes); + area =3D alloc_vm_area(XEN_PAGE_SIZE * nr_grefs, info->ptes); if (!area) { kfree(node); return -ENOMEM; } =20 for (i =3D 0; i < nr_grefs; i++) - phys_addrs[i] =3D arbitrary_virt_to_machine(ptes[i]).maddr; + info->phys_addrs[i] =3D + arbitrary_virt_to_machine(info->ptes[i]).maddr; =20 err =3D __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles, - phys_addrs, - GNTMAP_host_map | GNTMAP_contains_pte, + info, GNTMAP_host_map | GNTMAP_contains_pte, &leaked); if (err) goto failed; @@ -722,6 +726,8 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_devi= ce *dev, spin_unlock(&xenbus_valloc_lock); =20 *vaddr =3D area->addr; + info->node =3D NULL; + return 0; =20 failed: @@ -730,7 +736,6 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_devi= ce *dev, else pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs); =20 - kfree(node); return err; } =20 --=20 2.26.1 From nobody Fri May 3 03:44:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1589182335; cv=none; d=zohomail.com; s=zohoarc; b=YLVTwJRAIm/0wzI8EOq5RXCPjfDQx4QL1PIzLsJ2ymIfvULMaKz38F2+gbm8EtVoZCFpxqE5LRyOcx5tag7TwNC760RO8sIRSrzdbe00eR04I6RkcT8uOdUhh1jgVFEVF7cx0yf2XTXfSOj027wXgAtfr8N4B1pth8WwYZLdwzQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589182335; h=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=NU18x/FUrjaNsLNI8lQSGiUwrPT9i03qW7hrlnIRaNw=; b=XvZ568OBhVUsgn9q795omg2mYP/fiT1MdDbDqLTMOpNIXI2yMj+VvWl/OzT7bgSPUR4bbA921/eb5OzrPRWhr77fHLFPW4ymfrbN2KZl99yXnRUlI39T2Jh/M+PgERkjMeyH1nvN2ldiHBuVcFxDb/vR7zB/ZTBD0DnPzvWzURI= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) 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 1589182335930562.0524818381372; Mon, 11 May 2020 00:32:15 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jY2uv-0004KL-ON; Mon, 11 May 2020 07:32:01 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jY2uu-0004KE-JG for xen-devel@lists.xenproject.org; Mon, 11 May 2020 07:32:00 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7d0c3f72-9359-11ea-b07b-bc764e2007e4; Mon, 11 May 2020 07:31:55 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 00914AC90; Mon, 11 May 2020 07:31:56 +0000 (UTC) X-Inumbo-ID: 7d0c3f72-9359-11ea-b07b-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 Subject: [PATCH 2/2] xen/xenbus: let xenbus_map_ring_valloc() return errno values only Date: Mon, 11 May 2020 09:31:51 +0200 Message-Id: <20200511073151.19043-3-jgross@suse.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200511073151.19043-1-jgross@suse.com> References: <20200511073151.19043-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: Juergen Gross , Boris Ostrovsky , Stefano Stabellini Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Content-Type: text/plain; charset="utf-8" Today xenbus_map_ring_valloc() can return either a negative errno value (-ENOMEM or -EINVAL) or a grant status value. This is a mess as e.g -ENOMEM and GNTST_eagain have the same numeric value. Fix that by turning all grant mapping errors into -ENOENT. This is no problem as all callers of xenbus_map_ring_valloc() only use the return value to print an error message, and in case of mapping errors the grant status value has already been printed by __xenbus_map_ring() before. Signed-off-by: Juergen Gross Reviewed-by: Boris Ostrovsky --- drivers/xen/xenbus/xenbus_client.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus= _client.c index d8e5c5e4fa67..5e6b256ca916 100644 --- a/drivers/xen/xenbus/xenbus_client.c +++ b/drivers/xen/xenbus/xenbus_client.c @@ -456,8 +456,7 @@ EXPORT_SYMBOL_GPL(xenbus_free_evtchn); * Map @nr_grefs pages of memory into this domain from another * domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs * pages of virtual address space, maps the pages to that address, and - * sets *vaddr to that address. Returns 0 on success, and GNTST_* - * (see xen/include/interface/grant_table.h) or -ENOMEM / -EINVAL on + * sets *vaddr to that address. Returns 0 on success, and -errno on * error. If an error is returned, device will switch to * XenbusStateClosing and the error message will be saved in XenStore. */ @@ -477,18 +476,11 @@ int xenbus_map_ring_valloc(struct xenbus_device *dev,= grant_ref_t *gnt_refs, return -ENOMEM; =20 info->node =3D kzalloc(sizeof(*info->node), GFP_KERNEL); - if (!info->node) { + if (!info->node) err =3D -ENOMEM; - goto out; - } - - err =3D ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr); - - /* Some hypervisors are buggy and can return 1. */ - if (err > 0) - err =3D GNTST_general_error; + else + err =3D ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr); =20 - out: kfree(info->node); kfree(info); return err; @@ -507,7 +499,6 @@ static int __xenbus_map_ring(struct xenbus_device *dev, bool *leaked) { int i, j; - int err =3D GNTST_okay; =20 if (nr_grefs > XENBUS_MAX_RING_GRANTS) return -EINVAL; @@ -522,7 +513,6 @@ static int __xenbus_map_ring(struct xenbus_device *dev, =20 for (i =3D 0; i < nr_grefs; i++) { if (info->map[i].status !=3D GNTST_okay) { - err =3D info->map[i].status; xenbus_dev_fatal(dev, info->map[i].status, "mapping in shared page %d from domain %d", gnt_refs[i], dev->otherend_id); @@ -531,7 +521,7 @@ static int __xenbus_map_ring(struct xenbus_device *dev, handles[i] =3D info->map[i].handle; } =20 - return GNTST_okay; + return 0; =20 fail: for (i =3D j =3D 0; i < nr_grefs; i++) { @@ -554,7 +544,7 @@ static int __xenbus_map_ring(struct xenbus_device *dev, } } =20 - return err; + return -ENOENT; } =20 /** --=20 2.26.1