cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

user_pages.c (2575B)


      1// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
      2/*
      3 * Copyright(c) 2015-2017 Intel Corporation.
      4 */
      5
      6#include <linux/mm.h>
      7#include <linux/sched/signal.h>
      8#include <linux/device.h>
      9#include <linux/module.h>
     10
     11#include "hfi.h"
     12
     13static unsigned long cache_size = 256;
     14module_param(cache_size, ulong, S_IRUGO | S_IWUSR);
     15MODULE_PARM_DESC(cache_size, "Send and receive side cache size limit (in MB)");
     16
     17/*
     18 * Determine whether the caller can pin pages.
     19 *
     20 * This function should be used in the implementation of buffer caches.
     21 * The cache implementation should call this function prior to attempting
     22 * to pin buffer pages in order to determine whether they should do so.
     23 * The function computes cache limits based on the configured ulimit and
     24 * cache size. Use of this function is especially important for caches
     25 * which are not limited in any other way (e.g. by HW resources) and, thus,
     26 * could keeping caching buffers.
     27 *
     28 */
     29bool hfi1_can_pin_pages(struct hfi1_devdata *dd, struct mm_struct *mm,
     30			u32 nlocked, u32 npages)
     31{
     32	unsigned long ulimit = rlimit(RLIMIT_MEMLOCK), pinned, cache_limit,
     33		size = (cache_size * (1UL << 20)); /* convert to bytes */
     34	unsigned int usr_ctxts =
     35			dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt;
     36	bool can_lock = capable(CAP_IPC_LOCK);
     37
     38	/*
     39	 * Calculate per-cache size. The calculation below uses only a quarter
     40	 * of the available per-context limit. This leaves space for other
     41	 * pinning. Should we worry about shared ctxts?
     42	 */
     43	cache_limit = (ulimit / usr_ctxts) / 4;
     44
     45	/* If ulimit isn't set to "unlimited" and is smaller than cache_size. */
     46	if (ulimit != (-1UL) && size > cache_limit)
     47		size = cache_limit;
     48
     49	/* Convert to number of pages */
     50	size = DIV_ROUND_UP(size, PAGE_SIZE);
     51
     52	pinned = atomic64_read(&mm->pinned_vm);
     53
     54	/* First, check the absolute limit against all pinned pages. */
     55	if (pinned + npages >= ulimit && !can_lock)
     56		return false;
     57
     58	return ((nlocked + npages) <= size) || can_lock;
     59}
     60
     61int hfi1_acquire_user_pages(struct mm_struct *mm, unsigned long vaddr, size_t npages,
     62			    bool writable, struct page **pages)
     63{
     64	int ret;
     65	unsigned int gup_flags = FOLL_LONGTERM | (writable ? FOLL_WRITE : 0);
     66
     67	ret = pin_user_pages_fast(vaddr, npages, gup_flags, pages);
     68	if (ret < 0)
     69		return ret;
     70
     71	atomic64_add(ret, &mm->pinned_vm);
     72
     73	return ret;
     74}
     75
     76void hfi1_release_user_pages(struct mm_struct *mm, struct page **p,
     77			     size_t npages, bool dirty)
     78{
     79	unpin_user_pages_dirty_lock(p, npages, dirty);
     80
     81	if (mm) { /* during close after signal, mm can be NULL */
     82		atomic64_sub(npages, &mm->pinned_vm);
     83	}
     84}