Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp129158iob; Tue, 17 May 2022 21:12:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpP39ZF7bApvFnvJqvrF/OQurzPq2nMazogAEjnxJXwVqUAJBxNQDOOynFiGC5ZWs54WDC X-Received: by 2002:a17:90b:4c91:b0:1dc:57bc:4caa with SMTP id my17-20020a17090b4c9100b001dc57bc4caamr39678489pjb.10.1652847148790; Tue, 17 May 2022 21:12:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652847148; cv=none; d=google.com; s=arc-20160816; b=cUblGxgY+9X8KFvrvi3y88/28I/yZOh2WtehT63cSosTH4nMsJDqYo0Flrtm50WnPO b+2avfRPbTn2IqI1d//Sw0lfS3H30FsLD8mL8I6cXf8XhKMwyvnbPd/HOxwlH048NIHl 5MnHX+iEUmbP4Nx8npSWq1UVa8xhgdpiwzz49GoJrOhhrtzYumv2z9IntfkeH8NvHcUd gzQQ4hzLZds8mLa3qYouPsIFCl0137ePLzt4Z6T+GflfvpqSNZrY0nZmDo+PAhjbHCFk /Qb3NtAY7WaH/qSLLv83JOjneXTuLqb+3GfmAuKVRpJ8uYkZvy88X2D/DyZs1psKM7t+ wtBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=feA34fPwxNphtEeiKXjdqUR+G6zWJkwwZL1pSvCtX0M=; b=A60hIsuvcrCyeLnTLFqDVo9Co8OJKvsrgfvM0pK3bOmN3kJYb08NYv6vsgsB1z4+iQ xR7+q4aMtpi7ZslGSlwXfV7a67Xz3xMwifgdNBD1+TyEFOFtttYlhACUzp+gc8nGXA1N IyE/Edcj0Sx98nu/wJzVKMcwNIqBZvi7rTiDXw4jDoPGAzkjmEuijqQ9NNofK+af0nCp Qu6MGOuWecjVRKIrwS5NbJUgPbR1xrUmlg8PVQOQvdEbMCqCczSfuz3SZLX+5fUlTrTS E0hgQVnrV/HU5f44nJRXtBCgPlUWFs79CNsocaLLscx5kmvAgXX8E5qdPMdp2OipuOly KYdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=R9tybJsP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u3-20020a17090282c300b00161478f77e5si1234020plz.157.2022.05.17.21.12.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 21:12:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=R9tybJsP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E0311EC6B; Tue, 17 May 2022 20:43:02 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351874AbiEQSF6 (ORCPT + 99 others); Tue, 17 May 2022 14:05:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351904AbiEQSF1 (ORCPT ); Tue, 17 May 2022 14:05:27 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BF3150B2C for ; Tue, 17 May 2022 11:04:33 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id i20-20020a05600c355400b0039456976dcaso1437406wmq.1 for ; Tue, 17 May 2022 11:04:32 -0700 (PDT) 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=wqhws+AqdrtERLGauLSVdlN0TgoBe75PEpXOyI92jhmSVg3ttl8nnUyRo4F5JSvfbP s3zyyKKCFRtGbogHU8lceoe6Gu/nQ5MTZgNlr2LCEBoNYY/s4espes9wBjGh4bZVI7HB L0ywZb9ItoYd2L3OuAXOdB/vmS0TbyhE9zttXfTGo+hsPk5kH7Hmqx2qkK+gD+AgAmr9 FU55m7VdjiMmKhQXKc3NTg9XenNwkV97DjMnjFn/Otee/to2AmG8cHS2tAky05cDXBem S7yjjmwrm8Q05s7jMKjKEVjt0Et6KF3AU96hpckU+7Lbq19gB/bwJdzlHDcx5NAiZDG1 +QnA== X-Gm-Message-State: AOAM531TwHi4eCypqtJyoHbzkqWdg1cyaHdIaqRxw+3zmRLOAWZVVZ7a u+qVT4l0zFhMj/PGFMvk2m4= 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) 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) 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-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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-alloc.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; +static struct gen_pool *dma_pool; + static struct resource *target_resource; /* @@ -230,6 +233,161 @@ void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages) } EXPORT_SYMBOL(xen_free_unpopulated_pages); +static int fill_dma_pool(unsigned int nr_pages) +{ + struct dev_pagemap *pgmap; + struct resource *res, *tmp_res = NULL; + void *vaddr; + unsigned int alloc_pages = round_up(nr_pages, PAGES_PER_SECTION); + struct range mhp_range; + int ret; + + res = kzalloc(sizeof(*res), GFP_KERNEL); + if (!res) + return -ENOMEM; + + res->name = "Xen DMA pool"; + res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; + + mhp_range = mhp_get_pluggable_range(true); + + ret = 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 != &iomem_resource) { + tmp_res = kzalloc(sizeof(*tmp_res), GFP_KERNEL); + if (!res) { + ret = -ENOMEM; + goto err_insert; + } + + tmp_res->name = res->name; + tmp_res->start = res->start; + tmp_res->end = res->end; + tmp_res->flags = res->flags; + + ret = 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 = kzalloc(sizeof(*pgmap), GFP_KERNEL); + if (!pgmap) { + ret = -ENOMEM; + goto err_pgmap; + } + + pgmap->type = MEMORY_DEVICE_GENERIC; + pgmap->range = (struct range) { + .start = res->start, + .end = res->end, + }; + pgmap->nr_range = 1; + pgmap->owner = res; + + vaddr = memremap_pages(pgmap, NUMA_NO_NODE); + if (IS_ERR(vaddr)) { + pr_err("Cannot remap memory range\n"); + ret = PTR_ERR(vaddr); + goto err_memremap; + } + + ret = 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_pages, + struct page **pages) +{ + void *vaddr; + bool filled = 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) != DMA_BIT_MASK(64)) + return -EINVAL; + + while (!(vaddr = (void *)gen_pool_alloc(dma_pool, nr_pages * PAGE_SIZE))) { + if (filled) + return -ENOMEM; + else { + ret = fill_dma_pool(nr_pages); + if (ret) + return ret; + + filled = true; + } + } + + for (i = 0; i < nr_pages; i++) + pages[i] = 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_pages, + struct page **pages) +{ + void *vaddr; + + if (!dma_pool) + return; + + vaddr = 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 = NULL; + return ret; } + dma_pool = 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 *vec1, extern u64 xen_saved_max_mem_size; #endif +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_pages, + struct page **pages); +void xen_free_unpopulated_dma_pages(struct device *dev, unsigned int nr_pages, + 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 int 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 #endif /* _XEN_XEN_H */ -- 2.7.4