Re: [PATCH] lowmemorykiller: prevent multiple instances of low memorykiller

From: David Rientjes
Date: Tue Apr 16 2013 - 16:00:30 EST


On Tue, 16 Apr 2013, Oskar Andero wrote:

> > > The comment in shrinker.h is misleading, not the source code.
> > > do_shrinker_shrink() will fail for anything negative and 0.
> >
> > The comment is correct. The only acceptable negative return is -1.
> > Look at the second time do_shrinker_shrink() is called from
> > shrink_slab().
> >
> > 283 while (total_scan >= batch_size) {
> > 284 int nr_before;
> > 285
> > 286 nr_before = do_shrinker_shrink(shrinker, shrink, 0);
> > 287 shrink_ret = do_shrinker_shrink(shrinker, shrink,
> > 288 batch_size);
> > 289 if (shrink_ret == -1)
> > 290 break;
> > 291 if (shrink_ret < nr_before)
> > 292 ret += nr_before - shrink_ret;
> > 293 count_vm_events(SLABS_SCANNED, batch_size);
>
> Yes, the comment is correct with what is implemented in the code, but
> that doesn't mean the code is right. IMHO, relaying on magical numbers is highly
> questionable coding style.
>

No, it's not. This is controlled higher in shrink_slab() by this:

max_pass = do_shrinker_shrink(shrinker, shrink, 0);
if (max_pass <= 0)
continue;

and your patch is implemented incorrectly, i.e. it does not return
LMK_BUSY if the spinlock is contended which needlessly recalls the
shrinker later.

You have a couple of options:

- return -1 when the spinlock is contended immediately when
!sc->nr_to_scan (although it should really be a cmpxchg since a
spinlock isn't needed), or

- protect the for_each_process() loop in lowmem_shrink() with an
actual spinlock that will detect any previously killed process
since it will have the TIF_MEMDIE bit set.
--
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/