Skip to contentSkip to conversation

/dev/mem as optional device + associated tidy up

PATCH In review [email protected] 2 participants 5 messages Dec 7, 2014 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

cover /dev/mem as optional device + associated tidy up

From: Rob Ward <[email protected]>

Makes the /dev/mem device optional using the CONFIG_DEVMEM option.

This brings /dev/mem in line with /dev/kmem so it can de disabled. Disabling /dev/mem is preferable on devices that do not require direct access to memory in order to improve security. This is especially true on platforms where no restiction is placed on the areas of memory that can be accessed(for example mips) without custom modifcations to the kernel source.

The implementation of this functionality declares the mem_fops structure as __maybe_unused to eliminate the need for #ifdefs throughout the file.

Follow up changes are present to change CONFIG_DEVKMEM and CONFIG_DEVPORT to use __maybe_unused. This simplifies the code and make the implementations consistent.

Rob Ward (3):
drivers: char: mem: Make /dev/mem an optional device
drivers: char: mem: Simplify DEVKMEM configuration
drivers: char: mem: Simplify DEVPORT configuration

 drivers/char/Kconfig |  9 +++++++++
 drivers/char/mem.c   | 18 +++++-------------
 2 files changed, 14 insertions(+), 13 deletions(-)
Signature · 1 line
2.0.2
#1 lore raw

PATCH 1/3 drivers: char: mem: Make /dev/mem an optional device

From: Rob Ward <[email protected]>

Adds Kconfig option CONFIG_DEVMEM that allows the
/dev/mem device to be disabled.

Option defaults to /dev/mem enabled.

---
 drivers/char/Kconfig | 9 +++++++++
 drivers/char/mem.c   | 4 +++-
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/char/Kconfig +9 -0
index efefd12..a4af822 100644
@@ -6,6 +6,15 @@ menu "Character devices"
 
 source "drivers/tty/Kconfig"
 
+config DEVMEM
+	bool "/dev/mem virtual device support"
+	default y
+	help
+	  Say Y here if you want to support the /dev/mem device.
+	  The /dev/mem device is used to access areas of physical
+	  memory.
+	  When in doubt, say "Y".
+
 config DEVKMEM
 	bool "/dev/kmem virtual device support"
 	default y
drivers/char/mem.c +3 -1
index 524b707..b2ca476 100644
@@ -712,7 +712,7 @@ static int open_port(struct inode *inode, struct file *filp)
 #define open_mem	open_port
 #define open_kmem	open_mem
 
-static const struct file_operations mem_fops = {
+static const struct file_operations __maybe_unused mem_fops = {
 	.llseek		= memory_lseek,
 	.read		= read_mem,
 	.write		= write_mem,
@@ -782,7 +782,9 @@ static const struct memdev {
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
 } devlist[] = {
+#ifdef CONFIG_DEVMEM
 	 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
+#endif
 #ifdef CONFIG_DEVKMEM
 	 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
 #endif
Signature · 1 line
2.0.2
#2 lore raw

PATCH 2/3 drivers: char: mem: Simplify DEVKMEM configuration

In reply to [email protected]

From: Rob Ward <[email protected]>

Simplify the use of CONFIG_DEVKMEM by making the kmem_fops so that it is __maybe_unused.

This enabled the multiple #ifdef's used for this structure to be removed and brings it in line with the use of CONFIG_DEVMEM

This change should introduce no functional changes.

---
 drivers/char/mem.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/char/mem.c +1 -7
index b2ca476..4521fea 100644
@@ -338,7 +338,6 @@ static int mmap_mem(struct file *file, struct vm_area_struct *vma)
 	return 0;
 }
 
-#ifdef CONFIG_DEVKMEM
 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
 {
 	unsigned long pfn;
@@ -359,9 +358,7 @@ static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
 	vma->vm_pgoff = pfn;
 	return mmap_mem(file, vma);
 }
-#endif
 
-#ifdef CONFIG_DEVKMEM
 /*
  * This function reads the *virtual* memory as seen by the kernel.
  */
@@ -541,7 +538,6 @@ static ssize_t write_kmem(struct file *file, const char __user *buf,
 	*ppos = p;
 	return virtr + wrote ? : err;
 }
-#endif
 
 #ifdef CONFIG_DEVPORT
 static ssize_t read_port(struct file *file, char __user *buf,
@@ -721,8 +717,7 @@ static const struct file_operations __maybe_unused mem_fops = {
 	.get_unmapped_area = get_unmapped_area_mem,
 };
 
-#ifdef CONFIG_DEVKMEM
-static const struct file_operations kmem_fops = {
+static const struct file_operations __maybe_unused kmem_fops = {
 	.llseek		= memory_lseek,
 	.read		= read_kmem,
 	.write		= write_kmem,
@@ -730,7 +725,6 @@ static const struct file_operations kmem_fops = {
 	.open		= open_kmem,
 	.get_unmapped_area = get_unmapped_area_mem,
 };
-#endif
 
 static const struct file_operations null_fops = {
 	.llseek		= null_lseek,
Signature · 1 line
2.0.2
#3 lore raw

PATCH 3/3 drivers: char: mem: Simplify DEVPORT configuration

In reply to [email protected]

From: Rob Ward <[email protected]>

Simplify the use of CONFIG_DEVPORT by making the port_fops so that it includes __maybe_unused.

This enabled the multiple #ifdef's used for this structure to be removed and brings it in line with the use of CONFIG_DEVMEM

This change should introduce no functional changes.

---
 drivers/char/mem.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/char/mem.c +1 -5
index 4521fea..39315fe 100644
@@ -539,7 +539,6 @@ static ssize_t write_kmem(struct file *file, const char __user *buf,
 	return virtr + wrote ? : err;
 }
 
-#ifdef CONFIG_DEVPORT
 static ssize_t read_port(struct file *file, char __user *buf,
 			 size_t count, loff_t *ppos)
 {
@@ -580,7 +579,6 @@ static ssize_t write_port(struct file *file, const char __user *buf,
 	*ppos = i;
 	return tmp-buf;
 }
-#endif
 
 static ssize_t read_null(struct file *file, char __user *buf,
 			 size_t count, loff_t *ppos)
@@ -735,14 +733,12 @@ static const struct file_operations null_fops = {
 	.splice_write	= splice_write_null,
 };
 
-#ifdef CONFIG_DEVPORT
-static const struct file_operations port_fops = {
+static const struct file_operations __maybe_unused port_fops = {
 	.llseek		= memory_lseek,
 	.read		= read_port,
 	.write		= write_port,
 	.open		= open_port,
 };
-#endif
 
 static const struct file_operations zero_fops = {
 	.llseek		= zero_lseek,
Signature · 1 line
2.0.2
maintainer Acked-by on the v1 series
#4 lore raw
In reply to [email protected]
Quoted 11 lines · On Sunday 07 December 2014 15:40:32 [email protected] wrote:

From: Rob Ward <[email protected]>

Makes the /dev/mem device optional using the CONFIG_DEVMEM option.

This brings /dev/mem in line with /dev/kmem so it can de disabled. Disabling /dev/mem is preferable on devices that do not require direct access to memory in order to improve security. This is especially true on platforms where no restiction is placed on the areas of memory that can be accessed(for example mips) without custom modifcations to the kernel source.

Quoted 8 lines

The implementation of this functionality declares the mem_fops structure as __maybe_unused to eliminate the need for #ifdefs throughout the file.

Follow up changes are present to change CONFIG_DEVKMEM and CONFIG_DEVPORT to use __maybe_unused. This simplifies the code and make the implementations consistent.