Whamcloud - gitweb
f43c2774d9154d005ff8fc09f4e6f3b9c2ae025b
[fs/lustre-release.git] / lustre / llite / llite_mmap.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <linux/errno.h>
34 #include <linux/delay.h>
35 #include <linux/kernel.h>
36 #include <linux/mm.h>
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include "llite_internal.h"
41 #include <lustre_compat.h>
42
43 static const struct vm_operations_struct ll_file_vm_ops;
44
45 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
46                      unsigned long addr, size_t count)
47 {
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) |
51                                ~PAGE_MASK;
52 }
53
54 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
55                                size_t count)
56 {
57         struct vm_area_struct *vma, *ret = NULL;
58         ENTRY;
59
60         /* mmap_lock must have been held by caller. */
61         LASSERT(!mmap_write_trylock(mm));
62
63         for (vma = find_vma(mm, addr);
64              vma != NULL && vma->vm_start < (addr + count);
65              vma = vma->vm_next) {
66                 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
67                     vma->vm_flags & VM_SHARED) {
68                         ret = vma;
69                         break;
70                 }
71         }
72         RETURN(ret);
73 }
74
75 /**
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  *
81  * \return error codes from cl_io_init.
82  */
83 static struct cl_io *
84 ll_fault_io_init(struct lu_env *env, struct vm_area_struct *vma, pgoff_t index)
85 {
86         struct file            *file = vma->vm_file;
87         struct inode           *inode = file_inode(file);
88         struct cl_io           *io;
89         struct cl_fault_io     *fio;
90         int                     rc;
91         ENTRY;
92
93         if (ll_file_nolock(file))
94                 RETURN(ERR_PTR(-EOPNOTSUPP));
95
96 restart:
97         io = vvp_env_thread_io(env);
98         io->ci_obj = ll_i2info(inode)->lli_clob;
99         LASSERT(io->ci_obj != NULL);
100
101         fio = &io->u.ci_fault;
102         fio->ft_index = index;
103         fio->ft_executable = vma->vm_flags & VM_EXEC;
104
105         CDEBUG(D_MMAP,
106                DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx idx=%lu\n",
107                PFID(&ll_i2info(inode)->lli_fid), vma, vma->vm_start,
108                vma->vm_end, vma->vm_flags, fio->ft_index);
109
110         if (vma->vm_flags & VM_SEQ_READ)
111                 io->ci_seq_read = 1;
112         else if (vma->vm_flags & VM_RAND_READ)
113                 io->ci_rand_read = 1;
114
115         rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
116         if (rc == 0) {
117                 struct vvp_io *vio = vvp_env_io(env);
118                 struct ll_file_data *fd = file->private_data;
119
120                 LASSERT(vio->vui_cl.cis_io == io);
121
122                 /* mmap lock must be MANDATORY it has to cache
123                  * pages. */
124                 io->ci_lockreq = CILR_MANDATORY;
125                 vio->vui_fd = fd;
126         } else {
127                 LASSERT(rc < 0);
128                 cl_io_fini(env, io);
129                 if (io->ci_need_restart)
130                         goto restart;
131
132                 io = ERR_PTR(rc);
133         }
134
135         RETURN(io);
136 }
137
138 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
139 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
140                             bool *retry)
141 {
142         struct lu_env           *env;
143         struct cl_io            *io;
144         struct vvp_io           *vio;
145         int                      result;
146         __u16                    refcheck;
147         sigset_t old, new;
148         struct inode             *inode = NULL;
149         struct ll_inode_info     *lli;
150         ENTRY;
151
152         LASSERT(vmpage != NULL);
153         env = cl_env_get(&refcheck);
154         if (IS_ERR(env))
155                 RETURN(PTR_ERR(env));
156
157         io = ll_fault_io_init(env, vma, vmpage->index);
158         if (IS_ERR(io))
159                 GOTO(out, result = PTR_ERR(io));
160
161         result = io->ci_result;
162         if (result < 0)
163                 GOTO(out_io, result);
164
165         io->u.ci_fault.ft_mkwrite = 1;
166         io->u.ci_fault.ft_writable = 1;
167
168         vio = vvp_env_io(env);
169         vio->u.fault.ft_vma    = vma;
170         vio->u.fault.ft_vmpage = vmpage;
171
172         siginitsetinv(&new, sigmask(SIGKILL) | sigmask(SIGTERM));
173         sigprocmask(SIG_BLOCK, &new, &old);
174
175         inode = vvp_object_inode(io->ci_obj);
176         lli = ll_i2info(inode);
177
178         result = cl_io_loop(env, io);
179
180         sigprocmask(SIG_SETMASK, &old, NULL);
181
182         if (result == 0) {
183                 lock_page(vmpage);
184                 if (vmpage->mapping == NULL) {
185                         unlock_page(vmpage);
186
187                         /* page was truncated and lock was cancelled, return
188                          * ENODATA so that VM_FAULT_NOPAGE will be returned
189                          * to handle_mm_fault(). */
190                         if (result == 0)
191                                 result = -ENODATA;
192                 } else if (!PageDirty(vmpage)) {
193                         /* race, the page has been cleaned by ptlrpcd after
194                          * it was unlocked, it has to be added into dirty
195                          * cache again otherwise this soon-to-dirty page won't
196                          * consume any grants, even worse if this page is being
197                          * transferred because it will break RPC checksum.
198                          */
199                         unlock_page(vmpage);
200
201                         CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
202                                "been written out, retry.\n",
203                                vmpage, vmpage->index);
204
205                         *retry = true;
206                         result = -EAGAIN;
207                 }
208
209                 if (result == 0)
210                         set_bit(LLIF_DATA_MODIFIED, &lli->lli_flags);
211         }
212         EXIT;
213
214 out_io:
215         cl_io_fini(env, io);
216 out:
217         cl_env_put(env, &refcheck);
218         CDEBUG(D_MMAP, "%s mkwrite with %d\n", current->comm, result);
219         LASSERT(ergo(result == 0, PageLocked(vmpage)));
220
221         /* if page has been unmapped, presumably due to lock reclaim for
222          * concurrent usage, add some delay before retrying to prevent
223          * entering live-lock situation with competitors
224          */
225         if (result == -ENODATA && inode != NULL) {
226                 CDEBUG(D_MMAP, "delaying new page-fault for inode %p to "
227                                "prevent live-lock\n", inode);
228                 msleep(10);
229         }
230
231         return result;
232 }
233
234 static inline int to_fault_error(int result)
235 {
236         switch(result) {
237         case 0:
238                 result = VM_FAULT_LOCKED;
239                 break;
240         case -ENOMEM:
241                 result = VM_FAULT_OOM;
242                 break;
243         default:
244                 result = VM_FAULT_SIGBUS;
245                 break;
246         }
247         return result;
248 }
249
250 /**
251  * Lustre implementation of a vm_operations_struct::fault() method, called by
252  * VM to server page fault (both in kernel and user space).
253  *
254  * \param vma - is virtiual area struct related to page fault
255  * \param vmf - structure which describe type and address where hit fault
256  *
257  * \return allocated and filled _locked_ page for address
258  * \retval VM_FAULT_ERROR on general error
259  * \retval NOPAGE_OOM not have memory for allocate new page
260  */
261 static vm_fault_t ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
262 {
263         struct lu_env           *env;
264         struct cl_io            *io;
265         struct vvp_io           *vio = NULL;
266         struct page             *vmpage;
267         int                      result = 0;
268         int                      fault_ret = 0;
269         __u16                    refcheck;
270         ENTRY;
271
272         env = cl_env_get(&refcheck);
273         if (IS_ERR(env))
274                 RETURN(PTR_ERR(env));
275
276         if (ll_sbi_has_fast_read(ll_i2sbi(file_inode(vma->vm_file)))) {
277                 /* do fast fault */
278                 bool has_retry = vmf->flags & FAULT_FLAG_RETRY_NOWAIT;
279
280                 /* To avoid loops, instruct downstream to not drop mmap_sem */
281                 vmf->flags |= FAULT_FLAG_RETRY_NOWAIT;
282                 ll_cl_add(vma->vm_file, env, NULL, LCC_MMAP);
283                 fault_ret = ll_filemap_fault(vma, vmf);
284                 ll_cl_remove(vma->vm_file, env);
285                 if (!has_retry)
286                         vmf->flags &= ~FAULT_FLAG_RETRY_NOWAIT;
287
288                 /* - If there is no error, then the page was found in cache and
289                  *   uptodate;
290                  * - If VM_FAULT_RETRY is set, the page existed but failed to
291                  *   lock. We will try slow path to avoid loops.
292                  * - Otherwise, it should try normal fault under DLM lock. */
293                 if (!(fault_ret & VM_FAULT_RETRY) &&
294                     !(fault_ret & VM_FAULT_ERROR))
295                         GOTO(out, result = 0);
296
297                 fault_ret = 0;
298         }
299
300         io = ll_fault_io_init(env, vma, vmf->pgoff);
301         if (IS_ERR(io))
302                 GOTO(out, result = PTR_ERR(io));
303
304         result = io->ci_result;
305         if (result == 0) {
306                 vio = vvp_env_io(env);
307                 vio->u.fault.ft_vma       = vma;
308                 vio->u.fault.ft_vmpage    = NULL;
309                 vio->u.fault.ft_vmf = vmf;
310                 vio->u.fault.ft_flags = 0;
311                 vio->u.fault.ft_flags_valid = 0;
312
313                 /* May call ll_readpage() */
314                 ll_cl_add(vma->vm_file, env, io, LCC_MMAP);
315
316                 result = cl_io_loop(env, io);
317
318                 ll_cl_remove(vma->vm_file, env);
319
320                 /* ft_flags are only valid if we reached
321                  * the call to filemap_fault */
322                 if (vio->u.fault.ft_flags_valid)
323                         fault_ret = vio->u.fault.ft_flags;
324
325                 vmpage = vio->u.fault.ft_vmpage;
326                 if (result != 0 && vmpage != NULL) {
327                         put_page(vmpage);
328                         vmf->page = NULL;
329                 }
330         }
331         cl_io_fini(env, io);
332
333 out:
334         cl_env_put(env, &refcheck);
335         if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
336                 fault_ret |= to_fault_error(result);
337
338         CDEBUG(D_MMAP, "%s fault %d/%d\n", current->comm, fault_ret, result);
339         RETURN(fault_ret);
340 }
341
342 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
343 static vm_fault_t ll_fault(struct vm_fault *vmf)
344 {
345         struct vm_area_struct *vma = vmf->vma;
346 #else
347 static vm_fault_t ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
348 {
349 #endif
350         int count = 0;
351         bool printed = false;
352         bool cached;
353         vm_fault_t result;
354         ktime_t kstart = ktime_get();
355         sigset_t old, new;
356
357         result = pcc_fault(vma, vmf, &cached);
358         if (cached)
359                 goto out;
360
361         CDEBUG(D_MMAP, DFID": vma=%p start=%#lx end=%#lx vm_flags=%#lx\n",
362                PFID(&ll_i2info(file_inode(vma->vm_file))->lli_fid),
363                vma, vma->vm_start, vma->vm_end, vma->vm_flags);
364
365         /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
366          * so that it can be killed by admin but not cause segfault by
367          * other signals.
368          */
369         siginitsetinv(&new, sigmask(SIGKILL) | sigmask(SIGTERM));
370         sigprocmask(SIG_BLOCK, &new, &old);
371
372         /* make sure offset is not a negative number */
373         if (vmf->pgoff > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
374                 return VM_FAULT_SIGBUS;
375
376 restart:
377         result = ll_fault0(vma, vmf);
378         if (vmf->page &&
379             !(result & (VM_FAULT_RETRY | VM_FAULT_ERROR | VM_FAULT_LOCKED))) {
380                 struct page *vmpage = vmf->page;
381
382                 /* check if this page has been truncated */
383                 lock_page(vmpage);
384                 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
385                         unlock_page(vmpage);
386                         put_page(vmpage);
387                         vmf->page = NULL;
388
389                         if (!printed && ++count > 16) {
390                                 CWARN("the page is under heavy contention, maybe your app(%s) needs revising :-)\n",
391                                       current->comm);
392                                 printed = true;
393                         }
394
395                         goto restart;
396                 }
397
398                 result |= VM_FAULT_LOCKED;
399         }
400         sigprocmask(SIG_SETMASK, &old, NULL);
401
402 out:
403         if (vmf->page && result == VM_FAULT_LOCKED) {
404                 ll_rw_stats_tally(ll_i2sbi(file_inode(vma->vm_file)),
405                                   current->pid, vma->vm_file->private_data,
406                                   cl_offset(NULL, vmf->page->index), PAGE_SIZE,
407                                   READ);
408                 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
409                                    LPROC_LL_FAULT,
410                                    ktime_us_delta(ktime_get(), kstart));
411         }
412
413         return result;
414 }
415
416 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
417 static vm_fault_t ll_page_mkwrite(struct vm_fault *vmf)
418 {
419         struct vm_area_struct *vma = vmf->vma;
420 #else
421 static vm_fault_t ll_page_mkwrite(struct vm_area_struct *vma,
422                                   struct vm_fault *vmf)
423 {
424 #endif
425         int count = 0;
426         bool printed = false;
427         bool retry;
428         bool cached;
429         ktime_t kstart = ktime_get();
430         vm_fault_t result;
431
432         result = pcc_page_mkwrite(vma, vmf, &cached);
433         if (cached)
434                 goto out;
435
436         file_update_time(vma->vm_file);
437         do {
438                 retry = false;
439                 result = ll_page_mkwrite0(vma, vmf->page, &retry);
440
441                 if (!printed && ++count > 16) {
442                         const struct dentry *de = file_dentry(vma->vm_file);
443
444                         CWARN("app(%s): the page %lu of file "DFID" is under heavy contention\n",
445                               current->comm, vmf->pgoff,
446                               PFID(ll_inode2fid(de->d_inode)));
447                         printed = true;
448                 }
449         } while (retry);
450
451         switch (result) {
452         case 0:
453                 LASSERT(PageLocked(vmf->page));
454                 result = VM_FAULT_LOCKED;
455                 break;
456         case -ENODATA:
457         case -EFAULT:
458                 result = VM_FAULT_NOPAGE;
459                 break;
460         case -ENOMEM:
461                 result = VM_FAULT_OOM;
462                 break;
463         case -EAGAIN:
464                 result = VM_FAULT_RETRY;
465                 break;
466         default:
467                 result = VM_FAULT_SIGBUS;
468                 break;
469         }
470
471 out:
472         if (result == VM_FAULT_LOCKED) {
473                 ll_rw_stats_tally(ll_i2sbi(file_inode(vma->vm_file)),
474                                   current->pid, vma->vm_file->private_data,
475                                   cl_offset(NULL, vmf->page->index), PAGE_SIZE,
476                                   WRITE);
477                 ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
478                                    LPROC_LL_MKWRITE,
479                                    ktime_us_delta(ktime_get(), kstart));
480         }
481
482         return result;
483 }
484
485 /**
486  *  To avoid cancel the locks covering mmapped region for lock cache pressure,
487  *  we track the mapped vma count in vvp_object::vob_mmap_cnt.
488  */
489 static void ll_vm_open(struct vm_area_struct * vma)
490 {
491         struct inode *inode    = file_inode(vma->vm_file);
492         struct vvp_object *vob = cl_inode2vvp(inode);
493
494         ENTRY;
495         LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
496         atomic_inc(&vob->vob_mmap_cnt);
497         pcc_vm_open(vma);
498         EXIT;
499 }
500
501 /**
502  * Dual to ll_vm_open().
503  */
504 static void ll_vm_close(struct vm_area_struct *vma)
505 {
506         struct inode      *inode = file_inode(vma->vm_file);
507         struct vvp_object *vob   = cl_inode2vvp(inode);
508
509         ENTRY;
510         atomic_dec(&vob->vob_mmap_cnt);
511         LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
512         pcc_vm_close(vma);
513         EXIT;
514 }
515
516 static const struct vm_operations_struct ll_file_vm_ops = {
517         .fault                  = ll_fault,
518         .page_mkwrite           = ll_page_mkwrite,
519         .open                   = ll_vm_open,
520         .close                  = ll_vm_close,
521 };
522
523 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
524 {
525         struct inode *inode = file_inode(file);
526         ktime_t kstart = ktime_get();
527         bool cached;
528         int rc;
529
530         ENTRY;
531         CDEBUG(D_VFSTRACE | D_MMAP,
532                "VFS_Op: fid="DFID" vma=%p start=%#lx end=%#lx vm_flags=%#lx\n",
533                PFID(&ll_i2info(inode)->lli_fid),
534                vma, vma->vm_start, vma->vm_end, vma->vm_flags);
535
536         if (ll_file_nolock(file))
537                 RETURN(-EOPNOTSUPP);
538
539         rc = pcc_file_mmap(file, vma, &cached);
540         if (cached && rc != 0)
541                 RETURN(rc);
542
543         rc = generic_file_mmap(file, vma);
544         if (rc == 0) {
545                 vma->vm_ops = &ll_file_vm_ops;
546                 vma->vm_ops->open(vma);
547                 /* update the inode's size and mtime */
548                 if (!cached)
549                         rc = ll_glimpse_size(inode);
550         }
551
552         if (!rc)
553                 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MMAP,
554                                    ktime_us_delta(ktime_get(), kstart));
555
556         RETURN(rc);
557 }