2006-09-27 10:50:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
|
|
|
|
* reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of version 2 of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SN Platform Special Memory (mspec) Support
|
|
|
|
*
|
|
|
|
* This driver exports the SN special memory (mspec) facility to user
|
|
|
|
* processes.
|
|
|
|
* There are three types of memory made available thru this driver:
|
|
|
|
* fetchops, uncached and cached.
|
|
|
|
*
|
|
|
|
* Fetchops are atomic memory operations that are implemented in the
|
|
|
|
* memory controller on SGI SN hardware.
|
|
|
|
*
|
|
|
|
* Uncached are used for memory write combining feature of the ia64
|
|
|
|
* cpu.
|
|
|
|
*
|
|
|
|
* Cached are used for areas of memory that are used as cached addresses
|
|
|
|
* on our partition and used as uncached addresses from other partitions.
|
|
|
|
* Due to a design constraint of the SN2 Shub, you can not have processors
|
|
|
|
* on the same FSB perform both a cached and uncached reference to the
|
|
|
|
* same cache line. These special memory cached regions prevent the
|
|
|
|
* kernel from ever dropping in a TLB entry and therefore prevent the
|
|
|
|
* processor from ever speculating a cache line from this page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/mm.h>
|
2007-07-30 00:36:13 +02:00
|
|
|
#include <linux/fs.h>
|
2006-09-27 10:50:11 +02:00
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/numa.h>
|
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/pgtable.h>
|
2011-07-27 01:09:06 +02:00
|
|
|
#include <linux/atomic.h>
|
2006-09-27 10:50:11 +02:00
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/uncached.h>
|
|
|
|
#include <asm/sn/addrs.h>
|
|
|
|
#include <asm/sn/arch.h>
|
|
|
|
#include <asm/sn/mspec.h>
|
|
|
|
#include <asm/sn/sn_cpuid.h>
|
|
|
|
#include <asm/sn/io.h>
|
|
|
|
#include <asm/sn/bte.h>
|
|
|
|
#include <asm/sn/shubio.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define FETCHOP_ID "SGI Fetchop,"
|
|
|
|
#define CACHED_ID "Cached,"
|
|
|
|
#define UNCACHED_ID "Uncached"
|
|
|
|
#define REVISION "4.0"
|
|
|
|
#define MSPEC_BASENAME "mspec"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Page types allocated by the device.
|
|
|
|
*/
|
2007-09-19 07:46:31 +02:00
|
|
|
enum mspec_page_type {
|
2006-09-27 10:50:11 +02:00
|
|
|
MSPEC_FETCHOP = 1,
|
|
|
|
MSPEC_CACHED,
|
|
|
|
MSPEC_UNCACHED
|
|
|
|
};
|
|
|
|
|
2006-11-10 21:27:49 +01:00
|
|
|
#ifdef CONFIG_SGI_SN
|
2006-09-27 10:50:11 +02:00
|
|
|
static int is_sn2;
|
2006-11-10 21:27:49 +01:00
|
|
|
#else
|
|
|
|
#define is_sn2 0
|
|
|
|
#endif
|
2006-09-27 10:50:11 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* One of these structures is allocated when an mspec region is mmaped. The
|
|
|
|
* structure is pointed to by the vma->vm_private_data field in the vma struct.
|
|
|
|
* This structure is used to record the addresses of the mspec pages.
|
2007-09-19 07:46:31 +02:00
|
|
|
* This structure is shared by all vma's that are split off from the
|
|
|
|
* original vma when split_vma()'s are done.
|
|
|
|
*
|
|
|
|
* The refcnt is incremented atomically because mm->mmap_sem does not
|
|
|
|
* protect in fork case where multiple tasks share the vma_data.
|
2006-09-27 10:50:11 +02:00
|
|
|
*/
|
|
|
|
struct vma_data {
|
|
|
|
atomic_t refcnt; /* Number of vmas sharing the data. */
|
2007-09-19 07:46:31 +02:00
|
|
|
spinlock_t lock; /* Serialize access to this structure. */
|
2006-09-27 10:50:11 +02:00
|
|
|
int count; /* Number of pages allocated. */
|
2007-09-19 07:46:31 +02:00
|
|
|
enum mspec_page_type type; /* Type of pages allocated. */
|
|
|
|
int flags; /* See VMD_xxx below. */
|
|
|
|
unsigned long vm_start; /* Original (unsplit) base. */
|
|
|
|
unsigned long vm_end; /* Original (unsplit) end. */
|
2006-09-27 10:50:11 +02:00
|
|
|
unsigned long maddr[0]; /* Array of MSPEC addresses. */
|
|
|
|
};
|
|
|
|
|
2007-09-19 07:46:31 +02:00
|
|
|
#define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */
|
|
|
|
|
2006-09-27 10:50:11 +02:00
|
|
|
/* used on shub2 to clear FOP cache in the HUB */
|
|
|
|
static unsigned long scratch_page[MAX_NUMNODES];
|
|
|
|
#define SH2_AMO_CACHE_ENTRIES 4
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
mspec_zero_block(unsigned long addr, int len)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (is_sn2) {
|
|
|
|
if (is_shub2()) {
|
|
|
|
int nid;
|
|
|
|
void *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
|
|
|
|
p = (void *)TO_AMO(scratch_page[nid]);
|
|
|
|
|
|
|
|
for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
|
|
|
|
FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
|
|
|
|
p += FETCHOP_VAR_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
|
|
|
|
BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
|
|
|
|
} else {
|
|
|
|
memset((char *) addr, 0, len);
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mspec_open
|
|
|
|
*
|
|
|
|
* Called when a device mapping is created by a means other than mmap
|
2007-09-19 07:46:31 +02:00
|
|
|
* (via fork, munmap, etc.). Increments the reference count on the
|
|
|
|
* underlying mspec data so it is not freed prematurely.
|
2006-09-27 10:50:11 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mspec_open(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct vma_data *vdata;
|
|
|
|
|
|
|
|
vdata = vma->vm_private_data;
|
|
|
|
atomic_inc(&vdata->refcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mspec_close
|
|
|
|
*
|
|
|
|
* Called when unmapping a device mapping. Frees all mspec pages
|
2007-09-25 06:24:41 +02:00
|
|
|
* belonging to all the vma's sharing this vma_data structure.
|
2006-09-27 10:50:11 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mspec_close(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct vma_data *vdata;
|
2007-09-25 06:24:41 +02:00
|
|
|
int index, last_index;
|
2007-09-19 07:46:31 +02:00
|
|
|
unsigned long my_page;
|
2006-09-27 10:50:11 +02:00
|
|
|
|
|
|
|
vdata = vma->vm_private_data;
|
|
|
|
|
2007-09-25 06:24:41 +02:00
|
|
|
if (!atomic_dec_and_test(&vdata->refcnt))
|
|
|
|
return;
|
2007-09-19 07:46:31 +02:00
|
|
|
|
2007-09-25 06:24:41 +02:00
|
|
|
last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
|
|
|
|
for (index = 0; index < last_index; index++) {
|
2007-09-19 07:46:31 +02:00
|
|
|
if (vdata->maddr[index] == 0)
|
2006-09-27 10:50:11 +02:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Clear the page before sticking it back
|
|
|
|
* into the pool.
|
|
|
|
*/
|
2007-09-19 07:46:31 +02:00
|
|
|
my_page = vdata->maddr[index];
|
|
|
|
vdata->maddr[index] = 0;
|
2007-09-25 06:24:41 +02:00
|
|
|
if (!mspec_zero_block(my_page, PAGE_SIZE))
|
2008-04-25 22:22:19 +02:00
|
|
|
uncached_free_page(my_page, 1);
|
2006-09-27 10:50:11 +02:00
|
|
|
else
|
|
|
|
printk(KERN_WARNING "mspec_close(): "
|
2007-09-25 06:24:41 +02:00
|
|
|
"failed to zero page %ld\n", my_page);
|
2006-09-27 10:50:11 +02:00
|
|
|
}
|
2007-09-19 07:46:31 +02:00
|
|
|
|
|
|
|
if (vdata->flags & VMD_VMALLOCED)
|
2006-09-27 10:50:11 +02:00
|
|
|
vfree(vdata);
|
2007-09-19 07:46:31 +02:00
|
|
|
else
|
|
|
|
kfree(vdata);
|
2006-09-27 10:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-07-24 06:27:00 +02:00
|
|
|
* mspec_fault
|
2006-09-27 10:50:11 +02:00
|
|
|
*
|
|
|
|
* Creates a mspec page and maps it to user space.
|
|
|
|
*/
|
2008-07-24 06:27:00 +02:00
|
|
|
static int
|
|
|
|
mspec_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
2006-09-27 10:50:11 +02:00
|
|
|
{
|
|
|
|
unsigned long paddr, maddr;
|
|
|
|
unsigned long pfn;
|
2008-07-24 06:27:00 +02:00
|
|
|
pgoff_t index = vmf->pgoff;
|
2006-09-27 10:50:11 +02:00
|
|
|
struct vma_data *vdata = vma->vm_private_data;
|
|
|
|
|
|
|
|
maddr = (volatile unsigned long) vdata->maddr[index];
|
|
|
|
if (maddr == 0) {
|
2008-04-25 22:22:19 +02:00
|
|
|
maddr = uncached_alloc_page(numa_node_id(), 1);
|
2006-09-27 10:50:11 +02:00
|
|
|
if (maddr == 0)
|
2008-07-24 06:27:00 +02:00
|
|
|
return VM_FAULT_OOM;
|
2006-09-27 10:50:11 +02:00
|
|
|
|
|
|
|
spin_lock(&vdata->lock);
|
|
|
|
if (vdata->maddr[index] == 0) {
|
|
|
|
vdata->count++;
|
|
|
|
vdata->maddr[index] = maddr;
|
|
|
|
} else {
|
2008-04-25 22:22:19 +02:00
|
|
|
uncached_free_page(maddr, 1);
|
2006-09-27 10:50:11 +02:00
|
|
|
maddr = vdata->maddr[index];
|
|
|
|
}
|
|
|
|
spin_unlock(&vdata->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vdata->type == MSPEC_FETCHOP)
|
|
|
|
paddr = TO_AMO(maddr);
|
|
|
|
else
|
2006-11-10 21:27:49 +01:00
|
|
|
paddr = maddr & ~__IA64_UNCACHED_OFFSET;
|
2006-09-27 10:50:11 +02:00
|
|
|
|
|
|
|
pfn = paddr >> PAGE_SHIFT;
|
|
|
|
|
2008-07-24 06:27:00 +02:00
|
|
|
/*
|
|
|
|
* vm_insert_pfn can fail with -EBUSY, but in that case it will
|
|
|
|
* be because another thread has installed the pte first, so it
|
|
|
|
* is no problem.
|
|
|
|
*/
|
|
|
|
vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
|
|
|
|
|
|
|
|
return VM_FAULT_NOPAGE;
|
2006-09-27 10:50:11 +02:00
|
|
|
}
|
|
|
|
|
2009-09-27 20:29:37 +02:00
|
|
|
static const struct vm_operations_struct mspec_vm_ops = {
|
2006-09-27 10:50:11 +02:00
|
|
|
.open = mspec_open,
|
|
|
|
.close = mspec_close,
|
2008-07-24 06:27:00 +02:00
|
|
|
.fault = mspec_fault,
|
2006-09-27 10:50:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mspec_mmap
|
|
|
|
*
|
tree-wide: fix assorted typos all over the place
That is "success", "unknown", "through", "performance", "[re|un]mapping"
, "access", "default", "reasonable", "[con]currently", "temperature"
, "channel", "[un]used", "application", "example","hierarchy", "therefore"
, "[over|under]flow", "contiguous", "threshold", "enough" and others.
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2009-11-14 16:09:05 +01:00
|
|
|
* Called when mmapping the device. Initializes the vma with a fault handler
|
2006-09-27 10:50:11 +02:00
|
|
|
* and private data structure necessary to allocate, track, and free the
|
|
|
|
* underlying pages.
|
|
|
|
*/
|
|
|
|
static int
|
2007-09-19 07:46:31 +02:00
|
|
|
mspec_mmap(struct file *file, struct vm_area_struct *vma,
|
|
|
|
enum mspec_page_type type)
|
2006-09-27 10:50:11 +02:00
|
|
|
{
|
|
|
|
struct vma_data *vdata;
|
2007-09-19 07:46:31 +02:00
|
|
|
int pages, vdata_size, flags = 0;
|
2006-09-27 10:50:11 +02:00
|
|
|
|
|
|
|
if (vma->vm_pgoff != 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if ((vma->vm_flags & VM_SHARED) == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if ((vma->vm_flags & VM_WRITE) == 0)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
|
|
|
|
vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
|
|
|
|
if (vdata_size <= PAGE_SIZE)
|
2011-05-27 01:25:56 +02:00
|
|
|
vdata = kzalloc(vdata_size, GFP_KERNEL);
|
2007-09-19 07:46:31 +02:00
|
|
|
else {
|
2011-05-27 01:25:56 +02:00
|
|
|
vdata = vzalloc(vdata_size);
|
2007-09-19 07:46:31 +02:00
|
|
|
flags = VMD_VMALLOCED;
|
|
|
|
}
|
2006-09-27 10:50:11 +02:00
|
|
|
if (!vdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-09-19 07:46:31 +02:00
|
|
|
vdata->vm_start = vma->vm_start;
|
|
|
|
vdata->vm_end = vma->vm_end;
|
|
|
|
vdata->flags = flags;
|
2006-09-27 10:50:11 +02:00
|
|
|
vdata->type = type;
|
|
|
|
spin_lock_init(&vdata->lock);
|
|
|
|
vdata->refcnt = ATOMIC_INIT(1);
|
|
|
|
vma->vm_private_data = vdata;
|
|
|
|
|
2008-02-02 03:08:53 +01:00
|
|
|
vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP | VM_DONTEXPAND);
|
2006-09-27 10:50:11 +02:00
|
|
|
if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
|
|
|
|
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
|
|
|
|
vma->vm_ops = &mspec_vm_ops;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fetchop_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return mspec_mmap(file, vma, MSPEC_FETCHOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cached_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return mspec_mmap(file, vma, MSPEC_CACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
uncached_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return mspec_mmap(file, vma, MSPEC_UNCACHED);
|
|
|
|
}
|
|
|
|
|
2007-02-12 09:55:32 +01:00
|
|
|
static const struct file_operations fetchop_fops = {
|
2006-09-27 10:50:11 +02:00
|
|
|
.owner = THIS_MODULE,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 18:52:59 +02:00
|
|
|
.mmap = fetchop_mmap,
|
|
|
|
.llseek = noop_llseek,
|
2006-09-27 10:50:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice fetchop_miscdev = {
|
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
|
|
|
.name = "sgi_fetchop",
|
|
|
|
.fops = &fetchop_fops
|
|
|
|
};
|
|
|
|
|
2007-02-12 09:55:32 +01:00
|
|
|
static const struct file_operations cached_fops = {
|
2006-09-27 10:50:11 +02:00
|
|
|
.owner = THIS_MODULE,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 18:52:59 +02:00
|
|
|
.mmap = cached_mmap,
|
|
|
|
.llseek = noop_llseek,
|
2006-09-27 10:50:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice cached_miscdev = {
|
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
|
|
|
.name = "mspec_cached",
|
|
|
|
.fops = &cached_fops
|
|
|
|
};
|
|
|
|
|
2007-02-12 09:55:32 +01:00
|
|
|
static const struct file_operations uncached_fops = {
|
2006-09-27 10:50:11 +02:00
|
|
|
.owner = THIS_MODULE,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 18:52:59 +02:00
|
|
|
.mmap = uncached_mmap,
|
|
|
|
.llseek = noop_llseek,
|
2006-09-27 10:50:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice uncached_miscdev = {
|
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
|
|
|
.name = "mspec_uncached",
|
|
|
|
.fops = &uncached_fops
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mspec_init
|
|
|
|
*
|
|
|
|
* Called at boot time to initialize the mspec facility.
|
|
|
|
*/
|
|
|
|
static int __init
|
|
|
|
mspec_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int nid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The fetchop device only works on SN2 hardware, uncached and cached
|
|
|
|
* memory drivers should both be valid on all ia64 hardware
|
|
|
|
*/
|
2006-11-10 21:27:49 +01:00
|
|
|
#ifdef CONFIG_SGI_SN
|
2006-09-27 10:50:11 +02:00
|
|
|
if (ia64_platform_is("sn2")) {
|
|
|
|
is_sn2 = 1;
|
|
|
|
if (is_shub2()) {
|
|
|
|
ret = -ENOMEM;
|
2007-10-16 10:25:33 +02:00
|
|
|
for_each_node_state(nid, N_ONLINE) {
|
2006-09-27 10:50:11 +02:00
|
|
|
int actual_nid;
|
|
|
|
int nasid;
|
|
|
|
unsigned long phys;
|
|
|
|
|
2008-04-25 22:22:19 +02:00
|
|
|
scratch_page[nid] = uncached_alloc_page(nid, 1);
|
2006-09-27 10:50:11 +02:00
|
|
|
if (scratch_page[nid] == 0)
|
|
|
|
goto free_scratch_pages;
|
|
|
|
phys = __pa(scratch_page[nid]);
|
|
|
|
nasid = get_node_number(phys);
|
|
|
|
actual_nid = nasid_to_cnodeid(nasid);
|
|
|
|
if (actual_nid != nid)
|
|
|
|
goto free_scratch_pages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = misc_register(&fetchop_miscdev);
|
|
|
|
if (ret) {
|
|
|
|
printk(KERN_ERR
|
|
|
|
"%s: failed to register device %i\n",
|
|
|
|
FETCHOP_ID, ret);
|
|
|
|
goto free_scratch_pages;
|
|
|
|
}
|
|
|
|
}
|
2006-11-10 21:27:49 +01:00
|
|
|
#endif
|
2006-09-27 10:50:11 +02:00
|
|
|
ret = misc_register(&cached_miscdev);
|
|
|
|
if (ret) {
|
|
|
|
printk(KERN_ERR "%s: failed to register device %i\n",
|
|
|
|
CACHED_ID, ret);
|
|
|
|
if (is_sn2)
|
|
|
|
misc_deregister(&fetchop_miscdev);
|
|
|
|
goto free_scratch_pages;
|
|
|
|
}
|
|
|
|
ret = misc_register(&uncached_miscdev);
|
|
|
|
if (ret) {
|
|
|
|
printk(KERN_ERR "%s: failed to register device %i\n",
|
|
|
|
UNCACHED_ID, ret);
|
|
|
|
misc_deregister(&cached_miscdev);
|
|
|
|
if (is_sn2)
|
|
|
|
misc_deregister(&fetchop_miscdev);
|
|
|
|
goto free_scratch_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
|
|
|
|
MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
|
|
|
|
CACHED_ID, UNCACHED_ID);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
free_scratch_pages:
|
|
|
|
for_each_node(nid) {
|
|
|
|
if (scratch_page[nid] != 0)
|
2008-04-25 22:22:19 +02:00
|
|
|
uncached_free_page(scratch_page[nid], 1);
|
2006-09-27 10:50:11 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit
|
|
|
|
mspec_exit(void)
|
|
|
|
{
|
|
|
|
int nid;
|
|
|
|
|
|
|
|
misc_deregister(&uncached_miscdev);
|
|
|
|
misc_deregister(&cached_miscdev);
|
|
|
|
if (is_sn2) {
|
|
|
|
misc_deregister(&fetchop_miscdev);
|
|
|
|
|
|
|
|
for_each_node(nid) {
|
|
|
|
if (scratch_page[nid] != 0)
|
2008-04-25 22:22:19 +02:00
|
|
|
uncached_free_page(scratch_page[nid], 1);
|
2006-09-27 10:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(mspec_init);
|
|
|
|
module_exit(mspec_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
|
|
|
|
MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
|
|
|
|
MODULE_LICENSE("GPL");
|