From patchwork Fri Feb 17 16:09:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 9580329 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 02E6B600F6 for ; Fri, 17 Feb 2017 16:10:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E5F9828701 for ; Fri, 17 Feb 2017 16:10:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D7D6028733; Fri, 17 Feb 2017 16:10:51 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham 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 8882C28701 for ; Fri, 17 Feb 2017 16:10:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934768AbdBQQJ6 (ORCPT ); Fri, 17 Feb 2017 11:09:58 -0500 Received: from mail-wm0-f52.google.com ([74.125.82.52]:36778 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934514AbdBQQJz (ORCPT ); Fri, 17 Feb 2017 11:09:55 -0500 Received: by mail-wm0-f52.google.com with SMTP id c85so19393758wmi.1 for ; Fri, 17 Feb 2017 08:09:49 -0800 (PST) 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=sEs8ZQfgpywVORrt1qbX9p4jy65+jg52VV6icy5zTiw=; b=hWgp6eqGYrl5hXsZaZaZohRLTAbcjrhVm1RSUoSWPm24Ch6tXtPeczcsZbQkxvAqwy ymkGGh2lq4w6EJM3qfknbSIKUYTIHWcttp9T8jApscK0Y2hG8XLTJu6hKxERgF2/Cu6J bsgwImJPbqdM+b8h+HETZaVwlE+aJiFl6irUL04nHonW88150cMqDtehR7pa/xEwrklu laCxw2OY5UyaxOjSxt0SUYorXaI2VwN37sZZy34ob/R5iQcOeiY/3GsMvkfJsZNj+zK9 09o22YAgdWFNapQOOyVkEyKI1MIKtqT5PSQPd+tC85bk/q4tW34SvP/G+0MSSWA1zpmw inEA== X-Gm-Message-State: AMke39mHT43Or2BD9DqYCGxlYAQ22MCjka6X0NA1Zb5Xa6HNQMoTlOhkz8/pyzQu4Ihyimcv X-Received: by 10.28.38.2 with SMTP id m2mr4367747wmm.44.1487347788842; Fri, 17 Feb 2017 08:09:48 -0800 (PST) Received: from veci.piliscsaba.szeredi.hu (pool-dsl-2c-0018.externet.hu. [217.173.44.24]) by smtp.gmail.com with ESMTPSA id h23sm4538793wrc.48.2017.02.17.08.09.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2017 08:09:47 -0800 (PST) From: Miklos Szeredi To: Al Viro Cc: linux-unionfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 5/9] mm: ovl: copy-up on MAP_SHARED Date: Fri, 17 Feb 2017 17:09:34 +0100 Message-Id: <1487347778-18596-6-git-send-email-mszeredi@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1487347778-18596-1-git-send-email-mszeredi@redhat.com> References: <1487347778-18596-1-git-send-email-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 A corner case of a corner case is when - file opened for O_RDONLY - which is then memory mapped SHARED - file opened for O_WRONLY - contents modified - contents read back though the shared mapping Unfortunately it looks very difficult to do anything about the established shared map after the file is copied up. Instead when a read-only file is mapped shared overlayfs copies up the file before actually doing the map. This may result in unnecessary copy-ups (but so may copy-up on open(O_RDWR) for exampe). We can revisit this later if it turns out to be a performance problem in real life. Signed-off-by: Miklos Szeredi Tested-by: Amir Goldstein --- mm/util.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mm/util.c b/mm/util.c index 3cb2164f4099..6cea1c4f27f3 100644 --- a/mm/util.c +++ b/mm/util.c @@ -300,6 +300,28 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, ret = security_mmap_file(file, prot, flag); if (!ret) { + /* + * Special treatment for overlayfs: + * + * Take MAP_SHARED/PROT_READ as hint about future writes to the + * file (through another file descriptor). Caller might not + * have had such an intent, but we hope MAP_PRIVATE will be used + * in most such cases. + * + * If we don't copy up now and the file is modified, it becomes + * really difficult to change the mapping to match that of the + * file's content later. + * + * Copy up needs to be done without mmap_sem since it takes vfs + * locks which would potentially deadlock under mmap_sem. + */ + if ((flag & MAP_SHARED) && !(prot & PROT_WRITE) && file) { + void *p = d_real(file->f_path.dentry, NULL, O_WRONLY); + + if (IS_ERR(p)) + return PTR_ERR(p); + } + if (down_write_killable(&mm->mmap_sem)) return -EINTR; ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,