Skip to contentSkip to conversation

pvcalls-front: Avoid __get_free_pages(GFP_KERNEL) under spinlock

PATCH Superseded Wen Yang 4 participants 7 messages Nov 30, 2018 Xen hypervisor interface

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH v3 1/1 pvcalls-front: Avoid __get_free_pages(GFP_KERNEL) under spinlock

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
---
 drivers/xen/pvcalls-front.c | 71 ++++++++++++++++++++++++++++++-------
 1 file changed, 59 insertions(+), 12 deletions(-)
drivers/xen/pvcalls-front.c +59 -12
index 2f11ca72a281..a26f416daf46 100644
@@ -335,7 +335,43 @@ int pvcalls_front_socket(struct socket *sock)
 	return ret;
 }
 
-static int create_active(struct sock_mapping *map, int *evtchn)
+struct sock_mapping_active_ring {
+	struct pvcalls_data_intf *ring;
+	RING_IDX ring_order;
+	void *bytes;
+};
+
+static int alloc_active_ring(struct sock_mapping_active_ring *active_ring)
+{
+	active_ring->ring = NULL;
+	active_ring->bytes = NULL;
+
+	active_ring->ring = (struct pvcalls_data_intf *)
+		__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	if (active_ring->ring == NULL)
+		goto out_error;
+	active_ring->ring_order = PVCALLS_RING_ORDER;
+	active_ring->bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
+			PVCALLS_RING_ORDER);
+	if (active_ring->bytes == NULL)
+		goto out_error;
+
+	return 0;
+
+out_error:
+	free_pages((unsigned long)active_ring->bytes, active_ring->ring_order);
+	free_page((unsigned long)active_ring->ring);
+	return -ENOMEM;
+}
+
+static void free_active_ring(struct sock_mapping_active_ring *active_ring)
+{
+	free_pages((unsigned long)active_ring->bytes, active_ring->ring_order);
+	free_page((unsigned long)active_ring->ring);
+}
+
+static int create_active(struct sock_mapping *map, int *evtchn,
+		struct sock_mapping_active_ring *active_ring)
 {
 	void *bytes;
 	int ret = -ENOMEM, irq = -1, i;
@@ -343,15 +379,9 @@ static int create_active(struct sock_mapping *map, int *evtchn)
 	*evtchn = -1;
 	init_waitqueue_head(&map->active.inflight_conn_req);
 
-	map->active.ring = (struct pvcalls_data_intf *)
-		__get_free_page(GFP_KERNEL | __GFP_ZERO);
-	if (map->active.ring == NULL)
-		goto out_error;
-	map->active.ring->ring_order = PVCALLS_RING_ORDER;
-	bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
-					PVCALLS_RING_ORDER);
-	if (bytes == NULL)
-		goto out_error;
+	map->active.ring = active_ring->ring;
+	map->active.ring->ring_order = active_ring->ring_order;
+	bytes = active_ring->bytes;
 	for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
 		map->active.ring->ref[i] = gnttab_grant_foreign_access(
 			pvcalls_front_dev->otherend_id,
@@ -397,6 +427,7 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 	struct sock_mapping *map = NULL;
 	struct xen_pvcalls_request *req;
 	int notify, req_id, ret, evtchn;
+	struct sock_mapping_active_ring active_ring;
 
 	if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
 		return -EOPNOTSUPP;
@@ -406,15 +437,21 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 		return PTR_ERR(map);
 
 	bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
+	ret = alloc_active_ring(&active_ring);
+	if (ret < 0) {
+		pvcalls_exit_sock(sock);
+		return ret;
+	}
 
 	spin_lock(&bedata->socket_lock);
 	ret = get_request(bedata, &req_id);
 	if (ret < 0) {
 		spin_unlock(&bedata->socket_lock);
+		free_active_ring(&active_ring);
 		pvcalls_exit_sock(sock);
 		return ret;
 	}
-	ret = create_active(map, &evtchn);
+	ret = create_active(map, &evtchn, &active_ring);
 	if (ret < 0) {
 		spin_unlock(&bedata->socket_lock);
 		pvcalls_exit_sock(sock);
@@ -744,6 +781,7 @@ int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
 	struct sock_mapping *map2 = NULL;
 	struct xen_pvcalls_request *req;
 	int notify, req_id, ret, evtchn, nonblock;
+	struct sock_mapping_active_ring active_ring;
 
 	map = pvcalls_enter_sock(sock);
 	if (IS_ERR(map))
@@ -780,12 +818,20 @@ int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
 		}
 	}
 
