All of lore.kernel.org
 help / color / mirror / Atom feed
From: Finn Thain <fthain@telegraphics.com.au>
To: <linux-kernel@vger.kernel.org>, <linux-m68k@vger.kernel.org>,
	<linuxppc-dev@lists.ozlabs.org>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Paul Mackerras <paulus@samba.org>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [RFC 21/24] powerpc: Adopt nvram module for PPC64
Date: Sun, 31 May 2015 11:01:53 +1000	[thread overview]
Message-ID: <20150531010137.569476861@telegraphics.com.au> (raw)
In-Reply-To: 20150531010132.289674310@telegraphics.com.au

[-- Attachment #1: ppc64-adopt-drivers-char-nvram --]
[-- Type: text/plain, Size: 9694 bytes --]

Adopt nvram module to reduce code duplication.

The IOC_NVRAM_GET_OFFSET ioctl as implemented on PPC64 validates the offset
returned by pmac_get_partition(). Add this test to the nvram module.

Note that the old PPC32 generic_nvram module lacked this test.
So when CONFIG_PPC32 && CONFIG_PPC_PMAC, the IOC_NVRAM_GET_OFFSET ioctl
would have returned 0 (always). But when CONFIG_PPC64 && CONFIG_PPC_PMAC,
the IOC_NVRAM_GET_OFFSET ioctl would have returned -1 (which is -EPERM)
when the requested partition was not found.

With this patch, the result is now -ENOENT on both PPC32 and PPC64 when
the requested PowerMac NVRAM partition is not found.

This is a userspace-visible change, in the non-existent partition case,
which would be in an error path for an IOC_NVRAM_GET_OFFSET ioctl syscall.

Signed-off-by: Finn Thain <fthain@telegraphics.com.au>

---

BTW, the IOC_NVRAM_SYNC ioctl call returns -EINVAL on PPC64. This patch
retains this behaviour though it might be better to actually perform a sync.
Both PPC64 and PPC32 kernels implement ppc_md.nvram_sync() for Core99,
but on PPC64 the ioctl is unimplemented (unlike PPC32).

---
 arch/powerpc/Kconfig                     |    3 
 arch/powerpc/kernel/nvram_64.c           |  203 ++++---------------------------
 arch/powerpc/platforms/powermac/Makefile |    5 
 arch/powerpc/platforms/powermac/nvram.c  |    2 
 arch/powerpc/platforms/powermac/setup.c  |    2 
 drivers/char/nvram.c                     |    2 
 6 files changed, 38 insertions(+), 179 deletions(-)

Index: linux/arch/powerpc/Kconfig
===================================================================
--- linux.orig/arch/powerpc/Kconfig	2015-05-31 11:01:22.000000000 +1000
+++ linux/arch/powerpc/Kconfig	2015-05-31 11:01:24.000000000 +1000
@@ -177,10 +177,9 @@ config SYSVIPC_COMPAT
 	depends on COMPAT && SYSVIPC
 	default y
 
-# All PPC32s use generic nvram driver through ppc_md
 config HAVE_ARCH_NVRAM_OPS
 	bool
-	default y if PPC32
+	default y
 
 config SCHED_OMIT_FRAME_POINTER
 	bool
Index: linux/arch/powerpc/kernel/nvram_64.c
===================================================================
--- linux.orig/arch/powerpc/kernel/nvram_64.c	2015-05-31 11:00:59.000000000 +1000
+++ linux/arch/powerpc/kernel/nvram_64.c	2015-05-31 11:01:25.000000000 +1000
@@ -7,12 +7,6 @@
  *      2 of the License, or (at your option) any later version.
  *
  * /dev/nvram driver for PPC64
- *
- * This perhaps should live in drivers/char
- *
- * TODO: Split the /dev/nvram part (that one can use
- *       drivers/char/generic_nvram.c) from the arch & partition
- *       parsing code.
  */
 
 #include <linux/module.h>
@@ -731,153 +725,6 @@ static void oops_to_nvram(struct kmsg_du
 	spin_unlock_irqrestore(&lock, flags);
 }
 
-static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
-{
-	int size;
-
-	if (ppc_md.nvram_size == NULL)
-		return -ENODEV;
-	size = ppc_md.nvram_size();
-
-	switch (origin) {
-	case 1:
-		offset += file->f_pos;
-		break;
-	case 2:
-		offset += size;
-		break;
-	}
-	if (offset < 0)
-		return -EINVAL;
-	file->f_pos = offset;
-	return file->f_pos;
-}
-
-
-static ssize_t dev_nvram_read(struct file *file, char __user *buf,
-			  size_t count, loff_t *ppos)
-{
-	ssize_t ret;
-	char *tmp = NULL;
-	ssize_t size;
-
-	if (!ppc_md.nvram_size) {
-		ret = -ENODEV;
-		goto out;
-	}
-
-	size = ppc_md.nvram_size();
-	if (size < 0) {
-		ret = size;
-		goto out;
-	}
-
-	if (*ppos >= size) {
-		ret = 0;
-		goto out;
-	}
-
-	count = min_t(size_t, count, size - *ppos);
-	count = min(count, PAGE_SIZE);
-
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	ret = ppc_md.nvram_read(tmp, count, ppos);
-	if (ret <= 0)
-		goto out;
-
-	if (copy_to_user(buf, tmp, ret))
-		ret = -EFAULT;
-
-out:
-	kfree(tmp);
-	return ret;
-
-}
-
-static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
-			  size_t count, loff_t *ppos)
-{
-	ssize_t ret;
-	char *tmp = NULL;
-	ssize_t size;
-
-	ret = -ENODEV;
-	if (!ppc_md.nvram_size)
-		goto out;
-
-	ret = 0;
-	size = ppc_md.nvram_size();
-	if (*ppos >= size || size < 0)
-		goto out;
-
-	count = min_t(size_t, count, size - *ppos);
-	count = min(count, PAGE_SIZE);
-
-	ret = -ENOMEM;
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp)
-		goto out;
-
-	ret = -EFAULT;
-	if (copy_from_user(tmp, buf, count))
-		goto out;
-
-	ret = ppc_md.nvram_write(tmp, count, ppos);
-
-out:
-	kfree(tmp);
-	return ret;
-
-}
-
-static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
-			    unsigned long arg)
-{
-	switch(cmd) {
-#ifdef CONFIG_PPC_PMAC
-	case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
-		printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
-	case IOC_NVRAM_GET_OFFSET: {
-		int part, offset;
-
-		if (!machine_is(powermac))
-			return -EINVAL;
-		if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
-			return -EFAULT;
-		if (part < pmac_nvram_OF || part > pmac_nvram_NR)
-			return -EINVAL;
-		offset = pmac_get_partition(part);
-		if (offset < 0)
-			return offset;
-		if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
-			return -EFAULT;
-		return 0;
-	}
-#endif /* CONFIG_PPC_PMAC */
-	default:
-		return -EINVAL;
-	}
-}
-
-const struct file_operations nvram_fops = {
-	.owner		= THIS_MODULE,
-	.llseek		= dev_nvram_llseek,
-	.read		= dev_nvram_read,
-	.write		= dev_nvram_write,
-	.unlocked_ioctl	= dev_nvram_ioctl,
-};
-
-static struct miscdevice nvram_dev = {
-	NVRAM_MINOR,
-	"nvram",
-	&nvram_fops
-};
-
 
 #ifdef DEBUG_NVRAM
 static void __init nvram_print_partitions(char * label)
