From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 143DEC1975A for ; Tue, 17 Mar 2020 23:38:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C08B420714 for ; Tue, 17 Mar 2020 23:38:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="KG4WwadS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C08B420714 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 525586B0003; Tue, 17 Mar 2020 19:38:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D60E6B0006; Tue, 17 Mar 2020 19:38:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C47A6B0007; Tue, 17 Mar 2020 19:38:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 255206B0003 for ; Tue, 17 Mar 2020 19:38:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 03F2E5DD3 for ; Tue, 17 Mar 2020 23:38:53 +0000 (UTC) X-FDA: 76606471704.20.stove82_306b92641c618 X-HE-Tag: stove82_306b92641c618 X-Filterd-Recvd-Size: 4540 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Tue, 17 Mar 2020 23:38:52 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id m15so12586340pgv.12 for ; Tue, 17 Mar 2020 16:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=6NAvSqQjAH+KyV63wCW3yxaeT17M/3Lw10RhwB3fk+A=; b=KG4WwadSjpBKcYESb0EmTFojqQMjRG0w6Jmzlf3HlZmFnip439Iarc/+80AFww/aeR l6oHVBBYQuSB5kZCnXRsCiiG5tw+QT/rJyzGBQYCnF5F7YBEqsxEQ1u3Gawx91OCdB03 GytUR7DTUfbqccSpM9X6DzAn8KFUx9gOJ071Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=6NAvSqQjAH+KyV63wCW3yxaeT17M/3Lw10RhwB3fk+A=; b=rwc6h4pnSewZY4PUVw3x0MBCJnHpeEhqP8uTXFFoGDQXV69GthwqE8PIPPevZfC6NN mXE5QCAxIS91czGzSxsD5tx+caSR7+VLjbuSBUGNrJEDZMs9opVVvo+Mr8f7c6G6Nj0B q04W1eiCzJ1JtAXQKfzkoE7PeQagDFpCtpwJ9a8uGfpmwAS3Zoin2CeY3SZcGAwKRMK5 fdQhygVnWCrE7eiE5QLSBCTHzSzZ6YRtPT4D8d3ojcgDj7kGNSW0PjvMc8PCRgZXoLcH 79I5w0/2QYNJCD/Boy5e9WL/APYhB5qoLFWD36XHxjdMaKoN2pv2iWyFgfwTg62/mioP X7AQ== X-Gm-Message-State: ANhLgQ3Cq1RfqvO/Cr8a1JM999C6Fh8w07L/M7bo778Rbd2bYafx9+2W JOKQ+vc/CRdILi4Y4jfOY4+G3Q== X-Google-Smtp-Source: ADFU+vuOuOvkqwuNEgUcl1SHuVtmY9LU2Ud+h8LwDktqQo8kr+bzM7pLkUG9gfUJChPqCYwi6mkgkg== X-Received: by 2002:a62:b518:: with SMTP id y24mr1180904pfe.261.1584488331378; Tue, 17 Mar 2020 16:38:51 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id z63sm4193757pfb.20.2020.03.17.16.38.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Mar 2020 16:38:50 -0700 (PDT) Date: Tue, 17 Mar 2020 16:38:49 -0700 From: Kees Cook To: George Spelvin Cc: Dan Williams , linux-mm@kvack.org, Andrew Morton Subject: Re: [PATCH] mm/shuffle.c: optimize add_to_free_area_random() Message-ID: <202003171619.23210A7E0@keescook> References: <20200317135035.GA19442@SDF.ORG> <202003171435.41F7F0DF9@keescook> <20200317230612.GB19442@SDF.ORG> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200317230612.GB19442@SDF.ORG> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000009, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Mar 17, 2020 at 11:06:12PM +0000, George Spelvin wrote: > The most serious is that if two threads simultaneously observe > rand_bits == 1, but do their decrements separately, you end up > with rand_bits = 255 and you generate 255 consecutive 0 bits > before refilling the buffer. > > Since we're only generating random bits, a screwed-up answer occasionally > doesn't really matter (like the comment says "lack of locking is > deliberate"), but 255 screwed up bits is a bit much. Okay, I'm on board! :) Thanks for spelling this race out; I hadn't seen quite how nasty it could get. (Perhaps mention in the commit log for v2?) > I avoided changing the underlying locking model because I didn't > feel up to such an invasive change; I managed to fix the problems > I saw without going there. And shrink the code; tht seemed like > enough of a win to justify it to me. Fair enough. > The compiler is allowed to (in John Woods' memorable explanation) > produce code that makes demons fly out of your nose. (More plausibly, > it may simply crash.) So one thing that I see here that is still in the nasal demon realm is that the left-shift of a signed value, which is technically undefined behavior in C. (See the comment on check_shl_overflow().) Doing a signedness check is very cheap in the resulting machine code; but I suspect sticking to unsigned and reversing direction for a bottom-bit test too bad? i.e.: static unsigned long rand_bits; unsigned long r = READ_ONCE(rand_bits), rshift = r >> 1; if (unlikely(rshift == 0)) { r = get_random_long(); rshift = (r >> 1) | (0x1UL << (BITS_PER_LONG - 1)); } WRITE_ONCE(rand_bits, rshift); if (r & 1) add_to... else add_to...tail -- Kees Cook