Commit 3439dd86e34580384d3b58cf8d54a9283cd7a342

Authored by Porpoise
Committed by Linus Torvalds
1 parent 626ab0e69d

[PATCH] When CONFIG_BASE_SMALL=1, cascade() may enter an infinite loop

When CONFIG_BASE_SAMLL=1, cascade() in may enter the infinite loop.
Because of CONFIG_BASE_SMALL=1(TVR_BITS=6 and TVN_BITS=4), the list
base->tv5 may cascade into base->tv5.  So, the kernel enters the infinite
loop in the function cascade().

I created a test module to verify this bug, and a patch to fix it.

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/timer.h>
#if 0
#include <linux/kdb.h>
#else
#define kdb_printf printk
#endif

#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
#define TVN_SIZE (1 << TVN_BITS)
#define TVR_SIZE (1 << TVR_BITS)
#define TVN_MASK (TVN_SIZE - 1)
#define TVR_MASK (TVR_SIZE - 1)

#define TV_SIZE(N)  (N*TVN_BITS  + TVR_BITS)

struct timer_list timer0;
struct timer_list dummy_timer1;
struct timer_list dummy_timer2;

void dummy_timer_fun(unsigned long data) {
}
unsigned long j=0;
void check_timer_base(unsigned long data)
{
        kdb_printf("check_timer_base %08x\n",jiffies);
        mod_timer(&timer0,(jiffies & (~0xFFF)) + 0x1FFF);
}

int init_module(void)
{
        init_timer(&timer0);
        timer0.data = (unsigned long)0;
        timer0.function = check_timer_base;
        mod_timer(&timer0,jiffies+1);

        init_timer(&dummy_timer1);
        dummy_timer1.data = (unsigned long)0;
        dummy_timer1.function = dummy_timer_fun;

        init_timer(&dummy_timer2);
        dummy_timer2.data = (unsigned long)0;
        dummy_timer2.function = dummy_timer_fun;

        j=jiffies;
        j&=(~((1<<TV_SIZE(3))-1));
        j+=(1<<TV_SIZE(3));
        j+=(1<<TV_SIZE(4));

        kdb_printf("mod_timer %08x\n",j);

        mod_timer(&dummy_timer1, j );
        mod_timer(&dummy_timer2, j );

        return 0;
}

void cleanup_module()
{
        del_timer_sync(&timer0);
        del_timer_sync(&dummy_timer1);
        del_timer_sync(&dummy_timer2);
}

(Cleanups from Oleg)

[oleg@tv-sign.ru: use list_replace_init()]
Cc: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Matt Mackall <mpm@selenic.com>
Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

Showing 1 changed file with 9 additions and 13 deletions Side-by-side Diff

... ... @@ -383,23 +383,19 @@
383 383 static int cascade(tvec_base_t *base, tvec_t *tv, int index)
384 384 {
385 385 /* cascade all the timers from tv up one level */
386   - struct list_head *head, *curr;
  386 + struct timer_list *timer, *tmp;
  387 + struct list_head tv_list;
387 388  
388   - head = tv->vec + index;
389   - curr = head->next;
  389 + list_replace_init(tv->vec + index, &tv_list);
  390 +
390 391 /*
391   - * We are removing _all_ timers from the list, so we don't have to
392   - * detach them individually, just clear the list afterwards.
  392 + * We are removing _all_ timers from the list, so we
  393 + * don't have to detach them individually.
393 394 */
394   - while (curr != head) {
395   - struct timer_list *tmp;
396   -
397   - tmp = list_entry(curr, struct timer_list, entry);
398   - BUG_ON(tmp->base != base);
399   - curr = curr->next;
400   - internal_add_timer(base, tmp);
  395 + list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
  396 + BUG_ON(timer->base != base);
  397 + internal_add_timer(base, timer);
401 398 }
402   - INIT_LIST_HEAD(head);
403 399  
404 400 return index;
405 401 }