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>
36 #include <linux/file.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_lock must have been held by caller. */
61 LASSERT(!mmap_write_trylock(mm));
63 for (vma = find_vma(mm, addr);
64 vma != NULL && vma->vm_start < (addr + count);
66 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
67 vma->vm_flags & VM_SHARED) {
76 * API independent part for page fault initialization.
77 * \param env - corespondent lu_env to processing
78 * \param vma - virtual memory area addressed to page fault
79 * \param index - page index corespondent to fault.
80 * \param mkwrite - whether it is mmap write.
82 * \return error codes from cl_io_init.
85 ll_fault_io_init(struct lu_env *env, struct vm_area_struct *vma,
86 pgoff_t index, bool mkwrite)
88 struct file *file = vma->vm_file;
89 struct inode *inode = file_inode(file);
91 struct cl_fault_io *fio;
95 if (ll_file_nolock(file))
96 RETURN(ERR_PTR(-EOPNOTSUPP));
99 io = vvp_env_thread_io(env);
100 io->ci_obj = ll_i2info(inode)->lli_clob;
101 LASSERT(io->ci_obj != NULL);
103 fio = &io->u.ci_fault;
104 fio->ft_index = index;
105 fio->ft_executable = vma->vm_flags & VM_EXEC;
109 fio->ft_writable = 1;
113 DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
114 PFID(&ll_i2info(inode)->lli_fid), vma, vma->vm_start,
115 vma->vm_end, vma->vm_flags, fio->ft_index);
117 if (vma->vm_flags & VM_SEQ_READ)
119 else if (vma->vm_flags & VM_RAND_READ)
120 io->ci_rand_read = 1;
122 rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
124 struct vvp_io *vio = vvp_env_io(env);
125 struct ll_file_data *fd = file->private_data;
127 LASSERT(vio->vui_cl.cis_io == io);
129 /* mmap lock must be MANDATORY it has to cache
131 io->ci_lockreq = CILR_MANDATORY;
135 if (io->ci_need_restart)
144 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
145 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
154 struct inode *inode = NULL;
155 struct ll_inode_info *lli;
158 LASSERT(vmpage != NULL);
159 env = cl_env_get(&refcheck);
161 RETURN(PTR_ERR(env));
163 io = ll_fault_io_init(env, vma, vmpage->index, true);
165 GOTO(out, result = PTR_ERR(io));
167 result = io->ci_result;
169 GOTO(out_io, result);
171 vio = vvp_env_io(env);
172 vio->u.fault.ft_vma = vma;
173 vio->u.fault.ft_vmpage = vmpage;
175 siginitsetinv(&new, sigmask(SIGKILL) | sigmask(SIGTERM));
176 sigprocmask(SIG_BLOCK, &new, &old);
178 inode = vvp_object_inode(io->ci_obj);
179 lli = ll_i2info(inode);
181 result = cl_io_loop(env, io);
183 sigprocmask(SIG_SETMASK, &old, NULL);
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 set_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
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_OOM;
247 result = VM_FAULT_SIGBUS;
254 * Lustre implementation of a vm_operations_struct::fault() method, called by
255 * VM to server page fault (both in kernel and user space).
257 * \param vma - is virtiual area struct related to page fault
258 * \param vmf - structure which describe type and address where hit fault
260 * \return allocated and filled _locked_ page for address
261 * \retval VM_FAULT_ERROR on general error
262 * \retval NOPAGE_OOM not have memory for allocate new page
264 static vm_fault_t ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
266 struct inode *inode = file_inode(vma->vm_file);
269 struct vvp_io *vio = NULL;
276 env = cl_env_get(&refcheck);
278 RETURN(PTR_ERR(env));
280 if (ll_sbi_has_fast_read(ll_i2sbi(inode))) {
282 bool allow_retry = vmf->flags & FAULT_FLAG_ALLOW_RETRY;
283 bool has_retry = vmf->flags & FAULT_FLAG_RETRY_NOWAIT;
285 /* To avoid loops, instruct downstream to not drop mmap_sem */
287 * only need FAULT_FLAG_ALLOW_RETRY prior to Linux 5.1
288 * (6b4c9f4469819), where FAULT_FLAG_RETRY_NOWAIT is enough
289 * to not drop mmap_sem when failed to lock the page.
291 vmf->flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
292 ll_cl_add(inode, env, NULL, LCC_MMAP);
293 fault_ret = ll_filemap_fault(vma, vmf);
294 ll_cl_remove(inode, env);
296 vmf->flags &= ~FAULT_FLAG_RETRY_NOWAIT;
298 vmf->flags &= ~FAULT_FLAG_ALLOW_RETRY;
300 /* - If there is no error, then the page was found in cache and
302 * - If VM_FAULT_RETRY is set, the page existed but failed to
303 * lock. We will try slow path to avoid loops.
304 * - Otherwise, it should try normal fault under DLM lock. */
305 if (!(fault_ret & VM_FAULT_RETRY) &&
306 !(fault_ret & VM_FAULT_ERROR))
307 GOTO(out, result = 0);
312 io = ll_fault_io_init(env, vma, vmf->pgoff, false);
314 GOTO(out, result = PTR_ERR(io));
316 result = io->ci_result;
318 vio = vvp_env_io(env);
319 vio->u.fault.ft_vma = vma;
320 vio->u.fault.ft_vmpage = NULL;
321 vio->u.fault.ft_vmf = vmf;
322 vio->u.fault.ft_flags = 0;
323 vio->u.fault.ft_flags_valid = 0;
325 /* May call ll_readpage() */
326 ll_cl_add(inode, env, io, LCC_MMAP);
328 result = cl_io_loop(env, io);
330 ll_cl_remove(inode, env);
332 /* ft_flags are only valid if we reached
333 * the call to filemap_fault */
334 if (vio->u.fault.ft_flags_valid)
335 fault_ret = vio->u.fault.ft_flags;
337 vmpage = vio->u.fault.ft_vmpage;
338 if (result != 0 && vmpage != NULL) {
346 cl_env_put(env, &refcheck);
347 if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
348 fault_ret |= to_fault_error(result);
350 CDEBUG(D_MMAP, "%s fault %d/%d\n", current->comm, fault_ret, result);
354 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
355 static vm_fault_t ll_fault(struct vm_fault *vmf)
357 struct vm_area_struct *vma = vmf->vma;
359 static vm_fault_t ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
363 bool printed = false;
366 ktime_t kstart = ktime_get();
369 result = pcc_fault(vma, vmf, &cached);
373 CDEBUG(D_MMAP|D_IOTRACE,
374 DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
375 PFID(&ll_i2info(file_inode(vma->vm_file))->lli_fid),
376 vma, vma->vm_start, vma->vm_end, vma->vm_flags, vmf->pgoff);
378 /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
379 * so that it can be killed by admin but not cause segfault by
382 siginitsetinv(&new, sigmask(SIGKILL) | sigmask(SIGTERM));
383 sigprocmask(SIG_BLOCK, &new, &old);
385 /* make sure offset is not a negative number */
386 if (vmf->pgoff > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
387 return VM_FAULT_SIGBUS;
390 result = ll_fault0(vma, vmf);
392 !(result & (VM_FAULT_RETRY | VM_FAULT_ERROR | VM_FAULT_LOCKED))) {
393 struct page *vmpage = vmf->page;
395 /* check if this page has been truncated */
397 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
402 if (!printed && ++count > 16) {
403 CWARN("the page is under heavy contention, maybe your app(%s) needs revising :-)\n",
411 result |= VM_FAULT_LOCKED;
413 sigprocmask(SIG_SETMASK, &old, NULL);
416 if (vmf->page && result == VM_FAULT_LOCKED) {
417 ll_rw_stats_tally(ll_i2sbi(file_inode(vma->vm_file)),
418 current->pid, vma->vm_file->private_data,
419 cl_offset(NULL, vmf->page->index), PAGE_SIZE,
421 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
423 ktime_us_delta(ktime_get(), kstart));
427 "COMPLETED: "DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
428 PFID(&ll_i2info(file_inode(vma->vm_file))->lli_fid),
429 vma, vma->vm_start, vma->vm_end, vma->vm_flags, vmf->pgoff);
434 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
435 static vm_fault_t ll_page_mkwrite(struct vm_fault *vmf)
437 struct vm_area_struct *vma = vmf->vma;
439 static vm_fault_t ll_page_mkwrite(struct vm_area_struct *vma,
440 struct vm_fault *vmf)
444 bool printed = false;
447 ktime_t kstart = ktime_get();
450 CDEBUG(D_MMAP|D_IOTRACE,
451 DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
452 PFID(&ll_i2info(file_inode(vma->vm_file))->lli_fid),
453 vma, vma->vm_start, vma->vm_end, vma->vm_flags,
456 result = pcc_page_mkwrite(vma, vmf, &cached);
460 file_update_time(vma->vm_file);
463 result = ll_page_mkwrite0(vma, vmf->page, &retry);
465 if (!printed && ++count > 16) {
466 const struct dentry *de = file_dentry(vma->vm_file);
468 CWARN("app(%s): the page %lu of file "DFID" is under heavy contention\n",
469 current->comm, vmf->pgoff,
470 PFID(ll_inode2fid(de->d_inode)));
477 LASSERT(PageLocked(vmf->page));
478 result = VM_FAULT_LOCKED;
482 result = VM_FAULT_NOPAGE;
485 result = VM_FAULT_OOM;
488 result = VM_FAULT_RETRY;
491 result = VM_FAULT_SIGBUS;
496 if (result == VM_FAULT_LOCKED) {
497 ll_rw_stats_tally(ll_i2sbi(file_inode(vma->vm_file)),
498 current->pid, vma->vm_file->private_data,
499 cl_offset(NULL, vmf->page->index), PAGE_SIZE,
501 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
503 ktime_us_delta(ktime_get(), kstart));
507 "COMPLETED: "DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
508 PFID(&ll_i2info(file_inode(vma->vm_file))->lli_fid),
509 vma, vma->vm_start, vma->vm_end, vma->vm_flags,
515 * To avoid cancel the locks covering mmapped region for lock cache pressure,
516 * we track the mapped vma count in vvp_object::vob_mmap_cnt.
518 static void ll_vm_open(struct vm_area_struct * vma)
520 struct inode *inode = file_inode(vma->vm_file);
521 struct vvp_object *vob = cl_inode2vvp(inode);
524 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
525 atomic_inc(&vob->vob_mmap_cnt);
531 * Dual to ll_vm_open().
533 static void ll_vm_close(struct vm_area_struct *vma)
535 struct inode *inode = file_inode(vma->vm_file);
536 struct vvp_object *vob = cl_inode2vvp(inode);
539 atomic_dec(&vob->vob_mmap_cnt);
540 LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
545 static const struct vm_operations_struct ll_file_vm_ops = {
547 .page_mkwrite = ll_page_mkwrite,
549 .close = ll_vm_close,
552 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
554 struct inode *inode = file_inode(file);
555 ktime_t kstart = ktime_get();
560 CDEBUG(D_VFSTRACE | D_MMAP,
561 "VFS_Op: fid="DFID" vma=%p start=%#lx end=%#lx vm_flags=%#lx\n",
562 PFID(&ll_i2info(inode)->lli_fid),
563 vma, vma->vm_start, vma->vm_end, vma->vm_flags);
565 if (ll_file_nolock(file))
568 rc = pcc_file_mmap(file, vma, &cached);
569 if (cached && rc != 0)
572 rc = generic_file_mmap(file, vma);
574 vma->vm_ops = &ll_file_vm_ops;
575 vma->vm_ops->open(vma);
576 /* update the inode's size and mtime */
578 rc = ll_glimpse_size(inode);
582 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MMAP,
583 ktime_us_delta(ktime_get(), kstart));