Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2319743rwl; Fri, 6 Jan 2023 05:02:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXvZEB7BSJWQOd3mlJSTQyZQ05Rp3scdYawKt2wPC/HKTzmGU0G54Mwrf2cgOSlFsJMqHjLC X-Received: by 2002:a17:906:6dce:b0:7c3:e3ef:bca1 with SMTP id j14-20020a1709066dce00b007c3e3efbca1mr45609383ejt.60.1673010129396; Fri, 06 Jan 2023 05:02:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673010129; cv=none; d=google.com; s=arc-20160816; b=MMWq+pV54ZZkWcHUkaZLvlbptV4ky4fRIu7/s8Ma+RZK9iczTLADttbWe3MPD7rAkd GWWx+b+4xVfyd+H1IKEub7btFFbl7fLYUheySmiYeTBQSjm/t9Xw2JblTM3lSCSrRqxM 88+1iPjhys3bsPtsdco+2B/S/4vK4egxmMSm+39XEd95Q+g5z+bim5AJwv4PsAgQ2pmq pU1I2SihP6Cx25555D5UnaLxDxrJYT0MyPj3mm9gX/FNDSNQ7mt9aXWIUpPx5wlyn0xw MmGIbMDD/Yd/hD8vEMav7iop21jfs6IYke2tsbM28F5zozyh0XxLkQ+rGo0Jv1897f5L gRBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=srHps/lUyKaEXgs7RIdre+N+YWkkeUNqcr0IA+cF1k4=; b=uSv5VTUEoqOa2H67YtNdpColgYaN0jXttaDUTixq1uBC6e25zW8kcT52kbKI9oHeUv 1LiClkNggVzfG4iGxRCaQa0tfratqEopiliecl3dIIJfKWMiZWdmxNeqNhnhkueaZVIU 3mc7U77BI3wwGqFo72H0wJ8gKCnRSS7wWpqUIS6MenW1pnXo8WCe2kOu7zXfQNBJH4Aj r2xRL+wGDAjtxPcekCXLAkJ4t3XZJ50zf+o5HEY7PBMSkc45/Jqc3bbUkN4u6T00EQNN gSslpMpx+CsCNy41PciFqrwhRpY/mZ7hZX4dOA4LR9KoKCobDnoxpkAY1CGXYxzGLzRu jp5g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gs40-20020a1709072d2800b007829f6fed9dsi1532061ejc.232.2023.01.06.05.01.56; Fri, 06 Jan 2023 05:02:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234252AbjAFMyQ (ORCPT + 54 others); Fri, 6 Jan 2023 07:54:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233796AbjAFMxl (ORCPT ); Fri, 6 Jan 2023 07:53:41 -0500 Received: from out199-3.us.a.mail.aliyun.com (out199-3.us.a.mail.aliyun.com [47.90.199.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5B6B736E8; Fri, 6 Jan 2023 04:53:39 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R421e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045170;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VZ-Iakh_1673009615; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VZ-Iakh_1673009615) by smtp.aliyun-inc.com; Fri, 06 Jan 2023 20:53:36 +0800 From: Jingbo Xu To: xiang@kernel.org, chao@kernel.org, linux-erofs@lists.ozlabs.org Cc: huyue2@coolpad.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [RFC PATCH 5/6] erofs: implement .mmap for page cache sharing Date: Fri, 6 Jan 2023 20:53:29 +0800 Message-Id: <20230106125330.55529-6-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230106125330.55529-1-jefflexu@linux.alibaba.com> References: <20230106125330.55529-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In mmap(2), replace vma->vm_file with the anonymous file associated with the blob, so that the vma will be linked to the address_space of the blob. One thing worth noting is that, we return error early in mmap(2) if users attempt to map beyond the file size. Normally filesystems won't restrict this in mmap(2). The checking is done in the fault handler, and SIGBUS will be signaled to users if they actually attempt to access the area beyond the end of the file. However since vma->vm_file has been changed to the anonymous file in mmap(2), we can no way derive the file size of the original file. As file size is immutable in ro filesystem, let's fail early in mmap(2) in this case. Signed-off-by: Jingbo Xu --- fs/erofs/fscache.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c index 1f2a42dd1590..98341d4d9c0d 100644 --- a/fs/erofs/fscache.c +++ b/fs/erofs/fscache.c @@ -379,9 +379,46 @@ static ssize_t erofs_fscache_share_file_read_iter(struct kiocb *iocb, return already_read ? already_read : ret; } +static const struct vm_operations_struct erofs_fscache_share_file_vm_ops = { + .fault = filemap_fault, +}; + +static int erofs_fscache_share_file_mmap(struct file *file, + struct vm_area_struct *vma) +{ + struct inode *inode = file_inode(file); + /* since page cache sharing is enabled only when i_size <= chunk_size */ + struct erofs_map_blocks map = {}; /* .m_la = 0 */ + struct erofs_map_dev mdev; + int ret; + + if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) + return -EINVAL; + + ret = erofs_map_blocks(inode, &map, EROFS_GET_BLOCKS_RAW); + if (ret) + return ret; + + mdev = (struct erofs_map_dev) { + .m_deviceid = map.m_deviceid, + .m_pa = map.m_pa, + }; + ret = erofs_map_dev(inode->i_sb, &mdev); + if (ret) + return ret; + + vma_set_file(vma, mdev.m_fscache->file); + vma->vm_pgoff = (mdev.m_pa >> PAGE_SHIFT) + vma->vm_pgoff; + vma->vm_ops = &erofs_fscache_share_file_vm_ops; + + file_accessed(file); + return 0; +} + const struct file_operations erofs_fscache_share_file_fops = { .llseek = generic_file_llseek, .read_iter = erofs_fscache_share_file_read_iter, + .mmap = erofs_fscache_share_file_mmap, }; static void erofs_fscache_domain_put(struct erofs_domain *domain) -- 2.19.1.6.gb485710b