@@ -1026,6 +873,8 @@ loff_t __init nvram_create_partition(con
 	long size = 0;
 	int rc;
 
+	BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
+
 	/* Convert sizes from bytes to blocks */
 	req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
 	min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
@@ -1226,29 +1075,41 @@ int __init nvram_scan_partitions(void)
 	return err;
 }
 
-static int __init nvram_init(void)
+
+#if IS_ENABLED(CONFIG_NVRAM)
+
+static ssize_t ppc_nvram_read(char *buf, size_t count, loff_t *index)
 {
-	int rc;
-	
-	BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
+	if (ppc_md.nvram_read)
+		return ppc_md.nvram_read(buf, count, index);
+	return -EINVAL;
+}
 
-	if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
-		return  -ENODEV;
+static ssize_t ppc_nvram_write(char *buf, size_t count, loff_t *index)
+{
+	if (ppc_md.nvram_write)
+		return ppc_md.nvram_write(buf, count, index);
+	return -EINVAL;
+}
 
-  	rc = misc_register(&nvram_dev);
-	if (rc != 0) {
-		printk(KERN_ERR "nvram_init: failed to register device\n");
-		return rc;
-	}
-  	
-  	return rc;
+static ssize_t ppc_nvram_get_size(void)
+{
+	if (ppc_md.nvram_size)
+		return ppc_md.nvram_size();
+	return -ENODEV;
 }
 
-static void __exit nvram_cleanup(void)
+static long ppc_nvram_sync(void)
 {
-        misc_deregister( &nvram_dev );
+	return -EINVAL;
 }
 
-module_init(nvram_init);
-module_exit(nvram_cleanup);
-MODULE_LICENSE("GPL");
+const struct nvram_ops arch_nvram_ops = {
+	.read           = ppc_nvram_read,
+	.write          = ppc_nvram_write,
+	.get_size       = ppc_nvram_get_size,
+	.sync           = ppc_nvram_sync,
+};
+EXPORT_SYMBOL(arch_nvram_ops);
+
+#endif /* CONFIG_NVRAM */
Index: linux/arch/powerpc/platforms/powermac/Makefile
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/Makefile	2015-05-31 11:00:59.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/Makefile	2015-05-31 11:01:25.000000000 +1000
@@ -9,11 +9,6 @@ obj-y				+= pic.o setup.o time.o feature
 				   sleep.o low_i2c.o cache.o pfunc_core.o \
 				   pfunc_base.o udbg_scc.o udbg_adb.o
 obj-$(CONFIG_PMAC_BACKLIGHT)	+= backlight.o
