Skip to contentSkip to conversation

net: core: dev: Initialise napi state correctly

PATCH In review Dave McKay 3 participants 5 messages Jan 18, 2019 – Jan 23, 2019 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Dave McKay

author
#0 lore raw

PATCH 1/1 net: core: dev: Initialise napi state correctly

The state member of the napi_struct is not initialised correctly, it sets the SCHED bit without initialising the state to zero first. This results in peculiar behaviour if the original napi_struct didn't come from a zero initialised region to start with.

This patch just sets it directly using the appropriate bitfield constant.

---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/dev.c +1 -1
index 82f20022259d..250f97bf1973 100644
@@ -6276,7 +6276,7 @@ void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
 #ifdef CONFIG_NETPOLL
 	napi->poll_owner = -1;
 #endif
-	set_bit(NAPI_STATE_SCHED, &napi->state);
+	napi->state = NAPIF_STATE_SCHED;
 	napi_hash_add(napi);
 }
 EXPORT_SYMBOL(netif_napi_add);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 19 lines · Dave McKay

The state member of the napi_struct is not initialised correctly, it sets the SCHED bit without initialising the state to zero first. This results in peculiar behaviour if the original napi_struct didn't come from a zero initialised region to start with.

This patch just sets it directly using the appropriate bitfield constant.

---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/dev.c +0 -0
index 82f20022259d..250f97bf1973 100644

net/core/dev.c void netif_napi_add(struct net_device *dev, struct napi_struct *napi, · line 6282

Quoted 8 lines
net/core/dev.c +1 -1
@@ -6276,7 +6276,7 @@ void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
 #ifdef CONFIG_NETPOLL
 	napi->poll_owner = -1;
 #endif
-	set_bit(NAPI_STATE_SCHED, &napi->state);
+	napi->state = NAPIF_STATE_SCHED;
 	napi_hash_add(napi);
 }
 EXPORT_SYMBOL(netif_napi_add);

I am curious, which driver has exhibit any issue with current code ?

Thanks.

#2 lore raw
Quoted 17 lines · Eric Dumazet
Quoted 13 lines · Dave McKay

The state member of the napi_struct is not initialised correctly, it sets the SCHED bit without initialising the state to zero first. This results in peculiar behaviour if the original napi_struct didn't come from a zero initialised region to start with.

This patch just sets it directly using the appropriate bitfield constant.

---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
Quoted 6 lines
net/core/dev.c +0 -0
index 82f20022259d..250f97bf1973 100644

net/core/dev.c void netif_napi_add(struct net_device *dev, struct napi_struct *napi, · line 6282

Quoted 8 lines
net/core/dev.c +1 -1
@@ -6276,7 +6276,7 @@ void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
 #ifdef CONFIG_NETPOLL
      napi->poll_owner = -1;
 #endif
-     set_bit(NAPI_STATE_SCHED, &napi->state);
+     napi->state = NAPIF_STATE_SCHED;
      napi_hash_add(napi);
 }
 EXPORT_SYMBOL(netif_napi_add);

I am curious, which driver has exhibit any issue with current code ?

Hi Eric,
As far as I know, none! I suspect all the napi_adds() are called on areas
that come from kzalloc(). I sampled a few and all of them would not have hit this problem as the memory was allocated in zero initialised memory anyway.

I was working on an experimental out of tree driver that allocates the napi struct through kzalloc().
I could not understand why kzalloc() worked but kmalloc() sometimes didn't. I tracked it down to this.

Am I wrong here? Seemed to fix it for me!

Cheers!

A message that is not in the archive (sent off-list or not yet received): CAOhhRxvVAts0N=ptDSaL5FoY_Uo2UseH5Zow2Q2nOYO6ujAS0Q@mail.gmail.com

#4 lore raw
Quoted 36 lines · David McKay
Quoted 17 lines · Eric Dumazet
Quoted 13 lines · Dave McKay

The state member of the napi_struct is not initialised correctly, it sets the SCHED bit without initialising the state to zero first. This results in peculiar behaviour if the original napi_struct didn't come from a zero initialised region to start with.

This patch just sets it directly using the appropriate bitfield constant.

---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
Quoted 6 lines
net/core/dev.c +0 -0
index 82f20022259d..250f97bf1973 100644

net/core/dev.c void netif_napi_add(struct net_device *dev, struct napi_struct *napi, · line 6282

Quoted 8 lines
net/core/dev.c +1 -1
@@ -6276,7 +6276,7 @@ void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
 #ifdef CONFIG_NETPOLL
      napi->poll_owner = -1;
 #endif
-     set_bit(NAPI_STATE_SCHED, &napi->state);
+     napi->state = NAPIF_STATE_SCHED;
      napi_hash_add(napi);
 }
 EXPORT_SYMBOL(netif_napi_add);

I am curious, which driver has exhibit any issue with current code ?

Quoted 2 lines

gro_cell_init() maybe?

Perfect example of something that would break with your patch :/

alloc_percpu() clears all memory.

gro_cells_init() does :

set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state); netif_napi_add(dev, &cell->napi, gro_cell_poll, ...)

So clearing napi->state in netif_napi_add() would remove the NAPI_STATE_NO_BUSY_POLL setting.

maintainer
#5 lore raw
In reply to Dave McKay

From: Dave McKay <[email protected]>
Date: Fri, 18 Jan 2019 12:46:32 +0000

Quoted 9 lines

The state member of the napi_struct is not initialised correctly, it sets the SCHED bit without initialising the state to zero first. This results in peculiar behaviour if the original napi_struct didn't come from a zero initialised region to start with.

This patch just sets it directly using the appropriate bitfield constant.

Caller needs to pass in properly initialized memory, that means bzero()'d.