From patchwork Mon Sep 14 13:29:20 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: 11773777 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 90F1F618 for ; Mon, 14 Sep 2020 13:29:41 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 66824207BB for ; Mon, 14 Sep 2020 13:29:41 +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="LNYaV5ht" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 66824207BB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C1A65899A5; Mon, 14 Sep 2020 13:29:36 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-ed1-x543.google.com (mail-ed1-x543.google.com [IPv6:2a00:1450:4864:20::543]) by gabe.freedesktop.org (Postfix) with ESMTPS id E807489B49 for ; Mon, 14 Sep 2020 13:29:32 +0000 (UTC) Received: by mail-ed1-x543.google.com with SMTP id ay8so17565840edb.8 for ; Mon, 14 Sep 2020 06:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2sgKFqAzyYTWFS7DpgNkPf7xuqkExWpe0vtA6hya4p8=; b=LNYaV5htN1AoGcRr3qutKAdIAJ9K2NpHJmZnCLnFDlG8+zhSr8pFBVrIVZbK7upLCy XuYIA5pbVz06HyYiXjA0CX8OrBfsP8QYVTX6yJ60VAjafiTzs3Dr1q7h1S6WN1SGAvlY UXlmfxZ5/aBJS18uzwRmNUI3/lYVMDzcdWcPZzIRtlGG2B6QiV90AnQEfAOpca3J5cuM fi/i79PCqkzlb/1pkXfPwtPFhCm+AEfUwe9iWPeCcG3ahwbSU9PfGzYe8A8PulfEuuIe /yCSNERBUwZ4Herw4m2hVxb+aHQ2rdJcpWKX6iPZoqfXgRCIkWGGqsSIIr4ud7pRIc3R 2DEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2sgKFqAzyYTWFS7DpgNkPf7xuqkExWpe0vtA6hya4p8=; b=cotohp4YtRUTKzXFRIqi0m573znNbGFYkm7vyrAaCoWbnBc625pl4+YQbbGK4NyTjA PuRqyMnYyr7N0t6Qn4qP8nuN9tjRm3Ue195K8s9zq8olACddQUxoaYOTHXCUlQk+CjlB jPz7P6wIIVrmrG6A9g8lkVMIOvFswcWhhRWidT6zxbouMgvCGPMC8oX+XflzoF6Ga8i5 E6MWBDrESRBqz6ba7zeEeMTZ+M0IWAOd2ji4zlb894iUBZNCEg1oAyCujfyPRMQqYwTi mbegNaP++rvhI+2QWXBJoXUdU59gvxx65W/NA8AbKVGyw89H5hcS9IsPVGQ8291Mt4na 3DQw== X-Gm-Message-State: AOAM5339x8o+dGXS+o0TGIl6AtIBGo53oPHudsMQ0VslLp9R99LBY8Ik OV93hXXerjGVWgLPQ9ylbA0= X-Google-Smtp-Source: ABdhPJwqz+YzShnzPAVkh+LgWPDH996EZ9R5Asg2qCZETs+L6ijZw4dfeniXkQwebJgtgDxC1+lltA== X-Received: by 2002:aa7:cad3:: with SMTP id l19mr16523629edt.352.1600090171724; Mon, 14 Sep 2020 06:29:31 -0700 (PDT) Received: from abel.fritz.box ([2a02:908:1252:fb60:6179:d701:8021:da3d]) by smtp.gmail.com with ESMTPSA id d6sm9575625edm.31.2020.09.14.06.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Sep 2020 06:29:30 -0700 (PDT) From: " =?utf-8?q?Christian_K=C3=B6nig?= " X-Google-Original-From: =?utf-8?q?Christian_K=C3=B6nig?= To: akpm@linux-foundation.org Subject: [PATCH 2/2] mm: introduce vma_set_file function Date: Mon, 14 Sep 2020 15:29:20 +0200 Message-Id: <20200914132920.59183-3-christian.koenig@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200914132920.59183-1-christian.koenig@amd.com> References: <20200914132920.59183-1-christian.koenig@amd.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org, linux-media@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Add the new vma_set_file() function to allow changing vma->vm_file with the necessary refcount dance. Signed-off-by: Christian König Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/dma-buf/dma-buf.c | 16 +++++----------- include/linux/mm.h | 2 ++ mm/mmap.c | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 1699a8e309ef..672f3525ba74 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -1163,20 +1163,14 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, return -EINVAL; /* readjust the vma */ - get_file(dmabuf->file); - oldfile = vma->vm_file; - vma->vm_file = dmabuf->file; + oldfile = vma_set_file(vma, dmabuf->file); vma->vm_pgoff = pgoff; ret = dmabuf->ops->mmap(dmabuf, vma); - if (ret) { - /* restore old parameters on failure */ - vma->vm_file = oldfile; - fput(dmabuf->file); - } else { - if (oldfile) - fput(oldfile); - } + /* restore old parameters on failure */ + if (ret) + vma_set_file(vma, oldfile); + return ret; } diff --git a/include/linux/mm.h b/include/linux/mm.h index 1983e08f5906..398a6fdaad1e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2688,6 +2688,8 @@ static inline void vma_set_page_prot(struct vm_area_struct *vma) } #endif +struct file *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 40248d84ad5f..d3c3c510f643 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -136,6 +136,22 @@ 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. + */ +struct file *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); + + return file; +} + /* * Requires inode->i_mapping->i_mmap_rwsem */