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