3164 lines · 84.7 KB · C
RawDownload
/* *	linux/mm/filemap.c * * Copyright (C) 1994-1999  Linus Torvalds */ /* * This file handles the generic file mmap semantics used by * most "normal" filesystems (but you don't /have/ to use this: * the NFS filesystem used to do this differently, for example) */#include <linux/export.h>#include <linux/compiler.h>#include <linux/dax.h>#include <linux/fs.h>#include <linux/sched/signal.h>#include <linux/uaccess.h>#include <linux/capability.h>#include <linux/kernel_stat.h>#include <linux/gfp.h>#include <linux/mm.h>#include <linux/swap.h>#include <linux/mman.h>#include <linux/pagemap.h>#include <linux/file.h>#include <linux/uio.h>#include <linux/hash.h>#include <linux/writeback.h>#include <linux/backing-dev.h>#include <linux/pagevec.h>#include <linux/blkdev.h>#include <linux/security.h>#include <linux/cpuset.h>#include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */#include <linux/hugetlb.h>#include <linux/memcontrol.h>#include <linux/cleancache.h>#include <linux/rmap.h>#include "internal.h" #define CREATE_TRACE_POINTS#include <trace/events/filemap.h> /* * FIXME: remove all knowledge of the buffer layer from the core VM */#include <linux/buffer_head.h> /* for try_to_free_buffers */ #include <asm/mman.h> /* * Shared mappings implemented 30.11.1994. It's not fully working yet, * though. * * Shared mappings now work. 15.8.1995  Bruno. * * finished 'unifying' the page and buffer cache and SMP-threaded the * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> * * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> */ /* * Lock ordering: * *  ->i_mmap_rwsem		(truncate_pagecache) *    ->private_lock		(__free_pte->__set_page_dirty_buffers) *      ->swap_lock		(exclusive_swap_page, others) *        ->mapping->tree_lock * *  ->i_mutex *    ->i_mmap_rwsem		(truncate->unmap_mapping_range) * *  ->mmap_sem *    ->i_mmap_rwsem *      ->page_table_lock or pte_lock	(various, mainly in memory.c) *        ->mapping->tree_lock	(arch-dependent flush_dcache_mmap_lock) * *  ->mmap_sem *    ->lock_page		(access_process_vm) * *  ->i_mutex			(generic_perform_write) *    ->mmap_sem		(fault_in_pages_readable->do_page_fault) * *  bdi->wb.list_lock *    sb_lock			(fs/fs-writeback.c) *    ->mapping->tree_lock	(__sync_single_inode) * *  ->i_mmap_rwsem *    ->anon_vma.lock		(vma_adjust) * *  ->anon_vma.lock *    ->page_table_lock or pte_lock	(anon_vma_prepare and various) * *  ->page_table_lock or pte_lock *    ->swap_lock		(try_to_unmap_one) *    ->private_lock		(try_to_unmap_one) *    ->tree_lock		(try_to_unmap_one) *    ->zone_lru_lock(zone)	(follow_page->mark_page_accessed) *    ->zone_lru_lock(zone)	(check_pte_range->isolate_lru_page) *    ->private_lock		(page_remove_rmap->set_page_dirty) *    ->tree_lock		(page_remove_rmap->set_page_dirty) *    bdi.wb->list_lock		(page_remove_rmap->set_page_dirty) *    ->inode->i_lock		(page_remove_rmap->set_page_dirty) *    ->memcg->move_lock	(page_remove_rmap->lock_page_memcg) *    bdi.wb->list_lock		(zap_pte_range->set_page_dirty) *    ->inode->i_lock		(zap_pte_range->set_page_dirty) *    ->private_lock		(zap_pte_range->__set_page_dirty_buffers) * * ->i_mmap_rwsem *   ->tasklist_lock            (memory_failure, collect_procs_ao) */ static int page_cache_tree_insert(struct address_space *mapping,				  struct page *page, void **shadowp){	struct radix_tree_node *node;	void **slot;	int error; 	error = __radix_tree_create(&mapping->page_tree, page->index, 0,				    &node, &slot);	if (error)		return error;	if (*slot) {		void *p; 		p = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);		if (!radix_tree_exceptional_entry(p))			return -EEXIST; 		mapping->nrexceptional--;		if (!dax_mapping(mapping)) {			if (shadowp)				*shadowp = p;		} else {			/* DAX can replace empty locked entry with a hole */			WARN_ON_ONCE(p !=				dax_radix_locked_entry(0, RADIX_DAX_EMPTY));			/* Wakeup waiters for exceptional entry lock */			dax_wake_mapping_entry_waiter(mapping, page->index, p,						      true);		}	}	__radix_tree_replace(&mapping->page_tree, node, slot, page,			     workingset_update_node, mapping);	mapping->nrpages++;	return 0;} static void page_cache_tree_delete(struct address_space *mapping,				   struct page *page, void *shadow){	int i, nr; 	/* hugetlb pages are represented by one entry in the radix tree */	nr = PageHuge(page) ? 1 : hpage_nr_pages(page); 	VM_BUG_ON_PAGE(!PageLocked(page), page);	VM_BUG_ON_PAGE(PageTail(page), page);	VM_BUG_ON_PAGE(nr != 1 && shadow, page); 	for (i = 0; i < nr; i++) {		struct radix_tree_node *node;		void **slot; 		__radix_tree_lookup(&mapping->page_tree, page->index + i,				    &node, &slot); 		VM_BUG_ON_PAGE(!node && nr != 1, page); 		radix_tree_clear_tags(&mapping->page_tree, node, slot);		__radix_tree_replace(&mapping->page_tree, node, slot, shadow,				     workingset_update_node, mapping);	} 	if (shadow) {		mapping->nrexceptional += nr;		/*		 * Make sure the nrexceptional update is committed before		 * the nrpages update so that final truncate racing		 * with reclaim does not see both counters 0 at the		 * same time and miss a shadow entry.		 */		smp_wmb();	}	mapping->nrpages -= nr;} /* * Delete a page from the page cache and free it. Caller has to make * sure the page is locked and that nobody else uses it - or that usage * is safe.  The caller must hold the mapping's tree_lock. */void __delete_from_page_cache(struct page *page, void *shadow){	struct address_space *mapping = page->mapping;	int nr = hpage_nr_pages(page); 	trace_mm_filemap_delete_from_page_cache(page);	/*	 * if we're uptodate, flush out into the cleancache, otherwise	 * invalidate any existing cleancache entries.  We can't leave	 * stale data around in the cleancache once our page is gone	 */	if (PageUptodate(page) && PageMappedToDisk(page))		cleancache_put_page(page);	else		cleancache_invalidate_page(mapping, page); 	VM_BUG_ON_PAGE(PageTail(page), page);	VM_BUG_ON_PAGE(page_mapped(page), page);	if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {		int mapcount; 		pr_alert("BUG: Bad page cache in process %s  pfn:%05lx\n",			 current->comm, page_to_pfn(page));		dump_page(page, "still mapped when deleted");		dump_stack();		add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 		mapcount = page_mapcount(page);		if (mapping_exiting(mapping) &&		    page_count(page) >= mapcount + 2) {			/*			 * All vmas have already been torn down, so it's			 * a good bet that actually the page is unmapped,			 * and we'd prefer not to leak it: if we're wrong,			 * some other bad page check should catch it later.			 */			page_mapcount_reset(page);			page_ref_sub(page, mapcount);		}	} 	page_cache_tree_delete(mapping, page, shadow); 	page->mapping = NULL;	/* Leave page->index set: truncation lookup relies upon it */ 	/* hugetlb pages do not participate in page cache accounting. */	if (PageHuge(page))		return; 	__mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);	if (PageSwapBacked(page)) {		__mod_node_page_state(page_pgdat(page), NR_SHMEM, -nr);		if (PageTransHuge(page))			__dec_node_page_state(page, NR_SHMEM_THPS);	} else {		VM_BUG_ON_PAGE(PageTransHuge(page), page);	} 	/*	 * At this point page must be either written or cleaned by truncate.	 * Dirty page here signals a bug and loss of unwritten data.	 *	 * This fixes dirty accounting after removing the page entirely but	 * leaves PageDirty set: it has no effect for truncated page and	 * anyway will be cleared before returning page into buddy allocator.	 */	if (WARN_ON_ONCE(PageDirty(page)))		account_page_cleaned(page, mapping, inode_to_wb(mapping->host));} /** * delete_from_page_cache - delete page from page cache * @page: the page which the kernel is trying to remove from page cache * * This must be called only on pages that have been verified to be in the page * cache and locked.  It will never put the page into the free list, the caller * has a reference on the page. */void delete_from_page_cache(struct page *page){	struct address_space *mapping = page_mapping(page);	unsigned long flags;	void (*freepage)(struct page *); 	BUG_ON(!PageLocked(page)); 	freepage = mapping->a_ops->freepage; 	spin_lock_irqsave(&mapping->tree_lock, flags);	__delete_from_page_cache(page, NULL);	spin_unlock_irqrestore(&mapping->tree_lock, flags); 	if (freepage)		freepage(page); 	if (PageTransHuge(page) && !PageHuge(page)) {		page_ref_sub(page, HPAGE_PMD_NR);		VM_BUG_ON_PAGE(page_count(page) <= 0, page);	} else {		put_page(page);	}}EXPORT_SYMBOL(delete_from_page_cache); int filemap_check_errors(struct address_space *mapping){	int ret = 0;	/* Check for outstanding write errors */	if (test_bit(AS_ENOSPC, &mapping->flags) &&	    test_and_clear_bit(AS_ENOSPC, &mapping->flags))		ret = -ENOSPC;	if (test_bit(AS_EIO, &mapping->flags) &&	    test_and_clear_bit(AS_EIO, &mapping->flags))		ret = -EIO;	return ret;}EXPORT_SYMBOL(filemap_check_errors); static int filemap_check_and_keep_errors(struct address_space *mapping){	/* Check for outstanding write errors */	if (test_bit(AS_EIO, &mapping->flags))		return -EIO;	if (test_bit(AS_ENOSPC, &mapping->flags))		return -ENOSPC;	return 0;} /** * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range * @mapping:	address space structure to write * @start:	offset in bytes where the range starts * @end:	offset in bytes where the range ends (inclusive) * @sync_mode:	enable synchronous operation * * Start writeback against all of a mapping's dirty pages that lie * within the byte offsets <start, end> inclusive. * * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as * opposed to a regular memory cleansing writeback.  The difference between * these two operations is that if a dirty page/buffer is encountered, it must * be waited upon, and not just skipped over. */int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,				loff_t end, int sync_mode){	int ret;	struct writeback_control wbc = {		.sync_mode = sync_mode,		.nr_to_write = LONG_MAX,		.range_start = start,		.range_end = end,	}; 	if (!mapping_cap_writeback_dirty(mapping))		return 0; 	wbc_attach_fdatawrite_inode(&wbc, mapping->host);	ret = do_writepages(mapping, &wbc);	wbc_detach_inode(&wbc);	return ret;} static inline int __filemap_fdatawrite(struct address_space *mapping,	int sync_mode){	return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);} int filemap_fdatawrite(struct address_space *mapping){	return __filemap_fdatawrite(mapping, WB_SYNC_ALL);}EXPORT_SYMBOL(filemap_fdatawrite); int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,				loff_t end){	return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);}EXPORT_SYMBOL(filemap_fdatawrite_range); /** * filemap_flush - mostly a non-blocking flush * @mapping:	target address_space * * This is a mostly non-blocking flush.  Not suitable for data-integrity * purposes - I/O may not be started against all dirty pages. */int filemap_flush(struct address_space *mapping){	return __filemap_fdatawrite(mapping, WB_SYNC_NONE);}EXPORT_SYMBOL(filemap_flush); /** * filemap_range_has_page - check if a page exists in range. * @mapping:           address space within which to check * @start_byte:        offset in bytes where the range starts * @end_byte:          offset in bytes where the range ends (inclusive) * * Find at least one page in the range supplied, usually used to check if * direct writing in this range will trigger a writeback. */bool filemap_range_has_page(struct address_space *mapping,			   loff_t start_byte, loff_t end_byte){	pgoff_t index = start_byte >> PAGE_SHIFT;	pgoff_t end = end_byte >> PAGE_SHIFT;	struct pagevec pvec;	bool ret; 	if (end_byte < start_byte)		return false; 	if (mapping->nrpages == 0)		return false; 	pagevec_init(&pvec, 0);	if (!pagevec_lookup(&pvec, mapping, index, 1))		return false;	ret = (pvec.pages[0]->index <= end);	pagevec_release(&pvec);	return ret;}EXPORT_SYMBOL(filemap_range_has_page); static void __filemap_fdatawait_range(struct address_space *mapping,				     loff_t start_byte, loff_t end_byte){	pgoff_t index = start_byte >> PAGE_SHIFT;	pgoff_t end = end_byte >> PAGE_SHIFT;	struct pagevec pvec;	int nr_pages; 	if (end_byte < start_byte)		return; 	pagevec_init(&pvec, 0);	while ((index <= end) &&			(nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,			PAGECACHE_TAG_WRITEBACK,			min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {		unsigned i; 		for (i = 0; i < nr_pages; i++) {			struct page *page = pvec.pages[i]; 			/* until radix tree lookup accepts end_index */			if (page->index > end)				continue; 			wait_on_page_writeback(page);			ClearPageError(page);		}		pagevec_release(&pvec);		cond_resched();	}} /** * filemap_fdatawait_range - wait for writeback to complete * @mapping:		address space structure to wait for * @start_byte:		offset in bytes where the range starts * @end_byte:		offset in bytes where the range ends (inclusive) * * Walk the list of under-writeback pages of the given address space * in the given range and wait for all of them.  Check error status of * the address space and return it. * * Since the error status of the address space is cleared by this function, * callers are responsible for checking the return value and handling and/or * reporting the error. */int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,			    loff_t end_byte){	__filemap_fdatawait_range(mapping, start_byte, end_byte);	return filemap_check_errors(mapping);}EXPORT_SYMBOL(filemap_fdatawait_range); /** * filemap_fdatawait_keep_errors - wait for writeback without clearing errors * @mapping: address space structure to wait for * * Walk the list of under-writeback pages of the given address space * and wait for all of them.  Unlike filemap_fdatawait(), this function * does not clear error status of the address space. * * Use this function if callers don't handle errors themselves.  Expected * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2), * fsfreeze(8) */int filemap_fdatawait_keep_errors(struct address_space *mapping){	loff_t i_size = i_size_read(mapping->host); 	if (i_size == 0)		return 0; 	__filemap_fdatawait_range(mapping, 0, i_size - 1);	return filemap_check_and_keep_errors(mapping);}EXPORT_SYMBOL(filemap_fdatawait_keep_errors);

Lines 1–500 of 3164.