Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp9080557rwl; Wed, 11 Jan 2023 00:55:24 -0800 (PST) X-Google-Smtp-Source: AMrXdXv4iy978WtX3Ky510aV1VUkEdXOSn3rdlcY8QVyuFa8hG9TcqXAL0ia0Urmf6p8oix7BXpe X-Received: by 2002:a17:906:656:b0:7ee:1596:4b6 with SMTP id t22-20020a170906065600b007ee159604b6mr59335576ejb.59.1673427323865; Wed, 11 Jan 2023 00:55:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673427323; cv=none; d=google.com; s=arc-20160816; b=IhxrPM67yFAnqoA5sqfY5c9a5V4cT0l8/B6sLeY9WD5ASpgXtHGppUezipfh0G725B eeQm8Pq14ZdpSDLXwpqM+MjNDEv+niY1OZHIQ7jxS65C0FuPbCRPD5IT/Px8a05k9yWq kXH/BC0S79lTd7qRBgDMQgDMfDhVcYPHCf+kv43A5e8+yLNT4rB7y/OQxhk+8KKJ3+XU YpAFHZeaCb5nAScTAogig6cWrCRk6Nx21Z3am/tNnopVhglGfQ5Fvre0wHU1pvwPaOkV ZnFki38GdnbYXCz8jnfo+gPunMc8tTzKw94w+dkZAgq/PWPeaY1bfHzOQrv+qJ97cv4k JF+w== 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=4i8xNwCnt8JSLxAti17Ts6eJiDNGscxRZT7cx7D4KBY=; b=C3O1ekmW1VY7n9x46XVue98Hpd8MIs1kh4tKhrMiNiJWoVbIH4V8zMoAJ2YMomX5Tl xUzzAu572KZPx5uFI8m2r3R+BbgUdJNjjQzoNFuPRTjcFP/11H0j51jjIXJmPTd6aTTp 4ocMyV0ekWs6Yabtt+9M1SW6QB7IL+VYchKuI9zQBPAYyT3Mk+hoznM0Rie8rLfaADc6 Y8F5L3xI2O5oQyYOfqjcIg2zBoIr3OmKoWkUiN7cZI9w0E/G4zYzF+fVvtfUDzrgJJMG gTzIZlZbS2sVLKu5FzfNHTETjpXcFdHzHui8LplKGUTWaDuPbKLuC7lXLN5GC46AOy/2 EtXw== 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 xg3-20020a170907320300b007c158707538si13181705ejb.981.2023.01.11.00.55.11; Wed, 11 Jan 2023 00:55:23 -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 S236052AbjAKIcu (ORCPT + 53 others); Wed, 11 Jan 2023 03:32:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236015AbjAKIcP (ORCPT ); Wed, 11 Jan 2023 03:32:15 -0500 Received: from out30-7.freemail.mail.aliyun.com (out30-7.freemail.mail.aliyun.com [115.124.30.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33C353897; Wed, 11 Jan 2023 00:32:08 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R811e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046056;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0VZMek7P_1673425923; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0VZMek7P_1673425923) by smtp.aliyun-inc.com; Wed, 11 Jan 2023 16:32:04 +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: [PATCH v2 5/7] erofs: implement .mmap for page cache sharing Date: Wed, 11 Jan 2023 16:31:56 +0800 Message-Id: <20230111083158.23462-6-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230111083158.23462-1-jefflexu@linux.alibaba.com> References: <20230111083158.23462-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_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 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c index 4075d9519a7d..e3a65041c9b8 100644 --- a/fs/erofs/fscache.c +++ b/fs/erofs/fscache.c @@ -431,9 +431,40 @@ static ssize_t erofs_fscache_share_file_read_iter(struct kiocb *iocb, return already_read ? already_read : ret; } +vm_fault_t erofs_fscache_share_fault(struct vm_fault *vmf) +{ + struct file *realfile = vmf->vma->vm_file; + struct erofs_fscache_share_file_info *finfo = realfile->private_data; + pgoff_t index = vmf->pgoff; + + if (unlikely(index >= finfo->max_idx)) + return VM_FAULT_SIGBUS; + + return filemap_fault(vmf); +} + +static const struct vm_operations_struct erofs_fscache_share_file_vm_ops = { + .fault = erofs_fscache_share_fault, +}; + +static int erofs_fscache_share_file_mmap(struct file *file, + struct vm_area_struct *vma) +{ + struct file *realfile = file->private_data; + struct erofs_fscache_share_file_info *finfo = realfile->private_data; + + vma_set_file(vma, realfile); + vma->vm_pgoff = (finfo->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, .open = erofs_fscache_share_file_open, .release = erofs_fscache_share_file_release, }; -- 2.19.1.6.gb485710b