Skip to contentSkip to conversation

net:qrtr: fix atomic idr allocation in qrtr_port_assign()

PATCH In review Du Cheng 4 participants 9 messages Mar 27, 2021 – Mar 29, 2021

Subsystems (3)
lore
Share & tools
lore

Du Cheng

author
#0 lore raw

PATCH v2 1/1 net:qrtr: fix atomic idr allocation in qrtr_port_assign()

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +6 -0
index edb6ac17ceca..6361f169490e 100644
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();
 		if (!rc)
 			*port = min_port;
 	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
 		rc = -EACCES;
 	} else if (*port == QRTR_PORT_CTRL) {
 		min_port = 0;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC);
+		idr_preload_end();
 	} else {
 		min_port = *port;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC);
+		idr_preload_end();
 		if (!rc)
 			*port = min_port;
 	}
Signature · 1 line
2.27.0
#1 lore raw

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

thanks,

greg k-h

author
#2 lore raw
Quoted 35 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Quoted 7 lines

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

thanks,

greg k-h

From the comment above idr_preload() in lib/radix-tree.c:1460
/**
* idr_preload - preload for idr_alloc()
* @gfp_mask: allocation mask to use for preloading
*
* Preallocate memory to use for the next call to idr_alloc(). This function
* returns with preemption disabled. It will be enabled by idr_preload_end(). */

idr_alloc is a very simple wrapper around idr_alloc_u32().

On top of radix_tree_node_alloc() which is called by idr_alloc_u32(), there is this comment at line 244 in the same radix-tree.c
/*
* This assumes that the caller has performed appropriate preallocation, and
* that the caller has pinned this thread of control to the current CPU. */

Therefore the preload/preload_end are necessary, or at least should have preemption disabled

Regards,
Du Cheng

#3 lore raw
Quoted 63 lines · Du Cheng
Quoted 39 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

Quoted 6 lines

thanks,

greg k-h

From the comment above idr_preload() in lib/radix-tree.c:1460

/**
* idr_preload - preload for idr_alloc()
* @gfp_mask: allocation mask to use for preloading
*
* Preallocate memory to use for the next call to idr_alloc(). This function
* returns with preemption disabled. It will be enabled by idr_preload_end(). */

idr_alloc is a very simple wrapper around idr_alloc_u32().

On top of radix_tree_node_alloc() which is called by idr_alloc_u32(), there is this comment at line 244 in the same radix-tree.c
/*
* This assumes that the caller has performed appropriate preallocation, and
* that the caller has pinned this thread of control to the current CPU. */

Quoted 2 lines

Therefore the preload/preload_end are necessary, or at least should have preemption disabled

Ah, so it's disabling preemption that is the key here. Still odd, why is GFP_ATOMIC not sufficient in a normal idr_alloc() call to keep things from doing stuff like this? Feels like a lot of "internal knowledge" is needed here to use this api properly...

Matthew, is the above change really correct?

thanks,

greg k-h

#4 lore raw
Quoted 67 lines · Greg Kroah-Hartman
Quoted 63 lines · Du Cheng
Quoted 39 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

Quoted 6 lines

thanks,

greg k-h

From the comment above idr_preload() in lib/radix-tree.c:1460

/**
* idr_preload - preload for idr_alloc()
* @gfp_mask: allocation mask to use for preloading
*
* Preallocate memory to use for the next call to idr_alloc(). This function
* returns with preemption disabled. It will be enabled by idr_preload_end(). */

idr_alloc is a very simple wrapper around idr_alloc_u32().

On top of radix_tree_node_alloc() which is called by idr_alloc_u32(), there is this comment at line 244 in the same radix-tree.c
/*
* This assumes that the caller has performed appropriate preallocation, and
* that the caller has pinned this thread of control to the current CPU. */

Quoted 2 lines

Therefore the preload/preload_end are necessary, or at least should have preemption disabled

Quoted 6 lines

Ah, so it's disabling preemption that is the key here. Still odd, why is GFP_ATOMIC not sufficient in a normal idr_alloc() call to keep things from doing stuff like this? Feels like a lot of "internal knowledge" is needed here to use this api properly...

Matthew, is the above change really correct?

No.

https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/

#5 lore raw
Quoted 73 lines · Matthew Wilcox
Quoted 72 lines · Greg Kroah-Hartman
Quoted 63 lines · Du Cheng
Quoted 39 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

Quoted 6 lines

thanks,

greg k-h

From the comment above idr_preload() in lib/radix-tree.c:1460

/**
* idr_preload - preload for idr_alloc()
* @gfp_mask: allocation mask to use for preloading
*
* Preallocate memory to use for the next call to idr_alloc(). This function
* returns with preemption disabled. It will be enabled by idr_preload_end(). */

idr_alloc is a very simple wrapper around idr_alloc_u32().

On top of radix_tree_node_alloc() which is called by idr_alloc_u32(), there is this comment at line 244 in the same radix-tree.c
/*
* This assumes that the caller has performed appropriate preallocation, and
* that the caller has pinned this thread of control to the current CPU. */

Quoted 2 lines

Therefore the preload/preload_end are necessary, or at least should have preemption disabled

Ah, so it's disabling preemption that is the key here. Still odd, why is GFP_ATOMIC not sufficient in a normal idr_alloc() call to keep things from doing stuff like this? Feels like a lot of "internal knowledge" is needed here to use this api properly...

Quoted 8 lines
Quoted 1 line

Matthew, is the above change really correct?

No.

https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/

