4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #include <linux/kernel.h>
35 #include <linux/string.h>
36 #include <linux/stat.h>
37 #include <linux/errno.h>
38 #include <linux/unistd.h>
39 #include <linux/version.h>
40 #include <asm/uaccess.h>
43 #include <linux/stat.h>
44 #include <asm/uaccess.h>
46 #include <linux/pagemap.h>
48 #define DEBUG_SUBSYSTEM S_LLITE
50 #include "llite_internal.h"
51 #include <lustre_compat.h>
53 static const struct vm_operations_struct ll_file_vm_ops;
55 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
56 unsigned long addr, size_t count)
58 policy->l_extent.start = ((addr - vma->vm_start) & PAGE_MASK) +
59 (vma->vm_pgoff << PAGE_SHIFT);
60 policy->l_extent.end = (policy->l_extent.start + count - 1) |
64 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
67 struct vm_area_struct *vma, *ret = NULL;
70 /* mmap_sem must have been held by caller. */
71 LASSERT(!down_write_trylock(&mm->mmap_sem));
73 for(vma = find_vma(mm, addr);
74 vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
75 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
76 vma->vm_flags & VM_SHARED) {
85 * API independent part for page fault initialization.
86 * \param env - corespondent lu_env to processing
87 * \param vma - virtual memory area addressed to page fault
88 * \param index - page index corespondent to fault.
89 * \parm ra_flags - vma readahead flags.
91 * \return error codes from cl_io_init.
94 ll_fault_io_init(struct lu_env *env, struct vm_area_struct *vma,
95 pgoff_t index, unsigned long *ra_flags)
97 struct file *file = vma->vm_file;
98 struct inode *inode = file_inode(file);
100 struct cl_fault_io *fio;
104 if (ll_file_nolock(file))
105 RETURN(ERR_PTR(-EOPNOTSUPP));
108 io = vvp_env_thread_io(env);
109 io->ci_obj = ll_i2info(inode)->lli_clob;
110 LASSERT(io->ci_obj != NULL);
112 fio = &io->u.ci_fault;
113 fio->ft_index = index;
114 fio->ft_executable = vma->vm_flags&VM_EXEC;
117 * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
118 * the kernel will not read other pages not covered by ldlm in
119 * filemap_nopage. we do our readahead in ll_readpage.
121 if (ra_flags != NULL)
122 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
123 vma->vm_flags &= ~VM_SEQ_READ;
124 vma->vm_flags |= VM_RAND_READ;
126 CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
127 fio->ft_index, fio->ft_executable);
129 rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
131 struct vvp_io *vio = vvp_env_io(env);
132 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
134 LASSERT(vio->vui_cl.cis_io == io);
136 /* mmap lock must be MANDATORY it has to cache
138 io->ci_lockreq = CILR_MANDATORY;
143 if (io->ci_need_restart)
152 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
153 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
162 struct inode *inode = NULL;
163 struct ll_inode_info *lli;
166 LASSERT(vmpage != NULL);
167 env = cl_env_get(&refcheck);
169 RETURN(PTR_ERR(env));
171 io = ll_fault_io_init(env, vma, vmpage->index, NULL);
173 GOTO(out, result = PTR_ERR(io));
175 result = io->ci_result;
177 GOTO(out_io, result);
179 io->u.ci_fault.ft_mkwrite = 1;
180 io->u.ci_fault.ft_writable = 1;
182 vio = vvp_env_io(env);
183 vio->u.fault.ft_vma = vma;
184 vio->u.fault.ft_vmpage = vmpage;
186 set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
188 inode = vvp_object_inode(io->ci_obj);
189 lli = ll_i2info(inode);
191 result = cl_io_loop(env, io);
193 cfs_restore_sigs(set);
197 if (vmpage->mapping == NULL) {
200 /* page was truncated and lock was cancelled, return
201 * ENODATA so that VM_FAULT_NOPAGE will be returned
202 * to handle_mm_fault(). */
205 } else if (!PageDirty(vmpage)) {
206 /* race, the page has been cleaned by ptlrpcd after
207 * it was unlocked, it has to be added into dirty
208 * cache again otherwise this soon-to-dirty page won't
209 * consume any grants, even worse if this page is being
210 * transferred because it will break RPC checksum.
214 CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
215 "been written out, retry.\n",
216 vmpage, vmpage->index);
223 ll_file_set_flag(lli, LLIF_DATA_MODIFIED);
230 cl_env_put(env, &refcheck);
231 CDEBUG(D_MMAP, "%s mkwrite with %d\n", current->comm, result);
232 LASSERT(ergo(result == 0, PageLocked(vmpage)));
234 /* if page has been unmapped, presumably due to lock reclaim for
235 * concurrent usage, add some delay before retrying to prevent
236 * entering live-lock situation with competitors
238 if (result == -ENODATA && inode != NULL) {
239 CDEBUG(D_MMAP, "delaying new page-fault for inode %p to "
240 "prevent live-lock\n", inode);
247 static inline int to_fault_error(int result)
251 result = VM_FAULT_LOCKED;
254 result = VM_FAULT_NOPAGE;
257 result = VM_FAULT_OOM;
260 result = VM_FAULT_SIGBUS;
267 * Lustre implementation of a vm_operations_struct::fault() method, called by
268 * VM to server page fault (both in kernel and user space).
270 * \param vma - is virtiual area struct related to page fault
271 * \param vmf - structure which describe type and address where hit fault
273 * \return allocated and filled _locked_ page for address
274 * \retval VM_FAULT_ERROR on general error
275 * \retval NOPAGE_OOM not have memory for allocate new page
277 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
281 struct vvp_io *vio = NULL;
283 unsigned long ra_flags;
289 env = cl_env_get(&refcheck);
291 RETURN(PTR_ERR(env));
293 if (ll_sbi_has_fast_read(ll_i2sbi(file_inode(vma->vm_file)))) {
295 ll_cl_add(vma->vm_file, env, NULL, LCC_MMAP);
296 fault_ret = ll_filemap_fault(vma, vmf);
297 ll_cl_remove(vma->vm_file, env);
299 /* - If there is no error, then the page was found in cache and
301 * - If VM_FAULT_RETRY is set, the page existed but failed to
302 * lock. It will return to kernel and retry;
303 * - Otherwise, it should try normal fault under DLM lock. */
304 if ((fault_ret & VM_FAULT_RETRY) ||
305 !(fault_ret & VM_FAULT_ERROR))
306 GOTO(out, result = 0);
311 io = ll_fault_io_init(env, vma, vmf->pgoff, &ra_flags);
313 GOTO(out, result = PTR_ERR(io));
315 result = io->ci_result;
317 vio = vvp_env_io(env);
318 vio->u.fault.ft_vma = vma;
319 vio->u.fault.ft_vmpage = NULL;
320 vio->u.fault.ft_vmf = vmf;
321 vio->u.fault.ft_flags = 0;
322 vio->u.fault.ft_flags_valid = 0;
324 /* May call ll_readpage() */
325 ll_cl_add(vma->vm_file, env, io, LCC_MMAP);
327 result = cl_io_loop(env, io);
329 ll_cl_remove(vma->vm_file, env);
331 /* ft_flags are only valid if we reached
332 * the call to filemap_fault */
333 if (vio->u.fault.ft_flags_valid)
334 fault_ret = vio->u.fault.ft_flags;
336 vmpage = vio->u.fault.ft_vmpage;
337 if (result != 0 && vmpage != NULL) {
344 vma->vm_flags |= ra_flags;
347 cl_env_put(env, &refcheck);
348 if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
349 fault_ret |= to_fault_error(result);
351 CDEBUG(D_MMAP, "%s fault %d/%d\n", current->comm, fault_ret, result);
355 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
356 static int ll_fault(struct vm_fault *vmf)
358 struct vm_area_struct *vma = vmf->vma;
360 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
364 bool printed = false;
368 /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
369 * so that it can be killed by admin but not cause segfault by
371 set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
373 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
377 result = ll_fault0(vma, vmf);
378 if (!(result & (VM_FAULT_RETRY | VM_FAULT_ERROR | VM_FAULT_LOCKED))) {
379 struct page *vmpage = vmf->page;
381 /* check if this page has been truncated */
383 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
388 if (!printed && ++count > 16) {
389 CWARN("the page is under heavy contention,"
390 "maybe your app(%s) needs revising :-)\n",
398 result |= VM_FAULT_LOCKED;
400 cfs_restore_sigs(set);
404 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
405 static int ll_page_mkwrite(struct vm_fault *vmf)
407 struct vm_area_struct *vma = vmf->vma;
409 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
413 bool printed = false;
417 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
418 LPROC_LL_MKWRITE, 1);
420 file_update_time(vma->vm_file);
423 result = ll_page_mkwrite0(vma, vmf->page, &retry);
425 if (!printed && ++count > 16) {
426 const struct dentry *de = file_dentry(vma->vm_file);
428 CWARN("app(%s): the page %lu of file "DFID" is under"
429 " heavy contention\n",
430 current->comm, vmf->pgoff,
431 PFID(ll_inode2fid(de->d_inode)));
438 LASSERT(PageLocked(vmf->page));
439 result = VM_FAULT_LOCKED;
443 result = VM_FAULT_NOPAGE;
446 result = VM_FAULT_OOM;
449 result = VM_FAULT_RETRY;
452 result = VM_FAULT_SIGBUS;
460 * To avoid cancel the locks covering mmapped region for lock cache pressure,
461 * we track the mapped vma count in vvp_object::vob_mmap_cnt.
463 static void ll_vm_open(struct vm_area_struct * vma)
465 struct inode *inode = file_inode(vma->vm_file);
466 struct vvp_object *vob = cl_inode2vvp(inode);
469 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
470 atomic_inc(&vob->vob_mmap_cnt);
475 * Dual to ll_vm_open().
477 static void ll_vm_close(struct vm_area_struct *vma)
479 struct inode *inode = file_inode(vma->vm_file);
480 struct vvp_object *vob = cl_inode2vvp(inode);
483 atomic_dec(&vob->vob_mmap_cnt);
484 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
488 /* XXX put nice comment here. talk about __free_pte -> dirty pages and
489 * nopage's reference passing to the pte */
490 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
495 LASSERTF(last > first, "last %llu first %llu\n", last, first);
496 if (mapping_mapped(mapping)) {
498 unmap_mapping_range(mapping, first + PAGE_SIZE - 1,
499 last - first + 1, 0);
505 static const struct vm_operations_struct ll_file_vm_ops = {
507 .page_mkwrite = ll_page_mkwrite,
509 .close = ll_vm_close,
512 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
514 struct inode *inode = file_inode(file);
518 if (ll_file_nolock(file))
521 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
522 rc = generic_file_mmap(file, vma);
524 vma->vm_ops = &ll_file_vm_ops;
525 vma->vm_ops->open(vma);
526 /* update the inode's size and mtime */
527 rc = ll_glimpse_size(inode);