-# CONFIG_NVRAM is an arch. independent tristate symbol, for pmac32 we really
-# need this to be a bool.  Cheat here and pretend CONFIG_NVRAM=m is really
-# CONFIG_NVRAM=y
 obj-$(CONFIG_NVRAM:m=y)		+= nvram.o
-# ppc64 pmac doesn't define CONFIG_NVRAM but needs nvram stuff
-obj-$(CONFIG_PPC64)		+= nvram.o
 obj-$(CONFIG_PPC32)		+= bootx_init.o
 obj-$(CONFIG_SMP)		+= smp.o
Index: linux/arch/powerpc/platforms/powermac/setup.c
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/setup.c	2015-05-31 11:01:22.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/setup.c	2015-05-31 11:01:25.000000000 +1000
@@ -321,7 +321,7 @@ static void __init pmac_setup_arch(void)
 	find_via_pmu();
 	smu_init();
 
-#if IS_ENABLED(CONFIG_NVRAM) || defined(CONFIG_PPC64)
+#if IS_ENABLED(CONFIG_NVRAM)
 	pmac_nvram_init();
 #endif
 
Index: linux/drivers/char/nvram.c
===================================================================
--- linux.orig/drivers/char/nvram.c	2015-05-31 11:01:21.000000000 +1000
+++ linux/drivers/char/nvram.c	2015-05-31 11:01:25.000000000 +1000
@@ -342,6 +342,8 @@ static long nvram_misc_ioctl(struct file
 		if (part < pmac_nvram_OF || part > pmac_nvram_NR)
 			return -EINVAL;
 		offset = pmac_get_partition(part);
+		if (offset < 0)
+			return offset;
 		if (copy_to_user((void __user *)arg,
 		                 &offset, sizeof(offset)) != 0)
 			return -EFAULT;
Index: linux/arch/powerpc/platforms/powermac/nvram.c
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/nvram.c	2015-05-31 11:01:15.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/nvram.c	2015-05-31 11:01:25.000000000 +1000
@@ -629,6 +629,8 @@ bail:
 
 int pmac_get_partition(int partition)
 {
+	if (nvram_partitions[partition] < 0)
+		return -ENOENT;
 	return nvram_partitions[partition];
 }
 



WARNING: multiple messages have this Message-ID (diff)
From: Finn Thain <fthain@telegraphics.com.au>
To: linux-kernel@vger.kernel.org, linux-m68k@vger.kernel.org,
	linuxppc-dev@lists.ozlabs.org,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Paul Mackerras <paulus@samba.org>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [RFC 21/24] powerpc: Adopt nvram module for PPC64
Date: Sun, 31 May 2015 11:01:53 +1000	[thread overview]
Message-ID: <20150531010137.569476861@telegraphics.com.au> (raw)
In-Reply-To: 20150531010132.289674310@telegraphics.com.au

[-- Attachment #1: ppc64-adopt-drivers-char-nvram --]
[-- Type: text/plain, Size: 9692 bytes --]

Adopt nvram module to reduce code duplication.

The IOC_NVRAM_GET_OFFSET ioctl as implemented on PPC64 validates the offset
returned by pmac_get_partition(). Add this test to the nvram module.

Note that the old PPC32 generic_nvram module lacked this test.
So when CONFIG_PPC32 && CONFIG_PPC_PMAC, the IOC_NVRAM_GET_OFFSET ioctl
would have returned 0 (always). But when CONFIG_PPC64 && CONFIG_PPC_PMAC,
the IOC_NVRAM_GET_OFFSET ioctl would have returned -1 (which is -EPERM)
when the requested partition was not found.

With this patch, the result is now -ENOENT on both PPC32 and PPC64 when
the requested PowerMac NVRAM partition is not found.

This is a userspace-visible change, in the non-existent partition case,
which would be in an error path for an IOC_NVRAM_GET_OFFSET ioctl syscall.

Signed-off-by: Finn Thain <fthain@telegraphics.com.au>

---

BTW, the IOC_NVRAM_SYNC ioctl call returns -EINVAL on PPC64. This patch
retains this behaviour though it might be better to actually perform a sync.
Both PPC64 and PPC32 kernels implement ppc_md.nvram_sync() for Core99,
but on PPC64 the ioctl is unimplemented (unlike PPC32).

---
 arch/powerpc/Kconfig                     |    3 
 arch/powerpc/kernel/nvram_64.c           |  203 ++++---------------------------
 arch/powerpc/platforms/powermac/Makefile |    5 
 arch/powerpc/platforms/powermac/nvram.c  |    2 
 arch/powerpc/platforms/powermac/setup.c  |    2 
 drivers/char/nvram.c                     |    2 
 6 files changed, 38 insertions(+), 179 deletions(-)

Index: linux/arch/powerpc/Kconfig
===================================================================
--- linux.orig/arch/powerpc/Kconfig	2015-05-31 11:01:22.000000000 +1000
+++ linux/arch/powerpc/Kconfig	2015-05-31 11:01:24.000000000 +1000
@@ -177,10 +177,9 @@ config SYSVIPC_COMPAT
 	depends on COMPAT && SYSVIPC
 	default y
 
-# All PPC32s use generic nvram driver through ppc_md
 config HAVE_ARCH_NVRAM_OPS
 	bool
-	default y if PPC32
+	default y
 
 config SCHED_OMIT_FRAME_POINTER
 	bool
Index: linux/arch/powerpc/kernel/nvram_64.c
===================================================================
--- linux.orig/arch/powerpc/kernel/nvram_64.c	2015-05-31 11:00:59.000000000 +1000
+++ linux/arch/powerpc/kernel/nvram_64.c	2015-05-31 11:01:25.000000000 +1000
@@ -7,12 +7,6 @@
  *      2 of the License, or (at your option) any later version.
  *
  * /dev/nvram driver for PPC64
- *
- * This perhaps should live in drivers/char
- *
- * TODO: Split the /dev/nvram part (that one can use
- *       drivers/char/generic_nvram.c) from the arch & partition
- *       parsing code.
  */
 
 #include <linux/module.h>
@@ -731,153 +725,6 @@ static void oops_to_nvram(struct kmsg_du
 	spin_unlock_irqrestore(&lock, flags);
 }
 
-static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
-{
-	int size;
-
-	if (ppc_md.nvram_size == NULL)
-		return -ENODEV;
-	size = ppc_md.nvram_size();
-
-	switch (origin) {
-	case 1:
-		offset += file->f_pos;
-		break;
-	case 2:
-		offset += size;
-		break;
-	}
-	if (offset < 0)
-		return -EINVAL;
-	file->f_pos = offset;
-	return file->f_pos;
-}
-
-
-static ssize_t dev_nvram_read(struct file *file, char __user *buf,
-			  size_t count, loff_t *ppos)
-{
-	ssize_t ret;
-	char *tmp = NULL;
-	ssize_t size;
-
-	if (!ppc_md.nvram_size) {
-		ret = -ENODEV;
-		goto out;
-	}
-
-	size = ppc_md.nvram_size();
-	if (size < 0) {
-		ret = size;
-		goto out;
-	}
-
-	if (*ppos >= size) {
-		ret = 0;
-		goto out;
-	}
-
-	count = min_t(size_t, count, size - *ppos);
-	count = min(count, PAGE_SIZE);
-
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	ret = ppc_md.nvram_read(tmp, count, ppos);
-	if (ret <= 0)
-		goto out;
-
-	if (copy_to_user(buf, tmp, ret))
-		ret = -EFAULT;
-
-out:
-	kfree(tmp);
-	return ret;
-
-}
-
-static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
-			  size_t count, loff_t *ppos)
-{
-	ssize_t ret;
-	char *tmp = NULL;
-	ssize_t size;
-
-	ret = -ENODEV;
-	if (!ppc_md.nvram_size)
-		goto out;
-
-	ret = 0;
-	size = ppc_md.nvram_size();
-	if (*ppos >= size || size < 0)
-		goto out;
-
-	count = min_t(size_t, count, size - *ppos);
-	count = min(count, PAGE_SIZE);
-
-	ret = -ENOMEM;
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp)
-		goto out;
-
-	ret = -EFAULT;
-	if (copy_from_user(tmp, buf, count))
-		goto out;
-
-	ret = ppc_md.nvram_write(tmp, count, ppos);
-
-out:
-	kfree(tmp);
-	return ret;
-
-}
-
-static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
-			    unsigned long arg)
-{
-	switch(cmd) {
-#ifdef CONFIG_PPC_PMAC
-	case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
-		printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
-	case IOC_NVRAM_GET_OFFSET: {
-		int part, offset;
-
-		if (!machine_is(powermac))
-			return -EINVAL;
-		if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
-			return -EFAULT;
-		if (part < pmac_nvram_OF || part > pmac_nvram_NR)
-			return -EINVAL;
-		offset = pmac_get_partition(part);
-		if (offset < 0)
-			return offset;
-		if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
-			return -EFAULT;
-		return 0;
-	}
-#endif /* CONFIG_PPC_PMAC */
-	default:
-		return -EINVAL;
-	}
-}
-
-const struct file_operations nvram_fops = {
-	.owner		= THIS_MODULE,
-	.llseek		= dev_nvram_llseek,
-	.read		= dev_nvram_read,
-	.write		= dev_nvram_write,
-	.unlocked_ioctl	= dev_nvram_ioctl,
-};
-
-static struct miscdevice nvram_dev = {
-	NVRAM_MINOR,
-	"nvram",
-	&nvram_fops
-};
-
 
 #ifdef DEBUG_NVRAM
 static void __init nvram_print_partitions(char * label)
@@ -1026,6 +873,8 @@ loff_t __init nvram_create_partition(con
 	long size = 0;
 	int rc;
 
+	BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
+
 	/* Convert sizes from bytes to blocks */
 	req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
 	min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
@@ -1226,29 +1075,41 @@ int __init nvram_scan_partitions(void)
 	return err;
 }
 
-static int __init nvram_init(void)
+
+#if IS_ENABLED(CONFIG_NVRAM)
+
+static ssize_t ppc_nvram_read(char *buf, size_t count, loff_t *index)
 {
-	int rc;
-	
-	BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
+	if (ppc_md.nvram_read)
+		return ppc_md.nvram_read(buf, count, index);
+	return -EINVAL;
+}
 
-	if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
-		return  -ENODEV;
+static ssize_t ppc_nvram_write(char *buf, size_t count, loff_t *index)
+{
+	if (ppc_md.nvram_write)
+		return ppc_md.nvram_write(buf, count, index);
+	return -EINVAL;
+}
 
-  	rc = misc_register(&nvram_dev);
-	if (rc != 0) {
-		printk(KERN_ERR "nvram_init: failed to register device\n");
-		return rc;
-	}
-  	
-  	return rc;
+static ssize_t ppc_nvram_get_size(void)
+{
+	if (ppc_md.nvram_size)
+		return ppc_md.nvram_size();
+	return -ENODEV;
 }
 
