Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3468125imm; Tue, 29 May 2018 07:46:25 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo6KX4YvPFrgiuh2wNkoFbM6LuOeM3CENxU7TzwPCjCd3oJaI3Wr25S0ld/EIjvUh4BtskC X-Received: by 2002:a17:902:8d85:: with SMTP id v5-v6mr17935379plo.93.1527605185922; Tue, 29 May 2018 07:46:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527605185; cv=none; d=google.com; s=arc-20160816; b=okrXq3FLZsiRz5bwx/J3wOnQ3wZsxc1qfJzC60CqItUV9fhcqZTjK6VGNOnhw1YDzY hFiMvnIZwi039319zc8YgeAJ+Wf73ppqEeUHxD7IMKnPdIzfoc8Ezr2b+szWrN5cQMUW c1VFWIbsEbpJgCHvKMo0IFm0NqAbkY6ihPrmu7wbgO6BLBaQ/OP+PNSumf+pzzZhjsWo 3TfSuu6cKYbqnjYYkR2tR2/JQf7rweslGIohW4iLUMq18DbXKoBJnEdmumYS/YYKfzqr Ir/dxkuc/EAXkh0/n0jPMSQxiOYrvVDN6HSDsTaSHujUdUtNpasIrOpaOJpV67S4og1n MVwA== 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=7nbADk4iFltv/SJdZron98ONwrOcW3M1LnSvJufu3yk=; b=b4JtD1IvNPpgKniH23Z1+Kl+5SV+UodQOKhZNxMLfGdivGxVg9HWzpJ3tjTPrKb7B0 FIBuvpfU7N7gO3CGjYz8yTuWIMNcu3Lhx8v/ndx0jTED7s/+WXdte/gMbdW1XiJVuEGu QHpdUYsnD+6X2X/jHvHep3nYyeRr7XnKDPhb7BLXtE5qpQ4i0IxP58Ulcagoq6HWYHXH twBtaeukJeeHrigOk1U0KHmY2oPyxUyHc2xhneK0k/34Frl21QBGxiY1x3NeXsR0MfAq YqhK3MaMDJuRZ5W+JnxxvbEF3pCYekpM/C6P2n4SODs/8guZPrwcoMe/E+msG2QDq8Wd ui+w== 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 t128-v6si26091251pgt.368.2018.05.29.07.46.11; Tue, 29 May 2018 07:46:25 -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 S936519AbeE2OpD (ORCPT + 99 others); Tue, 29 May 2018 10:45:03 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:42085 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935745AbeE2OoN (ORCPT ); Tue, 29 May 2018 10:44:13 -0400 Received: by mail-wr0-f196.google.com with SMTP id w10-v6so25990267wrk.9 for ; Tue, 29 May 2018 07:44:13 -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=7nbADk4iFltv/SJdZron98ONwrOcW3M1LnSvJufu3yk=; b=sv5Bv/7c9fEGXgxXlBkTOqD/qpKPdDp1AraZNHY5x2FLnnr7pbBENplPh86qEHO5uu Aa0DOsnbrkAEiT6HqZCG1TO5MFgUPsbARiclbmEwQPA2nWyd5wYiMrg09TXU49NDJGnA wLwdi0K1lsfQx/6PNzcwLy0ky+Nug0RLTTMnge1fLz4SObU2eeSLLd0NlLH4M7qnpWsp QrbCbd0iprBKhnWQ4N7oj8SEGVMhcnnZUmpuDBSRHXuCSWnt9n4sJf/kwf4dZz7gm4aI Tg+54hbiD+Oj74g81fIBBB0szy+AlDEJulyZh9cHPpdsCqBvuabmUmupN20dp0vu+uWn Qy1w== X-Gm-Message-State: ALKqPwftJ8sS2l2bZIbDoX7cZn1uQt0e0RzPLx1GHX0/RQbzC4HEzW4A pZM8D+JY/SauwSUMjYZRfPVD9ILFuug= X-Received: by 2002:adf:e687:: with SMTP id r7-v6mr7289525wrm.14.1527605052309; Tue, 29 May 2018 07:44:12 -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 t198-v6sm18834422wmt.23.2018.05.29.07.44.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 29 May 2018 07:44:11 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 26/39] ovl: copy-up on MAP_SHARED Date: Tue, 29 May 2018 16:43:26 +0200 Message-Id: <20180529144339.16538-27-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180529144339.16538-1-mszeredi@redhat.com> References: <20180529144339.16538-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..5d1d40d745c5 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 ef4bcc80572f..266692ce9a9a 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "overlayfs.h" @@ -259,6 +260,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; @@ -476,6 +497,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 3f6e39a2f51e..be4f1664f662 100644 --- a/fs/overlayfs/overlayfs.h +++ b/fs/overlayfs/overlayfs.h @@ -266,6 +266,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 211975921a90..900ed4c39919 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; @@ -1379,6 +1400,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