proc_misc.c 6.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  linux/fs/proc/proc_misc.c
 *
 *  linux/fs/proc/array.c
 *  Copyright (C) 1992  by Linus Torvalds
 *  based on ideas by Darren Senn
 *
 *  This used to be the part of array.c. See the rest of history and credits
 *  there. I took this into a separate file and switched the thing to generic
 *  proc_file_inode_operations, leaving in array.c only per-process stuff.
 *  Inumbers allocation made dynamic (via create_proc_entry()).  AV, May 1999.
 *
 * Changes:
 * Fulton Green      :  Encapsulated position metric calculations.
 *			<kernel@FultonGreen.com>
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/kernel_stat.h>
23
#include <linux/fs.h>
L
Linus Torvalds 已提交
24 25 26
#include <linux/tty.h>
#include <linux/string.h>
#include <linux/mman.h>
27
#include <linux/quicklist.h>
L
Linus Torvalds 已提交
28 29 30 31 32
#include <linux/proc_fs.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/mmzone.h>
#include <linux/pagemap.h>
33
#include <linux/irq.h>
A
Adrian Bunk 已提交
34
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
35 36
#include <linux/swap.h>
#include <linux/slab.h>
A
Adrian Bunk 已提交
37
#include <linux/genhd.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44
#include <linux/smp.h>
#include <linux/signal.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/seq_file.h>
#include <linux/times.h>
#include <linux/profile.h>
A
Andrew Morton 已提交
45
#include <linux/utsname.h>
L
Linus Torvalds 已提交
46 47 48 49
#include <linux/blkdev.h>
#include <linux/hugetlb.h>
#include <linux/jiffies.h>
#include <linux/vmalloc.h>
50
#include <linux/crash_dump.h>
51
#include <linux/pid_namespace.h>
52
#include <linux/bootmem.h>
L
Linus Torvalds 已提交
53 54 55 56 57 58 59
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/io.h>
#include <asm/tlb.h>
#include <asm/div64.h>
#include "internal.h"

N
Nikita Danilov 已提交
60 61 62 63 64
static int zoneinfo_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &zoneinfo_op);
}

65
static const struct file_operations proc_zoneinfo_file_operations = {
N
Nikita Danilov 已提交
66 67 68 69 70 71
	.open		= zoneinfo_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= seq_release,
};

L
Linus Torvalds 已提交
72 73 74 75
static int vmstat_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &vmstat_op);
}
76
static const struct file_operations proc_vmstat_file_operations = {
L
Linus Torvalds 已提交
77 78 79 80 81 82
	.open		= vmstat_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= seq_release,
};

83
#ifdef CONFIG_BLOCK
L
Linus Torvalds 已提交
84 85 86 87
static int diskstats_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &diskstats_op);
}
88
static const struct file_operations proc_diskstats_operations = {
L
Linus Torvalds 已提交
89 90 91 92 93
	.open		= diskstats_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= seq_release,
};
94
#endif
L
Linus Torvalds 已提交
95 96

#ifdef CONFIG_MODULES
97
extern const struct seq_operations modules_op;
L
Linus Torvalds 已提交
98 99 100 101
static int modules_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &modules_op);
}
102
static const struct file_operations proc_modules_operations = {
L
Linus Torvalds 已提交
103 104 105 106 107 108 109
	.open		= modules_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= seq_release,
};
#endif

110
#ifdef CONFIG_PROC_PAGE_MONITOR
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#define KPMSIZE sizeof(u64)
#define KPMMASK (KPMSIZE - 1)
/* /proc/kpagecount - an array exposing page counts
 *
 * Each entry is a u64 representing the corresponding
 * physical page count.
 */
static ssize_t kpagecount_read(struct file *file, char __user *buf,
			     size_t count, loff_t *ppos)
{
	u64 __user *out = (u64 __user *)buf;
	struct page *ppage;
	unsigned long src = *ppos;
	unsigned long pfn;
	ssize_t ret = 0;
	u64 pcount;

	pfn = src / KPMSIZE;
	count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
	if (src & KPMMASK || count & KPMMASK)
131
		return -EINVAL;
132 133

	while (count > 0) {
134 135 136 137
		ppage = NULL;
		if (pfn_valid(pfn))
			ppage = pfn_to_page(pfn);
		pfn++;
138 139 140
		if (!ppage)
			pcount = 0;
		else
141
			pcount = page_mapcount(ppage);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

		if (put_user(pcount, out++)) {
			ret = -EFAULT;
			break;
		}

		count -= KPMSIZE;
	}

	*ppos += (char __user *)out - buf;
	if (!ret)
		ret = (char __user *)out - buf;
	return ret;
}