-static void __exit nvram_cleanup(void)
+static long ppc_nvram_sync(void)
 {
-        misc_deregister( &nvram_dev );
+	return -EINVAL;
 }
 
-module_init(nvram_init);
-module_exit(nvram_cleanup);
-MODULE_LICENSE("GPL");
+const struct nvram_ops arch_nvram_ops = {
+	.read           = ppc_nvram_read,
+	.write          = ppc_nvram_write,
+	.get_size       = ppc_nvram_get_size,
+	.sync           = ppc_nvram_sync,
+};
+EXPORT_SYMBOL(arch_nvram_ops);
+
+#endif /* CONFIG_NVRAM */
Index: linux/arch/powerpc/platforms/powermac/Makefile
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/Makefile	2015-05-31 11:00:59.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/Makefile	2015-05-31 11:01:25.000000000 +1000
@@ -9,11 +9,6 @@ obj-y				+= pic.o setup.o time.o feature
 				   sleep.o low_i2c.o cache.o pfunc_core.o \
 				   pfunc_base.o udbg_scc.o udbg_adb.o
 obj-$(CONFIG_PMAC_BACKLIGHT)	+= backlight.o
-# CONFIG_NVRAM is an arch. independent tristate symbol, for pmac32 we really
-# need this to be a bool.  Cheat here and pretend CONFIG_NVRAM=m is really
-# CONFIG_NVRAM=y
 obj-$(CONFIG_NVRAM:m=y)		+= nvram.o
-# ppc64 pmac doesn't define CONFIG_NVRAM but needs nvram stuff
-obj-$(CONFIG_PPC64)		+= nvram.o
 obj-$(CONFIG_PPC32)		+= bootx_init.o
 obj-$(CONFIG_SMP)		+= smp.o
Index: linux/arch/powerpc/platforms/powermac/setup.c
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/setup.c	2015-05-31 11:01:22.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/setup.c	2015-05-31 11:01:25.000000000 +1000
@@ -321,7 +321,7 @@ static void __init pmac_setup_arch(void)
 	find_via_pmu();
 	smu_init();
 
-#if IS_ENABLED(CONFIG_NVRAM) || defined(CONFIG_PPC64)
+#if IS_ENABLED(CONFIG_NVRAM)
 	pmac_nvram_init();
 #endif
 
