Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
0002 /*
0003  * Copyright(c) 2015-2017 Intel Corporation.
0004  */
0005 
0006 #include <linux/mm.h>
0007 #include <linux/sched/signal.h>
0008 #include <linux/device.h>
0009 #include <linux/module.h>
0010 
0011 #include "hfi.h"
0012 
0013 static unsigned long cache_size = 256;
0014 module_param(cache_size, ulong, S_IRUGO | S_IWUSR);
0015 MODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
0016 
0017 /*
0018  * Determine whether the caller can pin pages.
0019  *
0020  * This function should be used in the implementation of buffer caches.
0021  * The cache implementation should call this function prior to attempting
0022  * to pin buffer pages in order to determine whether they should do so.
0023  * The function computes cache limits based on the configured ulimit and
0024  * cache size. Use of this function is especially important for caches
0025  * which are not limited in any other way (e.g. by HW resources) and, thus,
0026  * could keeping caching buffers.
0027  *
0028  */
0029 bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
0030             u32 nlocked, u32 npages)
0031 {
0032     unsigned long ulimit = rlimit(RLIMIT_MEMLOCK), pinned, cache_limit,
0033         size = (cache_size * (1UL << 20)); /* convert to bytes */
0034     unsigned int usr_ctxts =
0035             dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt;
0036     bool can_lock = capable(CAP_IPC_LOCK);
0037 
0038     /*
0039      * Calculate per-cache size. The calculation below uses only a quarter
0040      * of the available per-context limit. This leaves space for other
0041      * pinning. Should we worry about shared ctxts?
0042      */
0043     cache_limit = (ulimit / usr_ctxts) / 4;
0044 
0045     /* If ulimit isn't set to "unlimited" and is smaller than cache_size. */
0046     if (ulimit != (-1UL) && size > cache_limit)
0047         size = cache_limit;
0048 
0049     /* Convert to number of pages */
0050     size = DIV_ROUND_UP(size, PAGE_SIZE);
0051 
0052     pinned = atomic64_read(&mm->pinned_vm);
0053 
0054     /* First, check the absolute limit against all pinned pages. */
0055     if (pinned + npages >= ulimit && !can_lock)
0056         return false;
0057 
0058     return ((nlocked + npages) <= size) || can_lock;
0059 }
0060 
0061 int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
0062                 bool writable, struct page **pages)
0063 {
0064     int ret;
0065     unsigned int gup_flags = FOLL_LONGTERM | (writable ? FOLL_WRITE : 0);
0066 
0067     ret = pin_user_pages_fast(vaddr, npages, gup_flags, pages);
0068     if (ret < 0)
0069         return ret;
0070 
0071     atomic64_add(ret, &mm->pinned_vm);
0072 
0073     return ret;
0074 }
0075 
0076 void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
0077                  size_t npages, bool dirty)
0078 {
0079     unpin_user_pages_dirty_lock(p, npages, dirty);
0080 
0081     if (mm) { /* during close after signal, mm can be NULL */
0082         atomic64_sub(npages, &mm->pinned_vm);
0083     }
0084 }