Return-Path: Received: from fldsmtpe03.verizon.com ([140.108.26.142]:41810 "EHLO fldsmtpe03.verizon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751544AbdITEqw (ORCPT ); Wed, 20 Sep 2017 00:46:52 -0400 From: "Levin, Alexander (Sasha Levin)" Cc: Arvind Yadav , Helge Deller , "Levin, Alexander (Sasha Levin)" To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" Subject: [PATCH review for 3.18 26/30] parisc: perf: Fix potential NULL pointer dereference Date: Wed, 20 Sep 2017 04:45:56 +0000 Message-ID: <20170920044542.7571-26-alexander.levin@verizon.com> References: <20170920044542.7571-1-alexander.levin@verizon.com> In-Reply-To: <20170920044542.7571-1-alexander.levin@verizon.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Arvind Yadav [ Upstream commit 74e3f6e63da6c8e8246fba1689e040bc926b4a1a ] Fix potential NULL pointer dereference and clean up coding style errors (code indent, trailing whitespaces). Signed-off-by: Arvind Yadav Signed-off-by: Helge Deller Signed-off-by: Sasha Levin --- arch/parisc/kernel/perf.c | 94 ++++++++++++++++++++++++-------------------= ---- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/arch/parisc/kernel/perf.c b/arch/parisc/kernel/perf.c index ba0c053e25ae..2a2fff407ac4 100644 --- a/arch/parisc/kernel/perf.c +++ b/arch/parisc/kernel/perf.c @@ -39,7 +39,7 @@ * the PDC INTRIGUE calls. This is done to eliminate bugs introduced * in various PDC revisions. The code is much more maintainable * and reliable this way vs having to debug on every version of PDC - * on every box.=20 + * on every box. */ =20 #include @@ -195,8 +195,8 @@ static int perf_config(uint32_t *image_ptr); static int perf_release(struct inode *inode, struct file *file); static int perf_open(struct inode *inode, struct file *file); static ssize_t perf_read(struct file *file, char __user *buf, size_t cnt, = loff_t *ppos); -static ssize_t perf_write(struct file *file, const char __user *buf, size_= t count,=20 - loff_t *ppos); +static ssize_t perf_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos); static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long = arg); static void perf_start_counters(void); static int perf_stop_counters(uint32_t *raddr); @@ -222,7 +222,7 @@ extern void perf_intrigue_disable_perf_counters (void); /* * configure: * - * Configure the cpu with a given data image. First turn off the counters= ,=20 + * Configure the cpu with a given data image. First turn off the counters= , * then download the image, then turn the counters back on. */ static int perf_config(uint32_t *image_ptr) @@ -234,7 +234,7 @@ static int perf_config(uint32_t *image_ptr) error =3D perf_stop_counters(raddr); if (error !=3D 0) { printk("perf_config: perf_stop_counters =3D %ld\n", error); - return -EINVAL;=20 + return -EINVAL; } =20 printk("Preparing to write image\n"); @@ -242,7 +242,7 @@ printk("Preparing to write image\n"); error =3D perf_write_image((uint64_t *)image_ptr); if (error !=3D 0) { printk("perf_config: DOWNLOAD =3D %ld\n", error); - return -EINVAL;=20 + return -EINVAL; } =20 printk("Preparing to start counters\n"); @@ -254,7 +254,7 @@ printk("Preparing to start counters\n"); } =20 /* - * Open the device and initialize all of its memory. The device is only=20 + * Open the device and initialize all of its memory. The device is only * opened once, but can be "queried" by multiple processes that know its * file descriptor. */ @@ -298,8 +298,8 @@ static ssize_t perf_read(struct file *file, char __user= *buf, size_t cnt, loff_t * called on the processor that the download should happen * on. */ -static ssize_t perf_write(struct file *file, const char __user *buf, size_= t count,=20 - loff_t *ppos) +static ssize_t perf_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) { int err; size_t image_size; @@ -307,11 +307,11 @@ static ssize_t perf_write(struct file *file, const ch= ar __user *buf, size_t coun uint32_t interface_type; uint32_t test; =20 - if (perf_processor_interface =3D=3D ONYX_INTF)=20 + if (perf_processor_interface =3D=3D ONYX_INTF) image_size =3D PCXU_IMAGE_SIZE; - else if (perf_processor_interface =3D=3D CUDA_INTF)=20 + else if (perf_processor_interface =3D=3D CUDA_INTF) image_size =3D PCXW_IMAGE_SIZE; - else=20 + else return -EFAULT; =20 if (!capable(CAP_SYS_ADMIN)) @@ -331,22 +331,22 @@ static ssize_t perf_write(struct file *file, const ch= ar __user *buf, size_t coun =20 /* First check the machine type is correct for the requested image */ - if (((perf_processor_interface =3D=3D CUDA_INTF) && - (interface_type !=3D CUDA_INTF)) || - ((perf_processor_interface =3D=3D ONYX_INTF) && - (interface_type !=3D ONYX_INTF)))=20 + if (((perf_processor_interface =3D=3D CUDA_INTF) && + (interface_type !=3D CUDA_INTF)) || + ((perf_processor_interface =3D=3D ONYX_INTF) && + (interface_type !=3D ONYX_INTF))) return -EINVAL; =20 /* Next check to make sure the requested image is valid */ - if (((interface_type =3D=3D CUDA_INTF) &&=20 + if (((interface_type =3D=3D CUDA_INTF) && (test >=3D MAX_CUDA_IMAGES)) || - ((interface_type =3D=3D ONYX_INTF) &&=20 - (test >=3D MAX_ONYX_IMAGES)))=20 + ((interface_type =3D=3D ONYX_INTF) && + (test >=3D MAX_ONYX_IMAGES))) return -EINVAL; =20 /* Copy the image into the processor */ - if (interface_type =3D=3D CUDA_INTF)=20 + if (interface_type =3D=3D CUDA_INTF) return perf_config(cuda_images[test]); else return perf_config(onyx_images[test]); @@ -360,7 +360,7 @@ static ssize_t perf_write(struct file *file, const char= __user *buf, size_t coun static void perf_patch_images(void) { #if 0 /* FIXME!! */ -/*=20 +/* * NOTE: this routine is VERY specific to the current TLB image. * If the image is changed, this routine might also need to be changed. */ @@ -368,9 +368,9 @@ static void perf_patch_images(void) extern void $i_dtlb_miss_2_0(); extern void PA2_0_iva(); =20 - /*=20 + /* * We can only use the lower 32-bits, the upper 32-bits should be 0 - * anyway given this is in the kernel=20 + * anyway given this is in the kernel */ uint32_t itlb_addr =3D (uint32_t)&($i_itlb_miss_2_0); uint32_t dtlb_addr =3D (uint32_t)&($i_dtlb_miss_2_0); @@ -378,21 +378,21 @@ static void perf_patch_images(void) =20 if (perf_processor_interface =3D=3D ONYX_INTF) { /* clear last 2 bytes */ - onyx_images[TLBMISS][15] &=3D 0xffffff00; =20 + onyx_images[TLBMISS][15] &=3D 0xffffff00; /* set 2 bytes */ onyx_images[TLBMISS][15] |=3D (0x000000ff&((dtlb_addr) >> 24)); onyx_images[TLBMISS][16] =3D (dtlb_addr << 8)&0xffffff00; onyx_images[TLBMISS][17] =3D itlb_addr; =20 /* clear last 2 bytes */ - onyx_images[TLBHANDMISS][15] &=3D 0xffffff00; =20 + onyx_images[TLBHANDMISS][15] &=3D 0xffffff00; /* set 2 bytes */ onyx_images[TLBHANDMISS][15] |=3D (0x000000ff&((dtlb_addr) >> 24)); onyx_images[TLBHANDMISS][16] =3D (dtlb_addr << 8)&0xffffff00; onyx_images[TLBHANDMISS][17] =3D itlb_addr; =20 /* clear last 2 bytes */ - onyx_images[BIG_CPI][15] &=3D 0xffffff00; =20 + onyx_images[BIG_CPI][15] &=3D 0xffffff00; /* set 2 bytes */ onyx_images[BIG_CPI][15] |=3D (0x000000ff&((dtlb_addr) >> 24)); onyx_images[BIG_CPI][16] =3D (dtlb_addr << 8)&0xffffff00; @@ -405,24 +405,24 @@ static void perf_patch_images(void) =20 } else if (perf_processor_interface =3D=3D CUDA_INTF) { /* Cuda interface */ - cuda_images[TLBMISS][16] =3D =20 + cuda_images[TLBMISS][16] =3D (cuda_images[TLBMISS][16]&0xffff0000) | ((dtlb_addr >> 8)&0x0000ffff); - cuda_images[TLBMISS][17] =3D=20 + cuda_images[TLBMISS][17] =3D ((dtlb_addr << 24)&0xff000000) | ((itlb_addr >> 16)&0x000000ff); cuda_images[TLBMISS][18] =3D (itlb_addr << 16)&0xffff0000; =20 - cuda_images[TLBHANDMISS][16] =3D=20 + cuda_images[TLBHANDMISS][16] =3D (cuda_images[TLBHANDMISS][16]&0xffff0000) | ((dtlb_addr >> 8)&0x0000ffff); - cuda_images[TLBHANDMISS][17] =3D=20 + cuda_images[TLBHANDMISS][17] =3D ((dtlb_addr << 24)&0xff000000) | ((itlb_addr >> 16)&0x000000ff); cuda_images[TLBHANDMISS][18] =3D (itlb_addr << 16)&0xffff0000; =20 - cuda_images[BIG_CPI][16] =3D=20 + cuda_images[BIG_CPI][16] =3D (cuda_images[BIG_CPI][16]&0xffff0000) | ((dtlb_addr >> 8)&0x0000ffff); - cuda_images[BIG_CPI][17] =3D=20 + cuda_images[BIG_CPI][17] =3D ((dtlb_addr << 24)&0xff000000) | ((itlb_addr >> 16)&0x000000ff); cuda_images[BIG_CPI][18] =3D (itlb_addr << 16)&0xffff0000; } else { @@ -434,7 +434,7 @@ static void perf_patch_images(void) =20 /* * ioctl routine - * All routines effect the processor that they are executed on. Thus you= =20 + * All routines effect the processor that they are executed on. Thus you * must be running on the processor that you wish to change. */ =20 @@ -460,7 +460,7 @@ static long perf_ioctl(struct file *file, unsigned int = cmd, unsigned long arg) } =20 /* copy out the Counters */ - if (copy_to_user((void __user *)arg, raddr,=20 + if (copy_to_user((void __user *)arg, raddr, sizeof (raddr)) !=3D 0) { error =3D -EFAULT; break; @@ -488,7 +488,7 @@ static const struct file_operations perf_fops =3D { .open =3D perf_open, .release =3D perf_release }; -=09 + static struct miscdevice perf_dev =3D { MISC_DYNAMIC_MINOR, PA_PERF_DEV, @@ -595,7 +595,7 @@ static int perf_stop_counters(uint32_t *raddr) /* OR sticky2 (bit 1496) to counter2 bit 32 */ tmp64 |=3D (userbuf[23] >> 8) & 0x0000000080000000; raddr[2] =3D (uint32_t)tmp64; - =09 + /* Counter3 is bits 1497 to 1528 */ tmp64 =3D (userbuf[23] >> 7) & 0x00000000ffffffff; /* OR sticky3 (bit 1529) to counter3 bit 32 */ @@ -617,7 +617,7 @@ static int perf_stop_counters(uint32_t *raddr) userbuf[22] =3D 0; userbuf[23] =3D 0; =20 - /*=20 + /* * Write back the zeroed bytes + the image given * the read was destructive. */ @@ -625,13 +625,13 @@ static int perf_stop_counters(uint32_t *raddr) } else { =20 /* - * Read RDR-15 which contains the counters and sticky bits=20 + * Read RDR-15 which contains the counters and sticky bits */ if (!perf_rdr_read_ubuf(15, userbuf)) { return -13; } =20 - /*=20 + /* * Clear out the counters */ perf_rdr_clear(15); @@ -644,7 +644,7 @@ static int perf_stop_counters(uint32_t *raddr) raddr[2] =3D (uint32_t)((userbuf[1] >> 32) & 0x00000000ffffffffUL); raddr[3] =3D (uint32_t)(userbuf[1] & 0x00000000ffffffffUL); } -=20 + return 0; } =20 @@ -682,7 +682,7 @@ static int perf_rdr_read_ubuf(uint32_t rdr_num, uint64_= t *buffer) i =3D tentry->num_words; while (i--) { buffer[i] =3D 0; - }=09 + } =20 /* Check for bits an even number of 64 */ if ((xbits =3D width & 0x03f) !=3D 0) { @@ -808,18 +808,22 @@ static int perf_write_image(uint64_t *memaddr) } =20 runway =3D ioremap_nocache(cpu_device->hpa.start, 4096); + if (!runway) { + pr_err("perf_write_image: ioremap failed!\n"); + return -ENOMEM; + } =20 /* Merge intrigue bits into Runway STATUS 0 */ tmp64 =3D __raw_readq(runway + RUNWAY_STATUS) & 0xffecfffffffffffful; - __raw_writeq(tmp64 | (*memaddr++ & 0x0013000000000000ul),=20 + __raw_writeq(tmp64 | (*memaddr++ & 0x0013000000000000ul), runway + RUNWAY_STATUS); -=09 + /* Write RUNWAY DEBUG registers */ for (i =3D 0; i < 8; i++) { __raw_writeq(*memaddr++, runway + RUNWAY_DEBUG); } =20 - return 0;=20 + return 0; } =20 /* @@ -843,7 +847,7 @@ printk("perf_rdr_write\n"); perf_rdr_shift_out_U(rdr_num, buffer[i]); } else { perf_rdr_shift_out_W(rdr_num, buffer[i]); - }=09 + } } printk("perf_rdr_write done\n"); } --=20 2.11.0