From patchwork Fri Jan 11 15:07:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Souptick Joarder X-Patchwork-Id: 10758187 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D2BC8746 for ; Fri, 11 Jan 2019 15:03:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B81612A02D for ; Fri, 11 Jan 2019 15:03:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A906D2A07E; Fri, 11 Jan 2019 15:03:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 007A42A02D for ; Fri, 11 Jan 2019 15:03:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0005F8E0002; Fri, 11 Jan 2019 10:03:15 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id ECB9B8E0001; Fri, 11 Jan 2019 10:03:14 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D94F08E0002; Fri, 11 Jan 2019 10:03:14 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by kanga.kvack.org (Postfix) with ESMTP id 96B6D8E0001 for ; Fri, 11 Jan 2019 10:03:14 -0500 (EST) Received: by mail-pf1-f198.google.com with SMTP id b17so10480332pfc.11 for ; Fri, 11 Jan 2019 07:03:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:date:from:to:cc:subject :message-id:mime-version:content-disposition:user-agent; bh=OKnr+eVdmA4Yca5asvAl3VJvv/8NsBSwy+KQiY0Jcow=; b=ATLAtxW3GrSAHf6xxoei8j/8PbtPIBd9+HU7FZU0Zh9YedGmGP5TIktxBHmS01CsbG HyW5RjRLRcYJIqHK3i+tkRRmVR92m1ktkDx6gfno2u9GDp48YxwyJwTpryQfCVy7DfnB HwgQVq2OBFDCxVH4BAR8L9Y5y08UZcIgp/YqfLTQH7/z+n6ed9T/3+Frx0tYM71Mmomn ApOi8U/Q8bh1Xy0bigGgJnbdMXkWOJY0yzahnkwYVWtGYyx3Jrgaemo1uUyNJB/irJ1w uBuw+hL7aJfTQB3K+uXwvE0qzsHQxANh3rVY0+CRxy2vZJjCy/wB05+LmDGsR6PJ0FWj hYLA== X-Gm-Message-State: AJcUukcPKKSPUhrX/FEcXv/xES4GKQ064rvdklBQ96yWDN1V5UHeG0C0 9MX1kItbQHlIgm4ux5PibxGPUB+FW16AdUx56r+X1DWIjJNQQIwGb60zXs0Hhiq//xjNj79KImI 2W/b5L2p3PrFr4KljhG3Kw1fR0BypOWD5Ofz0tFUnQQ8RZ/O4OrwrhQ/aazuJgOaSOj36bXtkEY 6jZCm7E3FCjLLkEABn6LlF9BmAfxRwvChNBh86uAdVqrzfG5NUWxJ1zD8MYuRRxrL0TncLI6YJW I8305d+O/lc9VUlsOZYdi/VymmqyOZPqjb6gOrcRyVDjtfm3vXZ6obmGhksWhrLgiIYOBGwooaV 3vQac+Adzdba0BDkjgwBNGU7EGtZxsLAkiHlW1w9xEcqmULtbzpgc/9DZvJ8f93X9vg1cdeoTIT T X-Received: by 2002:a65:6392:: with SMTP id h18mr13829471pgv.107.1547218994243; Fri, 11 Jan 2019 07:03:14 -0800 (PST) X-Received: by 2002:a65:6392:: with SMTP id h18mr13829399pgv.107.1547218993284; Fri, 11 Jan 2019 07:03:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547218993; cv=none; d=google.com; s=arc-20160816; b=tcYYw2Urh+q4+Cum0IMMKpSu8mitj3sMFMM8Vv0YBCR/OP9CvY/Gbbcpd5BAMsFQCb MkRXh6Zr8z7o48Xba7j95twT699izUz9ualPVxdtJs3Plufi5+oEWZb5CdGkHO/KuzSd cxfdePiD3oqb5qatX3WhbezMWHdtoNvswWDzYd0kprt0qM1pVJjs/KaaP/79cjQzhY9T jCr7t7Vr89/u8N87oplrhz0NazhNIcIYA+zdyeJNEJbHsQKqLDZVUJsRR31fnxCiErKC 6nla7HJkUS5Mpe8idQ7DSeL0QGQNQ6zP9lTJ8t9SGgYFBO0DhWCSEcMDicXXCiq5X8TC Og0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:content-disposition:mime-version:message-id:subject:cc :to:from:date:dkim-signature; bh=OKnr+eVdmA4Yca5asvAl3VJvv/8NsBSwy+KQiY0Jcow=; b=vuEH/N+1MqYdqySHbLoxlutcZYzIII9645jodhnys0PqdAe5VcCUeBijrV4nakWzUp q5K5/O+M/UPab2CXqU+uWoctpU7cJL3Fm2IzB2edqFmiDvYP3GTsqm4CFj6+YR3sHkwa 7OBbz+GPKfUvxaIAZyIbjT0FXwjXJWUyqEe8N8VEMcYmSHqkeu3KXrZBRy2LmYGwPsAU vIjrI02YxLDDJYMBWHZx1HHcHTqdFtd7mptnMjgT3fZU+RrhpaobeV6/s5vM8DjVf1cG ck49p75Hm8zVmf0vZZBJTTb6u0x5mDEhL0mmHUhyWgrb+PekcV0OtbVMAXrx62wvpI4e jsyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ecFP8Fvc; spf=pass (google.com: domain of jrdr.linux@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jrdr.linux@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id n87sor3794675pfh.64.2019.01.11.07.03.13 for (Google Transport Security); Fri, 11 Jan 2019 07:03:13 -0800 (PST) Received-SPF: pass (google.com: domain of jrdr.linux@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ecFP8Fvc; spf=pass (google.com: domain of jrdr.linux@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=jrdr.linux@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=OKnr+eVdmA4Yca5asvAl3VJvv/8NsBSwy+KQiY0Jcow=; b=ecFP8FvcstIqu0NB/u52GoYNOTcWG2DvcB07IgY8lEs9qJPP4QRrMpFsf79PdPyxVo +95bi0qc4yFsVreg3UGS61GpTOuoYCQnYZyc+5rX9mPEq6iWXgJAAzC0Dic6/1Ac4kdd ToSp1I9tu34JSInsxojwgpUz/v6muGU8hTz3xenp7NxE8zdhMiFKGpknt4LFIcIdPB8A yk0tCLiaSAkmPdaK/WpQa8EEuvjBTrzwO+wwZHg4X6ykM3D78CNfnJIFfuAC4GhqGeJE 2By7VfXiT62OJFnrYXEMYkgKZWvN35vVUGiGIWMLm2+uUIMEr0RRqcicvSTWrx7DGnxD TQHw== X-Google-Smtp-Source: ALg8bN4N+0OC/ohlRldBlS6pqDxN4tNRJaVupZC6A5wD5JEK2YvVMDK1VGgLB+11HRGTGie4+RIV4Q== X-Received: by 2002:aa7:80d7:: with SMTP id a23mr14629976pfn.86.1547218992868; Fri, 11 Jan 2019 07:03:12 -0800 (PST) Received: from jordon-HP-15-Notebook-PC ([49.207.52.190]) by smtp.gmail.com with ESMTPSA id d129sm119031814pfc.31.2019.01.11.07.03.10 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 11 Jan 2019 07:03:11 -0800 (PST) Date: Fri, 11 Jan 2019 20:37:12 +0530 From: Souptick Joarder To: akpm@linux-foundation.org, willy@infradead.org, mhocko@suse.com, kirill.shutemov@linux.intel.com, vbabka@suse.cz, riel@surriel.com, sfr@canb.auug.org.au, rppt@linux.vnet.ibm.com, peterz@infradead.org, linux@armlinux.org.uk, robin.murphy@arm.com, iamjoonsoo.kim@lge.com, treding@nvidia.com, keescook@chromium.org, m.szyprowski@samsung.com, stefanr@s5r6.in-berlin.de, hjc@rock-chips.com, heiko@sntech.de, airlied@linux.ie, oleksandr_andrushchenko@epam.com, joro@8bytes.org, pawel@osciak.com, kyungmin.park@samsung.com, mchehab@kernel.org, boris.ostrovsky@oracle.com, jgross@suse.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux1394-devel@lists.sourceforge.net, dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, xen-devel@lists.xen.org, iommu@lists.linux-foundation.org, linux-media@vger.kernel.org Subject: [PATCH 1/9] mm: Introduce new vm_insert_range and vm_insert_range_buggy API Message-ID: <20190111150712.GA2696@jordon-HP-15-Notebook-PC> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) 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: X-Virus-Scanned: ClamAV using ClamSMTP Previouly drivers have their own way of mapping range of kernel pages/memory into user vma and this was done by invoking vm_insert_page() within a loop. As this pattern is common across different drivers, it can be generalized by creating new functions and use it across the drivers. vm_insert_range() is the API which could be used to mapped kernel memory/pages in drivers which has considered vm_pgoff vm_insert_range_buggy() is the API which could be used to map range of kernel memory/pages in drivers which has not considered vm_pgoff. vm_pgoff is passed default as 0 for those drivers. We _could_ then at a later "fix" these drivers which are using vm_insert_range_buggy() to behave according to the normal vm_pgoff offsetting simply by removing the _buggy suffix on the function name and if that causes regressions, it gives us an easy way to revert. Signed-off-by: Souptick Joarder Suggested-by: Russell King Suggested-by: Matthew Wilcox --- include/linux/mm.h | 4 +++ mm/memory.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ mm/nommu.c | 14 ++++++++++ 3 files changed, 99 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 5411de9..9d1dff6 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2514,6 +2514,10 @@ unsigned long change_prot_numa(struct vm_area_struct *vma, int remap_pfn_range(struct vm_area_struct *, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t); int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *); +int vm_insert_range(struct vm_area_struct *vma, struct page **pages, + unsigned long num); +int vm_insert_range_buggy(struct vm_area_struct *vma, struct page **pages, + unsigned long num); vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn); vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr, diff --git a/mm/memory.c b/mm/memory.c index 4ad2d29..00e66df 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1520,6 +1520,87 @@ int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, } EXPORT_SYMBOL(vm_insert_page); +/** + * __vm_insert_range - insert range of kernel pages into user vma + * @vma: user vma to map to + * @pages: pointer to array of source kernel pages + * @num: number of pages in page array + * @offset: user's requested vm_pgoff + * + * This allows drivers to insert range of kernel pages they've allocated + * into a user vma. + * + * If we fail to insert any page into the vma, the function will return + * immediately leaving any previously inserted pages present. Callers + * from the mmap handler may immediately return the error as their caller + * will destroy the vma, removing any successfully inserted pages. Other + * callers should make their own arrangements for calling unmap_region(). + * + * Context: Process context. + * Return: 0 on success and error code otherwise. + */ +static int __vm_insert_range(struct vm_area_struct *vma, struct page **pages, + unsigned long num, unsigned long offset) +{ + unsigned long count = vma_pages(vma); + unsigned long uaddr = vma->vm_start; + int ret, i; + + /* Fail if the user requested offset is beyond the end of the object */ + if (offset > num) + return -ENXIO; + + /* Fail if the user requested size exceeds available object size */ + if (count > num - offset) + return -ENXIO; + + for (i = 0; i < count; i++) { + ret = vm_insert_page(vma, uaddr, pages[offset + i]); + if (ret < 0) + return ret; + uaddr += PAGE_SIZE; + } + + return 0; +} + +/** + * vm_insert_range - insert range of kernel pages starts with non zero offset + * @vma: user vma to map to + * @pages: pointer to array of source kernel pages + * @num: number of pages in page array + * + * Maps an object consisting of `num' `pages', catering for the user's + * requested vm_pgoff + * + * Context: Process context. Called by mmap handlers. + * Return: 0 on success and error code otherwise. + */ +int vm_insert_range(struct vm_area_struct *vma, struct page **pages, + unsigned long num) +{ + return __vm_insert_range(vma, pages, num, vma->vm_pgoff); +} +EXPORT_SYMBOL(vm_insert_range); + +/** + * vm_insert_range_buggy - insert range of kernel pages starts with zero offset + * @vma: user vma to map to + * @pages: pointer to array of source kernel pages + * @num: number of pages in page array + * + * Maps a set of pages, always starting at page[0] + * + * Context: Process context. Called by mmap handlers. + * Return: 0 on success and error code otherwise. + */ +int vm_insert_range_buggy(struct vm_area_struct *vma, struct page **pages, + unsigned long num) +{ + return __vm_insert_range(vma, pages, num, 0); +} +EXPORT_SYMBOL(vm_insert_range_buggy); + static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, pfn_t pfn, pgprot_t prot, bool mkwrite) { diff --git a/mm/nommu.c b/mm/nommu.c index 749276b..21d101e 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -473,6 +473,20 @@ int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, } EXPORT_SYMBOL(vm_insert_page); +int vm_insert_range(struct vm_area_struct *vma, struct page **pages, + unsigned long num) +{ + return -EINVAL; +} +EXPORT_SYMBOL(vm_insert_range); + +int vm_insert_range_buggy(struct vm_area_struct *vma, struct page **pages, + unsigned long num) +{ + return -EINVAL; +} +EXPORT_SYMBOL(vm_insert_range_buggy); + /* * sys_brk() for the most part doesn't need the global kernel * lock, except when an application is doing something nasty