+	ret = alloc_active_ring(&active_ring);
+	if (ret < 0) {
+		clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
+				(void *)&map->passive.flags);
+		pvcalls_exit_sock(sock);
+		return ret;
+	}
 	spin_lock(&bedata->socket_lock);
 	ret = get_request(bedata, &req_id);
 	if (ret < 0) {
 		clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
 			  (void *)&map->passive.flags);
 		spin_unlock(&bedata->socket_lock);
+		free_active_ring(&active_ring);
 		pvcalls_exit_sock(sock);
 		return ret;
 	}
@@ -794,10 +840,11 @@ int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
 		clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
 			  (void *)&map->passive.flags);
 		spin_unlock(&bedata->socket_lock);
+		free_active_ring(&active_ring);
 		pvcalls_exit_sock(sock);
 		return -ENOMEM;
 	}
-	ret = create_active(map2, &evtchn);
+	ret = create_active(map2, &evtchn, &active_ring);
 	if (ret < 0) {
 		kfree(map2);
 		clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
Signature · 1 line
2.19.1
maintainer
#1 lore raw
Quoted 26 lines · Wen Yang

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
Quoted 1 line

---

This patch is malformed. Please send it via an appropriate tool/mailer.

See e.g. Documentation/process/submitting-patches.rst or Documentation/process/email-clients.rst in the Linux kernel source tree.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

maintainer
#2 lore raw
In reply to Wen Yang
Quoted 26 lines · Wen Yang

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
Quoted 1 line

---

This patch is malformed. Please send it via an appropriate tool/mailer.

See e.g. Documentation/process/submitting-patches.rst or Documentation/process/email-clients.rst in the Linux kernel source tree.

Juergen

#3 lore raw
In reply to Wen Yang
Quoted 38 lines · Wen Yang

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
---
 drivers/xen/pvcalls-front.c | 71 ++++++++++++++++++++++++++++++-------
 1 file changed, 59 insertions(+), 12 deletions(-)
drivers/xen/pvcalls-front.c +0 -0
index 2f11ca72a281..a26f416daf46 100644
@@ -335,7 +335,43 @@ int pvcalls_front_socket(struct socket *sock)
 	return ret;
 }
 

drivers/xen/pvcalls-front.c int pvcalls_front_socket(struct socket *sock) · line 346

Quoted 10 lines
drivers/xen/pvcalls-front.c +9 -1
-static int create_active(struct sock_mapping *map, int *evtchn)
+struct sock_mapping_active_ring {
+	struct pvcalls_data_intf *ring;
+	RING_IDX ring_order;
+	void *bytes;
+};
+
+static int alloc_active_ring(struct sock_mapping_active_ring *active_ring)
+{
+	active_ring->ring = NULL;

This is not necessary.

Quoted 32 lines
drivers/xen/pvcalls-front.c +21 -0
+	active_ring->bytes = NULL;
+
+	active_ring->ring = (struct pvcalls_data_intf *)
+		__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	if (active_ring->ring == NULL)
+		goto out_error;
+	active_ring->ring_order = PVCALLS_RING_ORDER;
+	active_ring->bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
+			PVCALLS_RING_ORDER);
+	if (active_ring->bytes == NULL)
+		goto out_error;
+
+	return 0;
+
+out_error:
+	free_pages((unsigned long)active_ring->bytes, active_ring->ring_order);
+	free_page((unsigned long)active_ring->ring);
+	return -ENOMEM;
+}
+
 
 
 
@@ -397,6 +427,7 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 	struct sock_mapping *map = NULL;
 	struct xen_pvcalls_request *req;
 	int notify, req_id, ret, evtchn;
+	struct sock_mapping_active_ring active_ring;
 
 	if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
 		return -EOPNOTSUPP;

drivers/xen/pvcalls-front.c int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr, · line 440

Quoted 4 lines
drivers/xen/pvcalls-front.c +1 -0
@@ -406,15 +437,21 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 		return PTR_ERR(map);
 
 	bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
