Re: [PATCH 1/3] devmem: remove redundant test on len

From: Wu Fengguang
Date: Sat Sep 12 2009 - 10:33:16 EST


On Sat, Sep 12, 2009 at 08:00:47AM +0800, Andrew Morton wrote:
> On Fri, 11 Sep 2009 10:23:34 +0800
> Wu Fengguang <fengguang.wu@xxxxxxxxx> wrote:
>
> > The len test in write_kmem() is always true, so can be reduced.
> >
> > CC: Marcelo Tosatti <mtosatti@xxxxxxxxxx>
> > CC: Greg Kroah-Hartman <gregkh@xxxxxxx>
> > CC: Mark Brown <broonie@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
> > CC: Johannes Berg <johannes@xxxxxxxxxxxxxxxx>
> > CC: Avi Kivity <avi@xxxxxxxxxxxx>
> > Signed-off-by: Wu Fengguang <fengguang.wu@xxxxxxxxx>
> > ---
> > drivers/char/mem.c | 14 ++++++--------
> > 1 file changed, 6 insertions(+), 8 deletions(-)
> >
> > --- linux.orig/drivers/char/mem.c
> > +++ linux/drivers/char/mem.c
> > @@ -580,18 +580,16 @@ static ssize_t write_kmem(struct file *
> > while (count > 0) {
> > int len = count;
> >
> > if (len > PAGE_SIZE)
> > len = PAGE_SIZE;
> > - if (len) {
> > - written = copy_from_user(kbuf, buf, len);
> > - if (written) {
> > - if (wrote + virtr)
> > - break;
> > - free_page((unsigned long)kbuf);
> > - return -EFAULT;
> > - }
> > + written = copy_from_user(kbuf, buf, len);
> > + if (written) {
> > + if (wrote + virtr)
> > + break;
> > + free_page((unsigned long)kbuf);
> > + return -EFAULT;
> > }
> > len = vwrite(kbuf, (char *)p, len);
> > count -= len;
> > buf += len;
> > virtr += len;
>
> humpf. But take a closer look at what remains.

Yeah, it asks for more cleanup patches..

>
> Local var `written' is unneeded here.
>
>
> Which makes us look at what `written' _does_ do:
>
> if (written != wrote)
> return written;
> wrote = written;
>
> lolwhowrotethat?
>
> local var `written' can at least be made local to the first loop.

Right, I'll make this straight.

> write_kmem() has a lot of typecasts which indicates that the choices of
> types were inappropriate.

It seems hard to improve because of a fundamental issue: the same
value is taken as both virtual and physical address, and also be
compared and calculated as numbers..

Thanks,
Fengguang

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/