Received: by 10.192.165.148 with SMTP id m20csp2828250imm; Mon, 7 May 2018 01:46:02 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrFQE2k5CL89EijLn9Ee3EucJlREcMZu/VJdIu+r+N0Enmej6XlIE3gt70DsUDddUMVE9+9 X-Received: by 2002:a17:902:d909:: with SMTP id c9-v6mr10831287plz.293.1525682762197; Mon, 07 May 2018 01:46:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525682762; cv=none; d=google.com; s=arc-20160816; b=OuI7NnNoXQ+wy9ttmU96DJcT45j0xU1N54rJkmq9XExxZZO2xTdmJagKuxRWd33CBx 8Lq+LJPkHWeK7aDXPil+wcbyldI50kglhJ7lqwICI9qNGlOGyfqrsq1JLbD5iaJedVJZ EOzzjYnV8GHBBuSb+B3I+NYIuyU9zZQPeiiYjkyqFFkEnzB3eIjRUGqnK28jd9uDeBP2 8mNuMz6XJoUDpLNkQpHVw46fS64wNWERKzP571CdX5qWHPiWe5LAe/7hOJc3GKWBmEYt qQSXwv18uhdbFxFPGDQLMdJauwIXxkJGEfQyRQ6JuIGi3StyuJjURgLJ3NxnrG3DUWpm lTEw== 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=Eqopn98pyiNsFJgp05z7Wz+e3n1pgyG1slZzh0pTJvI=; b=GClRkpN0sDep5stzYWQjdCsFSCwi3nQxCV9qCYehx+VBHuQ7cdK2AmbIdjsBD+JvOe 065RPVSoGmQH9pO/EKEmwkrpXtWES8dkjj5DEH+kv5v+rNsfcT8K2eq0EWGthSXh4Air BGglaTv1txt5W6KHM2GAa3wCHxl7SJas2pOt2ZySl167EsKelXl2NvtowB4vRYVkwR2I 84cBq647vJMEOSDrSxubCaww9+WLA6AWrWYKXcDejsbgXT8E/7yEUWAoiupUG6W7u1sF RWlYiq7zFlOz5aO6UOpL5vJMDFTvlMqWlQGmLxNPtC1Px4NhHH24xq9d8vJ+bAx3qf7+ 93Bg== 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 1-v6si21548827plv.217.2018.05.07.01.45.47; Mon, 07 May 2018 01:46:02 -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 S1752417AbeEGImp (ORCPT + 99 others); Mon, 7 May 2018 04:42:45 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:42176 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752192AbeEGIik (ORCPT ); Mon, 7 May 2018 04:38:40 -0400 Received: by mail-wr0-f194.google.com with SMTP id v5-v6so27852438wrf.9 for ; Mon, 07 May 2018 01:38:39 -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=Eqopn98pyiNsFJgp05z7Wz+e3n1pgyG1slZzh0pTJvI=; b=A5x4e+0bz7nvpVN45mvi30Ii9IclP01vCwJozTrqDevwpWsrVJFpwwF9WaJsfr4VuV u4LeTlnLC2ROrQoOom7AipP0/SfMUaEaJZcrF44XVCrMz9gw9Pun8H4JIVlP/35NnCNR y3owgcoPSp+HvYuz+5l88t9L5fUDEj7EKj2iUKAopO469dH/RnlejY4IZzBsS18hijs2 ImoZKMpCdHeq6HfmJagCSRhYeh+/duWsIIe/wr6cs6MGR5I1DYcDvovW+l6xHa0bINmY k7CZfSbqsuEVReDVOlMcInjGwYhxjAk3kXuHnvIjThT2uqV6wAXYqc6j/SgxYvWtOblG yFTA== X-Gm-Message-State: ALQs6tCJDXF6dCUU/Q59zUZu33NzJxD0SOa/ARpLv6PXIc/+hKLgTBAV RAoJVjv1XqkPklXZQDo/r7nYcw== X-Received: by 2002:adf:8e82:: with SMTP id q2-v6mr30845018wrb.53.1525682318757; Mon, 07 May 2018 01:38:38 -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.37 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 May 2018 01:38:38 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 23/35] ovl: copy-up on MAP_SHARED Date: Mon, 7 May 2018 10:37:55 +0200 Message-Id: <20180507083807.28792-24-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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, copy 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 --- fs/overlayfs/Kconfig | 21 +++++++++++++++++++++ fs/overlayfs/file.c | 22 ++++++++++++++++++++++ fs/overlayfs/overlayfs.h | 7 +++++++ fs/overlayfs/ovl_entry.h | 1 + fs/overlayfs/super.c | 22 ++++++++++++++++++++++ 5 files changed, 73 insertions(+) diff --git a/fs/overlayfs/Kconfig b/fs/overlayfs/Kconfig index 17032631c5cf..991c0a5a0e00 100644 --- a/fs/overlayfs/Kconfig +++ b/fs/overlayfs/Kconfig @@ -103,3 +103,24 @@ config OVERLAY_FS_XINO_AUTO For more information, see Documentation/filesystems/overlayfs.txt If unsure, say N. + +config OVERLAY_FS_COPY_UP_SHARED + bool "Overlayfs: copy up when mapping a file shared" + default n + depends on OVERLAY_FS + help + If this option is enabled then on mapping a file with MAP_SHARED + overlayfs copies up the file in anticipation of it being modified (just + like we copy up the file on O_WRONLY and O_RDWR in anticipation of + modification). This does not interfere with shared library loading, as + that uses MAP_PRIVATE. But there might be use cases out there where + this impacts performance and disk usage. + + This just selects the default, the feature can also be enabled or + disabled in the running kernel or individually on each overlay mount. + + To get maximally standard compliant behavior, enable this option. + + To get a maximally backward compatible kernel, disable this option. + + If unsure, say N. diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 2ac95c95e8e6..a60734ec89ec 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "overlayfs.h" @@ -256,6 +257,26 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) return ret; } +static int ovl_pre_mmap(struct file *file, unsigned long prot, + unsigned long flag) +{ + int err = 0; + + /* + * Take MAP_SHARED 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. + */ + if ((flag & MAP_SHARED) && ovl_copy_up_shared(file_inode(file)->i_sb)) + err = ovl_copy_up(file_dentry(file)); + + return err; +} + static int ovl_mmap(struct file *file, struct vm_area_struct *vma) { struct fd real; @@ -473,6 +494,7 @@ const struct file_operations ovl_file_operations = { .read_iter = ovl_read_iter, .write_iter = ovl_write_iter, .fsync = ovl_fsync, + .pre_mmap = ovl_pre_mmap, .mmap = ovl_mmap, .fallocate = ovl_fallocate, .unlocked_ioctl = ovl_ioctl, diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h index edbf69c8f45d..caaa47cea2aa 100644 --- a/fs/overlayfs/overlayfs.h +++ b/fs/overlayfs/overlayfs.h @@ -272,6 +272,13 @@ static inline unsigned int ovl_xino_bits(struct super_block *sb) return ofs->xino_bits; } +static inline bool ovl_copy_up_shared(struct super_block *sb) +{ + struct ovl_fs *ofs = sb->s_fs_info; + + return !(sb->s_flags & SB_RDONLY) && ofs->config.copy_up_shared; +} + /* namei.c */ int ovl_check_fh_len(struct ovl_fh *fh, int fh_len); diff --git a/fs/overlayfs/ovl_entry.h b/fs/overlayfs/ovl_entry.h index 41655a7d6894..3bea47c63fd9 100644 --- a/fs/overlayfs/ovl_entry.h +++ b/fs/overlayfs/ovl_entry.h @@ -18,6 +18,7 @@ struct ovl_config { const char *redirect_mode; bool index; bool nfs_export; + bool copy_up_shared; int xino; }; diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c index ad6a5baf226b..c3d8c7ea180f 100644 --- a/fs/overlayfs/super.c +++ b/fs/overlayfs/super.c @@ -56,6 +56,12 @@ module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); MODULE_PARM_DESC(ovl_xino_auto_def, "Auto enable xino feature"); +static bool ovl_copy_up_shared_def = + IS_ENABLED(CONFIG_OVERLAY_FS_COPY_UP_SHARED); +module_param_named(copy_up_shared, ovl_copy_up_shared_def, bool, 0644); +MODULE_PARM_DESC(ovl_copy_up_shared_def, + "Copy up when mapping a file shared"); + static void ovl_entry_stack_free(struct ovl_entry *oe) { unsigned int i; @@ -380,6 +386,9 @@ static int ovl_show_options(struct seq_file *m, struct dentry *dentry) "on" : "off"); if (ofs->config.xino != ovl_xino_def()) seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]); + if (ofs->config.copy_up_shared != ovl_copy_up_shared_def) + seq_printf(m, ",copy_up_shared=%s", + ofs->config.copy_up_shared ? "on" : "off"); return 0; } @@ -417,6 +426,8 @@ enum { OPT_XINO_ON, OPT_XINO_OFF, OPT_XINO_AUTO, + OPT_COPY_UP_SHARED_ON, + OPT_COPY_UP_SHARED_OFF, OPT_ERR, }; @@ -433,6 +444,8 @@ static const match_table_t ovl_tokens = { {OPT_XINO_ON, "xino=on"}, {OPT_XINO_OFF, "xino=off"}, {OPT_XINO_AUTO, "xino=auto"}, + {OPT_COPY_UP_SHARED_ON, "copy_up_shared=on"}, + {OPT_COPY_UP_SHARED_OFF, "copy_up_shared=off"}, {OPT_ERR, NULL} }; @@ -559,6 +572,14 @@ static int ovl_parse_opt(char *opt, struct ovl_config *config) config->xino = OVL_XINO_AUTO; break; + case OPT_COPY_UP_SHARED_ON: + config->copy_up_shared = true; + break; + + case OPT_COPY_UP_SHARED_OFF: + config->copy_up_shared = false; + break; + default: pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); return -EINVAL; @@ -1380,6 +1401,7 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) ofs->config.index = ovl_index_def; ofs->config.nfs_export = ovl_nfs_export_def; ofs->config.xino = ovl_xino_def(); + ofs->config.copy_up_shared = ovl_copy_up_shared_def; err = ovl_parse_opt((char *) data, &ofs->config); if (err) goto out_err; -- 2.14.3