Received: by 10.192.165.156 with SMTP id m28csp2087332imm; Thu, 12 Apr 2018 08:24:14 -0700 (PDT) X-Google-Smtp-Source: AIpwx48kAQrZIP04suiVc/X6+DY+3kOC1VpMQa4JZIo7SlMJ0dxMh6oHdmhXIoWM37TBnLaCVK+e X-Received: by 10.101.89.5 with SMTP id f5mr1043639pgu.428.1523546654071; Thu, 12 Apr 2018 08:24:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523546654; cv=none; d=google.com; s=arc-20160816; b=M5iq+415VaSZt9m90MR6G51cpd+p9nZo4IZebWn0ie23guQTx01IjDPFIpu22MvYn/ xAHEOzgleEutWI3mvJDryItYWotl4Xdab5H5mUagEGIm1cHMvW8shkSTvyBnUk/1AQ/d Ve5JGTpM9G/zhiLq+fc3OgmjgyoGwG44eUi9WFhAF20USA+dZZ2fS6bSUKShV4wBwFOQ Coi5hqHn093s+9Cf/lHB6LAEeeceEcPEIiC/8eDTnaXCq9mgn8IKK+KlcxKr/ItQTbon j1Tnj7RsTP8bbl+pu98yqpAQoQ89sA2S5U58qATKg8MYfwZRxlpoHVEbC/MuLDV602mv ONgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=HrdjNujPAv/3yoku7USJtGmWmqzZekNnxHdrLTOCD+U=; b=BULJgN4m9w4PXoCbK9WzMquKVakU9gIzfkQE4CTqBIthsAy5yVvST7rQEWglI8cYuo wm3twGYwsL4vZDLtiGBAFIfLZBZuoOviz+kJSmOaAQ5AP6MV7s41QeaSjBISw+KtUVn2 /RyM8GNYhMs8p/GhR2w3I3bFKrJOEf7JynEb0HPTQ2Q/5ghi0GCZlpX541a5CwDArtFw E3r6xtMq14z2nC8urYUKzQWpSmEyLMmI0yDckb9aSFhyXJabmdZ2vxlkEnrqRLLzMb2q +6NhjxP0vDccUVInChgZuBOxrEJ0u8hjrvt7L1sIWzf+utxhSEFoza8FxKvrM5lYS+NO Kg/g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a123si2443306pgc.43.2018.04.12.08.23.36; Thu, 12 Apr 2018 08:24:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754091AbeDLPSu (ORCPT + 99 others); Thu, 12 Apr 2018 11:18:50 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:40777 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753405AbeDLPI5 (ORCPT ); Thu, 12 Apr 2018 11:08:57 -0400 Received: by mail-wr0-f194.google.com with SMTP id v60so1557513wrc.7 for ; Thu, 12 Apr 2018 08:08:56 -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=HrdjNujPAv/3yoku7USJtGmWmqzZekNnxHdrLTOCD+U=; b=Pyz2pI9gOAUpodU96Z9Q2rVkEHP5IY2hF29Sm9LyZQxfBr5y/N+eir0NVlsq+khuKS nGzQu5RJVE1odWsmtzAYeySpIFTd9oHvK4YUJhU/9WRMDMz+EkdOZHwnu8d0vv2+KUjc MVdjJkenFuHxyLrXhlu6EIjxuYBnc+e+Zy9Ndoo2Kp9S9Ivgo3XqWBr6nHVT2JYscsAq LL0xGliD/3uLzm7cyD4wOhkPH+89fHcq/r0Ze8ni9Cxl3jfWMMbwLhKJFwoX5eykUabD TmZ/n1Q8//C/vFm+6Eawa5ukb19o9nP6iyh2WogeKfwLoeH+bKB9ZHfJHjMKI7UB9Ovb TtEw== X-Gm-Message-State: ALQs6tBMMviLOnc33gIcSD2+Jup+P/1WgwT71CPmpz15Dw1W73Ha9qzL UXyboUrj5Bd05fEv2sF07VUa9Q== X-Received: by 10.223.219.198 with SMTP id e6mr1195136wrj.212.1523545735939; Thu, 12 Apr 2018 08:08:55 -0700 (PDT) Received: from veci.piliscsaba.redhat.com (catv-176-63-54-97.catv.broadband.hu. [176.63.54.97]) by smtp.gmail.com with ESMTPSA id p197sm2621783wme.43.2018.04.12.08.08.54 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 12 Apr 2018 08:08:55 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 21/35] vfs: add f_op->pre_mmap() Date: Thu, 12 Apr 2018 17:08:12 +0200 Message-Id: <20180412150826.20988-22-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180412150826.20988-1-mszeredi@redhat.com> References: <20180412150826.20988-1-mszeredi@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 1add10f04b56..e28a2cf50fa7 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 c1250501364f..0a48f9077bad 100644 --- a/mm/util.c +++ b/mm/util.c @@ -350,6 +350,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, -- 2.14.3