From patchwork Fri Oct 9 15:03:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_K=C3=B6nig?= X-Patchwork-Id: 11826179 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 537F0139F for ; Fri, 9 Oct 2020 15:03:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C82FE22276 for ; Fri, 9 Oct 2020 15:03:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gzSB37MJ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C82FE22276 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2C5FF6B009B; Fri, 9 Oct 2020 11:03:49 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 22AAC6B009C; Fri, 9 Oct 2020 11:03:49 -0400 (EDT) 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 0A15B6B009D; Fri, 9 Oct 2020 11:03:49 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id CE3556B009B for ; Fri, 9 Oct 2020 11:03:48 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 1986A362E for ; Fri, 9 Oct 2020 15:03:48 +0000 (UTC) X-FDA: 77352706536.02.pies09_480375c271e1 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id DB890101276AD for ; Fri, 9 Oct 2020 15:03:47 +0000 (UTC) X-Spam-Summary: 1,0,0,8faf4c4f908fa0c5,d41d8cd98f00b204,ckoenig.leichtzumerken@gmail.com,,RULES_HIT:2:41:152:355:379:541:800:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1381:1437:1515:1516:1518:1535:1593:1594:1605:1676:1730:1747:1777:1792:1801:2393:2559:2562:3138:3139:3140:3141:3142:3308:3865:3866:3867:3870:3871:4049:4120:4250:4321:4605:5007:6119:6261:6653:6737:7903:9413:9592:11026:11473:11657:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12679:12895:12986:13894:14096:14097:14394:14659:14687:21080:21444:21451:21611:21627:21666:21990:30054:30064:30070,0,RBL:209.85.208.68:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.84.100;04yf7pc8c7crkphj1xj6feaxtrxjxycyda5i5d4mb4qp1h8akkdw8f73td5myop.jiknbq73ux56c58eqswyphorbn6uprobkw4depkzzybccm3ms6q73ugcnffp5em.w-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:1:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: pies09_480375c271e1 X-Filterd-Recvd-Size: 10000 Received: from mail-ed1-f68.google.com (mail-ed1-f68.google.com [209.85.208.68]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Fri, 9 Oct 2020 15:03:47 +0000 (UTC) Received: by mail-ed1-f68.google.com with SMTP id l16so9688649eds.3 for ; Fri, 09 Oct 2020 08:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=/KNFJOYdxSb2qiFFqZrfr76RXgP2ovb/rJFsndCmJ48=; b=gzSB37MJyFLgX68v0gj9Y0CBMFsZHW7vUgsUFgGyj4XT6aRMkgUFklHtBg/E7UM54d ucN5lwxsVw9F5e4KpeZGanQ2ykncrycSPnb8yYKJvlbcD+Gqu2VbY/jE1gG5agagP/Q2 +eMbApqM1L53LppSgymEEHPdAktgpvwHVDWe/qbWso8eN2FPN3lT2kbtOgvRJhiSHXcs VJQ5xE1oaQnSdTopgwegvvKQpQfMSGbWyLoSULRNIzvld78Rpqz7TyYbDPprsaiWNeR8 KMN1J8J7pRo2T9qdZXGOlwH56jXZo8tI9Qum/nRl5FOj5zepGkGts/AozS7x1GTmC/Hx YWsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/KNFJOYdxSb2qiFFqZrfr76RXgP2ovb/rJFsndCmJ48=; b=Z8tKW1PPEpEaQ0iIxmESywLFSttOSBAYYAFJOhF65nYmouPsFQILLQi+AbLSo03Q/B FJ8lTe/9yk+WNkf0jhbD6kQtvJ/RlpsiHHHYYddlwklF9Q/g2Wphr26YzdhHYFVWEeVG BdzpBjphNjNkikyXu+xnv9SHsqqPSIB8Ou7jiZPPNfqrH4wZpjl9Ehj7oHlhVUrTd52e CYdhFvT2WPW100Jr8pea5dYFZpraLFPvd4TIquq4RE1huap1JojuVFC5ZTQMu5FMDJ0q uP3CDJ1LErYbyPCx5Epux1i9eMAmwpdGfoGzlfpyeu56fT+QHwFZIVB0+NOHSYp+WvRK xPvg== X-Gm-Message-State: AOAM533N+MBgwLxH3zD+hiAAP5etyRu7TtzkWikCjz8lkZ5N6nFtkej4 nLcdQ0/1IlIRycmPmBEKulaBFQGm0Xg= X-Google-Smtp-Source: ABdhPJx6GcrD45dmKFlgGg1W3r7lj4kbvaCGRrSTGkzGy0phV2F1+TZcBxAnCoY4swhtx8cletX0RQ== X-Received: by 2002:a05:6402:3191:: with SMTP id di17mr5377edb.376.1602255825861; Fri, 09 Oct 2020 08:03:45 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:65f3:51d4:50d0:15b0]) by smtp.gmail.com with ESMTPSA id v14sm789364edy.68.2020.10.09.08.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Oct 2020 08:03:45 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linaro-mm-sig@lists.linaro.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, chris@chris-wilson.co.uk, airlied@redhat.com, akpm@linux-foundation.org, daniel@ffwll.ch, sumit.semwal@linaro.org, willy@infradead.org, jhubbard@nvidia.com, jgg@ziepe.ca Subject: [PATCH 2/6] mm: introduce vma_set_file function v3 Date: Fri, 9 Oct 2020 17:03:38 +0200 Message-Id: <20201009150342.1979-2-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201009150342.1979-1-christian.koenig@amd.com> References: <20201009150342.1979-1-christian.koenig@amd.com> MIME-Version: 1.0 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 the new vma_set_file() function to allow changing vma->vm_file with the necessary refcount dance. v2: add more users of this. v3: add missing EXPORT_SYMBOL, rebase on mmap cleanup, add comments why we drop the reference on two occasions. Signed-off-by: Christian König Reviewed-by: Daniel Vetter (v2) --- drivers/dma-buf/dma-buf.c | 5 +---- drivers/gpu/drm/etnaviv/etnaviv_gem.c | 4 +--- drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c | 3 +-- drivers/gpu/drm/i915/gem/i915_gem_mman.c | 5 +++-- drivers/gpu/drm/msm/msm_gem.c | 4 +--- drivers/gpu/drm/omapdrm/omap_gem.c | 3 +-- drivers/gpu/drm/vgem/vgem_drv.c | 3 +-- drivers/staging/android/ashmem.c | 6 +++--- include/linux/mm.h | 2 ++ mm/mmap.c | 15 +++++++++++++++ 10 files changed, 29 insertions(+), 21 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index edd57402a48a..8e6a114c6034 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -1160,10 +1160,7 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, return -EINVAL; /* readjust the vma */ - if (vma->vm_file) - fput(vma->vm_file); - - vma->vm_file = get_file(dmabuf->file); + vma_set_file(vma, dmabuf->file); vma->vm_pgoff = pgoff; return dmabuf->ops->mmap(dmabuf, vma); diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem.c b/drivers/gpu/drm/etnaviv/etnaviv_gem.c index 312e9d58d5a7..10ce267c0947 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_gem.c +++ b/drivers/gpu/drm/etnaviv/etnaviv_gem.c @@ -145,10 +145,8 @@ static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj, * address_space (so unmap_mapping_range does what we want, * in particular in the case of mmap'd dmabufs) */ - fput(vma->vm_file); - get_file(etnaviv_obj->base.filp); vma->vm_pgoff = 0; - vma->vm_file = etnaviv_obj->base.filp; + vma_set_file(vma, etnaviv_obj->base.filp); vma->vm_page_prot = vm_page_prot; } diff --git a/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c index fec0e1e3dc3e..8ce4c9e28b87 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c @@ -119,8 +119,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct * if (ret) return ret; - fput(vma->vm_file); - vma->vm_file = get_file(obj->base.filp); + vma_set_file(vma, obj->base.filp); return 0; } diff --git a/drivers/gpu/drm/i915/gem/i915_gem_mman.c b/drivers/gpu/drm/i915/gem/i915_gem_mman.c index 3d69e51f3e4d..ec28a6cde49b 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_mman.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_mman.c @@ -893,8 +893,9 @@ int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma) * requires avoiding extraneous references to their filp, hence why * we prefer to use an anonymous file for their mmaps. */ - fput(vma->vm_file); - vma->vm_file = anon; + vma_set_file(vma, anon); + /* Drop the initial creation reference, the vma is now holding one. */ + fput(anon); switch (mmo->mmap_type) { case I915_MMAP_TYPE_WC: diff --git a/drivers/gpu/drm/msm/msm_gem.c b/drivers/gpu/drm/msm/msm_gem.c index de915ff6f4b4..a71f42870d5e 100644 --- a/drivers/gpu/drm/msm/msm_gem.c +++ b/drivers/gpu/drm/msm/msm_gem.c @@ -223,10 +223,8 @@ int msm_gem_mmap_obj(struct drm_gem_object *obj, * address_space (so unmap_mapping_range does what we want, * in particular in the case of mmap'd dmabufs) */ - fput(vma->vm_file); - get_file(obj->filp); vma->vm_pgoff = 0; - vma->vm_file = obj->filp; + vma_set_file(vma, obj->filp); vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); } diff --git a/drivers/gpu/drm/omapdrm/omap_gem.c b/drivers/gpu/drm/omapdrm/omap_gem.c index 979d53a93c2b..0d4542ff1d7d 100644 --- a/drivers/gpu/drm/omapdrm/omap_gem.c +++ b/drivers/gpu/drm/omapdrm/omap_gem.c @@ -564,9 +564,8 @@ int omap_gem_mmap_obj(struct drm_gem_object *obj, * address_space (so unmap_mapping_range does what we want, * in particular in the case of mmap'd dmabufs) */ - fput(vma->vm_file); vma->vm_pgoff = 0; - vma->vm_file = get_file(obj->filp); + vma_set_file(vma, obj->filp); vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); } diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index fa54a6d1403d..ea0eecae5153 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -397,8 +397,7 @@ static int vgem_prime_mmap(struct drm_gem_object *obj, if (ret) return ret; - fput(vma->vm_file); - vma->vm_file = get_file(obj->filp); + vma_set_file(vma, obj->filp); vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); diff --git a/drivers/staging/android/ashmem.c b/drivers/staging/android/ashmem.c index 10b4be1f3e78..4789d36ddfd3 100644 --- a/drivers/staging/android/ashmem.c +++ b/drivers/staging/android/ashmem.c @@ -450,9 +450,9 @@ static int ashmem_mmap(struct file *file, struct vm_area_struct *vma) vma_set_anonymous(vma); } - if (vma->vm_file) - fput(vma->vm_file); - vma->vm_file = asma->file; + vma_set_file(vma, asma->file); + /* XXX: merge this with the get_file() above if possible */ + fput(asma->file); out: mutex_unlock(&ashmem_mutex); diff --git a/include/linux/mm.h b/include/linux/mm.h index ca6e6a81576b..f7a005153d02 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2693,6 +2693,8 @@ static inline void vma_set_page_prot(struct vm_area_struct *vma) } #endif +void vma_set_file(struct vm_area_struct *vma, struct file *file); + #ifdef CONFIG_NUMA_BALANCING unsigned long change_prot_numa(struct vm_area_struct *vma, unsigned long start, unsigned long end); diff --git a/mm/mmap.c b/mm/mmap.c index 3a2670d73355..19cd37c3ebac 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -136,6 +136,21 @@ void vma_set_page_prot(struct vm_area_struct *vma) WRITE_ONCE(vma->vm_page_prot, vm_page_prot); } +/* + * Change backing file, only valid to use during initial VMA setup. + */ +void vma_set_file(struct vm_area_struct *vma, struct file *file) +{ + if (file) + get_file(file); + + swap(vma->vm_file, file); + + if (file) + fput(file); +} +EXPORT_SYMBOL(vma_set_file); + /* * Requires inode->i_mapping->i_mmap_rwsem */