+	ret = alloc_active_ring(&active_ring);

Why not just alloc_active_ring(map)?

-boris

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Wen Yang
Quoted 38 lines · Wen Yang

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
---
 drivers/xen/pvcalls-front.c | 71 ++++++++++++++++++++++++++++++-------
 1 file changed, 59 insertions(+), 12 deletions(-)
drivers/xen/pvcalls-front.c +0 -0
index 2f11ca72a281..a26f416daf46 100644
@@ -335,7 +335,43 @@ int pvcalls_front_socket(struct socket *sock)
 	return ret;
 }
 

drivers/xen/pvcalls-front.c int pvcalls_front_socket(struct socket *sock) · line 346

Quoted 10 lines
drivers/xen/pvcalls-front.c +9 -1
-static int create_active(struct sock_mapping *map, int *evtchn)
+struct sock_mapping_active_ring {
+	struct pvcalls_data_intf *ring;
+	RING_IDX ring_order;
+	void *bytes;
+};
+
+static int alloc_active_ring(struct sock_mapping_active_ring *active_ring)
+{
+	active_ring->ring = NULL;

This is not necessary.

Quoted 32 lines
drivers/xen/pvcalls-front.c +21 -0
+	active_ring->bytes = NULL;
+
+	active_ring->ring = (struct pvcalls_data_intf *)
+		__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	if (active_ring->ring == NULL)
+		goto out_error;
+	active_ring->ring_order = PVCALLS_RING_ORDER;
+	active_ring->bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
+			PVCALLS_RING_ORDER);
+	if (active_ring->bytes == NULL)
+		goto out_error;
+
+	return 0;
+
+out_error:
+	free_pages((unsigned long)active_ring->bytes, active_ring->ring_order);
+	free_page((unsigned long)active_ring->ring);
+	return -ENOMEM;
+}
+
 
 
 
@@ -397,6 +427,7 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 	struct sock_mapping *map = NULL;
 	struct xen_pvcalls_request *req;
 	int notify, req_id, ret, evtchn;
+	struct sock_mapping_active_ring active_ring;
 
 	if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
 		return -EOPNOTSUPP;

drivers/xen/pvcalls-front.c int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr, · line 440

Quoted 4 lines
drivers/xen/pvcalls-front.c +1 -0
@@ -406,15 +437,21 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 		return PTR_ERR(map);
 
 	bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
+	ret = alloc_active_ring(&active_ring);

Why not just alloc_active_ring(map)?

-boris

#5 lore raw
Quoted 86 lines · Boris Ostrovsky
Quoted 38 lines · Wen Yang

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
---
 drivers/xen/pvcalls-front.c | 71 ++++++++++++++++++++++++++++++-------
 1 file changed, 59 insertions(+), 12 deletions(-)
drivers/xen/pvcalls-front.c +0 -0
index 2f11ca72a281..a26f416daf46 100644
@@ -335,7 +335,43 @@ int pvcalls_front_socket(struct socket *sock)
 	return ret;
 }
 

drivers/xen/pvcalls-front.c int pvcalls_front_socket(struct socket *sock) · line 346

Quoted 10 lines
drivers/xen/pvcalls-front.c +9 -1
-static int create_active(struct sock_mapping *map, int *evtchn)
+struct sock_mapping_active_ring {
+	struct pvcalls_data_intf *ring;
+	RING_IDX ring_order;
+	void *bytes;
+};
+
+static int alloc_active_ring(struct sock_mapping_active_ring *active_ring)
+{
+	active_ring->ring = NULL;

This is not necessary.

Quoted 34 lines
drivers/xen/pvcalls-front.c +21 -0
+	active_ring->bytes = NULL;
+
+	active_ring->ring = (struct pvcalls_data_intf *)
+		__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	if (active_ring->ring == NULL)
+		goto out_error;
+	active_ring->ring_order = PVCALLS_RING_ORDER;
+	active_ring->bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
+			PVCALLS_RING_ORDER);
+	if (active_ring->bytes == NULL)
+		goto out_error;
+
+	return 0;
+
+out_error:
+	free_pages((unsigned long)active_ring->bytes, active_ring->ring_order);
+	free_page((unsigned long)active_ring->ring);
+	return -ENOMEM;
+}
+
 
 
 
