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/
32 #include <linux/errno.h>
33 #include <linux/delay.h>
34 #include <linux/kernel.h>
37 #define DEBUG_SUBSYSTEM S_LLITE
39 #include "llite_internal.h"
40 #include <lustre_compat.h>
42 static const struct vm_operations_struct ll_file_vm_ops;
44 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
45 unsigned long addr, size_t count)
47 policy->l_extent.start = ((addr - vma->vm_start) & PAGE_MASK) +
48 (vma->vm_pgoff << PAGE_SHIFT);
49 policy->l_extent.end = (policy->l_extent.start + count - 1) |
53 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
56 struct vm_area_struct *vma, *ret = NULL;
59 /* mmap_lock must have been held by caller. */
60 LASSERT(!mmap_write_trylock(mm));
62 for (vma = find_vma(mm, addr);
63 vma != NULL && vma->vm_start < (addr + count);
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.
80 * \return error codes from cl_io_init.
83 ll_fault_io_init(struct lu_env *env, struct vm_area_struct *vma, pgoff_t index)
85 struct file *file = vma->vm_file;
86 struct inode *inode = file_inode(file);
88 struct cl_fault_io *fio;
92 if (ll_file_nolock(file))
93 RETURN(ERR_PTR(-EOPNOTSUPP));
96 io = vvp_env_thread_io(env);
97 io->ci_obj = ll_i2info(inode)->lli_clob;
98 LASSERT(io->ci_obj != NULL);
100 fio = &io->u.ci_fault;
101 fio->ft_index = index;
102 fio->ft_executable = vma->vm_flags & VM_EXEC;
105 DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
106 PFID(&ll_i2info(inode)->lli_fid), vma, vma->vm_start,
107 vma->vm_end, vma->vm_flags, fio->ft_index);
109 if (vma->vm_flags & VM_SEQ_READ)
111 else if (vma->vm_flags & VM_RAND_READ)
112 io->ci_rand_read = 1;
114 rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
116 struct vvp_io *vio = vvp_env_io(env);
117 struct ll_file_data *fd = file->private_data;
119 LASSERT(vio->vui_cl.cis_io == io);
121 /* mmap lock must be MANDATORY it has to cache
123 io->ci_lockreq = CILR_MANDATORY;
128 if (io->ci_need_restart)
137 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
138 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
147 struct inode *inode = NULL;
148 struct ll_inode_info *lli;
151 LASSERT(vmpage != NULL);
152 env = cl_env_get(&refcheck);
154 RETURN(PTR_ERR(env));
156 io = ll_fault_io_init(env, vma, vmpage->index);
158 GOTO(out, result = PTR_ERR(io));
160 result = io->ci_result;
162 GOTO(out_io, result);
164 io->u.ci_fault.ft_mkwrite = 1;
165 io->u.ci_fault.ft_writable = 1;
167 vio = vvp_env_io(env);
168 vio->u.fault.ft_vma = vma;
169 vio->u.fault.ft_vmpage = vmpage;
171 siginitsetinv(&new, sigmask(SIGKILL) | sigmask(SIGTERM));
172 sigprocmask(SIG_BLOCK, &new, &old);
174 inode = vvp_object_inode(io->ci_obj);
175 lli = ll_i2info(inode);
177 result = cl_io_loop(env, io);
179 sigprocmask(SIG_SETMASK, &old, NULL);
183 if (vmpage->mapping == NULL) {
186 /* page was truncated and lock was cancelled, return
187 * ENODATA so that VM_FAULT_NOPAGE will be returned
188 * to handle_mm_fault(). */
191 } else if (!PageDirty(vmpage)) {
192 /* race, the page has been cleaned by ptlrpcd after
193 * it was unlocked, it has to be added into dirty
194 * cache again otherwise this soon-to-dirty page won't
195 * consume any grants, even worse if this page is being
196 * transferred because it will break RPC checksum.
200 CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
201 "been written out, retry.\n",
202 vmpage, vmpage->index);
209 set_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
216 cl_env_put(env, &refcheck);
217 CDEBUG(D_MMAP, "%s mkwrite with %d\n", current->comm, result);
218 LASSERT(ergo(result == 0, PageLocked(vmpage)));
220 /* if page has been unmapped, presumably due to lock reclaim for
221 * concurrent usage, add some delay before retrying to prevent
222 * entering live-lock situation with competitors
224 if (result == -ENODATA && inode != NULL) {
225 CDEBUG(D_MMAP, "delaying new page-fault for inode %p to "
226 "prevent live-lock\n", inode);
233 static inline int to_fault_error(int result)
237 result = VM_FAULT_LOCKED;
240 result = VM_FAULT_OOM;
243 result = VM_FAULT_SIGBUS;
250 * Lustre implementation of a vm_operations_struct::fault() method, called by
251 * VM to server page fault (both in kernel and user space).
253 * \param vma - is virtiual area struct related to page fault
254 * \param vmf - structure which describe type and address where hit fault
256 * \return allocated and filled _locked_ page for address
257 * \retval VM_FAULT_ERROR on general error
258 * \retval NOPAGE_OOM not have memory for allocate new page
260 static vm_fault_t ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
264 struct vvp_io *vio = NULL;
271 env = cl_env_get(&refcheck);
273 RETURN(PTR_ERR(env));
275 if (ll_sbi_has_fast_read(ll_i2sbi(file_inode(vma->vm_file)))) {
277 bool has_retry = vmf->flags & FAULT_FLAG_RETRY_NOWAIT;
279 /* To avoid loops, instruct downstream to not drop mmap_sem */
280 vmf->flags |= FAULT_FLAG_RETRY_NOWAIT;
281 ll_cl_add(vma->vm_file, env, NULL, LCC_MMAP);
282 fault_ret = ll_filemap_fault(vma, vmf);
283 ll_cl_remove(vma->vm_file, env);
285 vmf->flags &= ~FAULT_FLAG_RETRY_NOWAIT;
287 /* - If there is no error, then the page was found in cache and
289 * - If VM_FAULT_RETRY is set, the page existed but failed to
290 * lock. We will try slow path to avoid loops.
291 * - Otherwise, it should try normal fault under DLM lock. */
292 if (!(fault_ret & VM_FAULT_RETRY) &&
293 !(fault_ret & VM_FAULT_ERROR))
294 GOTO(out, result = 0);
299 io = ll_fault_io_init(env, vma, vmf->pgoff);
301 GOTO(out, result = PTR_ERR(io));
303 result = io->ci_result;
305 vio = vvp_env_io(env);
306 vio->u.fault.ft_vma = vma;
307 vio->u.fault.ft_vmpage = NULL;
308 vio->u.fault.ft_vmf = vmf;
309 vio->u.fault.ft_flags = 0;
310 vio->u.fault.ft_flags_valid = 0;
312 /* May call ll_readpage() */
313 ll_cl_add(vma->vm_file, env, io, LCC_MMAP);
315 result = cl_io_loop(env, io);
317 ll_cl_remove(vma->vm_file, env);
319 /* ft_flags are only valid if we reached
320 * the call to filemap_fault */
321 if (vio->u.fault.ft_flags_valid)
322 fault_ret = vio->u.fault.ft_flags;
324 vmpage = vio->u.fault.ft_vmpage;
325 if (result != 0 && vmpage != NULL) {
333 cl_env_put(env, &refcheck);
334 if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
335 fault_ret |= to_fault_error(result);
337 CDEBUG(D_MMAP, "%s fault %d/%d\n", current->comm, fault_ret, result);
341 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
342 static vm_fault_t ll_fault(struct vm_fault *vmf)
344 struct vm_area_struct *vma = vmf->vma;
346 static vm_fault_t ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
350 bool printed = false;
353 ktime_t kstart = ktime_get();
356 result = pcc_fault(vma, vmf, &cached);
360 CDEBUG(D_MMAP, DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx\n",
361 PFID(&ll_i2info(file_inode(vma->vm_file))->lli_fid),
362 vma, vma->vm_start, vma->vm_end, vma->vm_flags);
364 /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
365 * so that it can be killed by admin but not cause segfault by
368 siginitsetinv(&new, sigmask(SIGKILL) | sigmask(SIGTERM));
369 sigprocmask(SIG_BLOCK, &new, &old);
371 /* make sure offset is not a negative number */
372 if (vmf->pgoff > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
373 return VM_FAULT_SIGBUS;
376 result = ll_fault0(vma, vmf);
378 !(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, maybe your app(%s) needs revising :-)\n",
397 result |= VM_FAULT_LOCKED;
399 sigprocmask(SIG_SETMASK, &old, NULL);
402 if (vmf->page && result == VM_FAULT_LOCKED) {
403 ll_rw_stats_tally(ll_i2sbi(file_inode(vma->vm_file)),
404 current->pid, vma->vm_file->private_data,
405 cl_offset(NULL, vmf->page->index), PAGE_SIZE,
407 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
409 ktime_us_delta(ktime_get(), kstart));
415 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
416 static vm_fault_t ll_page_mkwrite(struct vm_fault *vmf)
418 struct vm_area_struct *vma = vmf->vma;
420 static vm_fault_t ll_page_mkwrite(struct vm_area_struct *vma,
421 struct vm_fault *vmf)
425 bool printed = false;
428 ktime_t kstart = ktime_get();
431 result = pcc_page_mkwrite(vma, vmf, &cached);
435 file_update_time(vma->vm_file);
438 result = ll_page_mkwrite0(vma, vmf->page, &retry);
440 if (!printed && ++count > 16) {
441 const struct dentry *de = file_dentry(vma->vm_file);
443 CWARN("app(%s): the page %lu of file "DFID" is under heavy contention\n",
444 current->comm, vmf->pgoff,
445 PFID(ll_inode2fid(de->d_inode)));
452 LASSERT(PageLocked(vmf->page));
453 result = VM_FAULT_LOCKED;
457 result = VM_FAULT_NOPAGE;
460 result = VM_FAULT_OOM;
463 result = VM_FAULT_RETRY;
466 result = VM_FAULT_SIGBUS;
471 if (result == VM_FAULT_LOCKED) {
472 ll_rw_stats_tally(ll_i2sbi(file_inode(vma->vm_file)),
473 current->pid, vma->vm_file->private_data,
474 cl_offset(NULL, vmf->page->index), PAGE_SIZE,
476 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
478 ktime_us_delta(ktime_get(), kstart));
485 * To avoid cancel the locks covering mmapped region for lock cache pressure,
486 * we track the mapped vma count in vvp_object::vob_mmap_cnt.
488 static void ll_vm_open(struct vm_area_struct * vma)
490 struct inode *inode = file_inode(vma->vm_file);
491 struct vvp_object *vob = cl_inode2vvp(inode);
494 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
495 atomic_inc(&vob->vob_mmap_cnt);
501 * Dual to ll_vm_open().
503 static void ll_vm_close(struct vm_area_struct *vma)
505 struct inode *inode = file_inode(vma->vm_file);
506 struct vvp_object *vob = cl_inode2vvp(inode);
509 atomic_dec(&vob->vob_mmap_cnt);
510 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
515 static const struct vm_operations_struct ll_file_vm_ops = {
517 .page_mkwrite = ll_page_mkwrite,
519 .close = ll_vm_close,
522 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
524 struct inode *inode = file_inode(file);
525 ktime_t kstart = ktime_get();
530 CDEBUG(D_VFSTRACE | D_MMAP,
531 "VFS_Op: fid="DFID" vma=%p start=%#lx end=%#lx vm_flags=%#lx\n",
532 PFID(&ll_i2info(inode)->lli_fid),
533 vma, vma->vm_start, vma->vm_end, vma->vm_flags);
535 if (ll_file_nolock(file))
538 rc = pcc_file_mmap(file, vma, &cached);
539 if (cached && rc != 0)
542 rc = generic_file_mmap(file, vma);
544 vma->vm_ops = &ll_file_vm_ops;
545 vma->vm_ops->open(vma);
546 /* update the inode's size and mtime */
548 rc = ll_glimpse_size(inode);
552 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MMAP,
553 ktime_us_delta(ktime_get(), kstart));