License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 17:07:57 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* linux/drivers/char/mem.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
*
|
2010-03-11 02:21:52 +03:00
|
|
|
* Added devfs support.
|
2005-04-17 02:20:36 +04:00
|
|
|
* Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
|
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 18:09:05 +03:00
|
|
|
* Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/mman.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/capability.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/device.h>
|
2005-06-26 01:58:23 +04:00
|
|
|
#include <linux/highmem.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/backing-dev.h>
|
2016-07-27 01:26:15 +03:00
|
|
|
#include <linux/shmem_fs.h>
|
2007-06-04 11:59:47 +04:00
|
|
|
#include <linux/splice.h>
|
2006-10-13 19:42:10 +04:00
|
|
|
#include <linux/pfn.h>
|
2011-07-10 20:14:53 +04:00
|
|
|
#include <linux/export.h>
|
2012-07-11 09:18:44 +04:00
|
|
|
#include <linux/io.h>
|
2015-02-22 19:58:50 +03:00
|
|
|
#include <linux/uio.h>
|
2014-12-20 21:28:35 +03:00
|
|
|
#include <linux/uaccess.h>
|
2019-08-20 03:17:41 +03:00
|
|
|
#include <linux/security.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#ifdef CONFIG_IA64
|
|
|
|
# include <linux/efi.h>
|
|
|
|
#endif
|
|
|
|
|
2020-05-22 00:06:17 +03:00
|
|
|
#define DEVMEM_MINOR 1
|
2012-07-11 09:18:44 +04:00
|
|
|
#define DEVPORT_MINOR 4
|
|
|
|
|
2009-12-15 04:58:07 +03:00
|
|
|
static inline unsigned long size_inside_page(unsigned long start,
|
|
|
|
unsigned long size)
|
|
|
|
{
|
|
|
|
unsigned long sz;
|
|
|
|
|
2009-12-15 04:58:09 +03:00
|
|
|
sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
|
2009-12-15 04:58:07 +03:00
|
|
|
|
2009-12-15 04:58:09 +03:00
|
|
|
return min(sz, size);
|
2009-12-15 04:58:07 +03:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
|
2012-09-12 22:05:58 +04:00
|
|
|
static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2011-03-24 02:42:58 +03:00
|
|
|
return addr + count <= __pa(high_memory);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2006-01-08 12:04:13 +03:00
|
|
|
|
2006-07-10 15:45:27 +04:00
|
|
|
static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
|
2006-01-08 12:04:13 +03:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif
|
|
|
|
|
2008-07-18 02:26:59 +04:00
|
|
|
#ifdef CONFIG_STRICT_DEVMEM
|
2017-04-05 19:39:08 +03:00
|
|
|
static inline int page_is_allowed(unsigned long pfn)
|
|
|
|
{
|
|
|
|
return devmem_is_allowed(pfn);
|
|
|
|
}
|
2008-03-07 10:01:47 +03:00
|
|
|
static inline int range_is_allowed(unsigned long pfn, unsigned long size)
|
2008-04-25 01:40:47 +04:00
|
|
|
{
|
2008-03-07 10:01:47 +03:00
|
|
|
u64 from = ((u64)pfn) << PAGE_SHIFT;
|
|
|
|
u64 to = from + size;
|
|
|
|
u64 cursor = from;
|
|
|
|
|
|
|
|
while (cursor < to) {
|
2016-07-08 12:38:28 +03:00
|
|
|
if (!devmem_is_allowed(pfn))
|
2008-04-25 01:40:47 +04:00
|
|
|
return 0;
|
2008-03-07 10:01:47 +03:00
|
|
|
cursor += PAGE_SIZE;
|
|
|
|
pfn++;
|
2008-04-25 01:40:47 +04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
2017-04-05 19:39:08 +03:00
|
|
|
static inline int page_is_allowed(unsigned long pfn)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-07 10:01:47 +03:00
|
|
|
static inline int range_is_allowed(unsigned long pfn, unsigned long size)
|
2008-04-25 01:40:47 +04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-28 19:20:33 +04:00
|
|
|
#ifndef unxlate_dev_mem_ptr
|
|
|
|
#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
|
|
|
|
void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
|
2008-03-19 03:00:15 +03:00
|
|
|
{
|
|
|
|
}
|
2014-07-28 19:20:33 +04:00
|
|
|
#endif
|
2008-03-19 03:00:15 +03:00
|
|
|
|
2019-08-26 16:13:25 +03:00
|
|
|
static inline bool should_stop_iteration(void)
|
|
|
|
{
|
|
|
|
if (need_resched())
|
|
|
|
cond_resched();
|
2022-04-07 15:26:38 +03:00
|
|
|
return signal_pending(current);
|
2019-08-26 16:13:25 +03:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2010-03-11 02:21:52 +03:00
|
|
|
* This funcion reads the *physical* memory. The f_pos points directly to the
|
|
|
|
* memory location.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t read_mem(struct file *file, char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2012-09-12 22:05:58 +04:00
|
|
|
phys_addr_t p = *ppos;
|
2005-04-17 02:20:36 +04:00
|
|
|
ssize_t read, sz;
|
2014-07-28 19:20:33 +04:00
|
|
|
void *ptr;
|
2017-12-02 00:19:39 +03:00
|
|
|
char *bounce;
|
|
|
|
int err;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2014-01-30 12:48:02 +04:00
|
|
|
if (p != *ppos)
|
|
|
|
return 0;
|
|
|
|
|
2006-03-26 13:37:05 +04:00
|
|
|
if (!valid_phys_addr_range(p, count))
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EFAULT;
|
|
|
|
read = 0;
|
|
|
|
#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
|
|
|
|
/* we don't have page 0 mapped on sparc and m68k.. */
|
|
|
|
if (p < PAGE_SIZE) {
|
2009-12-15 04:58:09 +03:00
|
|
|
sz = size_inside_page(p, count);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (sz > 0) {
|
|
|
|
if (clear_user(buf, sz))
|
|
|
|
return -EFAULT;
|
2010-03-11 02:21:52 +03:00
|
|
|
buf += sz;
|
|
|
|
p += sz;
|
|
|
|
count -= sz;
|
|
|
|
read += sz;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-02 00:19:39 +03:00
|
|
|
bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
|
|
|
if (!bounce)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
while (count > 0) {
|
2009-12-15 04:58:08 +03:00
|
|
|
unsigned long remaining;
|
2018-03-28 00:06:14 +03:00
|
|
|
int allowed, probe;
|
2009-12-15 04:58:08 +03:00
|
|
|
|
2009-12-15 04:58:07 +03:00
|
|
|
sz = size_inside_page(p, count);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-12-02 00:19:39 +03:00
|
|
|
err = -EPERM;
|
2017-04-05 19:39:08 +03:00
|
|
|
allowed = page_is_allowed(p >> PAGE_SHIFT);
|
|
|
|
if (!allowed)
|
2017-12-02 00:19:39 +03:00
|
|
|
goto failed;
|
|
|
|
|
|
|
|
err = -EFAULT;
|
2017-04-05 19:39:08 +03:00
|
|
|
if (allowed == 2) {
|
|
|
|
/* Show zeros for restricted memory. */
|
|
|
|
remaining = clear_user(buf, sz);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* On ia64 if a page has been mapped somewhere as
|
|
|
|
* uncached, then it must also be accessed uncached
|
|
|
|
* by the kernel or data corruption may occur.
|
|
|
|
*/
|
|
|
|
ptr = xlate_dev_mem_ptr(p);
|
|
|
|
if (!ptr)
|
2017-12-02 00:19:39 +03:00
|
|
|
goto failed;
|
2017-04-05 19:39:08 +03:00
|
|
|
|
2020-06-17 10:37:53 +03:00
|
|
|
probe = copy_from_kernel_nofault(bounce, ptr, sz);
|
2017-04-05 19:39:08 +03:00
|
|
|
unxlate_dev_mem_ptr(p, ptr);
|
2018-03-28 00:06:14 +03:00
|
|
|
if (probe)
|
2017-12-02 00:19:39 +03:00
|
|
|
goto failed;
|
|
|
|
|
|
|
|
remaining = copy_to_user(buf, bounce, sz);
|
2017-04-05 19:39:08 +03:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2009-12-15 04:58:08 +03:00
|
|
|
if (remaining)
|
2017-12-02 00:19:39 +03:00
|
|
|
goto failed;
|
2008-03-19 03:00:15 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
buf += sz;
|
|
|
|
p += sz;
|
|
|
|
count -= sz;
|
|
|
|
read += sz;
|
2019-08-26 16:13:25 +03:00
|
|
|
if (should_stop_iteration())
|
|
|
|
break;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2017-12-02 00:19:39 +03:00
|
|
|
kfree(bounce);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
*ppos += read;
|
|
|
|
return read;
|
2017-12-02 00:19:39 +03:00
|
|
|
|
|
|
|
failed:
|
|
|
|
kfree(bounce);
|
|
|
|
return err;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t write_mem(struct file *file, const char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2012-09-12 22:05:58 +04:00
|
|
|
phys_addr_t p = *ppos;
|
2005-04-17 02:20:36 +04:00
|
|
|
ssize_t written, sz;
|
|
|
|
unsigned long copied;
|
|
|
|
void *ptr;
|
|
|
|
|
2014-01-30 12:48:02 +04:00
|
|
|
if (p != *ppos)
|
|
|
|
return -EFBIG;
|
|
|
|
|
2006-03-26 13:37:05 +04:00
|
|
|
if (!valid_phys_addr_range(p, count))
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
written = 0;
|
|
|
|
|
|
|
|
#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
|
|
|
|
/* we don't have page 0 mapped on sparc and m68k.. */
|
|
|
|
if (p < PAGE_SIZE) {
|
2009-12-15 04:58:09 +03:00
|
|
|
sz = size_inside_page(p, count);
|
2005-04-17 02:20:36 +04:00
|
|
|
/* Hmm. Do something? */
|
|
|
|
buf += sz;
|
|
|
|
p += sz;
|
|
|
|
count -= sz;
|
|
|
|
written += sz;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (count > 0) {
|
2017-04-05 19:39:08 +03:00
|
|
|
int allowed;
|
|
|
|
|
2009-12-15 04:58:07 +03:00
|
|
|
sz = size_inside_page(p, count);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-04-05 19:39:08 +03:00
|
|
|
allowed = page_is_allowed(p >> PAGE_SHIFT);
|
|
|
|
if (!allowed)
|
2008-03-19 03:00:15 +03:00
|
|
|
return -EPERM;
|
|
|
|
|
2017-04-05 19:39:08 +03:00
|
|
|
/* Skip actual writing when a page is marked as restricted. */
|
|
|
|
if (allowed == 1) {
|
|
|
|
/*
|
|
|
|
* On ia64 if a page has been mapped somewhere as
|
|
|
|
* uncached, then it must also be accessed uncached
|
|
|
|
* by the kernel or data corruption may occur.
|
|
|
|
*/
|
|
|
|
ptr = xlate_dev_mem_ptr(p);
|
|
|
|
if (!ptr) {
|
|
|
|
if (written)
|
|
|
|
break;
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
copied = copy_from_user(ptr, buf, sz);
|
|
|
|
unxlate_dev_mem_ptr(p, ptr);
|
|
|
|
if (copied) {
|
|
|
|
written += sz - copied;
|
|
|
|
if (written)
|
|
|
|
break;
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2008-03-19 03:00:15 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
buf += sz;
|
|
|
|
p += sz;
|
|
|
|
count -= sz;
|
|
|
|
written += sz;
|
2019-08-26 16:13:25 +03:00
|
|
|
if (should_stop_iteration())
|
|
|
|
break;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*ppos += written;
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
int __weak phys_mem_access_prot_allowed(struct file *file,
|
2008-03-19 03:00:20 +03:00
|
|
|
unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-01-08 12:04:10 +03:00
|
|
|
#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
|
2010-03-11 02:21:52 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Architectures vary in how they handle caching for addresses
|
|
|
|
* outside of main memory.
|
|
|
|
*
|
|
|
|
*/
|
2010-04-07 01:35:08 +04:00
|
|
|
#ifdef pgprot_noncached
|
2012-09-12 22:05:58 +04:00
|
|
|
static int uncached_access(struct file *file, phys_addr_t addr)
|
2010-03-11 02:21:52 +03:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_IA64)
|
|
|
|
/*
|
|
|
|
* On ia64, we ignore O_DSYNC because we cannot tolerate memory
|
|
|
|
* attribute aliases.
|
|
|
|
*/
|
|
|
|
return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Accessing memory above the top the kernel knows about or through a
|
|
|
|
* file pointer
|
|
|
|
* that was marked O_DSYNC will be done non-cached.
|
|
|
|
*/
|
|
|
|
if (file->f_flags & O_DSYNC)
|
|
|
|
return 1;
|
|
|
|
return addr >= __pa(high_memory);
|
|
|
|
#endif
|
|
|
|
}
|
2010-04-07 01:35:08 +04:00
|
|
|
#endif
|
2010-03-11 02:21:52 +03:00
|
|
|
|
2006-01-08 12:04:10 +03:00
|
|
|
static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
|
|
|
|
unsigned long size, pgprot_t vma_prot)
|
|
|
|
{
|
|
|
|
#ifdef pgprot_noncached
|
2012-09-12 22:05:58 +04:00
|
|
|
phys_addr_t offset = pfn << PAGE_SHIFT;
|
2006-01-08 12:04:10 +03:00
|
|
|
|
|
|
|
if (uncached_access(file, offset))
|
|
|
|
return pgprot_noncached(vma_prot);
|
|
|
|
#endif
|
|
|
|
return vma_prot;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-09-27 12:50:16 +04:00
|
|
|
#ifndef CONFIG_MMU
|
|
|
|
static unsigned long get_unmapped_area_mem(struct file *file,
|
|
|
|
unsigned long addr,
|
|
|
|
unsigned long len,
|
|
|
|
unsigned long pgoff,
|
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
if (!valid_mmap_phys_addr_range(pgoff, len))
|
|
|
|
return (unsigned long) -EINVAL;
|
2007-04-17 09:53:16 +04:00
|
|
|
return pgoff << PAGE_SHIFT;
|
2006-09-27 12:50:16 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 12:42:32 +03:00
|
|
|
/* permit direct mmap, for read, write or exec */
|
|
|
|
static unsigned memory_mmap_capabilities(struct file *file)
|
|
|
|
{
|
|
|
|
return NOMMU_MAP_DIRECT |
|
|
|
|
NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned zero_mmap_capabilities(struct file *file)
|
|
|
|
{
|
|
|
|
return NOMMU_MAP_COPY;
|
|
|
|
}
|
|
|
|
|
2006-09-27 12:50:16 +04:00
|
|
|
/* can't do an in-place private mapping if there's no MMU */
|
|
|
|
static inline int private_mapping_ok(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return vma->vm_flags & VM_MAYSHARE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
static inline int private_mapping_ok(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-27 22:29:37 +04:00
|
|
|
static const struct vm_operations_struct mmap_mem_ops = {
|
2008-07-24 08:27:07 +04:00
|
|
|
#ifdef CONFIG_HAVE_IOREMAP_PROT
|
|
|
|
.access = generic_access_phys
|
|
|
|
#endif
|
2008-03-19 03:00:21 +03:00
|
|
|
};
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static int mmap_mem(struct file *file, struct vm_area_struct *vma)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-01-08 12:04:13 +03:00
|
|
|
size_t size = vma->vm_end - vma->vm_start;
|
2017-05-13 00:42:58 +03:00
|
|
|
phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
|
|
|
|
|
2017-11-16 01:29:51 +03:00
|
|
|
/* Does it even fit in phys_addr_t? */
|
|
|
|
if (offset >> PAGE_SHIFT != vma->vm_pgoff)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-05-13 00:42:58 +03:00
|
|
|
/* It's illegal to wrap around the end of the physical address space. */
|
2017-06-03 01:36:39 +03:00
|
|
|
if (offset + (phys_addr_t)size - 1 < offset)
|
2017-05-13 00:42:58 +03:00
|
|
|
return -EINVAL;
|
2006-01-08 12:04:13 +03:00
|
|
|
|
2006-07-10 15:45:27 +04:00
|
|
|
if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
|
2006-01-08 12:04:13 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2006-09-27 12:50:16 +04:00
|
|
|
if (!private_mapping_ok(vma))
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2008-03-07 10:01:47 +03:00
|
|
|
if (!range_is_allowed(vma->vm_pgoff, size))
|
|
|
|
return -EPERM;
|
|
|
|
|
2008-03-19 03:00:20 +03:00
|
|
|
if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
|
|
|
|
&vma->vm_page_prot))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2005-10-29 04:46:18 +04:00
|
|
|
vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
|
2006-01-08 12:04:13 +03:00
|
|
|
size,
|
2005-04-17 02:20:36 +04:00
|
|
|
vma->vm_page_prot);
|
|
|
|
|
2008-03-19 03:00:21 +03:00
|
|
|
vma->vm_ops = &mmap_mem_ops;
|
|
|
|
|
mm: kill vma flag VM_RESERVED and mm->reserved_vm counter
A long time ago, in v2.4, VM_RESERVED kept swapout process off VMA,
currently it lost original meaning but still has some effects:
| effect | alternative flags
-+------------------------+---------------------------------------------
1| account as reserved_vm | VM_IO
2| skip in core dump | VM_IO, VM_DONTDUMP
3| do not merge or expand | VM_IO, VM_DONTEXPAND, VM_HUGETLB, VM_PFNMAP
4| do not mlock | VM_IO, VM_DONTEXPAND, VM_HUGETLB, VM_PFNMAP
This patch removes reserved_vm counter from mm_struct. Seems like nobody
cares about it, it does not exported into userspace directly, it only
reduces total_vm showed in proc.
Thus VM_RESERVED can be replaced with VM_IO or pair VM_DONTEXPAND | VM_DONTDUMP.
remap_pfn_range() and io_remap_pfn_range() set VM_IO|VM_DONTEXPAND|VM_DONTDUMP.
remap_vmalloc_range() set VM_DONTEXPAND | VM_DONTDUMP.
[akpm@linux-foundation.org: drivers/vfio/pci/vfio_pci.c fixup]
Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Carsten Otte <cotte@de.ibm.com>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Eric Paris <eparis@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Morris <james.l.morris@oracle.com>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Kentaro Takeda <takedakn@nttdata.co.jp>
Cc: Matt Helsley <matthltc@us.ibm.com>
Cc: Nick Piggin <npiggin@kernel.dk>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Robert Richter <robert.richter@amd.com>
Cc: Suresh Siddha <suresh.b.siddha@intel.com>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Venkatesh Pallipadi <venki@google.com>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-09 03:29:02 +04:00
|
|
|
/* Remap-pfn-range will mark the range VM_IO */
|
2005-04-17 02:20:36 +04:00
|
|
|
if (remap_pfn_range(vma,
|
|
|
|
vma->vm_start,
|
|
|
|
vma->vm_pgoff,
|
2006-01-08 12:04:13 +03:00
|
|
|
size,
|
2008-03-19 03:00:21 +03:00
|
|
|
vma->vm_page_prot)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EAGAIN;
|
2008-03-19 03:00:21 +03:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t read_port(struct file *file, char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
unsigned long i = *ppos;
|
|
|
|
char __user *tmp = buf;
|
|
|
|
|
Remove 'type' argument from access_ok() function
Nobody has actually used the type (VERIFY_READ vs VERIFY_WRITE) argument
of the user address range verification function since we got rid of the
old racy i386-only code to walk page tables by hand.
It existed because the original 80386 would not honor the write protect
bit when in kernel mode, so you had to do COW by hand before doing any
user access. But we haven't supported that in a long time, and these
days the 'type' argument is a purely historical artifact.
A discussion about extending 'user_access_begin()' to do the range
checking resulted this patch, because there is no way we're going to
move the old VERIFY_xyz interface to that model. And it's best done at
the end of the merge window when I've done most of my merges, so let's
just get this done once and for all.
This patch was mostly done with a sed-script, with manual fix-ups for
the cases that weren't of the trivial 'access_ok(VERIFY_xyz' form.
There were a couple of notable cases:
- csky still had the old "verify_area()" name as an alias.
- the iter_iov code had magical hardcoded knowledge of the actual
values of VERIFY_{READ,WRITE} (not that they mattered, since nothing
really used it)
- microblaze used the type argument for a debug printout
but other than those oddities this should be a total no-op patch.
I tried to fix up all architectures, did fairly extensive grepping for
access_ok() uses, and the changes are trivial, but I may have missed
something. Any missed conversion should be trivially fixable, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 05:57:57 +03:00
|
|
|
if (!access_ok(buf, count))
|
2010-03-11 02:21:52 +03:00
|
|
|
return -EFAULT;
|
2005-04-17 02:20:36 +04:00
|
|
|
while (count-- > 0 && i < 65536) {
|
2010-03-11 02:21:52 +03:00
|
|
|
if (__put_user(inb(i), tmp) < 0)
|
|
|
|
return -EFAULT;
|
2005-04-17 02:20:36 +04:00
|
|
|
i++;
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
*ppos = i;
|
|
|
|
return tmp-buf;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t write_port(struct file *file, const char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
unsigned long i = *ppos;
|
2013-02-06 14:37:20 +04:00
|
|
|
const char __user *tmp = buf;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
Remove 'type' argument from access_ok() function
Nobody has actually used the type (VERIFY_READ vs VERIFY_WRITE) argument
of the user address range verification function since we got rid of the
old racy i386-only code to walk page tables by hand.
It existed because the original 80386 would not honor the write protect
bit when in kernel mode, so you had to do COW by hand before doing any
user access. But we haven't supported that in a long time, and these
days the 'type' argument is a purely historical artifact.
A discussion about extending 'user_access_begin()' to do the range
checking resulted this patch, because there is no way we're going to
move the old VERIFY_xyz interface to that model. And it's best done at
the end of the merge window when I've done most of my merges, so let's
just get this done once and for all.
This patch was mostly done with a sed-script, with manual fix-ups for
the cases that weren't of the trivial 'access_ok(VERIFY_xyz' form.
There were a couple of notable cases:
- csky still had the old "verify_area()" name as an alias.
- the iter_iov code had magical hardcoded knowledge of the actual
values of VERIFY_{READ,WRITE} (not that they mattered, since nothing
really used it)
- microblaze used the type argument for a debug printout
but other than those oddities this should be a total no-op patch.
I tried to fix up all architectures, did fairly extensive grepping for
access_ok() uses, and the changes are trivial, but I may have missed
something. Any missed conversion should be trivially fixable, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 05:57:57 +03:00
|
|
|
if (!access_ok(buf, count))
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EFAULT;
|
|
|
|
while (count-- > 0 && i < 65536) {
|
|
|
|
char c;
|
2014-12-20 21:28:36 +03:00
|
|
|
|
2006-03-25 14:07:31 +03:00
|
|
|
if (__get_user(c, tmp)) {
|
|
|
|
if (tmp > buf)
|
|
|
|
break;
|
2010-03-11 02:21:52 +03:00
|
|
|
return -EFAULT;
|
2006-03-25 14:07:31 +03:00
|
|
|
}
|
2010-03-11 02:21:52 +03:00
|
|
|
outb(c, i);
|
2005-04-17 02:20:36 +04:00
|
|
|
i++;
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
*ppos = i;
|
|
|
|
return tmp-buf;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t read_null(struct file *file, char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t write_null(struct file *file, const char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:57:04 +03:00
|
|
|
static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
|
2013-05-08 03:18:27 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:57:04 +03:00
|
|
|
static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
|
2013-05-08 03:18:27 +04:00
|
|
|
{
|
2015-04-03 22:57:04 +03:00
|
|
|
size_t count = iov_iter_count(from);
|
|
|
|
iov_iter_advance(from, count);
|
|
|
|
return count;
|
2013-05-08 03:18:27 +04:00
|
|
|
}
|
|
|
|
|
2006-04-26 16:40:08 +04:00
|
|
|
static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
|
|
|
|
struct splice_desc *sd)
|
|
|
|
{
|
|
|
|
return sd->len;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
|
2006-04-26 16:40:08 +04:00
|
|
|
loff_t *ppos, size_t len, unsigned int flags)
|
|
|
|
{
|
|
|
|
return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
|
|
|
|
}
|
|
|
|
|
2022-08-19 23:20:33 +03:00
|
|
|
static int uring_cmd_null(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:04:12 +04:00
|
|
|
static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2014-08-18 18:04:12 +04:00
|
|
|
size_t written = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2014-08-18 18:04:12 +04:00
|
|
|
while (iov_iter_count(iter)) {
|
|
|
|
size_t chunk = iov_iter_count(iter), n;
|
2014-12-20 21:28:36 +03:00
|
|
|
|
remove ZERO_PAGE
The commit b5810039a54e5babf428e9a1e89fc1940fabff11 contains the note
A last caveat: the ZERO_PAGE is now refcounted and managed with rmap
(and thus mapcounted and count towards shared rss). These writes to
the struct page could cause excessive cacheline bouncing on big
systems. There are a number of ways this could be addressed if it is
an issue.
And indeed this cacheline bouncing has shown up on large SGI systems.
There was a situation where an Altix system was essentially livelocked
tearing down ZERO_PAGE pagetables when an HPC app aborted during startup.
This situation can be avoided in userspace, but it does highlight the
potential scalability problem with refcounting ZERO_PAGE, and corner
cases where it can really hurt (we don't want the system to livelock!).
There are several broad ways to fix this problem:
1. add back some special casing to avoid refcounting ZERO_PAGE
2. per-node or per-cpu ZERO_PAGES
3. remove the ZERO_PAGE completely
I will argue for 3. The others should also fix the problem, but they
result in more complex code than does 3, with little or no real benefit
that I can see.
Why? Inserting a ZERO_PAGE for anonymous read faults appears to be a
false optimisation: if an application is performance critical, it would
not be doing many read faults of new memory, or at least it could be
expected to write to that memory soon afterwards. If cache or memory use
is critical, it should not be working with a significant number of
ZERO_PAGEs anyway (a more compact representation of zeroes should be
used).
As a sanity check -- mesuring on my desktop system, there are never many
mappings to the ZERO_PAGE (eg. 2 or 3), thus memory usage here should not
increase much without it.
When running a make -j4 kernel compile on my dual core system, there are
about 1,000 mappings to the ZERO_PAGE created per second, but about 1,000
ZERO_PAGE COW faults per second (less than 1 ZERO_PAGE mapping per second
is torn down without being COWed). So removing ZERO_PAGE will save 1,000
page faults per second when running kbuild, while keeping it only saves
less than 1 page clearing operation per second. 1 page clear is cheaper
than a thousand faults, presumably, so there isn't an obvious loss.
Neither the logical argument nor these basic tests give a guarantee of no
regressions. However, this is a reasonable opportunity to try to remove
the ZERO_PAGE from the pagefault path. If it is found to cause regressions,
we can reintroduce it and just avoid refcounting it.
The /dev/zero ZERO_PAGE usage and TLB tricks also get nuked. I don't see
much use to them except on benchmarks. All other users of ZERO_PAGE are
converted just to use ZERO_PAGE(0) for simplicity. We can look at
replacing them all and maybe ripping out ZERO_PAGE completely when we are
more satisfied with this solution.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus "snif" Torvalds <torvalds@linux-foundation.org>
2007-10-16 12:24:40 +04:00
|
|
|
if (chunk > PAGE_SIZE)
|
|
|
|
chunk = PAGE_SIZE; /* Just for latency reasons */
|
2014-08-18 18:04:12 +04:00
|
|
|
n = iov_iter_zero(chunk, iter);
|
|
|
|
if (!n && iov_iter_count(iter))
|
|
|
|
return written ? written : -EFAULT;
|
|
|
|
written += n;
|
2009-06-10 07:40:25 +04:00
|
|
|
if (signal_pending(current))
|
|
|
|
return written ? written : -ERESTARTSYS;
|
2021-09-08 22:10:38 +03:00
|
|
|
if (!need_resched())
|
|
|
|
continue;
|
|
|
|
if (iocb->ki_flags & IOCB_NOWAIT)
|
|
|
|
return written ? written : -EAGAIN;
|
2005-04-17 02:20:36 +04:00
|
|
|
cond_resched();
|
|
|
|
}
|
2014-08-18 18:04:12 +04:00
|
|
|
return written;
|
2013-05-08 03:18:27 +04:00
|
|
|
}
|
|
|
|
|
2020-09-03 18:59:22 +03:00
|
|
|
static ssize_t read_zero(struct file *file, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
size_t cleared = 0;
|
|
|
|
|
|
|
|
while (count) {
|
|
|
|
size_t chunk = min_t(size_t, count, PAGE_SIZE);
|
2020-09-07 11:27:00 +03:00
|
|
|
size_t left;
|
2020-09-03 18:59:22 +03:00
|
|
|
|
2020-09-07 11:27:00 +03:00
|
|
|
left = clear_user(buf + cleared, chunk);
|
|
|
|
if (unlikely(left)) {
|
|
|
|
cleared += (chunk - left);
|
|
|
|
if (!cleared)
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-03 18:59:22 +03:00
|
|
|
cleared += chunk;
|
|
|
|
count -= chunk;
|
|
|
|
|
|
|
|
if (signal_pending(current))
|
2020-09-07 11:27:00 +03:00
|
|
|
break;
|
2020-09-03 18:59:22 +03:00
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleared;
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static int mmap_zero(struct file *file, struct vm_area_struct *vma)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
remove ZERO_PAGE
The commit b5810039a54e5babf428e9a1e89fc1940fabff11 contains the note
A last caveat: the ZERO_PAGE is now refcounted and managed with rmap
(and thus mapcounted and count towards shared rss). These writes to
the struct page could cause excessive cacheline bouncing on big
systems. There are a number of ways this could be addressed if it is
an issue.
And indeed this cacheline bouncing has shown up on large SGI systems.
There was a situation where an Altix system was essentially livelocked
tearing down ZERO_PAGE pagetables when an HPC app aborted during startup.
This situation can be avoided in userspace, but it does highlight the
potential scalability problem with refcounting ZERO_PAGE, and corner
cases where it can really hurt (we don't want the system to livelock!).
There are several broad ways to fix this problem:
1. add back some special casing to avoid refcounting ZERO_PAGE
2. per-node or per-cpu ZERO_PAGES
3. remove the ZERO_PAGE completely
I will argue for 3. The others should also fix the problem, but they
result in more complex code than does 3, with little or no real benefit
that I can see.
Why? Inserting a ZERO_PAGE for anonymous read faults appears to be a
false optimisation: if an application is performance critical, it would
not be doing many read faults of new memory, or at least it could be
expected to write to that memory soon afterwards. If cache or memory use
is critical, it should not be working with a significant number of
ZERO_PAGEs anyway (a more compact representation of zeroes should be
used).
As a sanity check -- mesuring on my desktop system, there are never many
mappings to the ZERO_PAGE (eg. 2 or 3), thus memory usage here should not
increase much without it.
When running a make -j4 kernel compile on my dual core system, there are
about 1,000 mappings to the ZERO_PAGE created per second, but about 1,000
ZERO_PAGE COW faults per second (less than 1 ZERO_PAGE mapping per second
is torn down without being COWed). So removing ZERO_PAGE will save 1,000
page faults per second when running kbuild, while keeping it only saves
less than 1 page clearing operation per second. 1 page clear is cheaper
than a thousand faults, presumably, so there isn't an obvious loss.
Neither the logical argument nor these basic tests give a guarantee of no
regressions. However, this is a reasonable opportunity to try to remove
the ZERO_PAGE from the pagefault path. If it is found to cause regressions,
we can reintroduce it and just avoid refcounting it.
The /dev/zero ZERO_PAGE usage and TLB tricks also get nuked. I don't see
much use to them except on benchmarks. All other users of ZERO_PAGE are
converted just to use ZERO_PAGE(0) for simplicity. We can look at
replacing them all and maybe ripping out ZERO_PAGE completely when we are
more satisfied with this solution.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus "snif" Torvalds <torvalds@linux-foundation.org>
2007-10-16 12:24:40 +04:00
|
|
|
#ifndef CONFIG_MMU
|
2005-04-17 02:20:36 +04:00
|
|
|
return -ENOSYS;
|
remove ZERO_PAGE
The commit b5810039a54e5babf428e9a1e89fc1940fabff11 contains the note
A last caveat: the ZERO_PAGE is now refcounted and managed with rmap
(and thus mapcounted and count towards shared rss). These writes to
the struct page could cause excessive cacheline bouncing on big
systems. There are a number of ways this could be addressed if it is
an issue.
And indeed this cacheline bouncing has shown up on large SGI systems.
There was a situation where an Altix system was essentially livelocked
tearing down ZERO_PAGE pagetables when an HPC app aborted during startup.
This situation can be avoided in userspace, but it does highlight the
potential scalability problem with refcounting ZERO_PAGE, and corner
cases where it can really hurt (we don't want the system to livelock!).
There are several broad ways to fix this problem:
1. add back some special casing to avoid refcounting ZERO_PAGE
2. per-node or per-cpu ZERO_PAGES
3. remove the ZERO_PAGE completely
I will argue for 3. The others should also fix the problem, but they
result in more complex code than does 3, with little or no real benefit
that I can see.
Why? Inserting a ZERO_PAGE for anonymous read faults appears to be a
false optimisation: if an application is performance critical, it would
not be doing many read faults of new memory, or at least it could be
expected to write to that memory soon afterwards. If cache or memory use
is critical, it should not be working with a significant number of
ZERO_PAGEs anyway (a more compact representation of zeroes should be
used).
As a sanity check -- mesuring on my desktop system, there are never many
mappings to the ZERO_PAGE (eg. 2 or 3), thus memory usage here should not
increase much without it.
When running a make -j4 kernel compile on my dual core system, there are
about 1,000 mappings to the ZERO_PAGE created per second, but about 1,000
ZERO_PAGE COW faults per second (less than 1 ZERO_PAGE mapping per second
is torn down without being COWed). So removing ZERO_PAGE will save 1,000
page faults per second when running kbuild, while keeping it only saves
less than 1 page clearing operation per second. 1 page clear is cheaper
than a thousand faults, presumably, so there isn't an obvious loss.
Neither the logical argument nor these basic tests give a guarantee of no
regressions. However, this is a reasonable opportunity to try to remove
the ZERO_PAGE from the pagefault path. If it is found to cause regressions,
we can reintroduce it and just avoid refcounting it.
The /dev/zero ZERO_PAGE usage and TLB tricks also get nuked. I don't see
much use to them except on benchmarks. All other users of ZERO_PAGE are
converted just to use ZERO_PAGE(0) for simplicity. We can look at
replacing them all and maybe ripping out ZERO_PAGE completely when we are
more satisfied with this solution.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus "snif" Torvalds <torvalds@linux-foundation.org>
2007-10-16 12:24:40 +04:00
|
|
|
#endif
|
|
|
|
if (vma->vm_flags & VM_SHARED)
|
|
|
|
return shmem_zero_setup(vma);
|
mm: fix vma_is_anonymous() false-positives
vma_is_anonymous() relies on ->vm_ops being NULL to detect anonymous
VMA. This is unreliable as ->mmap may not set ->vm_ops.
False-positive vma_is_anonymous() may lead to crashes:
next ffff8801ce5e7040 prev ffff8801d20eca50 mm ffff88019c1e13c0
prot 27 anon_vma ffff88019680cdd8 vm_ops 0000000000000000
pgoff 0 file ffff8801b2ec2d00 private_data 0000000000000000
flags: 0xff(read|write|exec|shared|mayread|maywrite|mayexec|mayshare)
------------[ cut here ]------------
kernel BUG at mm/memory.c:1422!
invalid opcode: 0000 [#1] SMP KASAN
CPU: 0 PID: 18486 Comm: syz-executor3 Not tainted 4.18.0-rc3+ #136
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google
01/01/2011
RIP: 0010:zap_pmd_range mm/memory.c:1421 [inline]
RIP: 0010:zap_pud_range mm/memory.c:1466 [inline]
RIP: 0010:zap_p4d_range mm/memory.c:1487 [inline]
RIP: 0010:unmap_page_range+0x1c18/0x2220 mm/memory.c:1508
Call Trace:
unmap_single_vma+0x1a0/0x310 mm/memory.c:1553
zap_page_range_single+0x3cc/0x580 mm/memory.c:1644
unmap_mapping_range_vma mm/memory.c:2792 [inline]
unmap_mapping_range_tree mm/memory.c:2813 [inline]
unmap_mapping_pages+0x3a7/0x5b0 mm/memory.c:2845
unmap_mapping_range+0x48/0x60 mm/memory.c:2880
truncate_pagecache+0x54/0x90 mm/truncate.c:800
truncate_setsize+0x70/0xb0 mm/truncate.c:826
simple_setattr+0xe9/0x110 fs/libfs.c:409
notify_change+0xf13/0x10f0 fs/attr.c:335
do_truncate+0x1ac/0x2b0 fs/open.c:63
do_sys_ftruncate+0x492/0x560 fs/open.c:205
__do_sys_ftruncate fs/open.c:215 [inline]
__se_sys_ftruncate fs/open.c:213 [inline]
__x64_sys_ftruncate+0x59/0x80 fs/open.c:213
do_syscall_64+0x1b9/0x820 arch/x86/entry/common.c:290
entry_SYSCALL_64_after_hwframe+0x49/0xbe
Reproducer:
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#define KCOV_INIT_TRACE _IOR('c', 1, unsigned long)
#define KCOV_ENABLE _IO('c', 100)
#define KCOV_DISABLE _IO('c', 101)
#define COVER_SIZE (1024<<10)
#define KCOV_TRACE_PC 0
#define KCOV_TRACE_CMP 1
int main(int argc, char **argv)
{
int fd;
unsigned long *cover;
system("mount -t debugfs none /sys/kernel/debug");
fd = open("/sys/kernel/debug/kcov", O_RDWR);
ioctl(fd, KCOV_INIT_TRACE, COVER_SIZE);
cover = mmap(NULL, COVER_SIZE * sizeof(unsigned long),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
munmap(cover, COVER_SIZE * sizeof(unsigned long));
cover = mmap(NULL, COVER_SIZE * sizeof(unsigned long),
PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
memset(cover, 0, COVER_SIZE * sizeof(unsigned long));
ftruncate(fd, 3UL << 20);
return 0;
}
This can be fixed by assigning anonymous VMAs own vm_ops and not relying
on it being NULL.
If ->mmap() failed to set ->vm_ops, mmap_region() will set it to
dummy_vm_ops. This way we will have non-NULL ->vm_ops for all VMAs.
Link: http://lkml.kernel.org/r/20180724121139.62570-4-kirill.shutemov@linux.intel.com
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Reported-by: syzbot+3f84280d52be9b7083cc@syzkaller.appspotmail.com
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-07-27 02:37:35 +03:00
|
|
|
vma_set_anonymous(vma);
|
remove ZERO_PAGE
The commit b5810039a54e5babf428e9a1e89fc1940fabff11 contains the note
A last caveat: the ZERO_PAGE is now refcounted and managed with rmap
(and thus mapcounted and count towards shared rss). These writes to
the struct page could cause excessive cacheline bouncing on big
systems. There are a number of ways this could be addressed if it is
an issue.
And indeed this cacheline bouncing has shown up on large SGI systems.
There was a situation where an Altix system was essentially livelocked
tearing down ZERO_PAGE pagetables when an HPC app aborted during startup.
This situation can be avoided in userspace, but it does highlight the
potential scalability problem with refcounting ZERO_PAGE, and corner
cases where it can really hurt (we don't want the system to livelock!).
There are several broad ways to fix this problem:
1. add back some special casing to avoid refcounting ZERO_PAGE
2. per-node or per-cpu ZERO_PAGES
3. remove the ZERO_PAGE completely
I will argue for 3. The others should also fix the problem, but they
result in more complex code than does 3, with little or no real benefit
that I can see.
Why? Inserting a ZERO_PAGE for anonymous read faults appears to be a
false optimisation: if an application is performance critical, it would
not be doing many read faults of new memory, or at least it could be
expected to write to that memory soon afterwards. If cache or memory use
is critical, it should not be working with a significant number of
ZERO_PAGEs anyway (a more compact representation of zeroes should be
used).
As a sanity check -- mesuring on my desktop system, there are never many
mappings to the ZERO_PAGE (eg. 2 or 3), thus memory usage here should not
increase much without it.
When running a make -j4 kernel compile on my dual core system, there are
about 1,000 mappings to the ZERO_PAGE created per second, but about 1,000
ZERO_PAGE COW faults per second (less than 1 ZERO_PAGE mapping per second
is torn down without being COWed). So removing ZERO_PAGE will save 1,000
page faults per second when running kbuild, while keeping it only saves
less than 1 page clearing operation per second. 1 page clear is cheaper
than a thousand faults, presumably, so there isn't an obvious loss.
Neither the logical argument nor these basic tests give a guarantee of no
regressions. However, this is a reasonable opportunity to try to remove
the ZERO_PAGE from the pagefault path. If it is found to cause regressions,
we can reintroduce it and just avoid refcounting it.
The /dev/zero ZERO_PAGE usage and TLB tricks also get nuked. I don't see
much use to them except on benchmarks. All other users of ZERO_PAGE are
converted just to use ZERO_PAGE(0) for simplicity. We can look at
replacing them all and maybe ripping out ZERO_PAGE completely when we are
more satisfied with this solution.
Signed-off-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus "snif" Torvalds <torvalds@linux-foundation.org>
2007-10-16 12:24:40 +04:00
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2016-07-27 01:26:15 +03:00
|
|
|
static unsigned long get_unmapped_area_zero(struct file *file,
|
|
|
|
unsigned long addr, unsigned long len,
|
|
|
|
unsigned long pgoff, unsigned long flags)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
if (flags & MAP_SHARED) {
|
|
|
|
/*
|
|
|
|
* mmap_zero() will call shmem_zero_setup() to create a file,
|
|
|
|
* so use shmem's get_unmapped_area in case it can be huge;
|
|
|
|
* and pass NULL for file as in mmap.c's get_unmapped_area(),
|
|
|
|
* so as not to confuse shmem with our handle on "/dev/zero".
|
|
|
|
*/
|
|
|
|
return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
|
|
|
|
return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
|
|
|
|
#else
|
|
|
|
return -ENOSYS;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
static ssize_t write_full(struct file *file, const char __user *buf,
|
2005-04-17 02:20:36 +04:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special lseek() function for /dev/null and /dev/zero. Most notably, you
|
|
|
|
* can fopen() both devices with "a" now. This was previously impossible.
|
|
|
|
* -- SRB.
|
|
|
|
*/
|
2010-03-11 02:21:52 +03:00
|
|
|
static loff_t null_lseek(struct file *file, loff_t offset, int orig)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return file->f_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The memory devices use the full 32/64 bits of the offset, and so we cannot
|
|
|
|
* check against negative addresses: they are ok. The return value is weird,
|
|
|
|
* though, in that case (0).
|
|
|
|
*
|
|
|
|
* also note that seeking relative to the "end of file" isn't supported:
|
|
|
|
* it has no meaning, so it returns -EINVAL.
|
|
|
|
*/
|
2010-03-11 02:21:52 +03:00
|
|
|
static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
loff_t ret;
|
|
|
|
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_lock(file_inode(file));
|
2005-04-17 02:20:36 +04:00
|
|
|
switch (orig) {
|
2010-03-11 02:21:52 +03:00
|
|
|
case SEEK_CUR:
|
|
|
|
offset += file->f_pos;
|
2020-08-24 01:36:59 +03:00
|
|
|
fallthrough;
|
2010-03-11 02:21:52 +03:00
|
|
|
case SEEK_SET:
|
|
|
|
/* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
|
2016-02-15 17:35:21 +03:00
|
|
|
if ((unsigned long long)offset >= -MAX_ERRNO) {
|
2010-03-11 02:21:52 +03:00
|
|
|
ret = -EOVERFLOW;
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
2010-03-11 02:21:52 +03:00
|
|
|
}
|
|
|
|
file->f_pos = offset;
|
|
|
|
ret = file->f_pos;
|
|
|
|
force_successful_syscall_return();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2016-01-22 23:40:57 +03:00
|
|
|
inode_unlock(file_inode(file));
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:37:20 +04:00
|
|
|
static int open_port(struct inode *inode, struct file *filp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2020-05-22 00:06:17 +03:00
|
|
|
int rc;
|
|
|
|
|
2019-08-20 03:17:41 +03:00
|
|
|
if (!capable(CAP_SYS_RAWIO))
|
|
|
|
return -EPERM;
|
|
|
|
|
2020-05-22 00:06:17 +03:00
|
|
|
rc = security_locked_down(LOCKDOWN_DEV_MEM);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (iminor(inode) != DEVMEM_MINOR)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use a unified address space to have a single point to manage
|
|
|
|
* revocations when drivers want to take over a /dev/mem mapped
|
|
|
|
* range.
|
|
|
|
*/
|
2020-11-27 19:41:24 +03:00
|
|
|
filp->f_mapping = iomem_get_mapping();
|
2020-05-22 00:06:17 +03:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define zero_lseek null_lseek
|
|
|
|
#define full_lseek null_lseek
|
|
|
|
#define write_zero write_null
|
2015-04-03 22:57:04 +03:00
|
|
|
#define write_iter_zero write_iter_null
|
2005-04-17 02:20:36 +04:00
|
|
|
#define open_mem open_port
|
|
|
|
|
2014-12-07 18:40:33 +03:00
|
|
|
static const struct file_operations __maybe_unused mem_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.llseek = memory_lseek,
|
|
|
|
.read = read_mem,
|
|
|
|
.write = write_mem,
|
|
|
|
.mmap = mmap_mem,
|
|
|
|
.open = open_mem,
|
2015-01-14 12:42:32 +03:00
|
|
|
#ifndef CONFIG_MMU
|
2006-09-27 12:50:16 +04:00
|
|
|
.get_unmapped_area = get_unmapped_area_mem,
|
2015-01-14 12:42:32 +03:00
|
|
|
.mmap_capabilities = memory_mmap_capabilities,
|
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2006-07-03 11:24:21 +04:00
|
|
|
static const struct file_operations null_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.llseek = null_lseek,
|
|
|
|
.read = read_null,
|
|
|
|
.write = write_null,
|
2015-04-03 22:57:04 +03:00
|
|
|
.read_iter = read_iter_null,
|
|
|
|
.write_iter = write_iter_null,
|
2006-04-26 16:40:08 +04:00
|
|
|
.splice_write = splice_write_null,
|
2022-08-19 23:20:33 +03:00
|
|
|
.uring_cmd = uring_cmd_null,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2014-12-07 18:40:35 +03:00
|
|
|
static const struct file_operations __maybe_unused port_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.llseek = memory_lseek,
|
|
|
|
.read = read_port,
|
|
|
|
.write = write_port,
|
|
|
|
.open = open_port,
|
|
|
|
};
|
|
|
|
|
2006-07-03 11:24:21 +04:00
|
|
|
static const struct file_operations zero_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.llseek = zero_lseek,
|
|
|
|
.write = write_zero,
|
2014-08-18 18:04:12 +04:00
|
|
|
.read_iter = read_iter_zero,
|
2020-09-03 18:59:22 +03:00
|
|
|
.read = read_zero,
|
2015-04-03 22:57:04 +03:00
|
|
|
.write_iter = write_iter_zero,
|
2005-04-17 02:20:36 +04:00
|
|
|
.mmap = mmap_zero,
|
2016-07-27 01:26:15 +03:00
|
|
|
.get_unmapped_area = get_unmapped_area_zero,
|
2015-01-14 12:42:32 +03:00
|
|
|
#ifndef CONFIG_MMU
|
|
|
|
.mmap_capabilities = zero_mmap_capabilities,
|
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2006-07-03 11:24:21 +04:00
|
|
|
static const struct file_operations full_fops = {
|
2005-04-17 02:20:36 +04:00
|
|
|
.llseek = full_lseek,
|
2014-08-18 18:04:12 +04:00
|
|
|
.read_iter = read_iter_zero,
|
2005-04-17 02:20:36 +04:00
|
|
|
.write = write_full,
|
|
|
|
};
|
|
|
|
|
2009-07-04 18:51:29 +04:00
|
|
|
static const struct memdev {
|
|
|
|
const char *name;
|
2011-07-24 04:24:48 +04:00
|
|
|
umode_t mode;
|
2009-07-04 18:51:29 +04:00
|
|
|
const struct file_operations *fops;
|
2015-01-14 12:42:32 +03:00
|
|
|
fmode_t fmode;
|
2009-07-04 18:51:29 +04:00
|
|
|
} devlist[] = {
|
2014-12-07 18:40:33 +03:00
|
|
|
#ifdef CONFIG_DEVMEM
|
2020-05-22 00:06:17 +03:00
|
|
|
[DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
|
2008-04-29 11:58:34 +04:00
|
|
|
#endif
|
2021-09-08 22:10:38 +03:00
|
|
|
[3] = { "null", 0666, &null_fops, FMODE_NOWAIT },
|
2007-05-08 11:28:17 +04:00
|
|
|
#ifdef CONFIG_DEVPORT
|
2015-01-14 12:42:32 +03:00
|
|
|
[4] = { "port", 0, &port_fops, 0 },
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif
|
2021-09-08 22:10:38 +03:00
|
|
|
[5] = { "zero", 0666, &zero_fops, FMODE_NOWAIT },
|
2015-01-14 12:42:32 +03:00
|
|
|
[7] = { "full", 0666, &full_fops, 0 },
|
random: restore O_NONBLOCK support
Prior to 5.6, when /dev/random was opened with O_NONBLOCK, it would
return -EAGAIN if there was no entropy. When the pools were unified in
5.6, this was lost. The post 5.6 behavior of blocking until the pool is
initialized, and ignoring O_NONBLOCK in the process, went unnoticed,
with no reports about the regression received for two and a half years.
However, eventually this indeed did break somebody's userspace.
So we restore the old behavior, by returning -EAGAIN if the pool is not
initialized. Unlike the old /dev/random, this can only occur during
early boot, after which it never blocks again.
In order to make this O_NONBLOCK behavior consistent with other
expectations, also respect users reading with preadv2(RWF_NOWAIT) and
similar.
Fixes: 30c08efec888 ("random: make /dev/random be almost like /dev/urandom")
Reported-by: Guozihua <guozihua@huawei.com>
Reported-by: Zhongguohua <zhongguohua1@huawei.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Andrew Lutomirski <luto@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2022-09-08 17:14:00 +03:00
|
|
|
[8] = { "random", 0666, &random_fops, FMODE_NOWAIT },
|
|
|
|
[9] = { "urandom", 0666, &urandom_fops, FMODE_NOWAIT },
|
2012-05-09 03:37:51 +04:00
|
|
|
#ifdef CONFIG_PRINTK
|
2015-01-14 12:42:32 +03:00
|
|
|
[11] = { "kmsg", 0644, &kmsg_fops, 0 },
|
2012-05-09 03:37:51 +04:00
|
|
|
#endif
|
2009-06-18 03:27:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int memory_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2009-07-04 18:51:29 +04:00
|
|
|
int minor;
|
|
|
|
const struct memdev *dev;
|
2009-06-18 03:27:48 +04:00
|
|
|
|
2009-07-04 18:51:29 +04:00
|
|
|
minor = iminor(inode);
|
|
|
|
if (minor >= ARRAY_SIZE(devlist))
|
2009-10-09 22:31:02 +04:00
|
|
|
return -ENXIO;
|
2009-06-18 03:27:48 +04:00
|
|
|
|
2009-07-04 18:51:29 +04:00
|
|
|
dev = &devlist[minor];
|
|
|
|
if (!dev->fops)
|
2009-10-09 22:31:02 +04:00
|
|
|
return -ENXIO;
|
2009-06-18 03:27:48 +04:00
|
|
|
|
2009-07-04 18:51:29 +04:00
|
|
|
filp->f_op = dev->fops;
|
2015-01-14 12:42:32 +03:00
|
|
|
filp->f_mode |= dev->fmode;
|
2010-10-02 01:20:22 +04:00
|
|
|
|
2009-07-04 18:51:29 +04:00
|
|
|
if (dev->fops->open)
|
2009-10-09 22:31:02 +04:00
|
|
|
return dev->fops->open(inode, filp);
|
|
|
|
|
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-07-03 11:24:21 +04:00
|
|
|
static const struct file_operations memory_fops = {
|
2010-03-11 02:21:52 +03:00
|
|
|
.open = memory_open,
|
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 20:52:59 +04:00
|
|
|
.llseek = noop_llseek,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2011-07-24 04:24:48 +04:00
|
|
|
static char *mem_devnode(struct device *dev, umode_t *mode)
|
2009-09-19 01:01:12 +04:00
|
|
|
{
|
|
|
|
if (mode && devlist[MINOR(dev->devt)].mode)
|
|
|
|
*mode = devlist[MINOR(dev->devt)].mode;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-23 20:53:09 +03:00
|
|
|
static struct class *mem_class;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
static int __init chr_dev_init(void)
|
|
|
|
{
|
2009-07-04 18:51:29 +04:00
|
|
|
int minor;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-03-11 02:21:52 +03:00
|
|
|
if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
|
2005-04-17 02:20:36 +04:00
|
|
|
printk("unable to get major %d for memory devs\n", MEM_MAJOR);
|
|
|
|
|
2005-03-23 20:53:09 +03:00
|
|
|
mem_class = class_create(THIS_MODULE, "mem");
|
2010-04-07 01:34:55 +04:00
|
|
|
if (IS_ERR(mem_class))
|
|
|
|
return PTR_ERR(mem_class);
|
|
|
|
|
2009-09-19 01:01:12 +04:00
|
|
|
mem_class->devnode = mem_devnode;
|
2009-07-04 18:51:29 +04:00
|
|
|
for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
|
|
|
|
if (!devlist[minor].name)
|
|
|
|
continue;
|
2012-07-11 09:18:44 +04:00
|
|
|
|
|
|
|
/*
|
2013-02-06 14:37:20 +04:00
|
|
|
* Create /dev/port?
|
2012-07-11 09:18:44 +04:00
|
|
|
*/
|
|
|
|
if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
|
|
|
|
continue;
|
|
|
|
|
2009-07-04 18:51:29 +04:00
|
|
|
device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
|
|
|
|
NULL, devlist[minor].name);
|
|
|
|
}
|
2006-07-26 04:13:31 +04:00
|
|
|
|
2010-08-06 19:34:43 +04:00
|
|
|
return tty_init();
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fs_initcall(chr_dev_init);
|