From patchwork Sat Aug 29 09:50:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengguang Xu X-Patchwork-Id: 11744321 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BAD9113B6 for ; Sat, 29 Aug 2020 09:52:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 886482075B for ; Sat, 29 Aug 2020 09:52:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mykernel.net header.i=cgxu519@mykernel.net header.b="f462YgeL" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 886482075B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mykernel.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 48E206B0006; Sat, 29 Aug 2020 05:52:01 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3F1306B0007; Sat, 29 Aug 2020 05:52:01 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21C396B0008; Sat, 29 Aug 2020 05:52:01 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0133.hostedemail.com [216.40.44.133]) by kanga.kvack.org (Postfix) with ESMTP id 068256B0006 for ; Sat, 29 Aug 2020 05:52:01 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B741412E5 for ; Sat, 29 Aug 2020 09:52:00 +0000 (UTC) X-FDA: 77203140000.06.can19_531514e2707d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 7A88010062C94 for ; Sat, 29 Aug 2020 09:52:00 +0000 (UTC) X-Spam-Summary: 30,2,0,3addae72f2cfc92a,d41d8cd98f00b204,cgxu519@mykernel.net,,RULES_HIT:41:69:355:379:541:800:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1513:1515:1516:1518:1521:1535:1543:1593:1594:1711:1730:1747:1777:1792:2194:2198:2199:2200:2393:2559:2562:2731:3138:3139:3140:3141:3142:3355:3865:3867:3868:3870:3871:3872:4250:4321:4605:5007:6120:6261:6653:8660:9592:10010:10400:11026:11473:11658:11914:12043:12291:12296:12297:12438:12555:12895:12986:13148:13230:14096:14097:14181:14721:21080:21324:21444:21451:21611:21627:21939:21987:21990:30003:30054:30070,0,RBL:163.53.93.243:@mykernel.net:.lbl8.mailshell.net-62.2.2.100 64.100.201.201;04y8zjkxwwryag9hd89zdsbzm4nsuop6dca19i3n94cgakecqd7grn3tukek1ys.cfuirgrongmagwmdwqyskk81ft4bgykis8z3rmjcbnmfyxz5r9kqdresrmtw5kf.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:1:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: can19_531514e2707d X-Filterd-Recvd-Size: 5986 Received: from sender2-op-o12.zoho.com.cn (sender2-op-o12.zoho.com.cn [163.53.93.243]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Sat, 29 Aug 2020 09:51:59 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; t=1598694691; cv=none; d=zoho.com.cn; s=zohoarc; b=e6IcCG4tFosqzZgL/24g7rTR8CwHArbFn8gqoyeP0usMaoVI6u6AceplXcn6hbW69hHYTCP4L1IzblceuMvXyosITHt8bWStg5jdPiODCObfFX5vScf/wRUodv5P9LVXq2BQ+P5pdYhuWGFGRHyW0s3yKwa2jRkxnEFsqzxXqmo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com.cn; s=zohoarc; t=1598694691; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=ccEWT3RLuwiPMu/BBIgZh5/ZgyfZBB6tStaJtH+L+JY=; b=LyzBXJnBuVVu6950HiaP7Fwrfz5g4BSeqm4kwWZGEa02R+JuXLeSpYdNh90xhJmcsLqMHNeK+VVG3mQtz9x8ZWlBbn++dEft84k6cUUWVmbddciR+BeIk2cVFAMm5Ca79aGeKIQGFhkjneKuNjIkA3AzbtpSVfXxhwmjnYWIZ+k= ARC-Authentication-Results: i=1; mx.zoho.com.cn; dkim=pass header.i=mykernel.net; spf=pass smtp.mailfrom=cgxu519@mykernel.net; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1598694691; s=zohomail; d=mykernel.net; i=cgxu519@mykernel.net; h=From:To:Cc:Message-ID:Subject:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type; bh=ccEWT3RLuwiPMu/BBIgZh5/ZgyfZBB6tStaJtH+L+JY=; b=f462YgeLswSRXtYIq+DvgwbLsEFk5nGTD1bHb+XueyiqlOt1/vAUH/qKwya3brpu 0UF3EModFvQfCwiHAU1nO9n9rnEyxIYz9xWBoNhxRpnMCvdtd+djaZK6y+x2ve6i+SC Mdx1wvDvj8FNOF0UwZi0X/QNCrla4UaH9o2L23AA= Received: from localhost.localdomain (116.30.194.36 [116.30.194.36]) by mx.zoho.com.cn with SMTPS id 1598694689422660.411788621191; Sat, 29 Aug 2020 17:51:29 +0800 (CST) From: Chengguang Xu To: linux-unionfs@vger.kernel.org, linux-mm@kvack.org Cc: miklos@szeredi.hu, akpm@linux-foundation.org, amir73il@gmail.com, riteshh@linux.ibm.com, Chengguang Xu Message-ID: <20200829095101.25350-2-cgxu519@mykernel.net> Subject: [RFC PATCH 1/3] mm: mmap: export necessary functions for overlayfs' mmap Date: Sat, 29 Aug 2020 17:50:59 +0800 X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200829095101.25350-1-cgxu519@mykernel.net> References: <20200829095101.25350-1-cgxu519@mykernel.net> MIME-Version: 1.0 X-ZohoCNMailClient: External X-Rspamd-Queue-Id: 7A88010062C94 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In shared mode mmap, if overlayfs' inode does not have data in upper layer, it should call maybe_unlock_mmap_for_io() to release lock and waiting for IO in ->fault handler. Meanwhile, in order to avoid endless retry we should also check flag FAULT_FLAG_TRIED carefully in ->fault handler. Signed-off-by: Chengguang Xu --- include/linux/mm.h | 2 ++ mm/filemap.c | 28 ++++++++++++++++++++++++++++ mm/internal.h | 22 ---------------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index dc7b87310c10..214b23734eed 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1656,6 +1656,8 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, pgoff_t nr, bool even_cows); void unmap_mapping_range(struct address_space *mapping, loff_t const holebegin, loff_t const holelen, int even_cows); +struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, struct file *fpin); +bool fault_flag_check(struct vm_fault *vmf, unsigned int flag); #else static inline vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address, unsigned int flags) diff --git a/mm/filemap.c b/mm/filemap.c index f0ae9a6308cb..8a226f8ca262 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2704,6 +2704,34 @@ int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) return -EINVAL; return generic_file_mmap(file, vma); } + +struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, + struct file *fpin) +{ + int flags = vmf->flags; + + if (fpin) + return fpin; + + /* + * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or + * anything, so we only pin the file and drop the mmap_lock if only + * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. + */ + if (fault_flag_allow_retry_first(flags) && + !(flags & FAULT_FLAG_RETRY_NOWAIT)) { + fpin = get_file(vmf->vma->vm_file); + mmap_read_unlock(vmf->vma->vm_mm); + } + return fpin; +} +EXPORT_SYMBOL(maybe_unlock_mmap_for_io); + +bool fault_flag_check(struct vm_fault *vmf, unsigned int flag) +{ + return vmf->flags & flag; +} +EXPORT_SYMBOL(fault_flag_check); #else vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf) { diff --git a/mm/internal.h b/mm/internal.h index 9886db20d94f..ef19235c6bf1 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -402,28 +402,6 @@ vma_address(struct page *page, struct vm_area_struct *vma) return max(start, vma->vm_start); } - -static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf, - struct file *fpin) -{ - int flags = vmf->flags; - - if (fpin) - return fpin; - - /* - * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or - * anything, so we only pin the file and drop the mmap_lock if only - * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt. - */ - if (fault_flag_allow_retry_first(flags) && - !(flags & FAULT_FLAG_RETRY_NOWAIT)) { - fpin = get_file(vmf->vma->vm_file); - mmap_read_unlock(vmf->vma->vm_mm); - } - return fpin; -} - #else /* !CONFIG_MMU */ static inline void clear_page_mlock(struct page *page) { } static inline void mlock_vma_page(struct page *page) { } From patchwork Sat Aug 29 09:51:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengguang Xu X-Patchwork-Id: 11744319 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 70634913 for ; Sat, 29 Aug 2020 09:52:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3E4B92075B for ; Sat, 29 Aug 2020 09:52:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mykernel.net header.i=cgxu519@mykernel.net header.b="VdCYBUiH" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3E4B92075B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mykernel.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5C2A26B0005; Sat, 29 Aug 2020 05:52:00 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 54A736B0006; Sat, 29 Aug 2020 05:52:00 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 412AB6B0007; Sat, 29 Aug 2020 05:52:00 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0146.hostedemail.com [216.40.44.146]) by kanga.kvack.org (Postfix) with ESMTP id 2B3696B0005 for ; Sat, 29 Aug 2020 05:52:00 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id EC9C112FB for ; Sat, 29 Aug 2020 09:51:59 +0000 (UTC) X-FDA: 77203139958.08.earth43_37014c82707d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id CCDF21819E626 for ; Sat, 29 Aug 2020 09:51:59 +0000 (UTC) X-Spam-Summary: 1,0,0,396d957ed67b15ac,d41d8cd98f00b204,cgxu519@mykernel.net,,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1513:1515:1516:1518:1521:1535:1543:1593:1594:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:3138:3139:3140:3141:3142:3355:3867:3868:4117:4321:4385:4605:5007:6120:6261:6653:7903:9592:10004:10400:11026:11658:11914:12043:12291:12296:12297:12438:12555:12683:12895:12986:14096:14097:14110:14181:14721:21080:21444:21451:21627:21796:21987:21990:30036:30054,0,RBL:163.53.93.243:@mykernel.net:.lbl8.mailshell.net-62.2.2.100 64.100.201.201;04yrs3e6z3r3opos3m9cc4arbk8u3ypphyz7c3nitimrqbxh471unds8t84tyyp.cmeg7g3mjh1ihdi4epnpr1ydnispyetohtzpjiy1h6tbi48hroq7ibsws4cr9uh.w-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:282,LUA_SUMMARY:none X-HE-Tag: earth43_37014c82707d X-Filterd-Recvd-Size: 6219 Received: from sender2-op-o12.zoho.com.cn (sender2-op-o12.zoho.com.cn [163.53.93.243]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Sat, 29 Aug 2020 09:51:57 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; t=1598694693; cv=none; d=zoho.com.cn; s=zohoarc; b=YBE7Pv1efxlf1AAxaIoD3yTwlut8GbiWLhItLSzb75VKosrMcu8E+zxtkQCO0o1GEr1wmARPmUzNbnLMqnVp2IDVcWb3f1aN2VGkzSpXNEmZqXftIwZ3ObDt7rADXjMu0YjgijZDPZkB9pQGk625k7kft4O51oOzJOtXJuvpo+o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com.cn; s=zohoarc; t=1598694693; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=/mgZ6omCjLzte5zMdd9RURwLPDtaIIQG4E0QLcHBYCk=; b=YQfHeJhqjNXXYa/tyIjUkhnXypDPjyK6xn5+H0Rmh5tDYpJ1hufAtjxLCAkiaRTqKIxiR7PDzUiUllGO7fOL3wTouJje6JBMmq3l1be3nHee6+AhMipxMPeiw3cFYRtIuPLBMl8wd6NMT0gu0BDhS4/bBd5lC2UFjJAwQCnQi4k= ARC-Authentication-Results: i=1; mx.zoho.com.cn; dkim=pass header.i=mykernel.net; spf=pass smtp.mailfrom=cgxu519@mykernel.net; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1598694693; s=zohomail; d=mykernel.net; i=cgxu519@mykernel.net; h=From:To:Cc:Message-ID:Subject:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type; bh=/mgZ6omCjLzte5zMdd9RURwLPDtaIIQG4E0QLcHBYCk=; b=VdCYBUiH8r7HhyaUv/Lu4nc8lsMH0dwYTU1Ps2qELaHkJCW1mF+ZnH5Jm8++hkKX 7Fp6O3tLZAFn5ZU0QJQyGMuAlPj+VFC5f/DJH+57pksWtaPu4++RxzKyIyMJ/k9L2Iy 28ZebKeyHWHgB5KPswewZ3S+Md5P1VQdcV7uK2nA= Received: from localhost.localdomain (116.30.194.36 [116.30.194.36]) by mx.zoho.com.cn with SMTPS id 1598694691937238.97463573795892; Sat, 29 Aug 2020 17:51:31 +0800 (CST) From: Chengguang Xu To: linux-unionfs@vger.kernel.org, linux-mm@kvack.org Cc: miklos@szeredi.hu, akpm@linux-foundation.org, amir73il@gmail.com, riteshh@linux.ibm.com, Chengguang Xu Message-ID: <20200829095101.25350-3-cgxu519@mykernel.net> Subject: [RFC PATCH 2/3] ovl: introduce struct ovl_file_entry Date: Sat, 29 Aug 2020 17:51:00 +0800 X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200829095101.25350-1-cgxu519@mykernel.net> References: <20200829095101.25350-1-cgxu519@mykernel.net> MIME-Version: 1.0 X-ZohoCNMailClient: External X-Rspamd-Queue-Id: CCDF21819E626 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce new struct ovl_file_entry to store real file and real vm_ops handler. Signed-off-by: Chengguang Xu --- fs/overlayfs/file.c | 64 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 0d940e29d62b..14ab5344a918 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -21,6 +21,39 @@ struct ovl_aio_req { struct fd fd; }; +struct ovl_file_entry { + struct file *realfile; + void *vm_ops; +}; + +struct file *ovl_get_realfile(struct file *file) +{ + struct ovl_file_entry *ofe = file->private_data; + + return ofe->realfile; +} + +void ovl_set_realfile(struct file *file, struct file *realfile) +{ + struct ovl_file_entry *ofe = file->private_data; + + ofe->realfile = realfile; +} + +void *ovl_get_real_vmops(struct file *file) +{ + struct ovl_file_entry *ofe = file->private_data; + + return ofe->vm_ops; +} + +void ovl_set_real_vmops(struct file *file, void *vm_ops) +{ + struct ovl_file_entry *ofe = file->private_data; + + ofe->vm_ops = vm_ops; +} + static struct kmem_cache *ovl_aio_request_cachep; static char ovl_whatisit(struct inode *inode, struct inode *realinode) @@ -105,14 +138,14 @@ static int ovl_change_flags(struct file *file, unsigned int flags) return 0; } -static int ovl_real_fdget_meta(const struct file *file, struct fd *real, +static int ovl_real_fdget_meta(struct file *file, struct fd *real, bool allow_meta) { struct inode *inode = file_inode(file); struct inode *realinode; real->flags = 0; - real->file = file->private_data; + real->file = ovl_get_realfile(file); if (allow_meta) realinode = ovl_inode_real(inode); @@ -134,7 +167,7 @@ static int ovl_real_fdget_meta(const struct file *file, struct fd *real, return 0; } -static int ovl_real_fdget(const struct file *file, struct fd *real) +static int ovl_real_fdget(struct file *file, struct fd *real) { return ovl_real_fdget_meta(file, real, false); } @@ -144,25 +177,36 @@ static int ovl_open(struct inode *inode, struct file *file) struct file *realfile; int err; + file->private_data = kzalloc(sizeof(struct ovl_file_entry), GFP_KERNEL); + if (!file->private_data) + return -ENOMEM; + err = ovl_maybe_copy_up(file_dentry(file), file->f_flags); if (err) - return err; + goto out; /* No longer need these flags, so don't pass them on to underlying fs */ file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); realfile = ovl_open_realfile(file, ovl_inode_realdata(inode)); - if (IS_ERR(realfile)) - return PTR_ERR(realfile); - - file->private_data = realfile; + if (IS_ERR(realfile)) { + err = PTR_ERR(realfile); + goto out; + } + ovl_set_realfile(file, realfile); return 0; +out: + kfree(file->private_data); + file->private_data = NULL; + return err; } static int ovl_release(struct inode *inode, struct file *file) { - fput(file->private_data); + fput(ovl_get_realfile(file)); + kfree(file->private_data); + file->private_data = NULL; return 0; } @@ -451,7 +495,7 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) static int ovl_mmap(struct file *file, struct vm_area_struct *vma) { - struct file *realfile = file->private_data; + struct file *realfile = ovl_get_realfile(file); const struct cred *old_cred; int ret; From patchwork Sat Aug 29 09:51:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengguang Xu X-Patchwork-Id: 11744323 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E0F013B6 for ; Sat, 29 Aug 2020 09:52:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3DFC22075B for ; Sat, 29 Aug 2020 09:52:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=mykernel.net header.i=cgxu519@mykernel.net header.b="GYosXaKl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3DFC22075B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mykernel.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7967A6B0007; Sat, 29 Aug 2020 05:52:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 71E956B0008; Sat, 29 Aug 2020 05:52:13 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60DDB6B000A; Sat, 29 Aug 2020 05:52:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0228.hostedemail.com [216.40.44.228]) by kanga.kvack.org (Postfix) with ESMTP id 4B3546B0007 for ; Sat, 29 Aug 2020 05:52:13 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 02ED3181AEF1A for ; Sat, 29 Aug 2020 09:52:13 +0000 (UTC) X-FDA: 77203140546.03.love93_40171442707d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id C8C8528A4E9 for ; Sat, 29 Aug 2020 09:52:12 +0000 (UTC) X-Spam-Summary: 1,0,0,bd983c4bcaa4a087,d41d8cd98f00b204,cgxu519@mykernel.net,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1437:1513:1515:1516:1518:1521:1535:1544:1593:1594:1605:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3867:4118:4321:5007:6119:6120:6261:6642:6653:7903:9592:10004:11026:11473:11658:11914:12043:12291:12296:12297:12438:12555:12663:12683:12895:12986:14096:14097:14110:14181:14721:21080:21444:21451:21611:21627:21990:30054,0,RBL:163.53.93.243:@mykernel.net:.lbl8.mailshell.net-62.2.2.100 64.100.201.201;04ygm1udkyx44griemknzg7agxn7qopx8wor98u3muakw5fia7g1dgtxs7mckud.cwikfdtjdjr8u5aax8sxhai3fbwp7ywug7eb179dincg3u7nm9pixi845w3hwaz.a-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:22,LUA_SUMMARY:none X-HE-Tag: love93_40171442707d X-Filterd-Recvd-Size: 7789 Received: from sender2-op-o12.zoho.com.cn (sender2-op-o12.zoho.com.cn [163.53.93.243]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Sat, 29 Aug 2020 09:52:10 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; t=1598694696; cv=none; d=zoho.com.cn; s=zohoarc; b=fyRT+djDryfSE/6F0Efu1feLw+nHWukrW/zuhcD7eFm1a893S3BOWn1nszwx+9oJDsBGqY62gEzTWPeZx0/KRoIpvUhg35Db5JhXA1fkoe5Yol3INpe3SBhagvNNol9qz74a7iWaC6cQj39dl19aDAwfyvDJOWmjj4umpc/hhoU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com.cn; s=zohoarc; t=1598694696; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=ObZqteZmBQYgsDj6DtN0x+jII/ZXa3ZgcjVA/akvIO4=; b=ORZk9SmyGVtUk62QrrdZQFN7RD7D3XvuJyALOdgkWYLFRXYJ0l6IqijUnQlGqLoW+yRRsCVrLNy030oVg0ap3wVS5M9Q82aluIAC0LCK63jnwd0vBZT4SGyoqtfLpOtxCrwMz4wox3rcqfHmfLPDzu3dMTIbjWBdup05usD9ggc= ARC-Authentication-Results: i=1; mx.zoho.com.cn; dkim=pass header.i=mykernel.net; spf=pass smtp.mailfrom=cgxu519@mykernel.net; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1598694696; s=zohomail; d=mykernel.net; i=cgxu519@mykernel.net; h=From:To:Cc:Message-ID:Subject:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type; bh=ObZqteZmBQYgsDj6DtN0x+jII/ZXa3ZgcjVA/akvIO4=; b=GYosXaKltMEFZfeEXC/NEjJNlCOrEp7lZsf5MunZz8JVw2KCe0hHF+A0KomLuB8S YqyJqpT17r7wcqavEx3sfTGjHkUWqsVtsBQcDLAYERVHj/RJlaergT86Fm2GvTXrrkq onqWnvkDCVF3exTL71pIXlrNv9K3azGZxOPiyMmc= Received: from localhost.localdomain (116.30.194.36 [116.30.194.36]) by mx.zoho.com.cn with SMTPS id 1598694693783332.6777969196331; Sat, 29 Aug 2020 17:51:33 +0800 (CST) From: Chengguang Xu To: linux-unionfs@vger.kernel.org, linux-mm@kvack.org Cc: miklos@szeredi.hu, akpm@linux-foundation.org, amir73il@gmail.com, riteshh@linux.ibm.com, Chengguang Xu Message-ID: <20200829095101.25350-4-cgxu519@mykernel.net> Subject: [RFC PATCH 3/3] ovl: implement stacked mmap for shared map Date: Sat, 29 Aug 2020 17:51:01 +0800 X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200829095101.25350-1-cgxu519@mykernel.net> References: <20200829095101.25350-1-cgxu519@mykernel.net> MIME-Version: 1.0 X-ZohoCNMailClient: External X-Rspamd-Queue-Id: C8C8528A4E9 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Implement stacked mmap for shared map to keep data consistency. Signed-off-by: Chengguang Xu --- fs/overlayfs/file.c | 120 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 6 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 14ab5344a918..db5ab200d984 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -21,9 +21,17 @@ struct ovl_aio_req { struct fd fd; }; +static vm_fault_t ovl_fault(struct vm_fault *vmf); +static vm_fault_t ovl_page_mkwrite(struct vm_fault *vmf); + +static const struct vm_operations_struct ovl_vm_ops = { + .fault = ovl_fault, + .page_mkwrite = ovl_page_mkwrite, +}; + struct ovl_file_entry { struct file *realfile; - void *vm_ops; + const struct vm_operations_struct *vm_ops; }; struct file *ovl_get_realfile(struct file *file) @@ -40,14 +48,15 @@ void ovl_set_realfile(struct file *file, struct file *realfile) ofe->realfile = realfile; } -void *ovl_get_real_vmops(struct file *file) +const struct vm_operations_struct *ovl_get_real_vmops(struct file *file) { struct ovl_file_entry *ofe = file->private_data; return ofe->vm_ops; } -void ovl_set_real_vmops(struct file *file, void *vm_ops) +void ovl_set_real_vmops(struct file *file, + const struct vm_operations_struct *vm_ops) { struct ovl_file_entry *ofe = file->private_data; @@ -493,11 +502,104 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) return ret; } +vm_fault_t ovl_fault(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; + struct file *realfile; + struct file *fpin, *tmp; + struct inode *inode = file_inode(file); + struct inode *realinode; + const struct cred *old_cred; + bool retry_allowed; + vm_fault_t ret; + int err = 0; + + if (fault_flag_check(vmf, FAULT_FLAG_TRIED)) { + realfile = ovl_get_realfile(file); + + if (!ovl_has_upperdata(inode) || + realfile->f_inode != ovl_inode_upper(inode) || + !realfile->f_op->mmap) + return VM_FAULT_SIGBUS; + + if (!ovl_get_real_vmops(file)) { + old_cred = ovl_override_creds(inode->i_sb); + err = call_mmap(realfile, vma); + revert_creds(old_cred); + + vma->vm_file = file; + if (err) { + vma->vm_ops = &ovl_vm_ops; + return VM_FAULT_SIGBUS; + } + ovl_set_real_vmops(file, vma->vm_ops); + vma->vm_ops = &ovl_vm_ops; + } + + retry_allowed = fault_flag_check(vmf, FAULT_FLAG_ALLOW_RETRY); + if (retry_allowed) + vma->vm_flags &= ~FAULT_FLAG_ALLOW_RETRY; + vma->vm_file = realfile; + ret = ovl_get_real_vmops(file)->fault(vmf); + vma->vm_file = file; + if (retry_allowed) + vma->vm_flags |= FAULT_FLAG_ALLOW_RETRY; + return ret; + + } else { + fpin = maybe_unlock_mmap_for_io(vmf, NULL); + if (!fpin) + return VM_FAULT_SIGBUS; + + ret = VM_FAULT_RETRY; + if (!ovl_has_upperdata(inode)) { + err = ovl_copy_up_with_data(file->f_path.dentry); + if (err) + goto out; + } + + realinode = ovl_inode_realdata(inode); + realfile = ovl_open_realfile(file, realinode); + if (IS_ERR(realfile)) + goto out; + + tmp = ovl_get_realfile(file); + ovl_set_realfile(file, realfile); + fput(tmp); + +out: + fput(fpin); + return ret; + } +} + +static vm_fault_t ovl_page_mkwrite(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; + struct file *realfile; + struct inode *inode = file_inode(file); + vm_fault_t ret; + + realfile = ovl_get_realfile(file); + + sb_start_pagefault(inode->i_sb); + file_update_time(file); + + vma->vm_file = realfile; + ret = ovl_get_real_vmops(file)->page_mkwrite(vmf); + vma->vm_file = file; + + sb_end_pagefault(inode->i_sb); + return ret; +} + static int ovl_mmap(struct file *file, struct vm_area_struct *vma) { struct file *realfile = ovl_get_realfile(file); const struct cred *old_cred; - int ret; + int ret = 0; if (!realfile->f_op->mmap) return -ENODEV; @@ -505,6 +607,13 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) if (WARN_ON(file != vma->vm_file)) return -EIO; + if (!ovl_has_upperdata(file_inode(file)) && + (vma->vm_flags & (VM_SHARED|VM_MAYSHARE))) { + vma->vm_ops = &ovl_vm_ops; + ovl_file_accessed(file); + return 0; + } + vma->vm_file = get_file(realfile); old_cred = ovl_override_creds(file_inode(file)->i_sb); @@ -517,10 +626,9 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) } else { /* Drop reference count from previous vm_file value */ fput(file); + ovl_file_accessed(file); } - ovl_file_accessed(file); - return ret; }