From patchwork Mon May 7 08:37:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 10383627 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A77D260353 for ; Mon, 7 May 2018 08:49:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E82628B63 for ; Mon, 7 May 2018 08:49:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9381228B65; Mon, 7 May 2018 08:49:04 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3DA5728B63 for ; Mon, 7 May 2018 08:49:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752096AbeEGIiX (ORCPT ); Mon, 7 May 2018 04:38:23 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:52462 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752054AbeEGIiR (ORCPT ); Mon, 7 May 2018 04:38:17 -0400 Received: by mail-wm0-f68.google.com with SMTP id w194so12002866wmf.2 for ; Mon, 07 May 2018 01:38:16 -0700 (PDT) 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; bh=vLS5twYGN82CGMzjXvaKwVjddk6EawQ4lb1D1IgRYqM=; b=DtoURtAHgFTE0qgaeynEgBxMwdq5Yryio/6jKdEyhlBHxmgX1dJIdUf0jJZeVKP0G2 iX8s+5lAmqQPi3A42zrnkmviK2g2l19jRZ4i4u70CezoxsdzSqCSvonRUQVfwKnIjEnt 4uFP03RMREmDSHz/ZSMx7PckeyblzKkPzcUEduc//Xj1QR71rjp2TPNtQLYSKZyejSRj Kh+Fdtb+fnbIhShUNSwmRUtobl1wl178a+naKK2v8BUOkxAdpRpJoroXd0xRjM3RbCrT NkqWuU0Bx1luBXPO/m1ii/8AKw12VrNDUgkRR0MpKGCnOTXGiu+F1b83KG9aGlbFpQNq SZAw== X-Gm-Message-State: ALKqPwdBKAhNTC4ABhZQp2MafsOZDzlU07UxM15CYt5PS6c+JsElv+kC ynGlb+iYYNRrvznVH5gjpIbZxQ== X-Google-Smtp-Source: AB8JxZpJ2Nfv6DUoMlC+hQDUY8IN5XL5Zf5fPGFMIYSqeHN4bwe/8mfE1kaODkO74ABGy2/Pmjx6gg== X-Received: by 10.28.144.21 with SMTP id s21mr158902wmd.75.1525682296053; Mon, 07 May 2018 01:38:16 -0700 (PDT) Received: from veci.piliscsaba.redhat.com (C2B0E321.catv.pool.telekom.hu. [194.176.227.33]) by smtp.gmail.com with ESMTPSA id h8-v6sm5908050wmc.16.2018.05.07.01.38.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 May 2018 01:38:15 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Al Viro Subject: [PATCH v2 03/35] vfs: add f_op->pre_mmap() Date: Mon, 7 May 2018 10:37:35 +0200 Message-Id: <20180507083807.28792-4-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180507083807.28792-1-mszeredi@redhat.com> References: <20180507083807.28792-1-mszeredi@redhat.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is needed by overlayfs to be able to copy up a file from a read-only lower layer to a writable layer when being mapped shared. When copying up, overlayfs takes VFS locks that would violate locking order when nested inside mmap_sem. Add a new f_op->pre_mmap method, which is called before taking mmap_sem. Signed-off-by: Miklos Szeredi --- Documentation/filesystems/Locking | 1 + Documentation/filesystems/vfs.txt | 3 +++ include/linux/fs.h | 1 + mm/util.c | 5 +++++ 4 files changed, 10 insertions(+) diff --git a/Documentation/filesystems/Locking b/Documentation/filesystems/Locking index 75d2d57e2c44..60e76060baff 100644 --- a/Documentation/filesystems/Locking +++ b/Documentation/filesystems/Locking @@ -442,6 +442,7 @@ prototypes: unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); + int (*pre_mmap) (struct file *, unsigned long, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); diff --git a/Documentation/filesystems/vfs.txt b/Documentation/filesystems/vfs.txt index 5fd325df59e2..2bc77ea8aef4 100644 --- a/Documentation/filesystems/vfs.txt +++ b/Documentation/filesystems/vfs.txt @@ -859,6 +859,7 @@ struct file_operations { unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); + int (*pre_mmap) (struct file *, unsigned long, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*mremap)(struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); @@ -906,6 +907,8 @@ otherwise noted. compat_ioctl: called by the ioctl(2) system call when 32 bit system calls are used on 64 bit kernels. + pre_mmap: called before mmap, without mmap_sem being held yet. + mmap: called by the mmap(2) system call open: called by the VFS when an inode should be opened. When the VFS diff --git a/include/linux/fs.h b/include/linux/fs.h index af49b55ff439..898fb798a3ff 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1716,6 +1716,7 @@ struct file_operations { __poll_t (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); + int (*pre_mmap) (struct file *, unsigned long, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); unsigned long mmap_supported_flags; int (*open) (struct inode *, struct file *); diff --git a/mm/util.c b/mm/util.c index 45fc3169e7b0..11cd375e1a19 100644 --- a/mm/util.c +++ b/mm/util.c @@ -352,6 +352,11 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, ret = security_mmap_file(file, prot, flag); if (!ret) { + if (file && file->f_op->pre_mmap) { + ret = file->f_op->pre_mmap(file, prot, flag); + if (ret) + return ret; + } if (down_write_killable(&mm->mmap_sem)) return -EINTR; ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,