18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2015-2017 Intel Corporation.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
168c2ecf20Sopenharmony_ci * General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * BSD LICENSE
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
228c2ecf20Sopenharmony_ci * are met:
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *  - Redistributions of source code must retain the above copyright
258c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
268c2ecf20Sopenharmony_ci *  - Redistributions in binary form must reproduce the above copyright
278c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
288c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
298c2ecf20Sopenharmony_ci *    distribution.
308c2ecf20Sopenharmony_ci *  - Neither the name of Intel Corporation nor the names of its
318c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
328c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include <linux/mm.h>
498c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
508c2ecf20Sopenharmony_ci#include <linux/device.h>
518c2ecf20Sopenharmony_ci#include <linux/module.h>
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#include "hfi.h"
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic unsigned long cache_size = 256;
568c2ecf20Sopenharmony_cimodule_param(cache_size, ulong, S_IRUGO | S_IWUSR);
578c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * Determine whether the caller can pin pages.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * This function should be used in the implementation of buffer caches.
638c2ecf20Sopenharmony_ci * The cache implementation should call this function prior to attempting
648c2ecf20Sopenharmony_ci * to pin buffer pages in order to determine whether they should do so.
658c2ecf20Sopenharmony_ci * The function computes cache limits based on the configured ulimit and
668c2ecf20Sopenharmony_ci * cache size. Use of this function is especially important for caches
678c2ecf20Sopenharmony_ci * which are not limited in any other way (e.g. by HW resources) and, thus,
688c2ecf20Sopenharmony_ci * could keeping caching buffers.
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_cibool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
728c2ecf20Sopenharmony_ci			u32 nlocked, u32 npages)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	unsigned long ulimit = rlimit(RLIMIT_MEMLOCK), pinned, cache_limit,
758c2ecf20Sopenharmony_ci		size = (cache_size * (1UL << 20)); /* convert to bytes */
768c2ecf20Sopenharmony_ci	unsigned int usr_ctxts =
778c2ecf20Sopenharmony_ci			dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt;
788c2ecf20Sopenharmony_ci	bool can_lock = capable(CAP_IPC_LOCK);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * Calculate per-cache size. The calculation below uses only a quarter
828c2ecf20Sopenharmony_ci	 * of the available per-context limit. This leaves space for other
838c2ecf20Sopenharmony_ci	 * pinning. Should we worry about shared ctxts?
848c2ecf20Sopenharmony_ci	 */
858c2ecf20Sopenharmony_ci	cache_limit = (ulimit / usr_ctxts) / 4;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* If ulimit isn't set to "unlimited" and is smaller than cache_size. */
888c2ecf20Sopenharmony_ci	if (ulimit != (-1UL) && size > cache_limit)
898c2ecf20Sopenharmony_ci		size = cache_limit;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/* Convert to number of pages */
928c2ecf20Sopenharmony_ci	size = DIV_ROUND_UP(size, PAGE_SIZE);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	pinned = atomic64_read(&mm->pinned_vm);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* First, check the absolute limit against all pinned pages. */
978c2ecf20Sopenharmony_ci	if (pinned + npages >= ulimit && !can_lock)
988c2ecf20Sopenharmony_ci		return false;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return ((nlocked + npages) <= size) || can_lock;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciint hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
1048c2ecf20Sopenharmony_ci			    bool writable, struct page **pages)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	int ret;
1078c2ecf20Sopenharmony_ci	unsigned int gup_flags = FOLL_LONGTERM | (writable ? FOLL_WRITE : 0);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	ret = pin_user_pages_fast(vaddr, npages, gup_flags, pages);
1108c2ecf20Sopenharmony_ci	if (ret < 0)
1118c2ecf20Sopenharmony_ci		return ret;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	atomic64_add(ret, &mm->pinned_vm);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return ret;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_civoid hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
1198c2ecf20Sopenharmony_ci			     size_t npages, bool dirty)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	unpin_user_pages_dirty_lock(p, npages, dirty);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (mm) { /* during close after signal, mm can be NULL */
1248c2ecf20Sopenharmony_ci		atomic64_sub(npages, &mm->pinned_vm);
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci}
127