Re: [PATCH] Fix array overflow in CFQ

Vivek Goyal
2010-10-19T12:33:39Z

On Tue, Oct 19, 2010 at 07:49:33AM -0400, Vivek Goyal wrote:
> On Tue, Oct 19, 2010 at 12:01:40PM +0200, Jens Axboe wrote:
> > On 2010-10-19 11:10, Andi Kleen wrote:
> > > From: Andi Kleen <ak@linux.intel.com>
> > >
> > > gcc 4.5 complains when compiling a recent rc with
> > >
> > > linux/block/cfq-iosched.c: In function ‘cfq_dispatch_requests’:
> > > linux/block/cfq-iosched.c:2156:3: warning: array subscript is above array bounds
> > >
> > > and it is right:
> > >
> > > slice = group_slice * count /
> > > max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
> > > cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
> > >
> > > busy_queues_avg can be indexed by this enum
> > >
> > > enum wl_prio_t {
> > > BE_WORKLOAD = 0,
> > > RT_WORKLOAD = 1,
> > > IDLE_WORKLOAD = 2,
> > > };
> > >
> > > in cfqd->serving_prio, but is only declared as
> > >
> > > unsigned int busy_queues_avg[2];
> > >
> > > which is clearly off by one. Fix this here.
> >
> > Indeed, that is definitely buggy. ->service_trees[][] looks buggy, too.
> > WTF?!
>
> Hi Jens,
>
> busy_queues_avg[] definitely looks buggy. Looks like I introduced this bug
> while converting corrado's logic to group logic. I will fix it in a while.
> Sorry for the goof up here.

Jens,

Staring at the code for some more time, it looks like that busy_queues_avg[]
is also not buggy (at least at run time).

We maintain busy_queues_avg() only for RT and BE class. For IDLE class, we
expire the workload immediately after a jiffy.

/* Choose next priority. RT > BE > IDLE */
if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
cfqd->serving_prio = RT_WORKLOAD;
else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
cfqd->serving_prio = BE_WORKLOAD;
else {
cfqd->serving_prio = IDLE_WORKLOAD;
cfqd->workload_expires = jiffies + 1;
return;
}

...
...
...

slice = group_slice * count /
max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd,
cfqg));

So for IDLE class, we return immediately from the function and never
execute cfqg->busy_queues_avg[IDLE].

Now to remove the gcc warning we can increase the size of busy_queues_avg[]
array but third field should always remain unused.

Thanks
Vivek