Index: linux/drivers/char/nvram.c
===================================================================
--- linux.orig/drivers/char/nvram.c	2015-05-31 11:01:21.000000000 +1000
+++ linux/drivers/char/nvram.c	2015-05-31 11:01:25.000000000 +1000
@@ -342,6 +342,8 @@ static long nvram_misc_ioctl(struct file
 		if (part < pmac_nvram_OF || part > pmac_nvram_NR)
 			return -EINVAL;
 		offset = pmac_get_partition(part);
+		if (offset < 0)
+			return offset;
 		if (copy_to_user((void __user *)arg,
 		                 &offset, sizeof(offset)) != 0)
 			return -EFAULT;
Index: linux/arch/powerpc/platforms/powermac/nvram.c
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/nvram.c	2015-05-31 11:01:15.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/nvram.c	2015-05-31 11:01:25.000000000 +1000
@@ -629,6 +629,8 @@ bail:
 
 int pmac_get_partition(int partition)
 {
+	if (nvram_partitions[partition] < 0)
+		return -ENOENT;
 	return nvram_partitions[partition];
 }
 

WARNING: multiple messages have this Message-ID (diff)
From: Finn Thain <fthain@telegraphics.com.au>
To: <linux-kernel@vger.kernel.org>, <linux-m68k@vger.kernel.org>,
	<linuxppc-dev@lists.ozlabs.org>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Paul Mackerras <paulus@samba.org>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [RFC 21/24] powerpc: Adopt nvram module for PPC64
Date: Sun, 31 May 2015 11:01:53 +1000	[thread overview]
Message-ID: <20150531010137.569476861@telegraphics.com.au> (raw)
In-Reply-To: 20150531010132.289674310@telegraphics.com.au

Adopt nvram module to reduce code duplication.

The IOC_NVRAM_GET_OFFSET ioctl as implemented on PPC64 validates the offset
returned by pmac_get_partition(). Add this test to the nvram module.

Note that the old PPC32 generic_nvram module lacked this test.
So when CONFIG_PPC32 && CONFIG_PPC_PMAC, the IOC_NVRAM_GET_OFFSET ioctl
would have returned 0 (always). But when CONFIG_PPC64 && CONFIG_PPC_PMAC,
the IOC_NVRAM_GET_OFFSET ioctl would have returned -1 (which is -EPERM)
when the requested partition was not found.

With this patch, the result is now -ENOENT on both PPC32 and PPC64 when
the requested PowerMac NVRAM partition is not found.

This is a userspace-visible change, in the non-existent partition case,
which would be in an error path for an IOC_NVRAM_GET_OFFSET ioctl syscall.

Signed-off-by: Finn Thain <fthain@telegraphics.com.au>

---

BTW, the IOC_NVRAM_SYNC ioctl call returns -EINVAL on PPC64. This patch
retains this behaviour though it might be better to actually perform a sync.
Both PPC64 and PPC32 kernels implement ppc_md.nvram_sync() for Core99,
but on PPC64 the ioctl is unimplemented (unlike PPC32).

---
 arch/powerpc/Kconfig                     |    3 
 arch/powerpc/kernel/nvram_64.c           |  203 ++++---------------------------
 arch/powerpc/platforms/powermac/Makefile |    5 
 arch/powerpc/platforms/powermac/nvram.c  |    2 
 arch/powerpc/platforms/powermac/setup.c  |    2 
 drivers/char/nvram.c                     |    2 
 6 files changed, 38 insertions(+), 179 deletions(-)

Index: linux/arch/powerpc/Kconfig
===================================================================
--- linux.orig/arch/powerpc/Kconfig	2015-05-31 11:01:22.000000000 +1000
+++ linux/arch/powerpc/Kconfig	2015-05-31 11:01:24.000000000 +1000
@@ -177,10 +177,9 @@ config SYSVIPC_COMPAT
 	depends on COMPAT && SYSVIPC
 	default y
 
-# All PPC32s use generic nvram driver through ppc_md
 config HAVE_ARCH_NVRAM_OPS
 	bool
-	default y if PPC32
+	default y
 
 config SCHED_OMIT_FRAME_POINTER
 	bool
Index: linux/arch/powerpc/kernel/nvram_64.c
===================================================================
--- linux.orig/arch/powerpc/kernel/nvram_64.c	2015-05-31 11:00:59.000000000 +1000
+++ linux/arch/powerpc/kernel/nvram_64.c	2015-05-31 11:01:25.000000000 +1000
@@ -7,12 +7,6 @@
  *      2 of the License, or (at your option) any later version.
  *
  * /dev/nvram driver for PPC64
- *
- * This perhaps should live in drivers/char
- *
- * TODO: Split the /dev/nvram part (that one can use
- *       drivers/char/generic_nvram.c) from the arch & partition
- *       parsing code.
  */
 
 #include <linux/module.h>
@@ -731,153 +725,6 @@ static void oops_to_nvram(struct kmsg_du
 	spin_unlock_irqrestore(&lock, flags);
 }
 
-static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
-{
-	int size;
-
-	if (ppc_md.nvram_size == NULL)
-		return -ENODEV;
-	size = ppc_md.nvram_size();
-
-	switch (origin) {
-	case 1:
-		offset += file->f_pos;
-		break;
-	case 2:
-		offset += size;
-		break;
-	}
-	if (offset < 0)
-		return -EINVAL;
-	file->f_pos = offset;
-	return file->f_pos;
-}
-
-
-static ssize_t dev_nvram_read(struct file *file, char __user *buf,
-			  size_t count, loff_t *ppos)
-{
-	ssize_t ret;
-	char *tmp = NULL;
-	ssize_t size;
-
-	if (!ppc_md.nvram_size) {
-		ret = -ENODEV;
-		goto out;
-	}
-
-	size = ppc_md.nvram_size();
-	if (size < 0) {
-		ret = size;
-		goto out;
-	}
-
-	if (*ppos >= size) {
-		ret = 0;
-		goto out;
-	}
-
-	count = min_t(size_t, count, size - *ppos);
-	count = min(count, PAGE_SIZE);
-
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	ret = ppc_md.nvram_read(tmp, count, ppos);
-	if (ret <= 0)
-		goto out;
-
-	if (copy_to_user(buf, tmp, ret))
-		ret = -EFAULT;
-
-out:
-	kfree(tmp);
-	return ret;
-
-}
-
-static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
-			  size_t count, loff_t *ppos)
-{
-	ssize_t ret;
-	char *tmp = NULL;
-	ssize_t size;
-
-	ret = -ENODEV;
-	if (!ppc_md.nvram_size)
-		goto out;
-
-	ret = 0;
-	size = ppc_md.nvram_size();
-	if (*ppos >= size || size < 0)
-		goto out;
-
-	count = min_t(size_t, count, size - *ppos);
-	count = min(count, PAGE_SIZE);
-
-	ret = -ENOMEM;
-	tmp = kmalloc(count, GFP_KERNEL);
-	if (!tmp)
-		goto out;
-
-	ret = -EFAULT;
-	if (copy_from_user(tmp, buf, count))
-		goto out;
-
-	ret = ppc_md.nvram_write(tmp, count, ppos);
-
-out:
-	kfree(tmp);
-	return ret;
-
-}
-
-static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
-			    unsigned long arg)
-{
-	switch(cmd) {
-#ifdef CONFIG_PPC_PMAC
-	case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
-		printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
-	case IOC_NVRAM_GET_OFFSET: {
-		int part, offset;
-
-		if (!machine_is(powermac))
-			return -EINVAL;
-		if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
-			return -EFAULT;
-		if (part < pmac_nvram_OF || part > pmac_nvram_NR)
-			return -EINVAL;
-		offset = pmac_get_partition(part);
-		if (offset < 0)
-			return offset;
-		if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
-			return -EFAULT;
-		return 0;
-	}
-#endif /* CONFIG_PPC_PMAC */
-	default:
-		return -EINVAL;
-	}
-}
-
-const struct file_operations nvram_fops = {
-	.owner		= THIS_MODULE,
-	.llseek		= dev_nvram_llseek,
-	.read		= dev_nvram_read,
-	.write		= dev_nvram_write,
-	.unlocked_ioctl	= dev_nvram_ioctl,
-};
-
-static struct miscdevice nvram_dev = {
-	NVRAM_MINOR,
-	"nvram",
-	&nvram_fops
-};
-
 
 #ifdef DEBUG_NVRAM
 static void __init nvram_print_partitions(char * label)
