From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from silver.osuosl.org (silver.osuosl.org [140.211.166.136]) by ash.osuosl.org (Postfix) with ESMTP id 79EFF1C226A for ; Mon, 2 Mar 2015 12:26:09 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 742CE2491D for ; Mon, 2 Mar 2015 12:26:09 +0000 (UTC) Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ks4lutwAQea5 for ; Mon, 2 Mar 2015 12:26:07 +0000 (UTC) Received: from aserp1040.oracle.com (aserp1040.oracle.com [141.146.126.69]) by silver.osuosl.org (Postfix) with ESMTPS id C9AA526B2E for ; Mon, 2 Mar 2015 12:26:07 +0000 (UTC) Date: Mon, 2 Mar 2015 15:25:57 +0300 From: Dan Carpenter Subject: Re: [RFC 1/7] staging: fbtft: add lcd register abstraction Message-ID: <20150302122557.GI5386@mwanda> References: <1425293669-15754-1-git-send-email-noralf@tronnes.org> <1425293669-15754-2-git-send-email-noralf@tronnes.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1425293669-15754-2-git-send-email-noralf@tronnes.org> List-Id: Linux Driver Project Developer List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: driverdev-devel-bounces@linuxdriverproject.org Sender: "devel" To: Noralf =?iso-8859-1?Q?Tr=F8nnes?= Cc: gregkh@linuxfoundation.org, driverdev-devel@linuxdriverproject.org On Mon, Mar 02, 2015 at 11:54:23AM +0100, Noralf Tr=F8nnes wrote: > diff --git a/drivers/staging/fbtft/lcdreg/lcdreg-debugfs.c b/drivers/stag= ing/fbtft/lcdreg/lcdreg-debugfs.c > new file mode 100644 > index 0000000..1cba4c2 > --- /dev/null > +++ b/drivers/staging/fbtft/lcdreg/lcdreg-debugfs.c > @@ -0,0 +1,272 @@ > +/* > + * Copyright (C) 2015 Noralf Tr=F8nnes > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License as published by > + * the Free Software Foundation; either version 2 of the License, or > + * (at your option) any later version. > + */ > + > +#include > +#include > +#include > +#include > + > +#include "lcdreg.h" > + > +static struct dentry *lcdreg_debugfs_root; > + > +static int lcdreg_userbuf_to_u32(const char __user *user_buf, size_t cou= nt, > + u32 *dest, size_t dest_size) > +{ > + char *buf, *start; > + unsigned long value; > + int ret =3D 0; > + int i; > + > + buf =3D kmalloc(count, GFP_KERNEL); > + if (!buf) > + return -ENOMEM; > + > + if (copy_from_user(buf, user_buf, count)) { > + kfree(buf); > + return -EFAULT; > + } > + buf[count] =3D 0; Off-by-one overflow. > + for (i =3D 0; i < count; i++) > + if (buf[i] =3D=3D ' ' || buf[i] =3D=3D '\n') > + buf[i] =3D 0; > + i =3D 0; > + start =3D buf; > + while (start < buf + count) { > + if (*start =3D=3D 0) { Use '\0' instead of 0. if (*start =3D=3D '\0') > + start++; > + continue; > + } > + if (i =3D=3D dest_size) { > + ret =3D -EFBIG; > + break; > + } > + if (kstrtoul(start, 16, &value)) { > + ret =3D -EINVAL; > + break; > + } Consider changing this to: ret =3D kstrtou32(start, 16, value); if (ret) break; > + dest[i++] =3D value; > + while (*start !=3D 0) > + start++; This while loop is not needed because of the if statement earlier. > + }; > + kfree(buf); > + if (ret) > + return ret; > + > + return i ? i : -EINVAL; > +} > + > +static ssize_t lcdreg_debugfs_write_file(struct file *file, > + const char __user *user_buf, > + size_t count, loff_t *ppos) > +{ > + struct lcdreg *reg =3D file->private_data; > + int ret; > + u32 txbuf[128]; > + > + ret =3D lcdreg_userbuf_to_u32(user_buf, count, txbuf, ARRAY_SIZE(txbuf)= ); > + if (ret <=3D 0) > + return -EINVAL; This function can't return zero so preserve the error code. if (ret < 0) return ret; > + > + add_taint(TAINT_USER, LOCKDEP_STILL_OK); > + > + lcdreg_lock(reg); > + ret =3D lcdreg_write_buf32(reg, txbuf[0], txbuf + 1, ret - 1); > + lcdreg_unlock(reg); > + > + return ret ? ret : count; > +} > + > +static const struct file_operations lcdreg_debugfs_write_fops =3D { > + .open =3D simple_open, > + .write =3D lcdreg_debugfs_write_file, > + .llseek =3D default_llseek, > +}; > + > +static ssize_t lcdreg_debugfs_read_wr(struct file *file, > + const char __user *user_buf, > + size_t count, loff_t *ppos) > +{ > + struct lcdreg *reg =3D file->private_data; > + struct lcdreg_transfer tr =3D { > + .index =3D (reg->quirks & LCDREG_INDEX0_ON_READ) ? 0 : 1, > + .width =3D reg->debugfs_read_width, > + .count =3D 1, > + }; > + u32 txbuf[1]; > + char *buf =3D NULL; > + int ret; > + > + ret =3D lcdreg_userbuf_to_u32(user_buf, count, txbuf, ARRAY_SIZE(txbuf)= ); > + if (ret !=3D 1) > + return -EINVAL; > + > + tr.buf =3D kmalloc(lcdreg_bytes_per_word(tr.width), GFP_KERNEL); > + if (!tr.buf) > + return -ENOMEM; > + > + add_taint(TAINT_USER, LOCKDEP_STILL_OK); > + > + lcdreg_lock(reg); > + ret =3D lcdreg_read(reg, txbuf[0], &tr); > + lcdreg_unlock(reg); > + if (ret) > + goto error_out; > + > + if (!reg->debugfs_read_result) { > + reg->debugfs_read_result =3D kmalloc(16, GFP_KERNEL); > + if (!reg->debugfs_read_result) { > + ret =3D -ENOMEM; > + goto error_out; > + } > + } Allocating here is strange. We only free ->debugfs_read_result on error so it's sort of buggy as well. Also is it racy if we have multiple readers and writers at once? > + > + buf =3D reg->debugfs_read_result; > + > + switch (tr.width) { > + case 8: > + snprintf(buf, 16, "%02x\n", *(u8 *)tr.buf); > + break; > + case 16: > + snprintf(buf, 16, "%04x\n", *(u16 *)tr.buf); > + break; > + case 24: > + case 32: > + snprintf(buf, 16, "%08x\n", *(u32 *)tr.buf); > + break; > + default: > + ret =3D -EINVAL; > + goto error_out; > + } > + > +error_out: > + kfree(tr.buf); > + if (ret) { > + kfree(reg->debugfs_read_result); > + reg->debugfs_read_result =3D NULL; > + return ret; > + } > + > + return count; It's often cleaner to separate error paths from the success paths. This is almost an excption because we free (tr.buf) on both paths. But it would like like this, if you decide to go that way: kfree(tr.buf); return 0; err_free: kfree(reg->debugfs_read_result); reg->debugfs_read_result =3D NULL; kfree(tr.buf); return ret; > +static inline void lcdreg_lock(struct lcdreg *reg) > +{ > + mutex_lock(®->lock); > +} > + > +static inline void lcdreg_unlock(struct lcdreg *reg) > +{ > + mutex_unlock(®->lock); > +} Don't add abstraction around locking. Everyone hates that. Currently we don't have any static analysis tools which do cross function locking analysis correctly. (I am partly to blame for that, sorry). But also we don't want to have to look it up to see if it a mutex or a spinlock. > +#if defined(CONFIG_DYNAMIC_DEBUG) > + > +#define lcdreg_dbg_transfer_buf(transfer) \ > +do { \ > + int groupsize =3D lcdreg_bytes_per_word(transfer->width); \ > + size_t len =3D min_t(size_t, 32, transfer->count * groupsize); \ > + \ > + print_hex_dump_debug(" buf=3D", DUMP_PREFIX_NONE, 32, \ > + groupsize, transfer->buf, len, false); \ > +} while (0) > + > +#elif defined(DEBUG) > + > +#define lcdreg_dbg_transfer_buf(transfer) \ > +do { \ > + int groupsize =3D lcdreg_bytes_per_word(transfer->width); \ > + size_t len =3D min_t(size_t, 32, transfer->count * groupsize); \ > + \ > + print_hex_dump(KERN_DEBUG, " buf=3D", DUMP_PREFIX_NONE, 32, \ > + groupsize, transfer->buf, len, false); \ > +} while (0) I don't understand this. Why can't we use print_hex_dump_debug() for both? In other words: #if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) ... #else Also can we make it an inline function? regards, dan carpenter _______________________________________________ devel mailing list devel@linuxdriverproject.org http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel