Skip to content

Commit ee74baa

Browse files
author
David S. Miller
committed
[PKTGEN]: Convert to kthread API.
Based upon a suggestion from Christoph Hellwig. This fixes various races in module load/unload handling too. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3136dcb commit ee74baa

1 file changed

Lines changed: 48 additions & 108 deletions

File tree

‎net/core/pktgen.c‎

Lines changed: 48 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
#include <linux/seq_file.h>
149149
#include <linux/wait.h>
150150
#include <linux/etherdevice.h>
151+
#include <linux/kthread.h>
151152
#include <net/checksum.h>
152153
#include <net/ipv6.h>
153154
#include <net/addrconf.h>
@@ -360,8 +361,7 @@ struct pktgen_thread {
360361
spinlock_t if_lock;
361362
struct list_head if_list; /* All device here */
362363
struct list_head th_list;
363-
int removed;
364-
char name[32];
364+
struct task_struct *tsk;
365365
char result[512];
366366
u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
367367

@@ -1689,7 +1689,7 @@ static int pktgen_thread_show(struct seq_file *seq, void *v)
16891689
BUG_ON(!t);
16901690

16911691
seq_printf(seq, "Name: %s max_before_softirq: %d\n",
1692-
t->name, t->max_before_softirq);
1692+
t->tsk->comm, t->max_before_softirq);
16931693

16941694
seq_printf(seq, "Running: ");
16951695

