Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA0544499AC for ; Thu, 9 Jul 2026 18:47:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783622876; cv=none; b=LotjujNlnDXnL+VxbCB1NLFayM8F917NnLxd0LH6tFwsmu20BxW+zuxlP6a5eud1a18WQwILVOuY66ozG+p2NuFrBJwNJcVO2qS/xg649OY4CQFEBa3nelmybMUoxPmsl5hCHCQwJOgij6+XmgEN7HokQyKQ0R2WQVpUuP7yWCw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783622876; c=relaxed/simple; bh=m0/rgKqzmTJbyqLMCO7DzhploB3XY5q9ObtSWdxQ2L0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Tpymp7lkm9jj/w1hxJnIQCD7HQ3AZWAsl+QLknWZbTt8jM/4JLN2aLaBEPxGJcGMHj3PaYLWeOLG7bNTxsKwBFlA6oVdhMb7jMD0dZwQZV88wWTISFQxPuyyGYYX/Tc0gAUg7LrIbhdO50BFWctsCQ9XqVegxqEcxItDVU2R478= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=n3TNjBBo; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="n3TNjBBo" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=S5peFVE/kDNvQpa6l5/g+1jooYT1uWZcYKJ+53pLsnI=; b=n3TNjBBo5vmhKRfE6Lwi0KbR1Z z7lXyyoSLLA9qDidKAkJHt5sJMKILB17EF1yHP/owXtWEgcK5/YGM3GAJEhoZ0pNki13Qyrfqh8uw O81P6nP8T+zoqY4uVHZ0nmmZJzsHdxvx+jjl06Z0LNtTOCyQRUd5n9Duf3bHQ4q0RjBOKkmdpOjHK WD1+sEAqSScIks2ukeYBqeZyE15YXGdA14G6vf8gXQx1QAWQUDZSVFbHrL3m2dQNbWVDMTjVLtN3W EFiCH0phNS19Dpg3rDuamHZGJOqDW9dX95LgDXs/O0+zfXcDbFaEpMKvWETEXqaKlF0evS6MLKloa 1LlB4BmQ==; Received: from willy by casper.infradead.org with local (Exim 4.99.1 #2 (Red Hat Linux)) id 1whtmh-00000005OhT-2ZYQ; Thu, 09 Jul 2026 18:47:43 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton , Jane Chu , linux-mm@kvack.org Cc: Muchun Song , Oscar Salvador , David Hildenbrand , Miaohe Lin , Naoya Horiguchi , Jan Kara , linux-fsdevel@vger.kernel.org, Christian Brauner , "Matthew Wilcox (Oracle)" Subject: [PATCH v2 10/10] hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter() Date: Thu, 9 Jul 2026 19:47:38 +0100 Message-ID: <20260709184740.1286561-11-willy@infradead.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260709184740.1286561-1-willy@infradead.org> References: <20260709184740.1286561-1-willy@infradead.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jane Chu generic_file_read_iter() now handles hugetlb poison, hugetlb page size and missing folios correctly, so we no longer need a separate hugetlbfs_read_iter(). Signed-off-by: Jane Chu Co-developed-by: Matthew Wilcox (Oracle) Signed-off-by: Matthew Wilcox (Oracle) --- fs/hugetlbfs/inode.c | 109 +------------------------------------------ 1 file changed, 1 insertion(+), 108 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 725bb945a440..a6f0459d79de 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -187,113 +187,6 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr, return mm_get_unmapped_area_vmflags(file, addr0, len, pgoff, flags, 0); } -/* - * Someone wants to read @bytes from a HWPOISON hugetlb @folio from @offset. - * Returns the maximum number of bytes one can read without touching the 1st raw - * HWPOISON page. - */ -static size_t adjust_range_hwpoison(struct folio *folio, size_t offset, - size_t bytes) -{ - struct page *page = folio_page(folio, offset / PAGE_SIZE); - size_t safe_bytes; - - if (hugetlb_page_hwpoison(folio, page)) - return 0; - /* Safe to read the remaining bytes in this page. */ - safe_bytes = PAGE_SIZE - (offset % PAGE_SIZE); - page++; - - /* Check each remaining page as long as we are not done yet. */ - for (; safe_bytes < bytes; safe_bytes += PAGE_SIZE, page++) - if (hugetlb_page_hwpoison(folio, page)) - break; - - return min(safe_bytes, bytes); -} - -/* - * Support for read() - Find the page attached to f_mapping and copy out the - * data. This provides functionality similar to filemap_read(). - */ -static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to) -{ - struct file *file = iocb->ki_filp; - struct hstate *h = hstate_file(file); - struct address_space *mapping = file->f_mapping; - struct inode *inode = mapping->host; - unsigned long index = iocb->ki_pos >> huge_page_shift(h); - unsigned long offset = iocb->ki_pos & ~huge_page_mask(h); - unsigned long end_index; - loff_t isize; - ssize_t retval = 0; - - while (iov_iter_count(to)) { - struct folio *folio; - size_t nr, copied, want; - - /* nr is the maximum number of bytes to copy from this page */ - nr = huge_page_size(h); - isize = i_size_read(inode); - if (!isize) - break; - end_index = (isize - 1) >> huge_page_shift(h); - if (index > end_index) - break; - if (index == end_index) { - nr = ((isize - 1) & ~huge_page_mask(h)) + 1; - if (nr <= offset) - break; - } - nr = nr - offset; - - /* Find the folio */ - folio = filemap_lock_hugetlb_folio(h, mapping, index); - if (IS_ERR(folio)) { - /* - * We have a HOLE, zero out the user-buffer for the - * length of the hole or request. - */ - copied = iov_iter_zero(nr, to); - } else { - folio_unlock(folio); - - if (!folio_test_hwpoison(folio)) - want = nr; - else { - /* - * Adjust how many bytes safe to read without - * touching the 1st raw HWPOISON page after - * offset. - */ - want = adjust_range_hwpoison(folio, offset, nr); - if (want == 0) { - folio_put(folio); - retval = -EIO; - break; - } - } - - /* - * We have the folio, copy it to user space buffer. - */ - copied = copy_folio_to_iter(folio, offset, want, to); - folio_put(folio); - } - offset += copied; - retval += copied; - if (copied != nr && iov_iter_count(to)) { - if (!retval) - retval = -EFAULT; - break; - } - index += offset >> huge_page_shift(h); - offset &= ~huge_page_mask(h); - } - iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset; - return retval; -} - static int hugetlbfs_write_begin(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned len, @@ -1215,7 +1108,7 @@ static void init_once(void *foo) } static const struct file_operations hugetlbfs_file_operations = { - .read_iter = hugetlbfs_read_iter, + .read_iter = generic_file_read_iter, .mmap = hugetlbfs_file_mmap, .fsync = noop_fsync, .get_unmapped_area = hugetlb_get_unmapped_area, -- 2.47.3