Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs_mem.c revision 1.4.28.1
      1  1.4.28.1    tls /*	$NetBSD: tmpfs_mem.c,v 1.4.28.1 2014/08/10 06:55:54 tls Exp $	*/
      2       1.1  rmind 
      3       1.1  rmind /*
      4       1.4  rmind  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      5       1.1  rmind  * All rights reserved.
      6       1.1  rmind  *
      7       1.4  rmind  * This code is derived from software contributed to The NetBSD Foundation
      8       1.4  rmind  * by Mindaugas Rasiukevicius.
      9       1.4  rmind  *
     10       1.1  rmind  * Redistribution and use in source and binary forms, with or without
     11       1.1  rmind  * modification, are permitted provided that the following conditions
     12       1.1  rmind  * are met:
     13       1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     14       1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     15       1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  rmind  *    documentation and/or other materials provided with the distribution.
     18       1.1  rmind  *
     19       1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  rmind  */
     31       1.1  rmind 
     32       1.1  rmind /*
     33       1.1  rmind  * tmpfs memory allocation routines.
     34       1.1  rmind  * Implements memory usage accounting and limiting.
     35       1.1  rmind  */
     36       1.1  rmind 
     37       1.1  rmind #include <sys/cdefs.h>
     38  1.4.28.1    tls __KERNEL_RCSID(0, "$NetBSD: tmpfs_mem.c,v 1.4.28.1 2014/08/10 06:55:54 tls Exp $");
     39       1.1  rmind 
     40       1.1  rmind #include <sys/param.h>
     41       1.1  rmind #include <sys/atomic.h>
     42       1.1  rmind #include <sys/kmem.h>
     43       1.1  rmind #include <sys/namei.h>
     44       1.1  rmind #include <sys/pool.h>
     45       1.1  rmind 
     46       1.1  rmind #include <fs/tmpfs/tmpfs.h>
     47       1.1  rmind 
     48       1.3  rmind extern struct pool	tmpfs_dirent_pool;
     49       1.3  rmind extern struct pool	tmpfs_node_pool;
     50       1.3  rmind 
     51       1.1  rmind void
     52       1.1  rmind tmpfs_mntmem_init(struct tmpfs_mount *mp, uint64_t memlimit)
     53       1.1  rmind {
     54       1.1  rmind 
     55       1.1  rmind 	mutex_init(&mp->tm_acc_lock, MUTEX_DEFAULT, IPL_NONE);
     56       1.1  rmind 	mp->tm_mem_limit = memlimit;
     57       1.1  rmind 	mp->tm_bytes_used = 0;
     58       1.1  rmind }
     59       1.1  rmind 
     60       1.1  rmind void
     61       1.1  rmind tmpfs_mntmem_destroy(struct tmpfs_mount *mp)
     62       1.1  rmind {
     63       1.1  rmind 
     64       1.1  rmind 	KASSERT(mp->tm_bytes_used == 0);
     65       1.1  rmind 	mutex_destroy(&mp->tm_acc_lock);
     66       1.1  rmind }
     67       1.1  rmind 
     68  1.4.28.1    tls int
     69  1.4.28.1    tls tmpfs_mntmem_set(struct tmpfs_mount *mp, uint64_t memlimit)
     70  1.4.28.1    tls {
     71  1.4.28.1    tls 	int error;
     72  1.4.28.1    tls 
     73  1.4.28.1    tls 	mutex_enter(&mp->tm_acc_lock);
     74  1.4.28.1    tls 	if (round_page(mp->tm_bytes_used) >= memlimit)
     75  1.4.28.1    tls 		error = EBUSY;
     76  1.4.28.1    tls 	else {
     77  1.4.28.1    tls 		error = 0;
     78  1.4.28.1    tls 		mp->tm_mem_limit = memlimit;
     79  1.4.28.1    tls 	}
     80  1.4.28.1    tls 	mutex_exit(&mp->tm_acc_lock);
     81  1.4.28.1    tls 	return error;
     82  1.4.28.1    tls }
     83  1.4.28.1    tls 
     84  1.4.28.1    tls 
     85  1.4.28.1    tls 
     86       1.1  rmind /*
     87       1.1  rmind  * tmpfs_mem_info: return the number of available memory pages.
     88       1.1  rmind  *
     89       1.1  rmind  * => If 'total' is true, then return _total_ amount of pages.
     90       1.1  rmind  * => If false, then return the amount of _free_ memory pages.
     91       1.1  rmind  *
     92  1.4.28.1    tls  * Remember to remove uvmexp.freetarg from the returned value to avoid
     93       1.1  rmind  * excessive memory usage.
     94       1.1  rmind  */
     95       1.1  rmind size_t
     96       1.1  rmind tmpfs_mem_info(bool total)
     97       1.1  rmind {
     98       1.1  rmind 	size_t size = 0;
     99       1.1  rmind 
    100       1.1  rmind 	/* XXX: unlocked */
    101       1.1  rmind 	size += uvmexp.swpgavail;
    102       1.1  rmind 	if (!total) {
    103       1.1  rmind 		size -= uvmexp.swpgonly;
    104       1.1  rmind 	}
    105       1.1  rmind 	size += uvmexp.free;
    106       1.1  rmind 	size += uvmexp.filepages;
    107       1.1  rmind 	if (size > uvmexp.wired) {
    108       1.1  rmind 		size -= uvmexp.wired;
    109       1.1  rmind 	} else {
    110       1.1  rmind 		size = 0;
    111       1.1  rmind 	}
    112       1.1  rmind 	return size;
    113       1.1  rmind }
    114       1.1  rmind 
    115       1.1  rmind uint64_t
    116       1.1  rmind tmpfs_bytes_max(struct tmpfs_mount *mp)
    117       1.1  rmind {
    118  1.4.28.1    tls 	psize_t freepages = tmpfs_mem_info(false);
    119       1.1  rmind 	uint64_t avail_mem;
    120       1.1  rmind 
    121  1.4.28.1    tls 	if (freepages < uvmexp.freetarg) {
    122       1.1  rmind 		freepages = 0;
    123       1.1  rmind 	} else {
    124  1.4.28.1    tls 		freepages -= uvmexp.freetarg;
    125       1.1  rmind 	}
    126       1.1  rmind 	avail_mem = round_page(mp->tm_bytes_used) + (freepages << PAGE_SHIFT);
    127       1.2  rmind 	return MIN(mp->tm_mem_limit, avail_mem);
    128       1.1  rmind }
    129       1.1  rmind 
    130       1.1  rmind size_t
    131       1.1  rmind tmpfs_pages_avail(struct tmpfs_mount *mp)
    132       1.1  rmind {
    133       1.1  rmind 
    134       1.1  rmind 	return (tmpfs_bytes_max(mp) - mp->tm_bytes_used) >> PAGE_SHIFT;
    135       1.1  rmind }
    136       1.1  rmind 
    137       1.1  rmind bool
    138       1.1  rmind tmpfs_mem_incr(struct tmpfs_mount *mp, size_t sz)
    139       1.1  rmind {
    140       1.2  rmind 	uint64_t lim;
    141       1.1  rmind 
    142       1.1  rmind 	mutex_enter(&mp->tm_acc_lock);
    143       1.2  rmind 	lim = tmpfs_bytes_max(mp);
    144       1.1  rmind 	if (mp->tm_bytes_used + sz >= lim) {
    145       1.1  rmind 		mutex_exit(&mp->tm_acc_lock);
    146       1.1  rmind 		return false;
    147       1.1  rmind 	}
    148       1.1  rmind 	mp->tm_bytes_used += sz;
    149       1.1  rmind 	mutex_exit(&mp->tm_acc_lock);
    150       1.1  rmind 	return true;
    151       1.1  rmind }
    152       1.1  rmind 
    153       1.1  rmind void
    154       1.1  rmind tmpfs_mem_decr(struct tmpfs_mount *mp, size_t sz)
    155       1.1  rmind {
    156       1.1  rmind 
    157       1.1  rmind 	mutex_enter(&mp->tm_acc_lock);
    158       1.1  rmind 	KASSERT(mp->tm_bytes_used >= sz);
    159       1.1  rmind 	mp->tm_bytes_used -= sz;
    160       1.1  rmind 	mutex_exit(&mp->tm_acc_lock);
    161       1.1  rmind }
    162       1.1  rmind 
    163       1.1  rmind struct tmpfs_dirent *
    164       1.1  rmind tmpfs_dirent_get(struct tmpfs_mount *mp)
    165       1.1  rmind {
    166       1.1  rmind 
    167       1.1  rmind 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_dirent))) {
    168       1.1  rmind 		return NULL;
    169       1.1  rmind 	}
    170       1.3  rmind 	return pool_get(&tmpfs_dirent_pool, PR_WAITOK);
    171       1.1  rmind }
    172       1.1  rmind 
    173       1.1  rmind void
    174       1.1  rmind tmpfs_dirent_put(struct tmpfs_mount *mp, struct tmpfs_dirent *de)
    175       1.1  rmind {
    176       1.1  rmind 
    177       1.1  rmind 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_dirent));
    178       1.3  rmind 	pool_put(&tmpfs_dirent_pool, de);
    179       1.1  rmind }
    180       1.1  rmind 
    181       1.1  rmind struct tmpfs_node *
    182       1.1  rmind tmpfs_node_get(struct tmpfs_mount *mp)
    183       1.1  rmind {
    184       1.1  rmind 
    185       1.4  rmind 	if (atomic_inc_uint_nv(&mp->tm_nodes_cnt) >= mp->tm_nodes_max) {
    186       1.4  rmind 		atomic_dec_uint(&mp->tm_nodes_cnt);
    187       1.4  rmind 		return NULL;
    188       1.4  rmind 	}
    189       1.1  rmind 	if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_node))) {
    190       1.1  rmind 		return NULL;
    191       1.1  rmind 	}
    192       1.3  rmind 	return pool_get(&tmpfs_node_pool, PR_WAITOK);
    193       1.1  rmind }
    194       1.1  rmind 
    195       1.1  rmind void
    196       1.1  rmind tmpfs_node_put(struct tmpfs_mount *mp, struct tmpfs_node *tn)
    197       1.1  rmind {
    198       1.1  rmind 
    199       1.4  rmind 	atomic_dec_uint(&mp->tm_nodes_cnt);
    200       1.1  rmind 	tmpfs_mem_decr(mp, sizeof(struct tmpfs_node));
    201       1.3  rmind 	pool_put(&tmpfs_node_pool, tn);
    202       1.1  rmind }
    203       1.1  rmind 
    204       1.1  rmind /*
    205       1.1  rmind  * Quantum size to round-up the tmpfs names in order to reduce re-allocations.
    206       1.1  rmind  */
    207       1.1  rmind 
    208       1.1  rmind #define	TMPFS_NAME_QUANTUM	(32)
    209       1.1  rmind 
    210       1.1  rmind char *
    211       1.1  rmind tmpfs_strname_alloc(struct tmpfs_mount *mp, size_t len)
    212       1.1  rmind {
    213       1.1  rmind 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
    214       1.1  rmind 
    215       1.1  rmind 	KASSERT(sz > 0 && sz <= 1024);
    216       1.1  rmind 	if (!tmpfs_mem_incr(mp, sz)) {
    217       1.1  rmind 		return NULL;
    218       1.1  rmind 	}
    219       1.1  rmind 	return kmem_alloc(sz, KM_SLEEP);
    220       1.1  rmind }
    221       1.1  rmind 
    222       1.1  rmind void
    223       1.1  rmind tmpfs_strname_free(struct tmpfs_mount *mp, char *str, size_t len)
    224       1.1  rmind {
    225       1.1  rmind 	const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM);
    226       1.1  rmind 
    227       1.1  rmind 	KASSERT(sz > 0 && sz <= 1024);
    228       1.1  rmind 	tmpfs_mem_decr(mp, sz);
    229       1.1  rmind 	kmem_free(str, sz);
    230       1.1  rmind }
    231       1.1  rmind 
    232       1.1  rmind bool
    233       1.1  rmind tmpfs_strname_neqlen(struct componentname *fcnp, struct componentname *tcnp)
    234       1.1  rmind {
    235       1.1  rmind 	const size_t fln = roundup2(fcnp->cn_namelen, TMPFS_NAME_QUANTUM);
    236       1.1  rmind 	const size_t tln = roundup2(tcnp->cn_namelen, TMPFS_NAME_QUANTUM);
    237       1.1  rmind 
    238       1.1  rmind 	return (fln != tln) || memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fln);
    239       1.1  rmind }
    240