Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1337972pxk; Fri, 18 Sep 2020 09:46:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyUHlw42tXlRLXVDImFHL4QDRcwcDl9zPOiR4BoIFY1nYeegRbLfvrO8B4Q9ACGOZ+DfPGQ X-Received: by 2002:a50:bb26:: with SMTP id y35mr41014008ede.234.1600447574360; Fri, 18 Sep 2020 09:46:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600447574; cv=none; d=google.com; s=arc-20160816; b=vNclGKaO4uVvjFuK2ORFu1ldBBvC45uNXCMstZoTCydS7a5Ipc3F7u8/u1oFD4Y6Cl pCdgmnoGdFod2BU48D9Hl6Jcvbd7vNHc9y/fSFGvu8R8N7v2EEA/e26HWn3g6bzwG8YC pB6bqNzNN/4pGaFqWkHZY07/EIVq4H5l1r2k3LzFM2IBdj/XMFSLHIiO2AC2HjsLcMZ0 ZPAOBgSKtOj/HcRT5sWnfCOTNMXtmqTPTAxSwYmqMHVxYnPKpLGxryaweeZZfpyPl9tR wn+wa7n4+GmpczR595Z0zjPNrYyicMrqVW1SOrzNDkfHxz6ax3NWFonsC9ggUerQpgd9 dnIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=69oCjFxEJdGUlaKFJvOKKJe894mXXG8veyEGu1EhEJ0=; b=VkvWdN+ngjgUgMhVMxf6ODzb6OwpMwMYD2mjGtT0iUG3iUekQ8BwX2PU8Ei3JCQdvY PAKdezsP2k7po13C14jJAkqdy3mGcyThBXBlXJ7w2sYTW4/WAHLBFZHokhX5EE4YtOcI SHFoHJIxWaMDQwJC959K6v6aJMwVIjvFf9/aAss2yKGcxXIEuLVw2CbntcbP/sCK6ShE gx6Y/zjay7/Y3SQiwyiRLvDedlyYAi3myZd5GE2cUZiA/ddg7RjgpWFHk1vrA7a0LPaJ /g2Ifd8Kr3sdYDFDucEht/j/n+2Rpnx6eF1mMeLMw87mszyC12wvTRSj1X9Zqz3iZZ7p spaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=VlNEUv9L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g10si2853344edv.314.2020.09.18.09.45.50; Fri, 18 Sep 2020 09:46:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=VlNEUv9L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726427AbgIRQoI (ORCPT + 99 others); Fri, 18 Sep 2020 12:44:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725955AbgIRQoI (ORCPT ); Fri, 18 Sep 2020 12:44:08 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCE4CC0613CE for ; Fri, 18 Sep 2020 09:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=69oCjFxEJdGUlaKFJvOKKJe894mXXG8veyEGu1EhEJ0=; b=VlNEUv9LV+aY4SkVXB3pcBhQK7 +4f1IfC2eWnutDmDJHsF4O75LR/vsSEVed4BUN269IhW+paaZFAbeUUdsKcoQ0gNW7Kfmr+VPkUHA GvwA1P+iQoGls6ETWLNao2T8t7Mr8sE0yPVdtkjO/LK1Nj783nR91OuW9xdY/1vbPRffrxGVOeG9X 2sDnFATRgmtwPufmWpiyG7yWGqlpIhXWxZxQ7v2AcX7oVTThZ31QMmE6iQTzxt/29V6Jf9U4Wv6wu SyZcuuzTerz8XZ6jnd8TFvT9ezWRbKXv4ugYyyk7ROHTMayUrFcx1u8hULLBACrBBpTGGl5PxhvUl bs5MaWXw==; Received: from 089144214092.atnat0023.highway.a1.net ([89.144.214.92] helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kJJUL-0007LP-9v; Fri, 18 Sep 2020 16:43:57 +0000 From: Christoph Hellwig To: Andrew Morton Cc: Peter Zijlstra , Boris Ostrovsky , Juergen Gross , Stefano Stabellini , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Minchan Kim , Nitin Gupta , x86@kernel.org, xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org Subject: [PATCH 2/6] mm: add a vmap_pfn function Date: Fri, 18 Sep 2020 18:37:20 +0200 Message-Id: <20200918163724.2511-3-hch@lst.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200918163724.2511-1-hch@lst.de> References: <20200918163724.2511-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a proper helper to remap PFNs into kernel virtual space so that drivers don't have to abuse alloc_vm_area and open coded PTE manipulation for it. Signed-off-by: Christoph Hellwig --- include/linux/vmalloc.h | 1 + mm/Kconfig | 3 +++ mm/vmalloc.c | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 0221f852a7e1a3..8ecd92a947ee0c 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -121,6 +121,7 @@ extern void vfree_atomic(const void *addr); extern void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot); +void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot); extern void vunmap(const void *addr); extern int remap_vmalloc_range_partial(struct vm_area_struct *vma, diff --git a/mm/Kconfig b/mm/Kconfig index 6c974888f86f97..6fa7ba1199eb1e 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -815,6 +815,9 @@ config DEVICE_PRIVATE memory; i.e., memory that is only accessible from the device (or group of devices). You likely also want to select HMM_MIRROR. +config VMAP_PFN + bool + config FRAME_VECTOR bool diff --git a/mm/vmalloc.c b/mm/vmalloc.c index be4724b916b3e7..59f2afcf26c312 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2407,6 +2407,51 @@ void *vmap(struct page **pages, unsigned int count, } EXPORT_SYMBOL(vmap); +#ifdef CONFIG_VMAP_PFN +struct vmap_pfn_data { + unsigned long *pfns; + pgprot_t prot; + unsigned int idx; +}; + +static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private) +{ + struct vmap_pfn_data *data = private; + + if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx]))) + return -EINVAL; + *pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot)); + return 0; +} + +/** + * vmap_pfn - map an array of PFNs into virtually contiguous space + * @pfns: array of PFNs + * @count: number of pages to map + * @prot: page protection for the mapping + * + * Maps @count PFNs from @pfns into contiguous kernel virtual space and returns + * the start address of the mapping. + */ +void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot) +{ + struct vmap_pfn_data data = { .pfns = pfns, .prot = pgprot_nx(prot) }; + struct vm_struct *area; + + area = get_vm_area_caller(count * PAGE_SIZE, VM_IOREMAP, + __builtin_return_address(0)); + if (!area) + return NULL; + if (apply_to_page_range(&init_mm, (unsigned long)area->addr, + count * PAGE_SIZE, vmap_pfn_apply, &data)) { + free_vm_area(area); + return NULL; + } + return area->addr; +} +EXPORT_SYMBOL_GPL(vmap_pfn); +#endif /* CONFIG_VMAP_PFN */ + static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot, int node) { -- 2.28.0