From patchwork Fri Feb 3 03:01:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingbo Xu X-Patchwork-Id: 52275 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp620131wrn; Thu, 2 Feb 2023 19:32:13 -0800 (PST) X-Google-Smtp-Source: AK7set+UJGhnfDzXaPaO3VrbRZ6wCWadGh5dkYpR6STGeUcc+DhtN/hV8cJNvWFg3Zj1HQkA8rFy X-Received: by 2002:a17:906:745:b0:88c:d0b3:27d7 with SMTP id z5-20020a170906074500b0088cd0b327d7mr8943671ejb.24.1675395133282; Thu, 02 Feb 2023 19:32:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675395133; cv=none; d=google.com; s=arc-20160816; b=z1Y/ceGm9J6MwiM5LRaFfYlRzBNAcj9g4g6BlrPWmjjxyCH+Ikiq/9AbCKDHktZNrf lEWFsCXxIrLAXuMGM6J6fjJORYka/7yuEZyqle0/YC2JUdwYEe6qWHVW83dwCfX0z30/ pwNy2GOTJWylj4QrNpJuv1GcB3q9/i968R+86P+6Ddiovln98l4d5aEGbEngwd7tuwWm RXtEEG56TIIQMLaTmKfUfnwTDmERaTR+bmotE7PF95jrSYY8bjhusJ0YNT39Q7SoSnb+ dLvJFkx63oavowQhn3R5eEUS+cXEOsy92JkI8NiQYBqjtS8+wtYwLOvRxlcHnNPsdNbi Bn8g== 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=lMXqgAMyyA0qmwhhIEK+X014z1qqjdz51uMOp6xkE98=; b=KzkeYDa0k3fPngIUvCYCgZUjYxjlRR/9dt76izql6IPN1DO76yS4A5KvlJV4TsJ8uz OYVK5Qgpby92MqOtHkkLJgCI7ClYhGZ+XbySVQ3NIT0ALEI07N17tnMvvG2XfCSoRCSB M113R2HNpPHvIW2eDKldULotu3vco6FWLrXoF8c/iwuF6wv1J3NzUZNQaDma/A0JHZTI Db4xz7BdUj9ZzDYnv6Wh26pX4gJyc3wJsqK/wmoiymahrUwYCD7l/fM85JEUptKr3d0T 0hm/3ZDIUG7OgxDbFxsuNAOWsHV5BH02bOXHifPzjriggTLoqh2SPmtyGNi7mi4XjwOf gwBw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 9-20020a170906020900b0087bda7137f9si1926061ejd.339.2023.02.02.19.31.48; Thu, 02 Feb 2023 19:32:13 -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 S232421AbjBCDCb (ORCPT + 99 others); Thu, 2 Feb 2023 22:02:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232182AbjBCDCL (ORCPT ); Thu, 2 Feb 2023 22:02:11 -0500 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C78083929E; Thu, 2 Feb 2023 19:01:52 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=jefflexu@linux.alibaba.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---0Vamlo-T_1675393309; Received: from localhost(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0Vamlo-T_1675393309) by smtp.aliyun-inc.com; Fri, 03 Feb 2023 11:01:50 +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 v3 7/9] erofs: implement .mmap for page cache sharing Date: Fri, 3 Feb 2023 11:01:41 +0800 Message-Id: <20230203030143.73105-8-jefflexu@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230203030143.73105-1-jefflexu@linux.alibaba.com> References: <20230203030143.73105-1-jefflexu@linux.alibaba.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756779127383217021?= X-GMAIL-MSGID: =?utf-8?q?1756779127383217021?= 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 | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c index bdeb048b78b5..af6ba52bbe8b 100644 --- a/fs/erofs/fscache.c +++ b/fs/erofs/fscache.c @@ -432,9 +432,36 @@ static ssize_t erofs_fscache_share_file_read_iter(struct kiocb *iocb, return res; } +vm_fault_t erofs_fscache_share_fault(struct vm_fault *vmf) +{ + struct erofs_fscache_finfo *finfo = vmf->vma->vm_file->private_data; + + if (unlikely(vmf->pgoff >= 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_finfo *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, };