From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id B8C4A1F462 for ; Thu, 23 May 2019 18:33:30 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:cc:references:from:subject:message-id:date :mime-version:in-reply-to:content-type :content-transfer-encoding; q=dns; s=default; b=gNIl+n9M7tlVerrw kXC2oSHY8wDz/Al7I4qwrj3alN7fm/P0hRkazmxKAzBzuSxJxT78m56pOh2uF9zZ mAb4RrxL3l8Co2iGB+kze8755L73Oc/KvodtZAX9VQF3f6r17q8ye3T33rMzAYFi 5JRdZBrYzYOFjToGnGyUpKHooAY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:cc:references:from:subject:message-id:date :mime-version:in-reply-to:content-type :content-transfer-encoding; s=default; bh=uqFiIibUTg//v7nCViTgRD nAzvg=; b=AztUo8YhO5KNqDdVQjCjMbE3tkUx0eEutJVKCxac6KhEwr07Z/acf7 nnhUKid3a/lhOihIpKprrkuGfMcLO+VZmRtdIM4iuUGUYObtnwGpkvFqbVHQx4sC LTZfY4BA6gXj2rnb0mi7F8bUY1CnKaahs+RaiplYIpqBC/NAd1ZR4= Received: (qmail 17634 invoked by alias); 23 May 2019 18:33:27 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 17464 invoked by uid 89); 23 May 2019 18:33:27 -0000 Authentication-Results: sourceware.org; auth=none X-HELO: mail-vs1-f67.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=to:cc:references:from:openpgp:autocrypt:subject:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=H1BK8aHURWQEbhGIWiHbQ/dq9catOB8jTAUiHtOm+UY=; b=ByTt5aYtKzMppjj7tVFLi7J0j97LUDoTP9hnvGGcC8lRI4bi45/0KdTXtduR2x11wz f8EgD5wJvVC6kAg0v3S5oJQoD9oa7u5cN7tKYYbNIgnvuQs3NWf4FPohC/8DaaD+D3zK VDNOs9u0TCONNBeIOTXIubZjQMqNg+Hr46c/TeN6Pz2nln1Al6Uh9hac4m/pVM9eNPYF fWO2dS9ggNvm9tI83OdKJRr69zwfDo4fgT3c/B4X2mBXv6/jaUHLUkTtI/iCRp1yYCso 5/emblnNjFpARR7rjLnY2PxiM8JmPprC4nJunFvGh5DnrOiaQae8PStISF0SsAdBFGbm o79Q== To: Florian Weimer Cc: Carlos O'Donell , libc-alpha@sourceware.org References: <20190523133048.14922-1-adhemerval.zanella@linaro.org> <77021a69-c1a9-b41a-4396-5201915e3fa3@linaro.org> <87blztulg7.fsf@oldenburg2.str.redhat.com> From: Adhemerval Zanella Openpgp: preference=signencrypt Subject: Re: [PATCH] nptl: Fix deadlock on atfork handler which calls dlclose (BZ#24595) Message-ID: Date: Thu, 23 May 2019 15:33:21 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <87blztulg7.fsf@oldenburg2.str.redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 23/05/2019 14:31, Florian Weimer wrote: > * Adhemerval Zanella: > >> We can go back to old behaviour of using a lock-free registration on the >> atfork lists, make a lock-free copy on fork, and handle the synchronization >> with __unregister_atfork using locks plus futexes. It would require all the >> old complexity of mixing lock-free algorithms with locks accesses plus using >> an unbounded alloca on fork (maybe we could just use malloc to allocate >> the backup list, my understanding it is avoid to to add another issue to >> make fork async-signal-safe). > > I think we can avoid the async-signal-safety issue (for now) by not > using locks in single-threaded mode. pthread_atfork is not required to > be async-signal-safe, so we need not worry about fork/pthread_at_fork > interactions in single-threaded processes, beyond the modification of > the handler list from the handler itself. > > Maybe we can avoid making the copy for every fork call, using some sort > of copy-on-write list? On the reader side, the protocol would look like > this: > > lock the fork handler mutex > get pointer to the dynlist head from a global variable > increment the reference counter next to the dynlist head > unlock the fork handler mutex > > perform all the fork work, traversing the list as needed > (without any locking) > > lock the fork handler mutex > decrement the reference counter > if the counter is zero, delocate the entire data structure > (list and struct containing list head plus reference counter) > unlock the fork handler mutex > > On the registration/unregistration side (pthread_atfork, > __unregister_atfork), we would do this: > > lock the fork handler mutex > get pointer to the dynlist head from the global variable > if the reference counter is 1: > modify the list in place > else: > make a modified copy of the list > update the global variable to point to it > decrement the reference counter in the original list > unlock the fork handler mutex > > (The reference counter in the quiet state would have to be 1, because > the list is referenced from the global variable.) > > I think this is still simpler than the original scheme. It also ensures > that we are not modifying the list during the traversal in fork. > Instead, we update a list that will be used for future forks. > > What do you think? The solution sounds correct, but I don't have a strong opinion if this is really an improvement over a recursive lock plus a linked list. It potentially adds 'free' calls in fork for multithread mode if list needs to be deallocated. Also, since the locks is internal to register-atfork.c we might have a better control to make the exported interfaces not deadlock.