diff options
author | J. Bruce Fields <bfields@redhat.com> | 2011-11-29 17:00:26 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2012-01-25 20:24:48 -0500 |
commit | a141a5eb3ab45131cb168e7a561d662722b43ec3 (patch) | |
tree | f72da23ff947391cc5181842a34b39db81f1dc63 /net | |
parent | 7df22768c0af8769d805f6db21144d71d91fe13d (diff) |
svcrpc: avoid memory-corruption on pool shutdown
commit b4f36f88b3ee7cf26bf0be84e6c7fc15f84dcb71 upstream.
Socket callbacks use svc_xprt_enqueue() to add an xprt to a
pool->sp_sockets list. In normal operation a server thread will later
come along and take the xprt off that list. On shutdown, after all the
threads have exited, we instead manually walk the sv_tempsocks and
sv_permsocks lists to find all the xprt's and delete them.
So the sp_sockets lists don't really matter any more. As a result,
we've mostly just ignored them and hoped they would go away.
Which has gotten us into trouble; witness for example ebc63e531cc6
"svcrpc: fix list-corrupting race on nfsd shutdown", the result of Ben
Greear noticing that a still-running svc_xprt_enqueue() could re-add an
xprt to an sp_sockets list just before it was deleted. The fix was to
remove it from the list at the end of svc_delete_xprt(). But that only
made corruption less likely--I can see nothing that prevents a
svc_xprt_enqueue() from adding another xprt to the list at the same
moment that we're removing this xprt from the list. In fact, despite
the earlier xpo_detach(), I don't even see what guarantees that
svc_xprt_enqueue() couldn't still be running on this xprt.
So, instead, note that svc_xprt_enqueue() essentially does:
lock sp_lock
if XPT_BUSY unset
add to sp_sockets
unlock sp_lock
So, if we do:
set XPT_BUSY on every xprt.
Empty every sp_sockets list, under the sp_socks locks.
Then we're left knowing that the sp_sockets lists are all empty and will
stay that way, since any svc_xprt_enqueue() will check XPT_BUSY under
the sp_lock and see it set.
And *then* we can continue deleting the xprt's.
(Thanks to Jeff Layton for being correctly suspicious of this code....)
Cc: Ben Greear <greearb@candelatech.com>
Cc: Jeff Layton <jlayton@redhat.com>
Signed-off-by: J. Bruce Fields <bfields@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'net')
-rw-r--r-- | net/sunrpc/svc.c | 10 | ||||
-rw-r--r-- | net/sunrpc/svc_xprt.c | 48 |
2 files changed, 38 insertions, 20 deletions
diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c index 4d5cb99194c..ce5f111fe32 100644 --- a/net/sunrpc/svc.c +++ b/net/sunrpc/svc.c | |||
@@ -475,7 +475,15 @@ svc_destroy(struct svc_serv *serv) | |||
475 | printk("svc_destroy: no threads for serv=%p!\n", serv); | 475 | printk("svc_destroy: no threads for serv=%p!\n", serv); |
476 | 476 | ||
477 | del_timer_sync(&serv->sv_temptimer); | 477 | del_timer_sync(&serv->sv_temptimer); |
478 | 478 | /* | |
479 | * The set of xprts (contained in the sv_tempsocks and | ||
480 | * sv_permsocks lists) is now constant, since it is modified | ||
481 | * only by accepting new sockets (done by service threads in | ||
482 | * svc_recv) or aging old ones (done by sv_temptimer), or | ||
483 | * configuration changes (excluded by whatever locking the | ||
484 | * caller is using--nfsd_mutex in the case of nfsd). So it's | ||
485 | * safe to traverse those lists and shut everything down: | ||
486 | */ | ||
479 | svc_close_all(serv); | 487 | svc_close_all(serv); |
480 | 488 | ||
481 | if (serv->sv_shutdown) | 489 | if (serv->sv_shutdown) |
diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c index 9cb2621a882..9d7ed0b48b5 100644 --- a/net/sunrpc/svc_xprt.c +++ b/net/sunrpc/svc_xprt.c | |||
@@ -901,14 +901,7 @@ void svc_delete_xprt(struct svc_xprt *xprt) | |||
901 | spin_lock_bh(&serv->sv_lock); | 901 | spin_lock_bh(&serv->sv_lock); |
902 | if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags)) | 902 | if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags)) |
903 | list_del_init(&xprt->xpt_list); | 903 | list_del_init(&xprt->xpt_list); |
904 | /* | 904 | BUG_ON(!list_empty(&xprt->xpt_ready)); |
905 | * The only time we're called while xpt_ready is still on a list | ||
906 | * is while the list itself is about to be destroyed (in | ||
907 | * svc_destroy). BUT svc_xprt_enqueue could still be attempting | ||
908 | * to add new entries to the sp_sockets list, so we can't leave | ||
909 | * a freed xprt on it. | ||
910 | */ | ||
911 | list_del_init(&xprt->xpt_ready); | ||
912 | if (test_bit(XPT_TEMP, &xprt->xpt_flags)) | 905 | if (test_bit(XPT_TEMP, &xprt->xpt_flags)) |
913 | serv->sv_tmpcnt--; | 906 | serv->sv_tmpcnt--; |
914 | spin_unlock_bh(&serv->sv_lock); | 907 | spin_unlock_bh(&serv->sv_lock); |
@@ -939,28 +932,45 @@ EXPORT_SYMBOL_GPL(svc_close_xprt); | |||
939 | static void svc_close_list(struct list_head *xprt_list) | 932 | static void svc_close_list(struct list_head *xprt_list) |
940 | { | 933 | { |
941 | struct svc_xprt *xprt; | 934 | struct svc_xprt *xprt; |
942 | struct svc_xprt *tmp; | ||
943 | 935 | ||
944 | /* | 936 | list_for_each_entry(xprt, xprt_list, xpt_list) { |
945 | * The server is shutting down, and no more threads are running. | ||
946 | * svc_xprt_enqueue() might still be running, but at worst it | ||
947 | * will re-add the xprt to sp_sockets, which will soon get | ||
948 | * freed. So we don't bother with any more locking, and don't | ||
949 | * leave the close to the (nonexistent) server threads: | ||
950 | */ | ||
951 | list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) { | ||
952 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | 937 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
953 | svc_delete_xprt(xprt); | 938 | set_bit(XPT_BUSY, &xprt->xpt_flags); |
954 | } | 939 | } |
955 | } | 940 | } |
956 | 941 | ||
957 | void svc_close_all(struct svc_serv *serv) | 942 | void svc_close_all(struct svc_serv *serv) |
958 | { | 943 | { |
944 | struct svc_pool *pool; | ||
945 | struct svc_xprt *xprt; | ||
946 | struct svc_xprt *tmp; | ||
947 | int i; | ||
948 | |||
959 | svc_close_list(&serv->sv_tempsocks); | 949 | svc_close_list(&serv->sv_tempsocks); |
960 | svc_close_list(&serv->sv_permsocks); | 950 | svc_close_list(&serv->sv_permsocks); |
951 | |||
952 | for (i = 0; i < serv->sv_nrpools; i++) { | ||
953 | pool = &serv->sv_pools[i]; | ||
954 | |||
955 | spin_lock_bh(&pool->sp_lock); | ||
956 | while (!list_empty(&pool->sp_sockets)) { | ||
957 | xprt = list_first_entry(&pool->sp_sockets, struct svc_xprt, xpt_ready); | ||
958 | list_del_init(&xprt->xpt_ready); | ||
959 | } | ||
960 | spin_unlock_bh(&pool->sp_lock); | ||
961 | } | ||
962 | /* | ||
963 | * At this point the sp_sockets lists will stay empty, since | ||
964 | * svc_enqueue will not add new entries without taking the | ||
965 | * sp_lock and checking XPT_BUSY. | ||
966 | */ | ||
967 | list_for_each_entry_safe(xprt, tmp, &serv->sv_tempsocks, xpt_list) | ||
968 | svc_delete_xprt(xprt); | ||
969 | list_for_each_entry_safe(xprt, tmp, &serv->sv_permsocks, xpt_list) | ||
970 | svc_delete_xprt(xprt); | ||
971 | |||
961 | BUG_ON(!list_empty(&serv->sv_permsocks)); | 972 | BUG_ON(!list_empty(&serv->sv_permsocks)); |
962 | BUG_ON(!list_empty(&serv->sv_tempsocks)); | 973 | BUG_ON(!list_empty(&serv->sv_tempsocks)); |
963 | |||
964 | } | 974 | } |
965 | 975 | ||
966 | /* | 976 | /* |