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