From patchwork Mon Jul 15 09:44:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pankaj Raghav (Samsung)" X-Patchwork-Id: 13733198 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74C15C3DA5D for ; Mon, 15 Jul 2024 09:45:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09A936B009B; Mon, 15 Jul 2024 05:45:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 048946B009C; Mon, 15 Jul 2024 05:45:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2CAD6B009D; Mon, 15 Jul 2024 05:45:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BE4416B009B for ; Mon, 15 Jul 2024 05:45:38 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 490BBA5258 for ; Mon, 15 Jul 2024 09:45:38 +0000 (UTC) X-FDA: 82341504756.04.89BD598 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by imf02.hostedemail.com (Postfix) with ESMTP id 842B88000C for ; Mon, 15 Jul 2024 09:45:36 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=DziKE2LF; dmarc=pass (policy=quarantine) header.from=pankajraghav.com; spf=pass (imf02.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721036695; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xbld/pizLMEJfrPYBqwQ81gjcSKIsZj5n7TDu8kDUY0=; b=KBJb8MzeiRHuFFictEm66eNn2t9CJUCjWuoKN9tfrDVvi8j9zGRz7s9lxklOuwUHVAgAc3 7l4qBKsor7SLkzGlUY7UeRRZTAlNXsz+sNPi7tuf/uSrku0N3yr2ewhW6mv3IQMmsHOL5/ 3Z1matd3tZrJULH/tX2djvG7N53gyaY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721036695; a=rsa-sha256; cv=none; b=th4o84CCCxKdkThUYUQmFgK6CnjxXIqAwOny9u6xEqPnAqf8iic63Vk7WjjziI0bWkI9F0 Ho6KGLzlkxb8nOxU8sB+T0xcy6MlbBupiE9X1Ua11SKErlbSZr72ifb2uVypsvOmYEH7OI onLhyZPvLDRcBUH9pd4/htvtaWLWXIU= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=pankajraghav.com header.s=MBO0001 header.b=DziKE2LF; dmarc=pass (policy=quarantine) header.from=pankajraghav.com; spf=pass (imf02.hostedemail.com: domain of kernel@pankajraghav.com designates 80.241.56.152 as permitted sender) smtp.mailfrom=kernel@pankajraghav.com Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4WMy4D6rpSz9slc; Mon, 15 Jul 2024 11:45:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pankajraghav.com; s=MBO0001; t=1721036733; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xbld/pizLMEJfrPYBqwQ81gjcSKIsZj5n7TDu8kDUY0=; b=DziKE2LFE+foxOGJVWPx7ePlNX35aSAPc4JT7wXBeonEpWgqtmj1s/uCtAHsolEfoAlwQY SFHmwH8TKmqeUWfMSvGVo1Z0aXhDPf8+DmViEFIO2v1b9HHxDDPPY3c8/PwyYU2RUGwKAH mkom7J2oa+9jxrEdMn5SXSewYicuV2NroLCHc+Pa2V3xv8RSElNGN2MOEZDnAJ4oIqYlRI mgWoCRcbeSUvnl3qbdNHoBCr/Ddebi04C9Qw4g/wOP3gXzvHsoP1IDR/qwFlJBa1/S71IX TJHSleYyo8MxFnpbaphpSvEblAhElowOxU8DcvzyXza3tjwqH9XWxmnUJioUwQ== From: "Pankaj Raghav (Samsung)" To: david@fromorbit.com, willy@infradead.org, chandan.babu@oracle.com, djwong@kernel.org, brauner@kernel.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, yang@os.amperecomputing.com, linux-mm@kvack.org, john.g.garry@oracle.com, linux-fsdevel@vger.kernel.org, hare@suse.de, p.raghav@samsung.com, mcgrof@kernel.org, gost.dev@samsung.com, cl@os.amperecomputing.com, linux-xfs@vger.kernel.org, kernel@pankajraghav.com, ryan.roberts@arm.com, hch@lst.de, Zi Yan Subject: [PATCH v10 05/10] filemap: cap PTE range to be created to allowed zero fill in folio_map_range() Date: Mon, 15 Jul 2024 11:44:52 +0200 Message-ID: <20240715094457.452836-6-kernel@pankajraghav.com> In-Reply-To: <20240715094457.452836-1-kernel@pankajraghav.com> References: <20240715094457.452836-1-kernel@pankajraghav.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 842B88000C X-Stat-Signature: xt9ibkc71id1bq4rk8wbjbxhcghbiwzi X-Rspam-User: X-HE-Tag: 1721036736-446737 X-HE-Meta: U2FsdG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m8J9zlbM2n1XNMAYqBEv155hiCSu7kRqi/KZXVj3XqwRppb72qwXoG6MUo+hWcfABsve3JnmFZyFBdEf0fvmyKrR2bUwQRzBu+FoCFF/IaXZMx9H4gMveJKe3BtYRf/KAClxmvwjNRTeu9HaEXnRHIw1peS6oOwIEUzM/zL/mRUTOVqLyn+g9hySojk8U5tIdux91779JZD/4wT3QsWrx3StjAV5xWH6+6L5ndoScheSjOzrrA2ruRgTT2Kfa/F/COzWDxarlcCAoTHr9/U6kVhYAUDmwlWahv4/VAicxS68W+MifGJ4L43rLT57Cf0jd9g9MBZWlBNqIhz9xMgW7tEKqeisc2dTb9in8SgfgBFHc2IiRCbfGCaBpWGLQ6OOiCCfRnkM23PwnfhhYOg13bffcmV16OKR1vua44RD+AJW1Q 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: List-Subscribe: List-Unsubscribe: From: Pankaj Raghav Usually the page cache does not extend beyond the size of the inode, therefore, no PTEs are created for folios that extend beyond the size. But with LBS support, we might extend page cache beyond the size of the inode as we need to guarantee folios of minimum order. While doing a read, do_fault_around() can create PTEs for pages that lie beyond the EOF leading to incorrect error return when accessing a page beyond the mapped file. Cap the PTE range to be created for the page cache up to the end of file(EOF) in filemap_map_pages() so that return error codes are consistent with POSIX[1] for LBS configurations. generic/749(currently in xfstest-dev patches-in-queue branch [0]) has been created to trigger this edge case. This also fixes generic/749 for tmpfs with huge=always on systems with 4k base page size. [0] https://lore.kernel.org/all/20240615002935.1033031-3-mcgrof@kernel.org/ [1](from mmap(2)) SIGBUS Attempted access to a page of the buffer that lies beyond the end of the mapped file. For an explanation of the treatment of the bytes in the page that corresponds to the end of a mapped file that is not a multiple of the page size, see NOTES. Signed-off-by: Luis Chamberlain Signed-off-by: Pankaj Raghav Reviewed-by: Hannes Reinecke Reviewed-by: Matthew Wilcox (Oracle) Reviewed-by: Darrick J. Wong --- mm/filemap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/filemap.c b/mm/filemap.c index d27e9ac54309d..d322109274532 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3608,7 +3608,7 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, struct vm_area_struct *vma = vmf->vma; struct file *file = vma->vm_file; struct address_space *mapping = file->f_mapping; - pgoff_t last_pgoff = start_pgoff; + pgoff_t file_end, last_pgoff = start_pgoff; unsigned long addr; XA_STATE(xas, &mapping->i_pages, start_pgoff); struct folio *folio; @@ -3634,6 +3634,10 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, goto out; } + file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1; + if (end_pgoff > file_end) + end_pgoff = file_end; + folio_type = mm_counter_file(folio); do { unsigned long end;