Skip to content

Commit 4fdcfa1

Browse files
nhormandavem330
authored andcommitted
drop_monitor: prevent init path from scheduling on the wrong cpu
I just noticed after some recent updates, that the init path for the drop monitor protocol has a minor error. drop monitor maintains a per cpu structure, that gets initalized from a single cpu. Normally this is fine, as the protocol isn't in use yet, but I recently made a change that causes a failed skb allocation to reschedule itself . Given the current code, the implication is that this workqueue reschedule will take place on the wrong cpu. If drop monitor is used early during the boot process, its possible that two cpus will access a single per-cpu structure in parallel, possibly leading to data corruption. This patch fixes the situation, by storing the cpu number that a given instance of this per-cpu data should be accessed from. In the case of a need for a reschedule, the cpu stored in the struct is assigned the rescheule, rather than the currently executing cpu Tested successfully by myself. Signed-off-by: Neil Horman <nhorman@tuxdriver.com> CC: David Miller <davem@davemloft.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a472384 commit 4fdcfa1

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

‎net/core/drop_monitor.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct per_cpu_dm_data {
4949
struct sk_buff __rcu *skb;
5050
atomic_t dm_hit_count;
5151
struct timer_list send_timer;
52+
int cpu;
5253
};
5354

5455
struct dm_hw_stat_delta {
@@ -73,7 +74,6 @@ static int dm_hit_limit = 64;
7374
static int dm_delay = 1;
7475
static unsigned long dm_hw_check_delta = 2*HZ;
7576
static LIST_HEAD(hw_stats_list);
76-
static int initialized = 0;
7777

7878
static void reset_per_cpu_data(struct per_cpu_dm_data *data)
7979
{
@@ -96,8 +96,8 @@ static void reset_per_cpu_data(struct per_cpu_dm_data *data)
9696
sizeof(struct net_dm_alert_msg));
9797
msg = nla_data(nla);
9898
memset(msg, 0, al);
99-
} else if (initialized)
100-
schedule_work_on(smp_processor_id(), &data->dm_alert_work);
99+
} else
100+
schedule_work_on(data->cpu, &data->dm_alert_work);
101101

102102
/*
103103
* Don't need to lock this, since we are guaranteed to only
@@ -121,6 +121,8 @@ static void send_dm_alert(struct work_struct *unused)
121121
struct sk_buff *skb;
122122
struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
123123

124+
WARN_ON_ONCE(data->cpu != smp_processor_id());
125+
124126
/*
125127
* Grab the skb we're about to send
126128
*/
@@ -404,14 +406,14 @@ static int __init init_net_drop_monitor(void)
404406

405407
for_each_present_cpu(cpu) {
406408
data = &per_cpu(dm_cpu_data, cpu);
407-
reset_per_cpu_data(data);
409+
data->cpu = cpu;
408410
INIT_WORK(&data->dm_alert_work, send_dm_alert);
409411
init_timer(&data->send_timer);
410412
data->send_timer.data = cpu;
411413
data->send_timer.function = sched_send_work;
414+
reset_per_cpu_data(data);
412415
}
413416

414-
initialized = 1;
415417

416418
goto out;
417419

0 commit comments

Comments
 (0)