From nobody Thu May 2 17:03:07 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; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1652810710; cv=none; d=zohomail.com; s=zohoarc; b=j9agZr8H28XMjTR/j0KM2bpqa0U+rRPmh6SKRfk6tJaLIXX6UooBP3mT1XSAsvE3ay4x91xAczc0Ur4WS3atnMo1dh7JsQiZx9uJlDLKsZ7qd8r37xpkR+/OV0qA4SoH3u08koO8NJyyg7IBWxdxNIa0u+qQnN6ypm73DqsLTKM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652810710; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=feA34fPwxNphtEeiKXjdqUR+G6zWJkwwZL1pSvCtX0M=; b=JgfA/t/XsS2EGIfAgvbPqRkC/O3tLVztpum85FkQSUaKVFWUr8D633Bs4xijWIwbeyzscL/yRuuW8ywbXvlzRIms2z2YKMIAZyUuU/rA6ulnd+Hu7CcoFS/d5UpJFeC4A2Yg0lXPHoyF2bnWGfq8A6Iua+Ec5+Q7X1OCaJ4Rdww= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1652810710490379.1758561041761; Tue, 17 May 2022 11:05:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.331382.554893 (Exim 4.92) (envelope-from ) id 1nr1Yh-0004i4-Ck; Tue, 17 May 2022 18:04:35 +0000 Received: by outflank-mailman (output) from mailman id 331382.554893; Tue, 17 May 2022 18:04:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nr1Yh-0004hx-7t; Tue, 17 May 2022 18:04:35 +0000 Received: by outflank-mailman (input) for mailman id 331382; Tue, 17 May 2022 18:04:33 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nr1Yf-0004Vy-M8 for xen-devel@lists.xenproject.org; Tue, 17 May 2022 18:04:33 +0000 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [2a00:1450:4864:20::332]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id cd7906a0-d60b-11ec-bd2c-47488cf2e6aa; Tue, 17 May 2022 20:04:32 +0200 (CEST) Received: by mail-wm1-x332.google.com with SMTP id p5-20020a1c2905000000b003970dd5404dso1694971wmp.0 for ; Tue, 17 May 2022 11:04:32 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id c3-20020adfc6c3000000b0020c5253d8dasm12978625wrh.38.2022.05.17.11.04.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 May 2022 11:04:30 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cd7906a0-d60b-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=feA34fPwxNphtEeiKXjdqUR+G6zWJkwwZL1pSvCtX0M=; b=R9tybJsPD2LXpfmErzHoPXBX5wnEtfuDO/IJAB6QCSqRFyn0tmpyLs+kAw80+IaSbH pjiCvnnNRDCffcM9mu3d1ood+LURdpWIFg78PL2VKrwl9A2+kkifa3UGVHKprWGRSwfX aJuwVKVPiDt2NhFn2R/zUgVFXVPxPu4aYOjQvc7E2AUAJqRj+Rs9KT9EU49neFZttysM 434/nYP+gHYgcCFdpQy9EGyZuPKMjwt5uUgjwQHcsN+Xtuwf7j0WR+K35KIBX1GDPKNH QtuU5Tp2Hk/cePtJTq5KyBfyFVkEn7SMd5tpbIGwcJuVggfFrSFTTvHd6Oa2KpyhqP4k erTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=feA34fPwxNphtEeiKXjdqUR+G6zWJkwwZL1pSvCtX0M=; b=XKHENItP3pTvFq5JTg+BtK44LJVPW/PWdHfXwUrGsVryr/4lNjUnyzLgFjwJftPHOj bLfPDruWmkX+xtJM2BrBFd6yhr1flCJdzzqENQUYXGaBZMVAjaiJe7Us6/ggsxz/VXe5 kmnhNBezpFB8bXh4aSSsElWEBlaGIixecTbE+5GIEl7uUXgJpkj9Iv8V9qvvmMORiBSr lMOC8P0BUtCsAdp0/GyW+jP0HPpHPcx0LUASyLxBvBr+pGJ4onrBPS8Khoz9tVXZALf0 urZgFnVP5qDY3eht5FvIqh6ANjJj9773prWO+Lc98pqqEQLv2rahQYryZSgpGDd/bp62 AMww== X-Gm-Message-State: AOAM533xYGTVWCdQ119T1nIxD9RSJewXHQYyrSTYWEydz65AjwMFcILJ Uw8kkW3BHb/JXl71uyLzkpUPSfgfLkw= X-Google-Smtp-Source: ABdhPJz7TB36wEMhYWeFrz4ToySZ+jaRfOo1ZPZFJbivzuABi6OYRgg8cTziB7YMzVs5jZu3iWnS7Q== X-Received: by 2002:a1c:4c06:0:b0:394:65c4:bd03 with SMTP id z6-20020a1c4c06000000b0039465c4bd03mr22488549wmf.8.1652810671355; Tue, 17 May 2022 11:04:31 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Oleksandr Tyshchenko , Stefano Stabellini , Boris Ostrovsky , Juergen Gross , Julien Grall Subject: [RFC PATCH 1/2] xen/unpopulated-alloc: Introduce helpers for DMA allocations Date: Tue, 17 May 2022 21:04:17 +0300 Message-Id: <1652810658-27810-2-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1652810658-27810-1-git-send-email-olekstysh@gmail.com> References: <1652810658-27810-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1652810711552100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Add ability to allocate unpopulated DMAable (contiguous) pages suitable for grant mapping into. This is going to be used by gnttab code (see gnttab_dma_alloc_pages()). TODO: There is a code duplication in fill_dma_pool(). Also pool oparations likely need to be protected by the lock. Signed-off-by: Oleksandr Tyshchenko --- drivers/xen/unpopulated-alloc.c | 167 ++++++++++++++++++++++++++++++++++++= ++++ include/xen/xen.h | 15 ++++ 2 files changed, 182 insertions(+) diff --git a/drivers/xen/unpopulated-alloc.c b/drivers/xen/unpopulated-allo= c.c index a39f2d3..bca0198 100644 --- a/drivers/xen/unpopulated-alloc.c +++ b/drivers/xen/unpopulated-alloc.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -16,6 +17,8 @@ static DEFINE_MUTEX(list_lock); static struct page *page_list; static unsigned int list_count; =20 +static struct gen_pool *dma_pool; + static struct resource *target_resource; =20 /* @@ -230,6 +233,161 @@ void xen_free_unpopulated_pages(unsigned int nr_pages= , struct page **pages) } EXPORT_SYMBOL(xen_free_unpopulated_pages); =20 +static int fill_dma_pool(unsigned int nr_pages) +{ + struct dev_pagemap *pgmap; + struct resource *res, *tmp_res =3D NULL; + void *vaddr; + unsigned int alloc_pages =3D round_up(nr_pages, PAGES_PER_SECTION); + struct range mhp_range; + int ret; + + res =3D kzalloc(sizeof(*res), GFP_KERNEL); + if (!res) + return -ENOMEM; + + res->name =3D "Xen DMA pool"; + res->flags =3D IORESOURCE_MEM | IORESOURCE_BUSY; + + mhp_range =3D mhp_get_pluggable_range(true); + + ret =3D allocate_resource(target_resource, res, + alloc_pages * PAGE_SIZE, mhp_range.start, mhp_range.end, + PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL); + if (ret < 0) { + pr_err("Cannot allocate new IOMEM resource\n"); + goto err_resource; + } + + /* + * Reserve the region previously allocated from Xen resource to avoid + * re-using it by someone else. + */ + if (target_resource !=3D &iomem_resource) { + tmp_res =3D kzalloc(sizeof(*tmp_res), GFP_KERNEL); + if (!res) { + ret =3D -ENOMEM; + goto err_insert; + } + + tmp_res->name =3D res->name; + tmp_res->start =3D res->start; + tmp_res->end =3D res->end; + tmp_res->flags =3D res->flags; + + ret =3D request_resource(&iomem_resource, tmp_res); + if (ret < 0) { + pr_err("Cannot request resource %pR (%d)\n", tmp_res, ret); + kfree(tmp_res); + goto err_insert; + } + } + + pgmap =3D kzalloc(sizeof(*pgmap), GFP_KERNEL); + if (!pgmap) { + ret =3D -ENOMEM; + goto err_pgmap; + } + + pgmap->type =3D MEMORY_DEVICE_GENERIC; + pgmap->range =3D (struct range) { + .start =3D res->start, + .end =3D res->end, + }; + pgmap->nr_range =3D 1; + pgmap->owner =3D res; + + vaddr =3D memremap_pages(pgmap, NUMA_NO_NODE); + if (IS_ERR(vaddr)) { + pr_err("Cannot remap memory range\n"); + ret =3D PTR_ERR(vaddr); + goto err_memremap; + } + + ret =3D gen_pool_add_virt(dma_pool, (unsigned long)vaddr, res->start, + alloc_pages * PAGE_SIZE, NUMA_NO_NODE); + if (ret) + goto err_pool; + + return 0; + +err_pool: + memunmap_pages(pgmap); +err_memremap: + kfree(pgmap); +err_pgmap: + if (tmp_res) { + release_resource(tmp_res); + kfree(tmp_res); + } +err_insert: + release_resource(res); +err_resource: + kfree(res); + return ret; +} + +/** + * xen_alloc_unpopulated_dma_pages - alloc unpopulated DMAable pages + * @dev: valid struct device pointer + * @nr_pages: Number of pages + * @pages: pages returned + * @return 0 on success, error otherwise + */ +int xen_alloc_unpopulated_dma_pages(struct device *dev, unsigned int nr_pa= ges, + struct page **pages) +{ + void *vaddr; + bool filled =3D false; + unsigned int i; + int ret; + + if (!dma_pool) + return -ENODEV; + + /* XXX Handle devices which support 64-bit DMA address only for now */ + if (dma_get_mask(dev) !=3D DMA_BIT_MASK(64)) + return -EINVAL; + + while (!(vaddr =3D (void *)gen_pool_alloc(dma_pool, nr_pages * PAGE_SIZE)= )) { + if (filled) + return -ENOMEM; + else { + ret =3D fill_dma_pool(nr_pages); + if (ret) + return ret; + + filled =3D true; + } + } + + for (i =3D 0; i < nr_pages; i++) + pages[i] =3D virt_to_page(vaddr + PAGE_SIZE * i); + + return 0; +} +EXPORT_SYMBOL(xen_alloc_unpopulated_dma_pages); + +/** + * xen_free_unpopulated_dma_pages - return unpopulated DMAable pages + * @dev: valid struct device pointer + * @nr_pages: Number of pages + * @pages: pages to return + */ +void xen_free_unpopulated_dma_pages(struct device *dev, unsigned int nr_pa= ges, + struct page **pages) +{ + void *vaddr; + + if (!dma_pool) + return; + + vaddr =3D page_to_virt(pages[0]); + + gen_pool_free(dma_pool, (unsigned long)vaddr, nr_pages * PAGE_SIZE); +} +EXPORT_SYMBOL(xen_free_unpopulated_dma_pages); + static int __init unpopulated_init(void) { int ret; @@ -241,8 +399,17 @@ static int __init unpopulated_init(void) if (ret) { pr_err("xen:unpopulated: Cannot initialize target resource\n"); target_resource =3D NULL; + return ret; } =20 + dma_pool =3D gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE); + if (!dma_pool) { + pr_err("xen:unpopulated: Cannot create DMA pool\n"); + return -ENOMEM; + } + + gen_pool_set_algo(dma_pool, gen_pool_best_fit, NULL); + return ret; } early_initcall(unpopulated_init); diff --git a/include/xen/xen.h b/include/xen/xen.h index a99bab8..a6a7a59 100644 --- a/include/xen/xen.h +++ b/include/xen/xen.h @@ -52,9 +52,15 @@ bool xen_biovec_phys_mergeable(const struct bio_vec *vec= 1, extern u64 xen_saved_max_mem_size; #endif =20 +struct device; + #ifdef CONFIG_XEN_UNPOPULATED_ALLOC int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages= ); void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages= ); +int xen_alloc_unpopulated_dma_pages(struct device *dev, unsigned int nr_pa= ges, + struct page **pages); +void xen_free_unpopulated_dma_pages(struct device *dev, unsigned int nr_pa= ges, + struct page **pages); #include int arch_xen_unpopulated_init(struct resource **res); #else @@ -69,6 +75,15 @@ static inline void xen_free_unpopulated_pages(unsigned i= nt nr_pages, { xen_free_ballooned_pages(nr_pages, pages); } +static inline int xen_alloc_unpopulated_dma_pages(struct device *dev, + unsigned int nr_pages, struct page **pages) +{ + return -1; +} +static inline void xen_free_unpopulated_dma_pages(struct device *dev, + unsigned int nr_pages, struct page **pages) +{ +} #endif =20 #endif /* _XEN_XEN_H */ --=20 2.7.4 From nobody Thu May 2 17:03:07 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; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1652810712; cv=none; d=zohomail.com; s=zohoarc; b=Hc6DnpnEPsLPKg5ZXzm8Ub8Hzld/mj0xasXOOjh/YBiPoCKoxJDz3WY2Tx6GAHqsvzoDKnK4xUsbyxJjKAsbQQpYpV4CguIPWj+qcR8O4mj22D+cUbJ3Vv3ApesMmUsyIQ6lKYEjVmrRubUIs06+wpgcyiippuLJAhBR7S7Oyu8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652810712; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=xUxnvY0wWYvQFwVkkKnZ0hYzB77/1hWqL1wilzE489c=; b=A9napXzEpozOeeCEE4CzrpxxXetfhDAaWG4FzDraokuOxMEQswmRXU8FNd/84Kg3inokGKd2m2rJpNOZOnQMjX6Qu4G7tU05QK4ywvkqS59RqsjP93rtY797keXO3QdI9OwWD1RQ0XAei3ztmjaRg9ISm/MvkeGZAKy/gswrY+Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1652810712811801.7749745138199; Tue, 17 May 2022 11:05:12 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.331383.554897 (Exim 4.92) (envelope-from ) id 1nr1Yh-0004kq-Mq; Tue, 17 May 2022 18:04:35 +0000 Received: by outflank-mailman (output) from mailman id 331383.554897; Tue, 17 May 2022 18:04:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nr1Yh-0004kR-H3; Tue, 17 May 2022 18:04:35 +0000 Received: by outflank-mailman (input) for mailman id 331383; Tue, 17 May 2022 18:04:34 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nr1Yg-0004Vy-FR for xen-devel@lists.xenproject.org; Tue, 17 May 2022 18:04:34 +0000 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [2a00:1450:4864:20::334]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id cdef61e8-d60b-11ec-bd2c-47488cf2e6aa; Tue, 17 May 2022 20:04:33 +0200 (CEST) Received: by mail-wm1-x334.google.com with SMTP id c190-20020a1c35c7000000b0038e37907b5bso1895125wma.0 for ; Tue, 17 May 2022 11:04:33 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id c3-20020adfc6c3000000b0020c5253d8dasm12978625wrh.38.2022.05.17.11.04.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 May 2022 11:04:31 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cdef61e8-d60b-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xUxnvY0wWYvQFwVkkKnZ0hYzB77/1hWqL1wilzE489c=; b=kb8ddVNGcr5cVeafnyHPeYNXQERd/qF31HFkfUdq2R0uWdYsIrV5qJc5rqOLz8AJ2R LeqlzPakVREbISyj+rzFqGHtWdSCexiQvfb3BzQZY4Gb6WIGH1gnWvSpBsaSAoOAgJPw Y2gQVbghkdgpguWD5R6ZUeT1mykXPwp1lsXvUamDbQaFqmfp7ZJs19gwZMPPJzKo2yI5 qsxQdCIXfE44XW+Ui9ZV8j0XprUOYnEv+bHcF2+ibhH3I9NCtjxHxMwKRYDXdrArzigQ AkB6D1SgJLfFBjy+aEuoy/qrUgNvZwQus2tAbvnVgDGakrkC9Xvt3cWzsnYci380bHt5 MjFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xUxnvY0wWYvQFwVkkKnZ0hYzB77/1hWqL1wilzE489c=; b=babiyVOFuR1fChbdaLJ8r5mJB91tqUFB4syB6bKZ6SV1whYpKNMCWpwvPa83PWsLJR 79J4xD+xceunW/JkYDA4JEG08VvZHKpUQBLRleDYFsJgrbmrLjFK7MSHPsNiM+j+yhge 3n6//6kYpqm0nE2fy+x47dRviFrH+Wj4uZJVzWp3wTQwioZVx2HxPpI81dJj2kD1ZroE hXDZLew0T2vxnG6ocebFhfOrCgc+w12PwrdmlVzCYhtT2GbilnCBVrfC/TX6QTLG3zBZ 7ALIO4/84l8BcOfjliALjEjtlrzDby3Z0TbBBJR2x5xgmu+LDlx8OMcDgkC+5JV8eL8k zx9Q== X-Gm-Message-State: AOAM531Ek7ITc86+RLsWC/ss/PMyO1WPRNXjCMhAtW8EQ0o1SDLas1mL 9hHw3ZMn5p+3mbNcWS0i/x1WpnFyju8= X-Google-Smtp-Source: ABdhPJzy8So6LUCbbqsaVyGqZpkGWh1nIJifHTvsH6tXSl1Cxhg/tQhSmkYmww8KULDXmX3Xv/O7fQ== X-Received: by 2002:a05:600c:1989:b0:394:8dc0:b57f with SMTP id t9-20020a05600c198900b003948dc0b57fmr33101152wmq.110.1652810672320; Tue, 17 May 2022 11:04:32 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Oleksandr Tyshchenko , Stefano Stabellini , Boris Ostrovsky , Juergen Gross , Julien Grall Subject: [RFC PATCH 2/2] xen/grant-table: Use unpopulated DMAable pages instead of real RAM ones Date: Tue, 17 May 2022 21:04:18 +0300 Message-Id: <1652810658-27810-3-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1652810658-27810-1-git-send-email-olekstysh@gmail.com> References: <1652810658-27810-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1652810713297100003 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Depends on CONFIG_XEN_UNPOPULATED_ALLOC. If enabled then unpopulated DMAable (contiguous) pages will be allocated for grant mapping into instead of ballooning out real RAM pages. TODO: Fallback to real RAM pages if xen_alloc_unpopulated_dma_pages() fails. Signed-off-by: Oleksandr Tyshchenko --- drivers/xen/grant-table.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c index 8ccccac..2bb4392 100644 --- a/drivers/xen/grant-table.c +++ b/drivers/xen/grant-table.c @@ -864,6 +864,25 @@ EXPORT_SYMBOL_GPL(gnttab_free_pages); */ int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args *args) { +#ifdef CONFIG_XEN_UNPOPULATED_ALLOC + int ret; + + ret =3D xen_alloc_unpopulated_dma_pages(args->dev, args->nr_pages, + args->pages); + if (ret < 0) + return ret; + + ret =3D gnttab_pages_set_private(args->nr_pages, args->pages); + if (ret < 0) { + gnttab_dma_free_pages(args); + return ret; + } + + args->vaddr =3D page_to_virt(args->pages[0]); + args->dev_bus_addr =3D page_to_phys(args->pages[0]); + + return ret; +#else unsigned long pfn, start_pfn; size_t size; int i, ret; @@ -910,6 +929,7 @@ int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args= *args) fail: gnttab_dma_free_pages(args); return ret; +#endif } EXPORT_SYMBOL_GPL(gnttab_dma_alloc_pages); =20 @@ -919,6 +939,12 @@ EXPORT_SYMBOL_GPL(gnttab_dma_alloc_pages); */ int gnttab_dma_free_pages(struct gnttab_dma_alloc_args *args) { +#ifdef CONFIG_XEN_UNPOPULATED_ALLOC + gnttab_pages_clear_private(args->nr_pages, args->pages); + xen_free_unpopulated_dma_pages(args->dev, args->nr_pages, args->pages); + + return 0; +#else size_t size; int i, ret; =20 @@ -946,6 +972,7 @@ int gnttab_dma_free_pages(struct gnttab_dma_alloc_args = *args) dma_free_wc(args->dev, size, args->vaddr, args->dev_bus_addr); return ret; +#endif } EXPORT_SYMBOL_GPL(gnttab_dma_free_pages); #endif --=20 2.7.4