@@ -397,6 +427,7 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 	struct sock_mapping *map = NULL;
 	struct xen_pvcalls_request *req;
 	int notify, req_id, ret, evtchn;
+	struct sock_mapping_active_ring active_ring;
 
 	if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
 		return -EOPNOTSUPP;
@@ -406,15 +437,21 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 		return PTR_ERR(map);
 
Quoted 4 lines

drivers/xen/pvcalls-front.c int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr, · line 440

Quoted 2 lines
drivers/xen/pvcalls-front.c +1 -0
 	bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
+	ret = alloc_active_ring(&active_ring);

Why not just alloc_active_ring(map)?

Yes, I think it would be better to pre-populate map (struct sock_mapping), rather than introducing one more new struct (struct sock_mapping_active_ring).

#6 lore raw
In reply to Boris Ostrovsky
Quoted 86 lines · Boris Ostrovsky
Quoted 38 lines · Wen Yang

The problem is that we call this with a spin lock held. The call tree is:
pvcalls_front_accept() holds bedata->socket_lock.

    -> create_active()
        -> __get_free_pages() uses GFP_KERNEL

The create_active() function is only called from pvcalls_front_accept() with a spin_lock held, The allocation is not allowed to sleep and GFP_KERNEL is not sufficient.

This issue was detected by using the Coccinelle software.

v2: Add a function doing the allocations which is called
outside the lock and passing the allocated data to create_active().
v3: Use the matching deallocators i.e., free_page()
and free_pages(), respectively.

CC Julia Lawall <[email protected]>
CC Boris Ostrovsky <[email protected]>
CC Juergen Gross <[email protected]>
CC Stefano Stabellini <[email protected]>
---
 drivers/xen/pvcalls-front.c | 71 ++++++++++++++++++++++++++++++-------
 1 file changed, 59 insertions(+), 12 deletions(-)
drivers/xen/pvcalls-front.c +0 -0
index 2f11ca72a281..a26f416daf46 100644
@@ -335,7 +335,43 @@ int pvcalls_front_socket(struct socket *sock)
 	return ret;
 }
 

drivers/xen/pvcalls-front.c int pvcalls_front_socket(struct socket *sock) · line 346

Quoted 10 lines
drivers/xen/pvcalls-front.c +9 -1
-static int create_active(struct sock_mapping *map, int *evtchn)
+struct sock_mapping_active_ring {
+	struct pvcalls_data_intf *ring;
+	RING_IDX ring_order;
+	void *bytes;
+};
+
+static int alloc_active_ring(struct sock_mapping_active_ring *active_ring)
+{
+	active_ring->ring = NULL;

This is not necessary.

Quoted 34 lines
drivers/xen/pvcalls-front.c +21 -0
+	active_ring->bytes = NULL;
+
+	active_ring->ring = (struct pvcalls_data_intf *)
+		__get_free_page(GFP_KERNEL | __GFP_ZERO);
+	if (active_ring->ring == NULL)
+		goto out_error;
+	active_ring->ring_order = PVCALLS_RING_ORDER;
+	active_ring->bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
+			PVCALLS_RING_ORDER);
+	if (active_ring->bytes == NULL)
+		goto out_error;
+
+	return 0;
+
+out_error:
+	free_pages((unsigned long)active_ring->bytes, active_ring->ring_order);
+	free_page((unsigned long)active_ring->ring);
+	return -ENOMEM;
+}
+
 
 
 
@@ -397,6 +427,7 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 	struct sock_mapping *map = NULL;
 	struct xen_pvcalls_request *req;
 	int notify, req_id, ret, evtchn;
+	struct sock_mapping_active_ring active_ring;
 
 	if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
 		return -EOPNOTSUPP;
@@ -406,15 +437,21 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
 		return PTR_ERR(map);
 
Quoted 4 lines

drivers/xen/pvcalls-front.c int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr, · line 440

Quoted 2 lines
drivers/xen/pvcalls-front.c +1 -0
 	bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
+	ret = alloc_active_ring(&active_ring);

Why not just alloc_active_ring(map)?

Yes, I think it would be better to pre-populate map (struct sock_mapping), rather than introducing one more new struct (struct sock_mapping_active_ring).

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel