Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5349184ybi; Wed, 12 Jun 2019 00:42:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqw12Z/GdNDpYzdNvQ8NOEqCXBqQAxGdTN8+2Ho0UBIJ6Yaw3HOmM45PC9hmbkWTGlUIXJZm X-Received: by 2002:a17:902:1125:: with SMTP id d34mr8094454pla.40.1560325358240; Wed, 12 Jun 2019 00:42:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560325358; cv=none; d=google.com; s=arc-20160816; b=CJHCpcdfp7J4oYZEFR/NWO6vyqktDccYVvzxOOBFvO6cMVRWhX5U8Uix9XwEcGSnJV suq6/koIusktrroiM41dbJmWvKXdp4w+K9Y4ySsCeT7krCN2Dy9jMaSUWmiqYaNPzsts aizqDitah0QxEx21BsvSlPR64xRrlhmARadFPxCVJ+/rrYsxh2LfaRg6p0gOdc6uOXOl i8Id2dDVsRd4LrqZ91o9yYgzfhzWQ31u6nEz/JxLYvFBrsj0p80SpPHRxcKL/G0Jkds2 sInmMZL5zWN33P7d0ocXUrRrKHJ2VCoJvK/p1ObM7zmeaJIHdH/ud3XuDNTKY0on6B6a zC9A== 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; bh=Q1Qi48oLh9iihq2LkIgrDslGg/Vx8JGXFp2UzVtaaTY=; b=tlQUoXvPxxQuNk13qX/tHGJEt+8J/7xVxhCVWh2Mfaq94sNBuoahGUHYgfUd9ZDqoJ DofeGw8RfPplc+eSGYn0FiOUgjr/vkSaBiMONlGCiniovDETS/H77qtyCxVRNuDIrWK8 /ROSHjdIUfz+TQ732RdOyY1CNsa0RgfXmYhp1Cdxat6LcL/FZnv0y92Rmjw8KyYDdHoV KMJOvB6vykEtVhi1vPIglwwvspSnund9Nt4E0ookS18unu+5APFjtoOW/EsS26Pxy1ji W83pCP8kX5jLSNndVpbq1C0LHch7s+ZOsHr8CFqsdoH8ltfwK8YQWOaXh0TPRUqSqkyI 4t1Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y34si15704916plb.186.2019.06.12.00.42.23; Wed, 12 Jun 2019 00:42:38 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2408540AbfFLC44 (ORCPT + 99 others); Tue, 11 Jun 2019 22:56:56 -0400 Received: from mail-qk1-f170.google.com ([209.85.222.170]:32942 "EHLO mail-qk1-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406516AbfFLC4z (ORCPT ); Tue, 11 Jun 2019 22:56:55 -0400 Received: by mail-qk1-f170.google.com with SMTP id r6so9091509qkc.0 for ; Tue, 11 Jun 2019 19:56:55 -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=Q1Qi48oLh9iihq2LkIgrDslGg/Vx8JGXFp2UzVtaaTY=; b=GrS5VQHHEPqZSj0SSqWX+li2jU9LgQ/XmJrzBwJgBXd308sYmDYSLhN7BlIJ+pWUt6 1LijH6Xf62AENQPgWYxgqRvsEz3CKDdvi3ko+tiKHYmMjwnjXiQEDJRnNIFCHL0cm1WW F8aADtDa8UDYiT88ZkkXLiy9HqrXbIYDTgzXI0vsl70VbFNVHMPKVf4EDDDoK8lmYEuN MJ2zngmTA9m2bh5osbmY/MObMZeqsgbZTsxFd7GtgRJAtqpE2x7NPFElscOL48Thkleu NTXmi3Jj7IbFMWb+LvihMeP1BQvezJ5xEENmTeqBJ6jI1ZwaHs6aeL6O0M1VjkciZn8k /fsQ== X-Gm-Message-State: APjAAAXaCrtQCnQd/iAXZ8noGhu6pBJg3yH03uIJTdBr3fMXfVdZsOeA GJOdv0jcBaYw/rR8+Hm5zjWJRGnnly/DtBUOOQfdqQ== X-Received: by 2002:a37:6708:: with SMTP id b8mr61258505qkc.141.1560308214695; Tue, 11 Jun 2019 19:56:54 -0700 (PDT) MIME-Version: 1.0 References: <70cda2a3-f246-d45b-f600-1f9d15ba22ff@gmail.com> <87eflmpqkb.fsf@notabene.neil.brown.name> <20190222211006.GA10987@redhat.com> <7f0aeb7b-fdaa-0625-f785-05c342047550@kernel.dk> <20190222235459.GA11726@redhat.com> <20190223024402.GA12407@redhat.com> In-Reply-To: From: John Dorminy Date: Tue, 11 Jun 2019 22:56:42 -0400 Message-ID: Subject: Re: block: be more careful about status in __bio_chain_endio To: Mike Snitzer Cc: Jens Axboe , NeilBrown , linux-block@vger.kernel.org, device-mapper development , Milan Broz , Linux Kernel Mailing List 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 Having studied the code in question more thoroughly, my first email's scenario can't occur, I believe. bio_put() contains a atomic_dec_and_test(), which (according to Documentation/atomic_t.txt), having a return value, are fully ordered and therefore impose a general memory barrier. A general memory barrier means that no value set before bio_put() may be observed afterwards, if I accurately understand Documentation/memory_barriers.txt. Thus, the scenario I imagined, with a load from inside __bio_chain_endio() being visible in bi_end_io(), cannot occur. However, the second email's scenario, of a compiler making two stores out of a conditional store, is still accurate according to my understanding. I continue to believe setting parent->bi_status needs to be a WRITE_ONCE() and any reading of parent->bi_status before bio_put() needs to be at least a READ_ONCE(). The last thing in that email is wrong for the same reason that the first email couldn't happen: the bio_put() general barrier means later accesses to the field from a single thread will freshly read the field and thereby not get an incorrectly cached value. As a concrete proposal, I believe either of the following work and fix the race NeilB described, as well as the compiler (or CPU) race I described: - if (!parent->bi_status) - parent->bi_status = bio->bi_status; + if (bio->bi_status) + WRITE_ONCE(parent->bi_status, bio->bi_status); --or-- - if (!parent->bi_status) - parent->bi_status = bio->bi_status; + if (!READ_ONCE(parent->bi_status) && bio->bi_status) + WRITE_ONCE(parent->bi_status, bio->bi_status); I believe the second of these might, but is not guaranteed to, preserve the first error observed in a child; I believe if you want to definitely save the first error you need an atomic.