From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by sourceware.org (Postfix) with ESMTPS id 3CF403955CAC for ; Thu, 19 May 2022 15:47:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3CF403955CAC Received: by mail-oi1-x22c.google.com with SMTP id m25so6968721oih.2 for ; Thu, 19 May 2022 08:47:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=tGG9P1Hi402TUajM0HvsUgB1cKoRnOml0UcDgU5sT0E=; b=mumb6wVpgxPzQrLNVxWg4tfN6C41nnalmumbL1LVVcR37MybB83L6yiXTYrho7w++E 0xUwB69eGiKIc4Y6HRfgEr8wTDbMtzDm29H+1/HqgMQbH2yWE1I6B6TX8KNzStf/SmYg K/YRInagoIUaOhijPjbh87hWbwQaPJ44GoALAjzUtL0r6tL+dV7w0Uv/SvbGQ6GUwoB0 /q7DFS1aPq1wLOyv58nBtJA/q+O6L3kjoRSomo7BO8xhXp/jUmHZR/ACCamMttUs4ya9 1nji2InJCeYpXG7q7Jb0unG8jOeBRY37irFkBbFkYT36Xii/jHNS4FuVIv1e/E5NEMAK zBTQ== X-Gm-Message-State: AOAM532HW7hjhHlTma3JCnz+uj9xFpxgHClU98wPd6WthDh3mCwbAcaz maxQ4MD1u7v6FVjHeIEbKOKiWQ== X-Google-Smtp-Source: ABdhPJwV6oTyfmiUA0JRNWInr3kAERRcblMFKexvF+HuCEukNYeVDbZtcwFLL1+EMf/vP8EyApnp/w== X-Received: by 2002:a05:6808:300f:b0:2fa:6fd5:9723 with SMTP id ay15-20020a056808300f00b002fa6fd59723mr2729389oib.202.1652975263011; Thu, 19 May 2022 08:47:43 -0700 (PDT) Received: from ?IPV6:2804:431:c7cb:cdd6:8dac:493f:c77a:4af8? ([2804:431:c7cb:cdd6:8dac:493f:c77a:4af8]) by smtp.gmail.com with ESMTPSA id g3-20020a056870c38300b000f200873c09sm331267oao.27.2022.05.19.08.47.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 May 2022 08:47:42 -0700 (PDT) Message-ID: <7d120257-c779-ee56-932d-17738067f0ea@linaro.org> Date: Thu, 19 May 2022 12:47:39 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCH] Avoid RMW of flags2 outside lock (BZ #27842) Content-Language: en-US To: Wilco Dijkstra , 'GNU C Library' References: From: Adhemerval Zanella In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 May 2022 15:47:46 -0000 On 19/05/2022 12:10, Wilco Dijkstra wrote: > > Remove an unconditional RMW on flags2 in flockfile - if _IO_FLAGS2_NEED_LOCK > is set, we are single-threaded and we can safely clear the flag. This fixes > BZ #27842. > I don't think this is correct because if the caller issues pthread_create after flockfile, funlockfile will not issues the correct operations. I have a fix that uses a different locking mechanism where the _IO_FLAGS2_NEED_LOCK is removed by moving both the thread id and single-thread optimization to the locks itself (on Linux tid has at maximum 30-bits, we can use 1 bits for the single-thread optimization and 1 bits for congestion optimization). The issue is now requires slight more memory operations to check if you can use single-thread optimization, since the FILE lock is not always present and you need to first check __flags. I would say that with currency scheme where _IO_FLAGS2_NEED_LOCK is stick, this is a benign data race (although still undesirable). > --- > > diff --git a/stdio-common/flockfile.c b/stdio-common/flockfile.c > index a5decb450f8d477e3105d02661282afeab58f88b..7ba9ab59082d2c1dfba7d5e9a91175c9dec7ec49 100644 > --- a/stdio-common/flockfile.c > +++ b/stdio-common/flockfile.c > @@ -22,7 +22,10 @@ > void > __flockfile (FILE *stream) > { > - stream->_flags2 |= _IO_FLAGS2_NEED_LOCK; > + /* If we're single-threaded, turn off single-thread optimizations > + when locking a file. Avoid updating flags2 if multi-threaded. */ > + if (!_IO_need_lock (stream)) > + stream->_flags2 |= _IO_FLAGS2_NEED_LOCK; > _IO_lock_lock (*stream->_lock); > } > weak_alias (__flockfile, flockfile);