Whamcloud - gitweb
LU-10149 llite: avoid live-lock when concurrent mmap()s
[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/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/string.h>
36 #include <linux/stat.h>
37 #include <linux/errno.h>
38 #include <linux/unistd.h>
39 #include <linux/version.h>
40 #include <asm/uaccess.h>
41
42 #include <linux/fs.h>
43 #include <linux/stat.h>
44 #include <asm/uaccess.h>
45 #include <linux/mm.h>
46 #include <linux/pagemap.h>
47
48 #define DEBUG_SUBSYSTEM S_LLITE
49
50 #include "llite_internal.h"
51 #include <lustre_compat.h>
52
53 static const struct vm_operations_struct ll_file_vm_ops;
54
55 void policy_from_vma(union ldlm_policy_data *policy, struct vm_area_struct *vma,
56                      unsigned long addr, size_t count)
57 {
58         policy->l_extent.start = ((addr - vma->vm_start) & PAGE_MASK) +
59                                  (vma->vm_pgoff << PAGE_SHIFT);
60         policy->l_extent.end = (policy->l_extent.start + count - 1) |
61                                ~PAGE_MASK;
62 }
63
64 struct vm_area_struct *our_vma(struct mm_struct *mm, unsigned long addr,
65                                size_t count)
66 {
67         struct vm_area_struct *vma, *ret = NULL;
68         ENTRY;
69
70         /* mmap_sem must have been held by caller. */
71         LASSERT(!down_write_trylock(&mm->mmap_sem));
72
73         for(vma = find_vma(mm, addr);
74             vma != NULL && vma->vm_start < (addr + count); vma = vma->vm_next) {
75                 if (vma->vm_ops && vma->vm_ops == &ll_file_vm_ops &&
76                     vma->vm_flags & VM_SHARED) {
77                         ret = vma;
78                         break;
79                 }
80         }
81         RETURN(ret);
82 }
83
84 /**
85  * API independent part for page fault initialization.
86  * \param env - corespondent lu_env to processing
87  * \param vma - virtual memory area addressed to page fault
88  * \param index - page index corespondent to fault.
89  * \parm ra_flags - vma readahead flags.
90  *
91  * \return error codes from cl_io_init.
92  */
93 static struct cl_io *
94 ll_fault_io_init(struct lu_env *env, struct vm_area_struct *vma,
95                  pgoff_t index, unsigned long *ra_flags)
96 {
97         struct file            *file = vma->vm_file;
98         struct inode           *inode = file_inode(file);
99         struct cl_io           *io;
100         struct cl_fault_io     *fio;
101         int                     rc;
102         ENTRY;
103
104         if (ll_file_nolock(file))
105                 RETURN(ERR_PTR(-EOPNOTSUPP));
106
107 restart:
108         io = vvp_env_thread_io(env);
109         io->ci_obj = ll_i2info(inode)->lli_clob;
110         LASSERT(io->ci_obj != NULL);
111
112         fio = &io->u.ci_fault;
113         fio->ft_index      = index;
114         fio->ft_executable = vma->vm_flags&VM_EXEC;
115
116         /*
117          * disable VM_SEQ_READ and use VM_RAND_READ to make sure that
118          * the kernel will not read other pages not covered by ldlm in
119          * filemap_nopage. we do our readahead in ll_readpage.
120          */
121         if (ra_flags != NULL)
122                 *ra_flags = vma->vm_flags & (VM_RAND_READ|VM_SEQ_READ);
123         vma->vm_flags &= ~VM_SEQ_READ;
124         vma->vm_flags |= VM_RAND_READ;
125
126         CDEBUG(D_MMAP, "vm_flags: %lx (%lu %d)\n", vma->vm_flags,
127                fio->ft_index, fio->ft_executable);
128
129         rc = cl_io_init(env, io, CIT_FAULT, io->ci_obj);
130         if (rc == 0) {
131                 struct vvp_io *vio = vvp_env_io(env);
132                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
133
134                 LASSERT(vio->vui_cl.cis_io == io);
135
136                 /* mmap lock must be MANDATORY it has to cache
137                  * pages. */
138                 io->ci_lockreq = CILR_MANDATORY;
139                 vio->vui_fd = fd;
140         } else {
141                 LASSERT(rc < 0);
142                 cl_io_fini(env, io);
143                 if (io->ci_need_restart)
144                         goto restart;
145
146                 io = ERR_PTR(rc);
147         }
148
149         RETURN(io);
150 }
151
152 /* Sharing code of page_mkwrite method for rhel5 and rhel6 */
153 static int ll_page_mkwrite0(struct vm_area_struct *vma, struct page *vmpage,
154                             bool *retry)
155 {
156         struct lu_env           *env;
157         struct cl_io            *io;
158         struct vvp_io           *vio;
159         int                      result;
160         __u16                    refcheck;
161         sigset_t                 set;
162         struct inode             *inode = NULL;
163         struct ll_inode_info     *lli;
164         ENTRY;
165
166         LASSERT(vmpage != NULL);
167         env = cl_env_get(&refcheck);
168         if (IS_ERR(env))
169                 RETURN(PTR_ERR(env));
170
171         io = ll_fault_io_init(env, vma, vmpage->index, NULL);
172         if (IS_ERR(io))
173                 GOTO(out, result = PTR_ERR(io));
174
175         result = io->ci_result;
176         if (result < 0)
177                 GOTO(out_io, result);
178
179         io->u.ci_fault.ft_mkwrite = 1;
180         io->u.ci_fault.ft_writable = 1;
181
182         vio = vvp_env_io(env);
183         vio->u.fault.ft_vma    = vma;
184         vio->u.fault.ft_vmpage = vmpage;
185
186         set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
187
188         inode = vvp_object_inode(io->ci_obj);
189         lli = ll_i2info(inode);
190
191         result = cl_io_loop(env, io);
192
193         cfs_restore_sigs(set);
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                         if (result == 0)
204                                 result = -ENODATA;
205                 } else if (!PageDirty(vmpage)) {
206                         /* race, the page has been cleaned by ptlrpcd after
207                          * it was unlocked, it has to be added into dirty
208                          * cache again otherwise this soon-to-dirty page won't
209                          * consume any grants, even worse if this page is being
210                          * transferred because it will break RPC checksum.
211                          */
212                         unlock_page(vmpage);
213
214                         CDEBUG(D_MMAP, "Race on page_mkwrite %p/%lu, page has "
215                                "been written out, retry.\n",
216                                vmpage, vmpage->index);
217
218                         *retry = true;
219                         result = -EAGAIN;
220                 }
221
222                 if (result == 0)
223                         ll_file_set_flag(lli, LLIF_DATA_MODIFIED);
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 "
240                                "prevent live-lock\n", 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 -EFAULT:
254                 result = VM_FAULT_NOPAGE;
255                 break;
256         case -ENOMEM:
257                 result = VM_FAULT_OOM;
258                 break;
259         default:
260                 result = VM_FAULT_SIGBUS;
261                 break;
262         }
263         return result;
264 }
265
266 /**
267  * Lustre implementation of a vm_operations_struct::fault() method, called by
268  * VM to server page fault (both in kernel and user space).
269  *
270  * \param vma - is virtiual area struct related to page fault
271  * \param vmf - structure which describe type and address where hit fault
272  *
273  * \return allocated and filled _locked_ page for address
274  * \retval VM_FAULT_ERROR on general error
275  * \retval NOPAGE_OOM not have memory for allocate new page
276  */
277 static int ll_fault0(struct vm_area_struct *vma, struct vm_fault *vmf)
278 {
279         struct lu_env           *env;
280         struct cl_io            *io;
281         struct vvp_io           *vio = NULL;
282         struct page             *vmpage;
283         unsigned long            ra_flags;
284         int                      result = 0;
285         int                      fault_ret = 0;
286         __u16                    refcheck;
287         ENTRY;
288
289         env = cl_env_get(&refcheck);
290         if (IS_ERR(env))
291                 RETURN(PTR_ERR(env));
292
293         if (ll_sbi_has_fast_read(ll_i2sbi(file_inode(vma->vm_file)))) {
294                 /* do fast fault */
295                 ll_cl_add(vma->vm_file, env, NULL, LCC_MMAP);
296                 fault_ret = ll_filemap_fault(vma, vmf);
297                 ll_cl_remove(vma->vm_file, env);
298
299                 /* - If there is no error, then the page was found in cache and
300                  *   uptodate;
301                  * - If VM_FAULT_RETRY is set, the page existed but failed to
302                  *   lock. It will return to kernel and retry;
303                  * - Otherwise, it should try normal fault under DLM lock. */
304                 if ((fault_ret & VM_FAULT_RETRY) ||
305                     !(fault_ret & VM_FAULT_ERROR))
306                         GOTO(out, result = 0);
307
308                 fault_ret = 0;
309         }
310
311         io = ll_fault_io_init(env, vma, vmf->pgoff, &ra_flags);
312         if (IS_ERR(io))
313                 GOTO(out, result = PTR_ERR(io));
314
315         result = io->ci_result;
316         if (result == 0) {
317                 vio = vvp_env_io(env);
318                 vio->u.fault.ft_vma       = vma;
319                 vio->u.fault.ft_vmpage    = NULL;
320                 vio->u.fault.ft_vmf = vmf;
321                 vio->u.fault.ft_flags = 0;
322                 vio->u.fault.ft_flags_valid = 0;
323
324                 /* May call ll_readpage() */
325                 ll_cl_add(vma->vm_file, env, io, LCC_MMAP);
326
327                 result = cl_io_loop(env, io);
328
329                 ll_cl_remove(vma->vm_file, env);
330
331                 /* ft_flags are only valid if we reached
332                  * the call to filemap_fault */
333                 if (vio->u.fault.ft_flags_valid)
334                         fault_ret = vio->u.fault.ft_flags;
335
336                 vmpage = vio->u.fault.ft_vmpage;
337                 if (result != 0 && vmpage != NULL) {
338                         put_page(vmpage);
339                         vmf->page = NULL;
340                 }
341         }
342         cl_io_fini(env, io);
343
344         vma->vm_flags |= ra_flags;
345
346 out:
347         cl_env_put(env, &refcheck);
348         if (result != 0 && !(fault_ret & VM_FAULT_RETRY))
349                 fault_ret |= to_fault_error(result);
350
351         CDEBUG(D_MMAP, "%s fault %d/%d\n", current->comm, fault_ret, result);
352         RETURN(fault_ret);
353 }
354
355 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
356 static int ll_fault(struct vm_fault *vmf)
357 {
358         struct vm_area_struct *vma = vmf->vma;
359 #else
360 static int ll_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
361 {
362 #endif
363         int count = 0;
364         bool printed = false;
365         int result;
366         sigset_t set;
367
368         /* Only SIGKILL and SIGTERM is allowed for fault/nopage/mkwrite
369          * so that it can be killed by admin but not cause segfault by
370          * other signals. */
371         set = cfs_block_sigsinv(sigmask(SIGKILL) | sigmask(SIGTERM));
372
373         ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
374                            LPROC_LL_FAULT, 1);
375
376 restart:
377         result = ll_fault0(vma, vmf);
378         if (!(result & (VM_FAULT_RETRY | VM_FAULT_ERROR | VM_FAULT_LOCKED))) {
379                 struct page *vmpage = vmf->page;
380
381                 /* check if this page has been truncated */
382                 lock_page(vmpage);
383                 if (unlikely(vmpage->mapping == NULL)) { /* unlucky */
384                         unlock_page(vmpage);
385                         put_page(vmpage);
386                         vmf->page = NULL;
387
388                         if (!printed && ++count > 16) {
389                                 CWARN("the page is under heavy contention,"
390                                       "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         cfs_restore_sigs(set);
401         return result;
402 }
403
404 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
405 static int ll_page_mkwrite(struct vm_fault *vmf)
406 {
407         struct vm_area_struct *vma = vmf->vma;
408 #else
409 static int ll_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
410 {
411 #endif
412         int count = 0;
413         bool printed = false;
414         bool retry;
415         int result;
416
417         ll_stats_ops_tally(ll_i2sbi(file_inode(vma->vm_file)),
418                            LPROC_LL_MKWRITE, 1);
419
420         file_update_time(vma->vm_file);
421         do {
422                 retry = false;
423                 result = ll_page_mkwrite0(vma, vmf->page, &retry);
424
425                 if (!printed && ++count > 16) {
426                         const struct dentry *de = file_dentry(vma->vm_file);
427
428                         CWARN("app(%s): the page %lu of file "DFID" is under"
429                               " heavy contention\n",
430                               current->comm, vmf->pgoff,
431                               PFID(ll_inode2fid(de->d_inode)));
432                         printed = true;
433                 }
434         } while (retry);
435
436         switch(result) {
437         case 0:
438                 LASSERT(PageLocked(vmf->page));
439                 result = VM_FAULT_LOCKED;
440                 break;
441         case -ENODATA:
442         case -EFAULT:
443                 result = VM_FAULT_NOPAGE;
444                 break;
445         case -ENOMEM:
446                 result = VM_FAULT_OOM;
447                 break;
448         case -EAGAIN:
449                 result = VM_FAULT_RETRY;
450                 break;
451         default:
452                 result = VM_FAULT_SIGBUS;
453                 break;
454         }
455
456         return result;
457 }
458
459 /**
460  *  To avoid cancel the locks covering mmapped region for lock cache pressure,
461  *  we track the mapped vma count in vvp_object::vob_mmap_cnt.
462  */
463 static void ll_vm_open(struct vm_area_struct * vma)
464 {
465         struct inode *inode    = file_inode(vma->vm_file);
466         struct vvp_object *vob = cl_inode2vvp(inode);
467
468         ENTRY;
469         LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
470         atomic_inc(&vob->vob_mmap_cnt);
471         EXIT;
472 }
473
474 /**
475  * Dual to ll_vm_open().
476  */
477 static void ll_vm_close(struct vm_area_struct *vma)
478 {
479         struct inode      *inode = file_inode(vma->vm_file);
480         struct vvp_object *vob   = cl_inode2vvp(inode);
481
482         ENTRY;
483         atomic_dec(&vob->vob_mmap_cnt);
484         LASSERT(atomic_read(&vob->vob_mmap_cnt) >= 0);
485         EXIT;
486 }
487
488 /* XXX put nice comment here.  talk about __free_pte -> dirty pages and
489  * nopage's reference passing to the pte */
490 int ll_teardown_mmaps(struct address_space *mapping, __u64 first, __u64 last)
491 {
492         int rc = -ENOENT;
493         ENTRY;
494
495         LASSERTF(last > first, "last %llu first %llu\n", last, first);
496         if (mapping_mapped(mapping)) {
497                 rc = 0;
498                 unmap_mapping_range(mapping, first + PAGE_SIZE - 1,
499                                     last - first + 1, 0);
500         }
501
502         RETURN(rc);
503 }
504
505 static const struct vm_operations_struct ll_file_vm_ops = {
506         .fault                  = ll_fault,
507         .page_mkwrite           = ll_page_mkwrite,
508         .open                   = ll_vm_open,
509         .close                  = ll_vm_close,
510 };
511
512 int ll_file_mmap(struct file *file, struct vm_area_struct * vma)
513 {
514         struct inode *inode = file_inode(file);
515         int rc;
516         ENTRY;
517
518         if (ll_file_nolock(file))
519                 RETURN(-EOPNOTSUPP);
520
521         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_MAP, 1);
522         rc = generic_file_mmap(file, vma);
523         if (rc == 0) {
524                 vma->vm_ops = &ll_file_vm_ops;
525                 vma->vm_ops->open(vma);
526                 /* update the inode's size and mtime */
527                 rc = ll_glimpse_size(inode);
528         }
529
530         RETURN(rc);
531 }