@@ -1026,6 +873,8 @@ loff_t __init nvram_create_partition(con
 	long size = 0;
 	int rc;
 
+	BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
+
 	/* Convert sizes from bytes to blocks */
 	req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
 	min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
@@ -1226,29 +1075,41 @@ int __init nvram_scan_partitions(void)
 	return err;
 }
 
-static int __init nvram_init(void)
+
+#if IS_ENABLED(CONFIG_NVRAM)
+
+static ssize_t ppc_nvram_read(char *buf, size_t count, loff_t *index)
 {
-	int rc;
-	
-	BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
+	if (ppc_md.nvram_read)
+		return ppc_md.nvram_read(buf, count, index);
+	return -EINVAL;
+}
 
-	if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
-		return  -ENODEV;
+static ssize_t ppc_nvram_write(char *buf, size_t count, loff_t *index)
+{
+	if (ppc_md.nvram_write)
+		return ppc_md.nvram_write(buf, count, index);
+	return -EINVAL;
+}
 
-  	rc = misc_register(&nvram_dev);
-	if (rc != 0) {
-		printk(KERN_ERR "nvram_init: failed to register device\n");
-		return rc;
-	}
-  	
-  	return rc;
+static ssize_t ppc_nvram_get_size(void)
+{
+	if (ppc_md.nvram_size)
+		return ppc_md.nvram_size();
+	return -ENODEV;
 }
 
-static void __exit nvram_cleanup(void)
+static long ppc_nvram_sync(void)
 {
-        misc_deregister( &nvram_dev );
+	return -EINVAL;
 }
 
-module_init(nvram_init);
-module_exit(nvram_cleanup);
-MODULE_LICENSE("GPL");
+const struct nvram_ops arch_nvram_ops = {
+	.read           = ppc_nvram_read,
+	.write          = ppc_nvram_write,
+	.get_size       = ppc_nvram_get_size,
+	.sync           = ppc_nvram_sync,
+};
+EXPORT_SYMBOL(arch_nvram_ops);
+
+#endif /* CONFIG_NVRAM */
Index: linux/arch/powerpc/platforms/powermac/Makefile
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/Makefile	2015-05-31 11:00:59.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/Makefile	2015-05-31 11:01:25.000000000 +1000
@@ -9,11 +9,6 @@ obj-y				+= pic.o setup.o time.o feature
 				   sleep.o low_i2c.o cache.o pfunc_core.o \
 				   pfunc_base.o udbg_scc.o udbg_adb.o
 obj-$(CONFIG_PMAC_BACKLIGHT)	+= backlight.o
-# CONFIG_NVRAM is an arch. independent tristate symbol, for pmac32 we really
-# need this to be a bool.  Cheat here and pretend CONFIG_NVRAM=m is really
-# CONFIG_NVRAM=y
 obj-$(CONFIG_NVRAM:m=y)		+= nvram.o
-# ppc64 pmac doesn't define CONFIG_NVRAM but needs nvram stuff
-obj-$(CONFIG_PPC64)		+= nvram.o
 obj-$(CONFIG_PPC32)		+= bootx_init.o
 obj-$(CONFIG_SMP)		+= smp.o
Index: linux/arch/powerpc/platforms/powermac/setup.c
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/setup.c	2015-05-31 11:01:22.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/setup.c	2015-05-31 11:01:25.000000000 +1000
@@ -321,7 +321,7 @@ static void __init pmac_setup_arch(void)
 	find_via_pmu();
 	smu_init();
 
-#if IS_ENABLED(CONFIG_NVRAM) || defined(CONFIG_PPC64)
+#if IS_ENABLED(CONFIG_NVRAM)
 	pmac_nvram_init();
 #endif
 
