From patchwork Fri Mar 26 05:55:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12165795 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D241EC433E0 for ; Fri, 26 Mar 2021 05:55:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6AD2961A36 for ; Fri, 26 Mar 2021 05:55:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6AD2961A36 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 171BE6B0072; Fri, 26 Mar 2021 01:55:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEC796B0074; Fri, 26 Mar 2021 01:55:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D400B6B0073; Fri, 26 Mar 2021 01:55:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0226.hostedemail.com [216.40.44.226]) by kanga.kvack.org (Postfix) with ESMTP id A94846B0070 for ; Fri, 26 Mar 2021 01:55:30 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 56D95840F for ; Fri, 26 Mar 2021 05:55:30 +0000 (UTC) X-FDA: 77960963220.36.BFA6D8D Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf26.hostedemail.com (Postfix) with ESMTP id 0B6DE40002C1 for ; Fri, 26 Mar 2021 05:55:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; 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=s84u+TwJThYObmjWKC4zV8i6uETAGMXJeQ3DQ6pqP3c=; b=gWD70eB5I7+vPuNtuRUeTBIc4Z xQAvTwojBG1Le3vsYAD2KwIKwVirGTuw8nqj/xo1S8d1KQbo890xiRYNke4nX67+mDSBm3+CkaMda cVsEdM8BcpyD/m/CGmhwDIAHg6Z24Zv/Mt/Tzv4i5dJNhwVxp/BjLuNFYoA0BSN6139GC3+D/NkUY 9Vlb4ECf4n9Ke5THiSDISzqiPeYKY0hV+jfsJxSeElbpc7WgczN8tJNq76azvQ063XqL5r8YCVik/ SSAoNb5rI7xPWmQdB7mXRHUS7nfJ0Lm4Ik88IWeRXoAbx5nRyNkuHdXcM8VdsDqmXNUBd86vHAWei 6mHz1ctA==; Received: from [2001:4bb8:191:f692:97ff:1e47:aee2:c7e5] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lPfRA-005AHn-Ei; Fri, 26 Mar 2021 05:55:12 +0000 From: Christoph Hellwig To: Andrew Morton , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi Cc: Chris Wilson , Daniel Vetter , Peter Zijlstra , intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org Subject: [PATCH 2/4] mm: add a io_mapping_map_user helper Date: Fri, 26 Mar 2021 06:55:03 +0100 Message-Id: <20210326055505.1424432-3-hch@lst.de> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210326055505.1424432-1-hch@lst.de> References: <20210326055505.1424432-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Stat-Signature: n1a3qyhhb1kzdph1fgqfg1xyxo1wcs34 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 0B6DE40002C1 Received-SPF: none (bombadil.srs.infradead.org>: No applicable sender policy available) receiver=imf26; identity=mailfrom; envelope-from=""; helo=bombadil.infradead.org; client-ip=198.137.202.133 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1616738125-676870 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a helper that calls remap_pfn_range for an struct io_mapping, relying on the pgprot pre-validation done when creating the mapping instead of doing it at runtime. Signed-off-by: Christoph Hellwig --- include/linux/io-mapping.h | 3 +++ mm/Kconfig | 3 +++ mm/Makefile | 1 + mm/io-mapping.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 mm/io-mapping.c diff --git a/include/linux/io-mapping.h b/include/linux/io-mapping.h index c093e81310a9b3..e9743cfd858527 100644 --- a/include/linux/io-mapping.h +++ b/include/linux/io-mapping.h @@ -220,3 +220,6 @@ io_mapping_free(struct io_mapping *iomap) } #endif /* _LINUX_IO_MAPPING_H */ + +int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma, + unsigned long addr, unsigned long pfn, unsigned long size); diff --git a/mm/Kconfig b/mm/Kconfig index 24c045b24b9506..6b0f2cfbfac0f3 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -872,4 +872,7 @@ config MAPPING_DIRTY_HELPERS config KMAP_LOCAL bool +# struct io_mapping based helper. Selected by drivers that need them +config IO_MAPPING + bool endmenu diff --git a/mm/Makefile b/mm/Makefile index 72227b24a61688..c0135e385984bb 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -120,3 +120,4 @@ obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o +obj-$(CONFIG_IO_MAPPING) += io-mapping.o diff --git a/mm/io-mapping.c b/mm/io-mapping.c new file mode 100644 index 00000000000000..01b3627999304e --- /dev/null +++ b/mm/io-mapping.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include + +/** + * io_mapping_map_user - remap an I/O mapping to userspace + * @iomap: the source io_mapping + * @vma: user vma to map to + * @addr: target user address to start at + * @pfn: physical address of kernel memory + * @size: size of map area + * + * Note: this is only safe if the mm semaphore is held when called. + */ +int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma, + unsigned long addr, unsigned long pfn, unsigned long size) +{ + vm_flags_t expected_flags = VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; + + if (WARN_ON_ONCE((vma->vm_flags & expected_flags) != expected_flags)) + return -EINVAL; + + /* We rely on prevalidation of the io-mapping to skip track_pfn(). */ + return remap_pfn_range_notrack(vma, addr, pfn, size, + __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) | + (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK))); +} +EXPORT_SYMBOL_GPL(io_mapping_map_user);