Skip to content

Commit 9f51c05

Browse files
tasksetBoris Ostrovsky
authored andcommitted
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. v4: It would be better to pre-populate map (struct sock_mapping), rather than introducing one more new struct. v5: Since allocating the data outside of this call it should also be freed outside, when create_active() fails. Move kzalloc(sizeof(*map2), GFP_ATOMIC) outside spinlock and use GFP_KERNEL instead. v6: Drop the superfluous calls. Suggested-by: Juergen Gross <jgross@suse.com> Suggested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com> Suggested-by: Stefano Stabellini <sstabellini@kernel.org> Signed-off-by: Wen Yang <wen.yang99@zte.com.cn> Acked-by: Stefano Stabellini <sstabellini@kernel.org> CC: Julia Lawall <julia.lawall@lip6.fr> CC: Boris Ostrovsky <boris.ostrovsky@oracle.com> CC: Juergen Gross <jgross@suse.com> CC: Stefano Stabellini <sstabellini@kernel.org> CC: xen-devel@lists.xenproject.org CC: linux-kernel@vger.kernel.org Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
1 parent 1f8ce09 commit 9f51c05

File tree

1 file changed

+59
-22
lines changed

1 file changed

+59
-22
lines changed

drivers/xen/pvcalls-front.c

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,39 @@ int pvcalls_front_socket(struct socket *sock)
341341
return ret;
342342
}
343343

344+
static void free_active_ring(struct sock_mapping *map)
345+
{
346+
free_pages((unsigned long)map->active.data.in,
347+
map->active.ring->ring_order);
348+
free_page((unsigned long)map->active.ring);
349+
}
350+
351+
static int alloc_active_ring(struct sock_mapping *map)
352+
{
353+
void *bytes;
354+
355+
map->active.ring = (struct pvcalls_data_intf *)
356+
get_zeroed_page(GFP_KERNEL);
357+
if (!map->active.ring)
358+
goto out;
359+
360+
map->active.ring->ring_order = PVCALLS_RING_ORDER;
361+
bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
362+
PVCALLS_RING_ORDER);
363+
if (!bytes)
364+
goto out;
365+
366+
map->active.data.in = bytes;
367+
map->active.data.out = bytes +
368+
XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
369+
370+
return 0;
371+
372+
out:
373+
free_active_ring(map);
374+
return -ENOMEM;
375+
}
376+
344377
static int create_active(struct sock_mapping *map, int *evtchn)
345378
{
346379
void *bytes;
@@ -349,15 +382,7 @@ static int create_active(struct sock_mapping *map, int *evtchn)
349382
*evtchn = -1;
350383
init_waitqueue_head(&map->active.inflight_conn_req);
351384

352-
map->active.ring = (struct pvcalls_data_intf *)
353-
__get_free_page(GFP_KERNEL | __GFP_ZERO);
354-
if (map->active.ring == NULL)
355-
goto out_error;
356-
map->active.ring->ring_order = PVCALLS_RING_ORDER;
357-
bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
358-
PVCALLS_RING_ORDER);
359-
if (bytes == NULL)
360-
goto out_error;
385+
bytes = map->active.data.in;
361386
for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
362387
map->active.ring->ref[i] = gnttab_grant_foreign_access(
363388
pvcalls_front_dev->otherend_id,
@@ -367,10 +392,6 @@ static int create_active(struct sock_mapping *map, int *evtchn)
367392
pvcalls_front_dev->otherend_id,
368393
pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
369394

370-
map->active.data.in = bytes;
371-
map->active.data.out = bytes +
372-
XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
373-
374395
ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
375396
if (ret)
376397
goto out_error;
@@ -391,8 +412,6 @@ static int create_active(struct sock_mapping *map, int *evtchn)
391412
out_error:
392413
if (*evtchn >= 0)
393414
xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
394-
free_pages((unsigned long)map->active.data.in, PVCALLS_RING_ORDER);
395-
free_page((unsigned long)map->active.ring);
396415
return ret;
397416
}
398417

@@ -412,17 +431,24 @@ int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
412431
return PTR_ERR(map);
413432

414433
bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
434+
ret = alloc_active_ring(map);
435+
if (ret < 0) {
436+
pvcalls_exit_sock(sock);
437+
return ret;
438+
}
415439

416440
spin_lock(&bedata->socket_lock);
417441
ret = get_request(bedata, &req_id);
418442
if (ret < 0) {
419443
spin_unlock(&bedata->socket_lock);
444+
free_active_ring(map);
420445
pvcalls_exit_sock(sock);
421446
return ret;
422447
}
423448
ret = create_active(map, &evtchn);
424449
if (ret < 0) {
425450
spin_unlock(&bedata->socket_lock);
451+
free_active_ring(map);
426452
pvcalls_exit_sock(sock);
427453
return ret;
428454
}
@@ -786,25 +812,36 @@ int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
786812
}
787813
}
788814

789-
spin_lock(&bedata->socket_lock);
790-
ret = get_request(bedata, &req_id);
791-
if (ret < 0) {
815+
map2 = kzalloc(sizeof(*map2), GFP_KERNEL);
816+
if (map2 == NULL) {
792817
clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
793818
(void *)&map->passive.flags);
794-
spin_unlock(&bedata->socket_lock);
819+
pvcalls_exit_sock(sock);
820+
return -ENOMEM;
821+
}
822+
ret = alloc_active_ring(map2);
823+
if (ret < 0) {
824+
clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
825+
(void *)&map->passive.flags);
826+
kfree(map2);
795827
pvcalls_exit_sock(sock);
796828
return ret;
797829
}
798-
map2 = kzalloc(sizeof(*map2), GFP_ATOMIC);
799-
if (map2 == NULL) {
830+
spin_lock(&bedata->socket_lock);
831+
ret = get_request(bedata, &req_id);
832+
if (ret < 0) {
800833
clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
801834
(void *)&map->passive.flags);
802835
spin_unlock(&bedata->socket_lock);
836+
free_active_ring(map2);
837+
kfree(map2);
803838
pvcalls_exit_sock(sock);
804-
return -ENOMEM;
839+
return ret;
805840
}
841+
806842
ret = create_active(map2, &evtchn);
807843
if (ret < 0) {
844+
free_active_ring(map2);
808845
kfree(map2);
809846
clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
810847
(void *)&map->passive.flags);

0 commit comments

Comments
 (0)