Index: linux/drivers/char/nvram.c
===================================================================
--- linux.orig/drivers/char/nvram.c	2015-05-31 11:01:21.000000000 +1000
+++ linux/drivers/char/nvram.c	2015-05-31 11:01:25.000000000 +1000
@@ -342,6 +342,8 @@ static long nvram_misc_ioctl(struct file
 		if (part < pmac_nvram_OF || part > pmac_nvram_NR)
 			return -EINVAL;
 		offset = pmac_get_partition(part);
+		if (offset < 0)
+			return offset;
 		if (copy_to_user((void __user *)arg,
 		                 &offset, sizeof(offset)) != 0)
 			return -EFAULT;
Index: linux/arch/powerpc/platforms/powermac/nvram.c
===================================================================
--- linux.orig/arch/powerpc/platforms/powermac/nvram.c	2015-05-31 11:01:15.000000000 +1000
+++ linux/arch/powerpc/platforms/powermac/nvram.c	2015-05-31 11:01:25.000000000 +1000
@@ -629,6 +629,8 @@ bail:
 
 int pmac_get_partition(int partition)
 {
+	if (nvram_partitions[partition] < 0)
+		return -ENOENT;
 	return nvram_partitions[partition];
 }
 

  parent reply	other threads:[~2015-05-31  1:17 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-31  1:01 [RFC 00/24] Re-use nvram module Finn Thain
2015-05-31  1:01 ` Finn Thain
2015-05-31  1:01 ` [RFC 01/24] macintosh/nvram: Remove as unused Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-06-01  7:40   ` Geert Uytterhoeven
2015-05-31  1:01 ` [RFC 02/24] scsi/atari_scsi: Dont select CONFIG_NVRAM Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 03/24] m68k/atari: Move Atari-specific code out of drivers/char/nvram.c Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 04/24] m68k/atari: Replace nvram_{read,write}_byte with arch_nvram_ops Finn Thain
2015-05-31  1:01   ` [RFC 04/24] m68k/atari: Replace nvram_{read, write}_byte " Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 05/24] char/nvram: Re-order functions to remove forward declarations and #ifdefs Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 06/24] char/nvram: Adopt arch_nvram_ops Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 07/24] x86/thinkpad_acpi: Use arch_nvram_ops methods instead of nvram_read_byte() and nvram_write_byte() Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  4:11   ` Henrique de Moraes Holschuh
2015-05-31  6:15     ` Finn Thain
2015-05-31 14:34       ` Henrique de Moraes Holschuh
2015-06-02 10:09         ` Henrique de Moraes Holschuh
2015-06-02 10:09           ` Henrique de Moraes Holschuh
2015-06-03  3:34           ` Darren Hart
2015-06-03  7:38             ` Finn Thain
2015-06-03 10:37             ` Henrique de Moraes Holschuh
2015-06-04  5:14               ` Darren Hart
2015-05-31  1:01 ` [RFC 08/24] char/nvram: Allow the set_checksum and initialize ioctls to be omitted Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 09/24] char/nvram: Implement NVRAM read/write methods Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 10/24] char/nvram: Use generic fixed_size_llseek() Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 11/24] m68k/atari: Implement arch_nvram_ops methods and enable CONFIG_HAVE_ARCH_NVRAM_OPS Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 12/24] char/nvram: Add "devname:nvram" module alias Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 13/24] powerpc: Cleanup nvram includes Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 14/24] powerpc: Add missing ppc_md.nvram_size for CHRP and PowerMac Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 15/24] powerpc: Implement arch_nvram_ops.get_size() and remove old nvram_* exports Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 16/24] powerpc: Implement nvram sync ioctl Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 17/24] powerpc, fbdev: Use arch_nvram_ops methods instead of nvram_read_byte() and nvram_write_byte() Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` [RFC 17/24] powerpc, fbdev: Use arch_nvram_ops methods instead of nvram_read_byte() and nvram_write_ Finn Thain
2015-05-31  1:01   ` [RFC 17/24] powerpc, fbdev: Use arch_nvram_ops methods instead of nvram_read_byte() and nvram_write_byte() Finn Thain
2015-05-31  1:01 ` [RFC 18/24] nvram: Drop nvram_* symbol exports and prototypes Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 19/24] powerpc: Remove CONFIG_GENERIC_NVRAM and adopt CONFIG_HAVE_ARCH_NVRAM_OPS Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 20/24] char/generic_nvram: Remove as unused Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` Finn Thain [this message]
2015-05-31  1:01   ` [RFC 21/24] powerpc: Adopt nvram module for PPC64 Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 22/24] m68k/mac: Adopt nvram module Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 23/24] m68k/mac: Fix PRAM accessors Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01 ` [RFC 24/24] m68k: Dispatch nvram_ops calls to Atari or Mac functions Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-05-31  1:01   ` Finn Thain
2015-06-01  8:31   ` Geert Uytterhoeven
2015-06-02  7:21     ` Finn Thain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150531010137.569476861@telegraphics.com.au \
    --to=fthain@telegraphics.com.au \
    --cc=arnd@arndb.de \
    --cc=benh@kernel.crashing.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-m68k@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    --cc=paulus@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.