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/errno.h>
34 #include <linux/delay.h>
35 #include <linux/kernel.h>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include "llite_internal.h"
41 #include <lustre_compat.h>
43 static const struct vm_operations_struct ll_file_vm_ops;
45 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
46 unsigned long addr, size_t count)
48 policy->l_extent.start = ((addr - vma->vm_start) & PAGE_MASK) +
49 (vma->vm_pgoff << PAGE_SHIFT);
50 policy->l_extent.end = (policy->l_extent.start + count - 1) |
54 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
57 struct vm_area_struct *vma, *ret = NULL;
60 /* mmap_sem must have been held by caller. */
61 LASSERT(!down_write_trylock(&mm->mmap_sem));
63 for(vma = find_vma(mm, addr);
64 vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
65 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
66 vma->vm_flags & VM_SHARED) {
75 * API independent part for page fault initialization.
76 * \param env - corespondent lu_env to processing
77 * \param vma - virtual memory area addressed to page fault
78 * \param index - page index corespondent to fault.
79 * \parm ra_flags - vma readahead flags.
81 * \return error codes from cl_io_init.
84 ll_fault_io_init(struct lu_env *env, struct vm_area_struct *vma,
85 pgoff_t index, unsigned long *ra_flags)
87 struct file *file = vma->vm_file;
88 struct inode *inode = file_inode(file);
90 struct cl_fault_io *fio;
94 if (ll_file_nolock(file))
95 RETURN(ERR_PTR(-EOPNOTSUPP));
98 io = vvp_env_thread_io(env);
99 io->ci_obj = ll_i2info(inode)->lli_clob;
100 LASSERT(io->ci_obj != NULL);
102 fio = &io->u.ci_fault;
103 fio->ft_index = index;
104 fio->ft_executable = vma->vm_flags&VM_EXEC;
107 * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
108 * the kernel will not read other pages not covered by ldlm in
109 * filemap_nopage. we do our readahead in ll_readpage.
111 if (ra_flags != NULL)
112 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
113 vma->vm_flags &= ~VM_SEQ_READ;
114 vma->vm_flags |= VM_RAND_READ;
116 CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
117 fio->ft_index, fio->ft_executable);
119 rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
121 struct vvp_io *vio = vvp_env_io(env);
122 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
124 LASSERT(vio->vui_cl.cis_io == io);
126 /* mmap lock must be MANDATORY it has to cache
128 io->ci_lockreq = CILR_MANDATORY;
133 if (io->ci_need_restart)
142 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
143 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
152 struct inode *inode = NULL;
153 struct ll_inode_info *lli;
156 LASSERT(vmpage != NULL);
157 env = cl_env_get(&refcheck);
159 RETURN(PTR_ERR(env));
161 io = ll_fault_io_init(env, vma, vmpage->index, NULL);
163 GOTO(out, result = PTR_ERR(io));
165 result = io->ci_result;
167 GOTO(out_io, result);
169 io->u.ci_fault.ft_mkwrite = 1;
170 io->u.ci_fault.ft_writable = 1;
172 vio = vvp_env_io(env);
173 vio->u.fault.ft_vma = vma;
174 vio->u.fault.ft_vmpage = vmpage;
176 set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
178 inode = vvp_object_inode(io->ci_obj);
179 lli = ll_i2info(inode);
181 result = cl_io_loop(env, io);
183 cfs_restore_sigs(set);
187 if (vmpage->mapping == NULL) {
190 /* page was truncated and lock was cancelled, return
191 * ENODATA so that VM_FAULT_NOPAGE will be returned
192 * to handle_mm_fault(). */
195 } else if (!PageDirty(vmpage)) {
196 /* race, the page has been cleaned by ptlrpcd after
197 * it was unlocked, it has to be added into dirty
198 * cache again otherwise this soon-to-dirty page won't
199 * consume any grants, even worse if this page is being
200 * transferred because it will break RPC checksum.
204 CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
205 "been written out, retry.\n",
206 vmpage, vmpage->index);
213 ll_file_set_flag(lli, LLIF_DATA_MODIFIED);
220 cl_env_put(env, &refcheck);
221 CDEBUG(D_MMAP, "%s mkwrite with %d\n", current->comm, result);
222 LASSERT(ergo(result == 0, PageLocked(vmpage)));
224 /* if page has been unmapped, presumably due to lock reclaim for
225 * concurrent usage, add some delay before retrying to prevent
226 * entering live-lock situation with competitors
228 if (result == -ENODATA && inode != NULL) {
229 CDEBUG(D_MMAP, "delaying new page-fault for inode %p to "
230 "prevent live-lock\n", inode);
237 static inline int to_fault_error(int result)
241 result = VM_FAULT_LOCKED;
244 result = VM_FAULT_NOPAGE;
247 result = VM_FAULT_OOM;
250 result = VM_FAULT_SIGBUS;
257 * Lustre implementation of a vm_operations_struct::fault() method, called by
258 * VM to server page fault (both in kernel and user space).
260 * \param vma - is virtiual area struct related to page fault
261 * \param vmf - structure which describe type and address where hit fault
263 * \return allocated and filled _locked_ page for address
264 * \retval VM_FAULT_ERROR on general error
265 * \retval NOPAGE_OOM not have memory for allocate new page
267 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
271 struct vvp_io *vio = NULL;
273 unsigned long ra_flags;
279 env = cl_env_get(&refcheck);
281 RETURN(PTR_ERR(env));
283 if (ll_sbi_has_fast_read(ll_i2sbi(file_inode(vma->vm_file)))) {
285 ll_cl_add(vma->vm_file, env, NULL, LCC_MMAP);
286 fault_ret = ll_filemap_fault(vma, vmf);
287 ll_cl_remove(vma->vm_file, env);
289 /* - If there is no error, then the page was found in cache and
291 * - If VM_FAULT_RETRY is set, the page existed but failed to
292 * lock. It will return to kernel and retry;
293 * - Otherwise, it should try normal fault under DLM lock. */
294 if ((fault_ret & VM_FAULT_RETRY) ||
295 !(fault_ret & VM_FAULT_ERROR))
296 GOTO(out, result = 0);
301 io = ll_fault_io_init(env, vma, vmf->pgoff, &ra_flags);
303 GOTO(out, result = PTR_ERR(io));
305 result = io->ci_result;
307 vio = vvp_env_io(env);
308 vio->u.fault.ft_vma = vma;
309 vio->u.fault.ft_vmpage = NULL;
310 vio->u.fault.ft_vmf = vmf;
311 vio->u.fault.ft_flags = 0;
312 vio->u.fault.ft_flags_valid = 0;
314 /* May call ll_readpage() */
315 ll_cl_add(vma->vm_file, env, io, LCC_MMAP);
317 result = cl_io_loop(env, io);
319 ll_cl_remove(vma->vm_file, env);
321 /* ft_flags are only valid if we reached
322 * the call to filemap_fault */
323 if (vio->u.fault.ft_flags_valid)
324 fault_ret = vio->u.fault.ft_flags;
326 vmpage = vio->u.fault.ft_vmpage;
327 if (result != 0 && vmpage != NULL) {
334 vma->vm_flags |= ra_flags;
337 cl_env_put(env, &refcheck);
338 if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
339 fault_ret |= to_fault_error(result);
341 CDEBUG(D_MMAP, "%s fault %d/%d\n", current->comm, fault_ret, result);
345 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
346 static int ll_fault(struct vm_fault *vmf)
348 struct vm_area_struct *vma = vmf->vma;
350 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
354 bool printed = false;
358 /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
359 * so that it can be killed by admin but not cause segfault by
361 set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
363 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
367 result = ll_fault0(vma, vmf);
368 if (!(result & (VM_FAULT_RETRY | VM_FAULT_ERROR | VM_FAULT_LOCKED))) {
369 struct page *vmpage = vmf->page;
371 /* check if this page has been truncated */
373 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
378 if (!printed && ++count > 16) {
379 CWARN("the page is under heavy contention,"
380 "maybe your app(%s) needs revising :-)\n",
388 result |= VM_FAULT_LOCKED;
390 cfs_restore_sigs(set);
394 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
395 static int ll_page_mkwrite(struct vm_fault *vmf)
397 struct vm_area_struct *vma = vmf->vma;
399 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
403 bool printed = false;
407 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
408 LPROC_LL_MKWRITE, 1);
410 file_update_time(vma->vm_file);
413 result = ll_page_mkwrite0(vma, vmf->page, &retry);
415 if (!printed && ++count > 16) {
416 const struct dentry *de = file_dentry(vma->vm_file);
418 CWARN("app(%s): the page %lu of file "DFID" is under"
419 " heavy contention\n",
420 current->comm, vmf->pgoff,
421 PFID(ll_inode2fid(de->d_inode)));
428 LASSERT(PageLocked(vmf->page));
429 result = VM_FAULT_LOCKED;
433 result = VM_FAULT_NOPAGE;
436 result = VM_FAULT_OOM;
439 result = VM_FAULT_RETRY;
442 result = VM_FAULT_SIGBUS;
450 * To avoid cancel the locks covering mmapped region for lock cache pressure,
451 * we track the mapped vma count in vvp_object::vob_mmap_cnt.
453 static void ll_vm_open(struct vm_area_struct * vma)
455 struct inode *inode = file_inode(vma->vm_file);
456 struct vvp_object *vob = cl_inode2vvp(inode);
459 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
460 atomic_inc(&vob->vob_mmap_cnt);
465 * Dual to ll_vm_open().
467 static void ll_vm_close(struct vm_area_struct *vma)
469 struct inode *inode = file_inode(vma->vm_file);
470 struct vvp_object *vob = cl_inode2vvp(inode);
473 atomic_dec(&vob->vob_mmap_cnt);
474 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
478 /* XXX put nice comment here. talk about __free_pte -> dirty pages and
479 * nopage's reference passing to the pte */
480 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
485 LASSERTF(last > first, "last %llu first %llu\n", last, first);
486 if (mapping_mapped(mapping)) {
488 unmap_mapping_range(mapping, first + PAGE_SIZE - 1,
489 last - first + 1, 0);
495 static const struct vm_operations_struct ll_file_vm_ops = {
497 .page_mkwrite = ll_page_mkwrite,
499 .close = ll_vm_close,
502 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
504 struct inode *inode = file_inode(file);
508 if (ll_file_nolock(file))
511 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
512 rc = generic_file_mmap(file, vma);
514 vma->vm_ops = &ll_file_vm_ops;
515 vma->vm_ops->open(vma);
516 /* update the inode's size and mtime */
517 rc = ll_glimpse_size(inode);