Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2879533rwb; Wed, 30 Nov 2022 12:09:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Y6oIggJrXl/yz5kAlzTJap7BtWrtzfcx+hA1j618Mp+C6Kaj5xvwAMq8LzEQaTAhKl82F X-Received: by 2002:a05:6402:530b:b0:461:f919:caa4 with SMTP id eo11-20020a056402530b00b00461f919caa4mr57785988edb.255.1669838982152; Wed, 30 Nov 2022 12:09:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669838982; cv=none; d=google.com; s=arc-20160816; b=GDVuMvyl9dYH/chj3c7qvI7JYcMIA2vCsc7F4IQSENGNAmpiXy3bMAjnwgRJN9Zg7t 1MsyPFwo8q7OA/hpmGtNPVjRb0yO8Mz5AoljZXYJcI5Pu/m57yUiddMf6YmABK8NGy0t 5CFLQ2InGgcaBR3q0oSsRyTkr4Q0g/qeEndCVA+l/pUxGe8pA4gU/c41eLO0Scw1i7Fh EjI3QVccbwCYYo6L+VNY/RJjbw7EaNL8vY9F0g0k4RDj5qnYf5a7rIFyzTiIoT9qjkD8 VZQTWl0/jwODRDavOociiWTxKPaEcgOaz+WUCMlCO8aPzOpF/6S256E6FFcHLt6gbuOT i4rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Cui/LxsuZ7z7QlTGbYIAupfZbeo4PSFTUr9t5PrYIgI=; b=C+PWuRmQMwffVwdwvBdzPy3Tb0E+K3nDfnjwQ5JNLv8RByMMTzDCam7WKamYQ3pAI0 QDjzI6Bk6QrPM5cSVfCxZ9qNXmjU7u9f9D/4EHakwXy6BBhnQXIldecc70HZwD8RomZv TjRkmhxFVOQISPWzS+n8dVFHzPDWfT+IYYwpMgDZOHa4TVBDHT0Qe19XjI8SUXBN9kOe kmgstaXRoXYSKmxNrnDncXvdK0IS1oGOrEE4isMNiuEkR0TyKCPQ4gs7ln5tZXNmMKlo UsxhwWEHmo94QmQK4M1MCNopl+auHL+XgwsV8pE/JsOLlUcpynKDZ9B9ynpaRleHqRVM MruQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=Iy5f+r9q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g22-20020a056402321600b0045d4b7ad162si1899246eda.546.2022.11.30.12.09.21; Wed, 30 Nov 2022 12:09:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=Iy5f+r9q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229700AbiK3TzN (ORCPT + 83 others); Wed, 30 Nov 2022 14:55:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbiK3TzL (ORCPT ); Wed, 30 Nov 2022 14:55:11 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F06D556D57 for ; Wed, 30 Nov 2022 11:55:10 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 97D6961D9F for ; Wed, 30 Nov 2022 19:55:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A39C9C433B5 for ; Wed, 30 Nov 2022 19:55:09 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="Iy5f+r9q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1669838106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Cui/LxsuZ7z7QlTGbYIAupfZbeo4PSFTUr9t5PrYIgI=; b=Iy5f+r9q97nmSCtEDuROrXHsNda3ocZILQdzOvzCtjPswpW/1XuOq179w1ekHyMZWvnEmT rIGFuUG4YF91e+dSa4P4HeGQz02dXLNcidyoUDKuy35COWq0q6yKnRV3kJdhJyfPxXiwTD n1jXo5he+4/qWIYFw1H33HlKcMsdzvI= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 23fdb4be (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Wed, 30 Nov 2022 19:55:06 +0000 (UTC) Received: by mail-ua1-f50.google.com with SMTP id n9so3286748uao.13 for ; Wed, 30 Nov 2022 11:55:06 -0800 (PST) X-Gm-Message-State: ANoB5pkNCd8PBGlZaizpp9F098xf2ERplC3A6bTe9F08W6nxMx3UDuDK q0/7IoftOwG0TBQ9hyYC1mScpk1/LrcyDirTDeI= X-Received: by 2002:ab0:7243:0:b0:3ad:4f5c:66ad with SMTP id d3-20020ab07243000000b003ad4f5c66admr36645092uap.65.1669838105535; Wed, 30 Nov 2022 11:55:05 -0800 (PST) MIME-Version: 1.0 References: <20221130020815.283814-1-Jason@zx2c4.com> In-Reply-To: From: "Jason A. Donenfeld" Date: Wed, 30 Nov 2022 20:54:54 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] random: align entropy_timer_state to cache line To: Eric Biggers Cc: linux-kernel@vger.kernel.org, Sultan Alsawaf Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Eric, On Wed, Nov 30, 2022 at 8:51 PM Eric Biggers wrote: > > On Wed, Nov 30, 2022 at 08:31:33PM +0100, Jason A. Donenfeld wrote: > > On Wed, Nov 30, 2022 at 7:59 PM Eric Biggers wrote: > > > > > > On Wed, Nov 30, 2022 at 11:04:23AM +0100, Jason A. Donenfeld wrote: > > > > > > diff --git a/drivers/char/random.c b/drivers/char/random.c > > > > > > index 67558b95d531..2494e08c76d8 100644 > > > > > > --- a/drivers/char/random.c > > > > > > +++ b/drivers/char/random.c > > > > > > @@ -1262,7 +1262,7 @@ static void __cold entropy_timer(struct timer_list *timer) > > > > > > static void __cold try_to_generate_entropy(void) > > > > > > { > > > > > > enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 15 }; > > > > > > - struct entropy_timer_state stack; > > > > > > + struct entropy_timer_state stack ____cacheline_aligned; > > > > > > > > > > Several years ago, there was a whole thing about how __attribute__((aligned)) to > > > > > more than 8 bytes doesn't actually work on stack variables in the kernel on x86, > > > > > because the kernel only keeps the stack 8-byte aligned but gcc assumes it is > > > > > 16-byte aligned. See > > > > > https://lore.kernel.org/linux-crypto/20170110143340.GA3787@gondor.apana.org.au/T/#t > > > > > > > > > > IIRC, nothing was done about it at the time. > > > > > > > > > > Has that been resolved in the intervening years? > > > > > > > > Maybe things are different for ____cacheline_aligned, which is 64 bytes. > > > > Reading that thread, it looks like it was a case of trying to align the > > > > stack to 16 bytes, but gcc assumed 16 bytes already while the kernel > > > > only gave it 8. So gcc didn't think it needed to emit any code to align > > > > it. Here, though, it's 64, and gcc certainly isn't assuming 64-byte > > > > stack alignment. > > > > > > > > Looking at the codegen, gcc appears to doing `rsp = (rsp & ~63) - 64`, > > > > which appears correct. > > > > > > Well, if gcc thinks the stack is already 16-byte aligned, then it would be > > > perfectly within its rights to do 'rsp = (rsp & ~47) - 64', right? You probably > > > don't want to be relying on an implementation detail of gcc codegen... > > > > The really pathological one would be ~48, which would just clear those > > two extra bits. I can't imagine gcc or clang ever deciding to do that. > > But I guess they could? > > > > What would you recommend here? kmalloc'ing it instead? Keeping things > > as is with ____cacheline_aligned, since this has always been broken, > > and it's not the end of the world? Something else? > > Well, other places in the kernel do the alignment manually: > > u8 __stack[sizeof(struct entropy_timer_state) + SMP_CACHE_BYTES - 1]; > struct entropy_timer_state *stack = (void *)PTR_ALIGN(__stack, SMP_CACHE_BYTES); > > It's silly, but I'm not aware of a better option. Well alright then, why not. I'll send a v2. Jason