Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1113981ybg; Wed, 23 Oct 2019 10:32:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqz56eyniP5uApnCn/6Y90/Y0j4WWDpo8w15gnIG0HIhPMvPcOvPevjsO3D/wGkyVgE+fB+d X-Received: by 2002:a17:906:7f94:: with SMTP id f20mr10520282ejr.333.1571851964782; Wed, 23 Oct 2019 10:32:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571851964; cv=none; d=google.com; s=arc-20160816; b=P/KYEPI7JvXZn5Ia4m54b0nFYsehN5p5ndzvCBdmRn7qHhF84TYkEmcU+WrMlLT1id MDNxTNeR1e5Wgll/fLBOYjRCsKBjdFxUYiXgZQQ4EnwFQ0Y968c8gyPlF8uHn7SNV6GW 2xbU5BmPVcPR52Z9Sf3MXXkUkoGtVMMVNFJ76gnf2e6fcBS2dz+jcw+8krATQ3ao2CMW anwKufsK7ssgPkYs5P0GHhAcc+nLLEFxqeAINcJLjcd7ms1ZmoshVpN5JEUUYgvJuBTQ TsjIhNN0P7+REcYz0959MyXaJ3wlTMJlOJhHmVKqpmqwkqhw4AVb43jcOgPqxA8IX7/Y jAUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=sHrU87Sp2KOFYTsvWXjU9c1Yq8vcTmrg/74YWaKD3a8=; b=xelu5WR1L7iuvtSChSZIh+6cSquws9NL5WBlPMvTUzm1ou4ESClzLqz/LAVqX3t+WT ZEV0+03a/UtRS0+Exm6GcOogSoy9CB89b15xS7zzUh8HfvPADsSTZvpmSJoRP0VSJrvu 25NodTEO9lpORavZoqmcID7PLcNQ92l9gKK/2ueoB4ALLpnl6EW0vIuddX2//jfAofFd ZVDWjmkzvbOB9mL5dLtWv50Vd8GBX3ld8SUe7qOnC7QE7KKjxz3Lbx0m8vywqQs+4jDi iiqiWUxSY69uXY0CYinTlcZyk0TNkglJvOlFI3bulISpCRj0J97drzu6ExnFIRokHq87 Z0Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=V0U8f57h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c1si4007394ejs.199.2019.10.23.10.32.20; Wed, 23 Oct 2019 10:32:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=V0U8f57h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390613AbfJWMkI (ORCPT + 99 others); Wed, 23 Oct 2019 08:40:08 -0400 Received: from mail-qt1-f193.google.com ([209.85.160.193]:40849 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732361AbfJWMkI (ORCPT ); Wed, 23 Oct 2019 08:40:08 -0400 Received: by mail-qt1-f193.google.com with SMTP id o49so24173160qta.7 for ; Wed, 23 Oct 2019 05:40:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sHrU87Sp2KOFYTsvWXjU9c1Yq8vcTmrg/74YWaKD3a8=; b=V0U8f57hbh/kqVmvb4NEPwMp/oVE2TvJJWWuT8r2xtWeb2kjEKRohU30BBj1D4O2un 4Asp50Vk4TlgIM4WOGwFVlt/TKVbThDn4wyzVG59Zg9+ehmrTo5NkWDVECEvYB/p05ij 89oRzrjxzraFKPjN92uVcsNx6zorvZm3IXBPH0CJnFkAR5xewOtEd+zm7O/1awHRUvka gzQC9ZXN4h8zvTHMuZSl/BqXLn6DFqWjwp8DRxUAQoKbeC/souX0wtOIWdofNAByg3P0 Va5RZDDIzcEaNgGF1aVCdJs+hTI8BcvIB0LhK1Gn37xmbwkx14+Hsx2pRSHXAnmh6ST1 LA9A== 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=sHrU87Sp2KOFYTsvWXjU9c1Yq8vcTmrg/74YWaKD3a8=; b=q2ITnv75okc/DSuUyuREC6RuYtkrR1p6IqOri+QArpp7EovilEx7ZPLLmDReGcEBSV QHQMfaFVxt9/l+ydkzxWcuZhZvvPBiGGsk/pLr5BC60uQXbEZgv2VCYRTbM0fYiGzTTY WTgt79JwSCU5vsdmKmGpv4pg6BzAxFcuFSgiQoXrSWlx1muINJRy6v6Yzl9/CLZezxEl FZv3k+aZBLNd46M1Hs7FjoCrJK/wj5tUvTwrapvyVw581xyjQ+bODx1m38qkCEjyIESL 0aZ/cNKziL+XLi9vq5P4mjoyE2G5K2qBq0SgGfYqq6yY51WPNZ//wLmVqlR2vIvBhHYB 1cIA== X-Gm-Message-State: APjAAAW2susggNs+JzbPvrExx+ahjhU6gZlYe+C7KOyNQROEdT/2VgCf ivJrAZ/M5TyigJrZ4WWB1yn4VDRC7ye9fMwP/scyyNtU X-Received: by 2002:a0c:fec3:: with SMTP id z3mr8670778qvs.122.1571834406888; Wed, 23 Oct 2019 05:40:06 -0700 (PDT) MIME-Version: 1.0 References: <20191009114809.8643-1-christian.brauner@ubuntu.com> <20191021113327.22365-1-christian.brauner@ubuntu.com> <20191023121603.GA16344@andrea.guest.corp.microsoft.com> In-Reply-To: <20191023121603.GA16344@andrea.guest.corp.microsoft.com> From: Dmitry Vyukov Date: Wed, 23 Oct 2019 14:39:55 +0200 Message-ID: Subject: Re: [PATCH v6] taskstats: fix data-race To: Andrea Parri Cc: Christian Brauner , Will Deacon , LKML , bsingharora@gmail.com, Marco Elver , stable , syzbot , syzkaller-bugs Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 23, 2019 at 2:16 PM Andrea Parri wrote: > > On Mon, Oct 21, 2019 at 01:33:27PM +0200, Christian Brauner wrote: > > When assiging and testing taskstats in taskstats_exit() there's a race > > when writing and reading sig->stats when a thread-group with more than > > one thread exits: > > > > cpu0: > > thread catches fatal signal and whole thread-group gets taken down > > do_exit() > > do_group_exit() > > taskstats_exit() > > taskstats_tgid_alloc() > > The tasks reads sig->stats without holding sighand lock. > > > > cpu1: > > task calls exit_group() > > do_exit() > > do_group_exit() > > taskstats_exit() > > taskstats_tgid_alloc() > > The task takes sighand lock and assigns new stats to sig->stats. > > > > The first approach used smp_load_acquire() and smp_store_release(). > > However, after having discussed this it seems that the data dependency > > for kmem_cache_alloc() would be fixed by WRITE_ONCE(). > > Furthermore, the smp_load_acquire() would only manage to order the stats > > check before the thread_group_empty() check. So it seems just using > > READ_ONCE() and WRITE_ONCE() will do the job and I wanted to bring this > > up for discussion at least. > > Mmh, the RELEASE was intended to order the memory initialization in > kmem_cache_zalloc() with the later ->stats pointer assignment; AFAICT, > there is no data dependency between such memory accesses. I agree. This needs smp_store_release. The latest version that I looked at contained: smp_store_release(&sig->stats, stats_new); > Correspondingly, the ACQUIRE was intended to order the ->stats pointer > load with later, _independent dereferences of the same pointer; the > latter are, e.g., in taskstats_exit() (but not thread_group_empty()). How these later loads can be completely independent of the pointer value? They need to obtain the pointer value from somewhere. And this can only be done by loaded it. And if a thread loads a pointer and then dereferences that pointer, that's a data/address dependency and we assume this is now covered by READ_ONCE. Or these later loads of the pointer can also race with the store? If so, I think they also need to use READ_ONCE (rather than turn this earlier pointer load into acquire). > Looking again, I see that fill_tgid_exit()'s dereferences of ->stats > are protected by ->siglock: maybe you meant to rely on such a critical > section pairing with the critical section in taskstats_tgid_alloc()? > > That memcpy(-, tsk->signal->stats, -) at the end of taskstats_exit() > also bugs me: could these dereferences of ->stats happen concurrently > with other stores to the same memory locations? > > Thanks, > Andrea