Ok, it looks like this code is just abandonded, should we remove it entirely as no one wants to maintain it?

thanks,

greg k-h

#6 lore raw
Quoted 78 lines · Greg Kroah-Hartman
Quoted 77 lines · Matthew Wilcox
Quoted 67 lines · Greg Kroah-Hartman
Quoted 63 lines · Du Cheng
Quoted 39 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

Quoted 6 lines

thanks,

greg k-h

From the comment above idr_preload() in lib/radix-tree.c:1460

/**
* idr_preload - preload for idr_alloc()
* @gfp_mask: allocation mask to use for preloading
*
* Preallocate memory to use for the next call to idr_alloc(). This function
* returns with preemption disabled. It will be enabled by idr_preload_end(). */

idr_alloc is a very simple wrapper around idr_alloc_u32().

On top of radix_tree_node_alloc() which is called by idr_alloc_u32(), there is this comment at line 244 in the same radix-tree.c
/*
* This assumes that the caller has performed appropriate preallocation, and
* that the caller has pinned this thread of control to the current CPU. */

Quoted 2 lines

Therefore the preload/preload_end are necessary, or at least should have preemption disabled

Quoted 6 lines

Ah, so it's disabling preemption that is the key here. Still odd, why is GFP_ATOMIC not sufficient in a normal idr_alloc() call to keep things from doing stuff like this? Feels like a lot of "internal knowledge" is needed here to use this api properly...

Matthew, is the above change really correct?

No.

Quoted 7 lines
Quoted 4 lines

https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/

Ok, it looks like this code is just abandonded, should we remove it entirely as no one wants to maintain it?

Fine by me. I don't use it. Better to get rid of abandonware than keep a potential source of security holes.

author
#7 lore raw
Quoted 84 lines · Matthew Wilcox
Quoted 83 lines · Greg Kroah-Hartman
Quoted 81 lines · Matthew Wilcox
Quoted 67 lines · Greg Kroah-Hartman
Quoted 63 lines · Du Cheng
Quoted 39 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 21 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +0 -0
index edb6ac17ceca..6361f169490e 100644

net/qrtr/qrtr.c static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) · line 727

Quoted 6 lines
net/qrtr/qrtr.c +2 -0
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

This seems "odd" to me. We are asking idr_alloc_u32() to abide by GFP_ATOMIC, so why do we need to "preload" it with the same type of allocation?

Is there something in the idr/radix/xarray code that can't really handle GFP_ATOMIC during a "normal" idr allocation that is causing this warning to be hit? Why is this change the "correct" one?

Quoted 6 lines

thanks,

greg k-h

From the comment above idr_preload() in lib/radix-tree.c:1460

/**
* idr_preload - preload for idr_alloc()
* @gfp_mask: allocation mask to use for preloading
*
* Preallocate memory to use for the next call to idr_alloc(). This function
* returns with preemption disabled. It will be enabled by idr_preload_end(). */

idr_alloc is a very simple wrapper around idr_alloc_u32().

On top of radix_tree_node_alloc() which is called by idr_alloc_u32(), there is this comment at line 244 in the same radix-tree.c
/*
* This assumes that the caller has performed appropriate preallocation, and
* that the caller has pinned this thread of control to the current CPU. */

Quoted 2 lines

Therefore the preload/preload_end are necessary, or at least should have preemption disabled

Quoted 6 lines

Ah, so it's disabling preemption that is the key here. Still odd, why is GFP_ATOMIC not sufficient in a normal idr_alloc() call to keep things from doing stuff like this? Feels like a lot of "internal knowledge" is needed here to use this api properly...

Matthew, is the above change really correct?

No.

https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/

Quoted 5 lines
Quoted 2 lines

Ok, it looks like this code is just abandonded, should we remove it entirely as no one wants to maintain it?

Fine by me. I don't use it. Better to get rid of abandonware than keep a potential source of security holes.

Hi Manivannan,

For your information.

Regards,
Du Cheng

#8 lore raw
Quoted 37 lines · Du Cheng
Quoted 36 lines · Matthew Wilcox
Quoted 35 lines · Greg Kroah-Hartman
Quoted 34 lines · Matthew Wilcox
Quoted 33 lines · Greg Kroah-Hartman
Quoted 32 lines · Du Cheng
Quoted 31 lines · Greg Kroah-Hartman

Adding the xarray maintainer...

Quoted 27 lines · Du Cheng

add idr_preload() and idr_preload_end() around idr_alloc_u32(GFP_ATOMIC) due to internal use of per_cpu variables, which requires preemption disabling/enabling.

reported as "BUG: "using smp_processor_id() in preemptible" by syzkaller

---
changelog
v1: change to GFP_KERNEL for idr_alloc_u32() but might sleep
v2: revert to GFP_ATOMIC but add preemption disable/enable protection

 net/qrtr/qrtr.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/qrtr/qrtr.c +2 -0
index edb6ac17ceca..6361f169490e 100644
@@ -722,17 +722,23 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
 		min_port = QRTR_MIN_EPH_SOCKET;
+		idr_preload(GFP_ATOMIC);
 		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
+		idr_preload_end();

[...]

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

Ok, it looks like this code is just abandonded, should we remove it entirely as no one wants to maintain it?

Fine by me. I don't use it. Better to get rid of abandonware than keep a potential source of security holes.

Hi Manivannan,

For your information.

Thanks for letting me know. I'll look into it once back to work.

Thanks,
Mani

Quoted 2 lines

Regards,
Du Cheng