Skip to content

Commit 08062af

Browse files
jdamato-fslykuba-moo
authored andcommitted
net: napi: Prevent overflow of napi_defer_hard_irqs
In commit 6f8b12d ("net: napi: add hard irqs deferral feature") napi_defer_irqs was added to net_device and napi_defer_irqs_count was added to napi_struct, both as type int. This value never goes below zero, so there is not reason for it to be a signed int. Change the type for both from int to u32, and add an overflow check to sysfs to limit the value to S32_MAX. The limit of S32_MAX was chosen because the practical limit before this patch was S32_MAX (anything larger was an overflow) and thus there are no behavioral changes introduced. If the extra bit is needed in the future, the limit can be raised. Before this patch: $ sudo bash -c 'echo 2147483649 > /sys/class/net/eth4/napi_defer_hard_irqs' $ cat /sys/class/net/eth4/napi_defer_hard_irqs -2147483647 After this patch: $ sudo bash -c 'echo 2147483649 > /sys/class/net/eth4/napi_defer_hard_irqs' bash: line 0: echo: write error: Numerical result out of range Similarly, /sys/class/net/XXXXX/tx_queue_len is defined as unsigned: include/linux/netdevice.h: unsigned int tx_queue_len; And has an overflow check: dev_change_tx_queue_len(..., unsigned long new_len): if (new_len != (unsigned int)new_len) return -ERANGE; Suggested-by: Jakub Kicinski <[email protected]> Signed-off-by: Joe Damato <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 22d6ada commit 08062af

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

Documentation/networking/net_cachelines/net_device.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ unsigned_int num_rx_queues
9999
unsigned_int real_num_rx_queues - read_mostly get_rps_cpu
100100
struct_bpf_prog* xdp_prog - read_mostly netif_elide_gro()
101101
unsigned_long gro_flush_timeout - read_mostly napi_complete_done
102-
int napi_defer_hard_irqs - read_mostly napi_complete_done
102+
u32 napi_defer_hard_irqs - read_mostly napi_complete_done
103103
unsigned_int gro_max_size - read_mostly skb_gro_receive
104104
unsigned_int gro_ipv4_max_size - read_mostly skb_gro_receive
105105
rx_handler_func_t* rx_handler read_mostly - __netif_receive_skb_core

include/linux/netdevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ struct napi_struct {
356356

357357
unsigned long state;
358358
int weight;
359-
int defer_hard_irqs_count;
359+
u32 defer_hard_irqs_count;
360360
unsigned long gro_bitmask;
361361
int (*poll)(struct napi_struct *, int);
362362
#ifdef CONFIG_NETPOLL
@@ -2075,7 +2075,7 @@ struct net_device {
20752075
unsigned int real_num_rx_queues;
20762076
struct netdev_rx_queue *_rx;
20772077
unsigned long gro_flush_timeout;
2078-
int napi_defer_hard_irqs;
2078+
u32 napi_defer_hard_irqs;
20792079
unsigned int gro_max_size;
20802080
unsigned int gro_ipv4_max_size;
20812081
rx_handler_func_t __rcu *rx_handler;

net/core/net-sysfs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#ifdef CONFIG_SYSFS
3333
static const char fmt_hex[] = "%#x\n";
3434
static const char fmt_dec[] = "%d\n";
35+
static const char fmt_uint[] = "%u\n";
3536
static const char fmt_ulong[] = "%lu\n";
3637
static const char fmt_u64[] = "%llu\n";
3738

@@ -425,6 +426,9 @@ NETDEVICE_SHOW_RW(gro_flush_timeout, fmt_ulong);
425426

426427
static int change_napi_defer_hard_irqs(struct net_device *dev, unsigned long val)
427428
{
429+
if (val > S32_MAX)
430+
return -ERANGE;
431+
428432
WRITE_ONCE(dev->napi_defer_hard_irqs, val);
429433
return 0;
430434
}
@@ -438,7 +442,7 @@ static ssize_t napi_defer_hard_irqs_store(struct device *dev,
438442

439443
return netdev_store(dev, attr, buf, len, change_napi_defer_hard_irqs);
440444
}
441-
NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_dec);
445+
NETDEVICE_SHOW_RW(napi_defer_hard_irqs, fmt_uint);
442446

443447
static ssize_t ifalias_store(struct device *dev, struct device_attribute *attr,
444448
const char *buf, size_t len)

0 commit comments

Comments
 (0)