From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb41.google.com (mail-yb1-xb41.google.com [IPv6:2607:f8b0:4864:20::b41]) by sourceware.org (Postfix) with ESMTPS id 435B4385700C for ; Sun, 20 Sep 2020 20:42:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 435B4385700C Received: by mail-yb1-xb41.google.com with SMTP id k2so8793428ybp.7 for ; Sun, 20 Sep 2020 13:42:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mLfMVJhi+Fg6s606d/w4UlPHyVw1vBgxQ19s7EvR4PE=; b=nXQd1tP9aWrBOqu8qY22nk3H8KsCJVPjqBrJNyrSuzf+t3e2RIHSsmxtpmaylAxl6A 36qHWxG6nor7Bsb4o9kpJJCE2r5PTxoPt3kS2ZNTBoOZ0VOv1oR+HwWcDi5nchwNv+HS vZTxDokWgxpRw8k3xf6P6uibvqXkxZ+PQseRVtztnM/Gnep515GIF9wwf81iOlny2TdX 69DzQWYm5/nsrTycMpqrI1E7X49LcmbaU6CavXsC5T1Y+WPgzWTtQ11eZue65iHo7OhC 9CtC1RiIgPuFrbMWhuQawJffBMXb+OcMmcBAz5BLBgagsefaviUNIyLDnOOeY3DigjnJ HzFA== X-Gm-Message-State: AOAM530jTQdc+0tqjgXxGra3m+xGHB121BFaVP5ZZ/veIpDpuj8rbbTi rQDaRKKi5TYq/gXpJfsT06By/XN5mj9PLgrBu+MU9g== X-Google-Smtp-Source: ABdhPJwDKnjm7CKw9crfVzrYp+J1X9e1lnUfihfOL3MQ/ZW3fLeRPNDkEKwVJQzwVJb1rN5hLhs3+/oSmnGmvXvf5X0= X-Received: by 2002:a25:b8cb:: with SMTP id g11mr27897256ybm.203.1600634546269; Sun, 20 Sep 2020 13:42:26 -0700 (PDT) MIME-Version: 1.0 References: <20200920095750.396908-1-vitalybuka@google.com> <20200920120957.852821-1-vitalybuka@google.com> In-Reply-To: <20200920120957.852821-1-vitalybuka@google.com> From: Paul Pluzhnikov Date: Sun, 20 Sep 2020 13:41:58 -0700 Message-ID: Subject: Re: [PATCH] stdlib: Fix data race in __run_exit_handlers To: Vitaly Buka Cc: GLIBC Devel Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-15.3 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Sun, 20 Sep 2020 20:42:28 -0000 On Sun, Sep 20, 2020 at 5:10 AM Vitaly Buka via Libc-alpha wrote: > +static void * > +threadfunc (void *unused) > +{ > + for (; done < 1e6;) > + { > + if (added < done + 100) > + { > + __cxa_atexit (&atexitcb, (void *)(++added), __dso_handle); Isn't there a data race on "added" here (in addition to a data race on "done")? What prevents two threads from observing "added == 100" at the same time and adding two calls with value of 101, which would later trigger abort() in exitcb()? > + /* With default 8MiB Linux stack size, creating 1024 threads can cause > + VM exhausiton on 32-bit machines. Reduce stack size of each thread to > + 128KiB for a maximum required VM size of 128MiB. */ This comment is far removed from the computation of kStacksize (and the name violates the naming conventions used here). I suggest: size_t stack_size = 128 << 10; /* 128KiB */ if (stack_size < PTHREAD_STACK_MIN) stack_size = PTHREAD_STACK_MIN; Also, I suspect that 32KiB would be more than enough for stack size here. > + for (i = 0; i < kNumThreads; ++i) Since kNumThreads isn't used anywhere else, I suggest making it a local: const int num_threads = 50; -- Paul Pluzhnikov