From patchwork Wed Nov 15 19:09:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 10060059 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 399296019D for ; Wed, 15 Nov 2017 19:12:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A3B62A25D for ; Wed, 15 Nov 2017 19:12:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EF412A25F; Wed, 15 Nov 2017 19:12:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 699F62A25D for ; Wed, 15 Nov 2017 19:12:00 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eF33q-0004RR-H6; Wed, 15 Nov 2017 19:09:22 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eF33o-0004RL-K6 for xen-devel@lists.xenproject.org; Wed, 15 Nov 2017 19:09:20 +0000 Received: from [85.158.137.68] by server-5.bemta-3.messagelabs.com id 5C/51-20834-FD09C0A5; Wed, 15 Nov 2017 19:09:19 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrJKsWRWlGSWpSXmKPExsVybKJssu69CTx RBtte6Vp83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBkLVr9gLdhgXtHYa9rA+E+vi5GLQ0hgIpPE iWN3WLsYOTlYBBwk1p15x9bFyMHBKBAj8eCHNUiYUSBMYvLlJawgYRYBbYn3S+VAwmwChhJ/n 2wCq5YAspd85gAJiwgoS3xs7WUHsZkF2hkl9ixyBCkRFgiQmDUlG8TkFLCRON+sCFLBK+At8X jJWxaIW3YwS3S+XcwIkhAV0JU49O8PG0SRoMTJmU9YIEb6Say9d5oVwvaWWLTmBusERsFZSMp mISmbhaQMwtaSOHu0mREm3jTzIBuErS1xeMUrqLiixOYDu+F6vx3cCTVTVGLFjTlQNaYSn5af YlvAyLWKUaM4tagstUjXyFAvqSgzPaMkNzEzR9fQwFgvN7W4ODE9NScxqVgvOT93EyMwquoZG Bh3MPbs9TvEKMnBpCTK6/ybO0qILyk/pTIjsTgjvqg0J7X4EKMMB4eSBK8XMEqFBItS01Mr0j JzgPENk5bg4FES4V3XD5TmLS5IzC3OTIdInWK05Di26fIfJo4n1+b9ZeJ4NvN1A7MQS15+Xqq UOO9dkAYBkIaM0jy4cbAUdIlRVkqYl5GBgUGIpyC1KDezBFX+FaM4B6OSMK8eyFU8mXklcFtf AR3EBHSQzQ1ukINKEhFSUg2M4bUrHgXtllP7L397w9HmaWpBkw/tdZwaovotXWjzocVW1Xz/k 1pmrPTacXurZ9Y0nfrV4hNPMu5NNbgZa6xVsHJzq9otPevO+OjrlnnGH45vbHxXtWSWywyzPx 8qmb112YPydp4teND4kDUgev3Fj6cnFZ+d2612qPxYRu/MxFJZx7MTTKz+K7EUZyQaajEXFSc CABXz2Ro8AwAA X-Env-Sender: sstabellini@kernel.org X-Msg-Ref: server-12.tower-31.messagelabs.com!1510772957!98325288!1 X-Originating-IP: [198.145.29.99] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 5619 invoked from network); 15 Nov 2017 19:09:18 -0000 Received: from mail.kernel.org (HELO mail.kernel.org) (198.145.29.99) by server-12.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 15 Nov 2017 19:09:18 -0000 Received: from sstabellini-ThinkPad-X260 (m810536d0.tmodns.net [208.54.5.129]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2B8E121903; Wed, 15 Nov 2017 19:09:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2B8E121903 Date: Wed, 15 Nov 2017 11:09:14 -0800 (PST) From: Stefano Stabellini X-X-Sender: sstabellini@sstabellini-ThinkPad-X260 To: Juergen Gross In-Reply-To: <12bea1d0-6504-d7c6-c47c-7cd197264536@suse.com> Message-ID: References: <1510006679-6381-1-git-send-email-sstabellini@kernel.org> <12bea1d0-6504-d7c6-c47c-7cd197264536@suse.com> User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-ID: Cc: xen-devel@lists.xenproject.org, Boris Ostrovsky , Stefano Stabellini Subject: Re: [Xen-devel] [PATCH] xen/pvcalls: fix potential endless loop in pvcalls-front.c X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP On Wed, 15 Nov 2017, Juergen Gross wrote: > >>> while(mutex_is_locked(&map->active.in_mutex.owner) || > >>> mutex_is_locked(&map->active.out_mutex.owner)) > >>> cpu_relax(); > >>> > >>> ? > >> I'm not convinced there isn't a race. > >> > >> In pvcalls_front_recvmsg() sock->sk->sk_send_head is being read and only > >> then in_mutex is taken. What happens if pvcalls_front_release() resets > >> sk_send_head and manages to test the mutex before the mutex is locked? > >> > >> Even in case this is impossible: the whole construct seems to be rather > >> fragile. I agree it looks fragile, and I agree that it might be best to avoid the usage of in_mutex and out_mutex as refcounts. More comments on this below. > > I think we can wait until pvcalls_refcount is 1 (i.e. it's only us) and > > not rely on mutex state. > > Yes, this would work. Yes, I agree it would work and for the sake of getting something in shape for the merge window I am attaching a patch for it. Please go ahead with it. Let me know if you need anything else immediately, and I'll work on it ASAP. However, I should note that this is a pretty big hammer we are using: the refcount is global, while we only need to wait until it's only us _on this specific socket_. We really need a per socket refcount. If we don't want to use the mutex internal counters, then we need another one. See the appended patch that introduces a per socket refcount. However, for the merge window, also using pvcalls_refcount is fine. The race Juergen is concerned about is only theoretically possible: recvmsg: release: test sk_send_head clear sk_send_head grab in_mutex test in_mutex Without kernel preemption is not possible for release to clear sk_send_head and test in_mutex after recvmsg tests sk_send_head and before recvmsg grabs in_mutex. But maybe we need to disable kernel preemption in recvmsg and sendmsg to stay on the safe side? The patch below introduces a per active socket refcount, so that we don't have to rely on in_mutex and out_mutex for refcounting. It also disables preemption in sendmsg and recvmsg in the region described above. I don't think this patch should go in immediately. We can take our time to figure out the best fix. xen/pvcalls: fix potential endless loop in pvcalls-front.c mutex_trylock() returns 1 if you take the lock and 0 if not. Assume you take in_mutex on the first try, but you can't take out_mutex. Next times you call mutex_trylock() in_mutex is going to fail. It's an endless loop. Solve the problem by waiting until the global refcount is 1 instead (the refcount is 1 when the only active pvcalls frontend function is pvcalls_front_release). Reported-by: Dan Carpenter Signed-off-by: Stefano Stabellini CC: boris.ostrovsky@oracle.com CC: jgross@suse.com diff --git a/drivers/xen/pvcalls-front.c b/drivers/xen/pvcalls-front.c index 0c1ec68..72a74c3 100644 --- a/drivers/xen/pvcalls-front.c +++ b/drivers/xen/pvcalls-front.c @@ -1048,8 +1048,7 @@ int pvcalls_front_release(struct socket *sock) * is set to NULL -- we only need to wait for the existing * waiters to return. */ - while (!mutex_trylock(&map->active.in_mutex) || - !mutex_trylock(&map->active.out_mutex)) + while (atomic_read(&pvcalls_refcount) > 1) cpu_relax(); pvcalls_front_free_map(bedata, map);