@@ -3112,7 +3112,7 @@ static void pktgen_rem_thread(struct pktgen_thread *t)
31123112
{
31133113
/* Remove from the thread list */
31143114

3115-
remove_proc_entry(t->name, pg_proc_dir);
3115+
remove_proc_entry(t->tsk->comm, pg_proc_dir);
31163116

31173117
mutex_lock(&pktgen_thread_lock);
31183118

@@ -3260,58 +3260,40 @@ out:;
32603260
* Main loop of the thread goes here
32613261
*/
32623262

3263-
static void pktgen_thread_worker(struct pktgen_thread *t)
3263+
static int pktgen_thread_worker(void *arg)
32643264
{
32653265
DEFINE_WAIT(wait);
3266+
struct pktgen_thread *t = arg;
32663267
struct pktgen_dev *pkt_dev = NULL;
32673268
int cpu = t->cpu;
3268-
sigset_t tmpsig;
32693269
u32 max_before_softirq;
32703270
u32 tx_since_softirq = 0;
32713271

3272-
daemonize("pktgen/%d", cpu);
3273-
3274-
/* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
3275-
3276-
spin_lock_irq(&current->sighand->siglock);
3277-
tmpsig = current->blocked;
3278-
siginitsetinv(&current->blocked,
3279-
sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
3280-
3281-
recalc_sigpending();
3282-
spin_unlock_irq(&current->sighand->siglock);
3283-
3284-
/* Migrate to the right CPU */
3285-
set_cpus_allowed(current, cpumask_of_cpu(cpu));
3286-
if (smp_processor_id() != cpu)
3287-
BUG();
3272+
BUG_ON(smp_processor_id() != cpu);
32883273

32893274
init_waitqueue_head(&t->queue);
32903275

3291-
t->control &= ~(T_TERMINATE);
3292-
t->control &= ~(T_RUN);
3293-
t->control &= ~(T_STOP);
3294-
t->control &= ~(T_REMDEVALL);
3295-
t->control &= ~(T_REMDEV);
3296-
32973276
t->pid = current->pid;
32983277

32993278
PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
33003279

33013280
max_before_softirq = t->max_before_softirq;
33023281

3303-
__set_current_state(TASK_INTERRUPTIBLE);
3304-
mb();
3282+
set_current_state(TASK_INTERRUPTIBLE);
33053283

3306-
while (1) {
3307-
3308-
__set_current_state(TASK_RUNNING);
3284+
while (!kthread_should_stop()) {
3285+
pkt_dev = next_to_run(t);
33093286

3310-
/*
3311-
* Get next dev to xmit -- if any.
3312-
*/
3287+
if (!pkt_dev &&
3288+
(t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3289+
== 0) {
3290+
prepare_to_wait(&(t->queue), &wait,
3291+
TASK_INTERRUPTIBLE);
3292+
schedule_timeout(HZ / 10);
3293+
finish_wait(&(t->queue), &wait);
3294+
}
33133295

3314-
pkt_dev = next_to_run(t);
3296+
__set_current_state(TASK_RUNNING);
33153297

33163298
if (pkt_dev) {
33173299

@@ -3329,21 +3311,8 @@ static void pktgen_thread_worker(struct pktgen_thread *t)
33293311
do_softirq();
33303312
tx_since_softirq = 0;
33313313
}
3332-
} else {
3333-
prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
3334-
schedule_timeout(HZ / 10);
3335-
finish_wait(&(t->queue), &wait);
33363314
}
33373315

3338-
/*
3339-
* Back from sleep, either due to the timeout or signal.
3340-
* We check if we have any "posted" work for us.
3341-
*/
3342-
3343-
if (t->control & T_TERMINATE || signal_pending(current))
3344-
/* we received a request to terminate ourself */
3345-
break;
3346-
33473316
if (t->control & T_STOP) {
33483317
pktgen_stop(t);
33493318
t->control &= ~(T_STOP);
@@ -3364,20 +3333,19 @@ static void pktgen_thread_worker(struct pktgen_thread *t)
33643333
t->control &= ~(T_REMDEV);
33653334
}
33663335

3367-
if (need_resched())
3368-
schedule();
3336+
set_current_state(TASK_INTERRUPTIBLE);
33693337
}
33703338

3371-
PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
3339+
PG_DEBUG(printk("pktgen: %s stopping all device\n", t->tsk->comm));
33723340
pktgen_stop(t);
33733341

3374-
PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
3342+
PG_DEBUG(printk("pktgen: %s removing all device\n", t->tsk->comm));
33753343
pktgen_rem_all_ifs(t);
33763344

3377-
PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
3345+
PG_DEBUG(printk("pktgen: %s removing thread.\n", t->tsk->comm));
33783346
pktgen_rem_thread(t);
33793347

3380-
t->removed = 1;
3348+
return 0;
33813349
}
33823350

33833351
static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
@@ -3495,74 +3463,49 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
34953463
return add_dev_to_thread(t, pkt_dev);
34963464
}
34973465

3498-
static struct pktgen_thread *__init pktgen_find_thread(const char *name)
3466+
static int __init pktgen_create_thread(int cpu)
34993467
{
35003468
struct pktgen_thread *t;
3501-
3502-
mutex_lock(&pktgen_thread_lock);
3503-
3504-
list_for_each_entry(t, &pktgen_threads, th_list)
3505-
if (strcmp(t->name, name) == 0) {
3506-
mutex_unlock(&pktgen_thread_lock);
3507-
return t;
3508-
}
3509-
3510-
mutex_unlock(&pktgen_thread_lock);
3511-
return NULL;
3512-
}
3513-
3514-
static int __init pktgen_create_thread(const char *name, int cpu)
3515-
{
3516-
int err;
3517-
struct pktgen_thread *t = NULL;
35183469
struct proc_dir_entry *pe;
3519-
3520-
if (strlen(name) > 31) {
3521-
printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
3522-
return -EINVAL;
3523-
}
3524-
3525-
if (pktgen_find_thread(name)) {
3526-
printk("pktgen: ERROR: thread: %s already exists\n", name);
3527-
return -EINVAL;
3528-
}
3470+
struct task_struct *p;
35293471

35303472
t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
35313473
if (!t) {
35323474
printk("pktgen: ERROR: out of memory, can't create new thread.\n");
35333475
return -ENOMEM;
35343476
}
35353477

3536-
strcpy(t->name, name);
35373478
spin_lock_init(&t->if_lock);
35383479
t->cpu = cpu;
35393480

3540-
pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3481+
INIT_LIST_HEAD(&t->if_list);
3482+
3483+
list_add_tail(&t->th_list, &pktgen_threads);
3484+
3485+
p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3486+
if (IS_ERR(p)) {
3487+
printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3488+
list_del(&t->th_list);
3489+
kfree(t);
3490+
return PTR_ERR(p);
3491+
}
3492+
kthread_bind(p, cpu);
3493+
t->tsk = p;
3494+
3495+
pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
35413496
if (!pe) {
35423497
printk("pktgen: cannot create %s/%s procfs entry.\n",
3543-
PG_PROC_DIR, t->name);
3498+
PG_PROC_DIR, t->tsk->comm);
3499+
kthread_stop(p);
3500+
list_del(&t->th_list);
35443501
kfree(t);
35453502
return -EINVAL;
35463503
}
35473504

35483505
pe->proc_fops = &pktgen_thread_fops;
35493506
pe->data = t;
35503507

3551-
INIT_LIST_HEAD(&t->if_list);
3552-
3553-
list_add_tail(&t->th_list, &pktgen_threads);
3554-
3555-
t->removed = 0;
3556-
3557-
err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
3558-
CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
3559-
if (err < 0) {
3560-
printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3561-
remove_proc_entry(t->name, pg_proc_dir);
3562-
list_del(&t->th_list);
3563-
kfree(t);
3564-
return err;
3565-
}
3508+
wake_up_process(p);
35663509

35673510
return 0;
35683511
}
@@ -3643,10 +3586,8 @@ static int __init pg_init(void)
36433586

36443587
for_each_online_cpu(cpu) {
36453588
int err;
3646-
char buf[30];
36473589

3648-
sprintf(buf, "kpktgend_%i", cpu);
3649-
err = pktgen_create_thread(buf, cpu);
3590+
err = pktgen_create_thread(cpu);
36503591
if (err)
36513592
printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
36523593
cpu, err);
@@ -3674,9 +3615,8 @@ static void __exit pg_cleanup(void)
36743615

36753616
list_for_each_safe(q, n, &pktgen_threads) {
36763617
t = list_entry(q, struct pktgen_thread, th_list);
3677-
t->control |= (T_TERMINATE);
3678-
3679-
wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
3618+
kthread_stop(t->tsk);
3619+
kfree(t);
36803620
}
36813621

36823622
/* Un-register us from receiving netdevice events */

0 commit comments

Comments
 (0)