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