static struct file_operations proc_kpagecount_operations = {
	.llseek = mem_lseek,
	.read = kpagecount_read,
};

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/* /proc/kpageflags - an array exposing page flags
 *
 * Each entry is a u64 representing the corresponding
 * physical page flags.
 */

/* These macros are used to decouple internal flags from exported ones */

#define KPF_LOCKED     0
#define KPF_ERROR      1
#define KPF_REFERENCED 2
#define KPF_UPTODATE   3
#define KPF_DIRTY      4
#define KPF_LRU        5
#define KPF_ACTIVE     6
#define KPF_SLAB       7
#define KPF_WRITEBACK  8
#define KPF_RECLAIM    9
#define KPF_BUDDY     10

#define kpf_copy_bit(flags, srcpos, dstpos) (((flags >> srcpos) & 1) << dstpos)

static ssize_t kpageflags_read(struct file *file, char __user *buf,
			     size_t count, loff_t *ppos)
{
	u64 __user *out = (u64 __user *)buf;
	struct page *ppage;
	unsigned long src = *ppos;
	unsigned long pfn;
	ssize_t ret = 0;
	u64 kflags, uflags;

	pfn = src / KPMSIZE;
	count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
	if (src & KPMMASK || count & KPMMASK)
197
		return -EINVAL;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

	while (count > 0) {
		ppage = NULL;
		if (pfn_valid(pfn))
			ppage = pfn_to_page(pfn);
		pfn++;
		if (!ppage)
			kflags = 0;
		else
			kflags = ppage->flags;

		uflags = kpf_copy_bit(KPF_LOCKED, PG_locked, kflags) |
			kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
			kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
			kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
			kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
			kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
			kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
			kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
			kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
			kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
			kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);

		if (put_user(uflags, out++)) {
			ret = -EFAULT;
			break;
		}

		count -= KPMSIZE;
	}

	*ppos += (char __user *)out - buf;
	if (!ret)
		ret = (char __user *)out - buf;
	return ret;
}

static struct file_operations proc_kpageflags_operations = {
	.llseek = mem_lseek,
	.read = kpageflags_read,
};
239
#endif /* CONFIG_PROC_PAGE_MONITOR */
240

L
Linus Torvalds 已提交
241 242 243 244 245 246 247
struct proc_dir_entry *proc_root_kcore;

void __init proc_misc_init(void)
{
	proc_symlink("mounts", NULL, "self/mounts");

	/* And now for trickier ones */
A
Alexey Dobriyan 已提交
248 249
	proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
	proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
250
#ifdef CONFIG_BLOCK
A
Alexey Dobriyan 已提交
251
	proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
252
#endif
L
Linus Torvalds 已提交
253
#ifdef CONFIG_MODULES
A
Alexey Dobriyan 已提交
254
	proc_create("modules", 0, NULL, &proc_modules_operations);
L
Linus Torvalds 已提交
255 256
#endif
#ifdef CONFIG_SCHEDSTATS
A
Alexey Dobriyan 已提交
257
	proc_create("schedstat", 0, NULL, &proc_schedstat_operations);
L
Linus Torvalds 已提交
258 259
#endif
#ifdef CONFIG_PROC_KCORE
A
Alexey Dobriyan 已提交
260 261
	proc_root_kcore = proc_create("kcore", S_IRUSR, NULL, &proc_kcore_operations);
	if (proc_root_kcore)
L
Linus Torvalds 已提交
262 263 264
		proc_root_kcore->size =
				(size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
#endif
265
#ifdef CONFIG_PROC_PAGE_MONITOR
A
Alexey Dobriyan 已提交
266 267
	proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
	proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
268
#endif
269
#ifdef CONFIG_PROC_VMCORE
A
Alexey Dobriyan 已提交
270
	proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations);
271
#endif
L
Linus Torvalds 已提交
272
}