Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp654127pxb; Thu, 17 Feb 2022 11:42:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJxmUXEDJyPkK8itxoITlw+hELTOuKAUWQF1ionKbyEzqEYktgeuq+ty71pQ04fNIUfQnxiW X-Received: by 2002:a17:902:a608:b0:14d:880e:20ed with SMTP id u8-20020a170902a60800b0014d880e20edmr4220148plq.119.1645126958229; Thu, 17 Feb 2022 11:42:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645126958; cv=none; d=google.com; s=arc-20160816; b=sIBAWPe9Tybw3fiT/0F6yYkcCrWTIwIwTRVojMktYpYFfnt2YsIA/94wRJlnv3gDC2 oeP8AjtfUZsjeRtJzxTj7pGhmH0NJCsQky79VbC72Q+csuCWHO9tV9QRYy8f7q/xKRhR CBDoeI49BQV32bVpBHcYHQvXwgW69ftaESD25QwVhAoj4kpDu71xOEuFK/6XejRNgI9l ajgGIgYi3bmlHGFzV1PxiyMjUVPXr6UwzsgGP3nontT44BgOMjH1FrzHnxJexG2iRc/U u22wM+nnDsaSi4E+T89BfzhsFmrUf40+9uC/2JDyRJI9LF+lph9V4yyOws7W6pa6xNme dwKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature :dkim-signature:date; bh=g46W2ujMzZ0SbZmNOAXSHQ2Kv0SmQ9Y1FWBTwikBicA=; b=m6H5zmw3IMAnaUMbqiOv81JX8JgRIn4XVvFJJz+K4DgZQWkezw4u3pbaXuLB+In9DF oHvB6flfrx3osqU7LutlxFlR+AkljtYamy4vg6TyzUcQHpDN0e/rrDu8THvmUFupoAKr AscFrxdfWB4aZcGRfaa+WIf7Jpky8aN4wluIKHNbZko4vWu2+pOwJWir11BHAD35rO9D CPlzT0X83peNlinLwGKNSh5uffLJ5xBV+NbrLfCWCEzL+SpXkREH3XL7U5AagyR9aUF1 gJhY9DST//ZrY7EIX7BqPI2aYkf+fa+ZG4yoQgdtTkZwhHW/W+24pckRORBoxW1MMF/J EGIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wRZEerD+; dkim=neutral (no key) header.i=@linutronix.de; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 132si8972450pga.257.2022.02.17.11.42.22; Thu, 17 Feb 2022 11:42:38 -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=@linutronix.de header.s=2020 header.b=wRZEerD+; dkim=neutral (no key) header.i=@linutronix.de; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243735AbiBQRd2 (ORCPT + 99 others); Thu, 17 Feb 2022 12:33:28 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:43852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232550AbiBQRd0 (ORCPT ); Thu, 17 Feb 2022 12:33:26 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 087A515F353 for ; Thu, 17 Feb 2022 09:33:11 -0800 (PST) Date: Thu, 17 Feb 2022 18:33:07 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1645119188; 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=g46W2ujMzZ0SbZmNOAXSHQ2Kv0SmQ9Y1FWBTwikBicA=; b=wRZEerD+adqSR/+xcb9EvZcSBM/QVOiCFzR7BoBpPdygu+sGYrXbnnOPidqSLhnw/qGxu0 uRIRqBQDjcC2GRl7MuYdoo1lKa4lSdPu9xowRDXjwYTx6MKi6p/vFhCXOMoyNPd56v0poe WEh1H7BP0/0pQvL8DRMup0hFeIE/FEmH08hJh+XPG96j5XPCPjlWw1A2nBYiNriMNQC2Gv DeHdEhu2fSs7ZMAD767TyKd4IOOddTvzG0EP09Y1If5MJeohKmTuezWSNYMtluzfv9DamJ xnTse5y7x3Jb/oPS1XSjaUn72laui2gFJewyGCQRy5lhUSXZ3Jm09UfiK1FRQA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1645119188; 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=g46W2ujMzZ0SbZmNOAXSHQ2Kv0SmQ9Y1FWBTwikBicA=; b=lcoMp3hftWA+yXVSqoHe+fD5HGlbPTHdTHa10wrGiw6nhwwCJlz2TSWoTd2EDwYhV1heiu a2HgKToD99OgJgDQ== From: Sebastian Andrzej Siewior To: "Jason A. Donenfeld" Cc: Jann Horn , Andy Lutomirski , Boqun Feng , Will Deacon , Peter Zijlstra , Ingo Molnar , Waiman Long , Sultan Alsawaf , Theodore Ts'o , Andy Lutomirski , Jonathan =?utf-8?Q?Neusch=C3=A4fer?= , LKML , Thomas Gleixner Subject: Re: [PATCH v3] random: remove batched entropy locking Message-ID: References: <20220204155142.56419-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On 2022-02-16 21:58:14 [+0100], Jason A. Donenfeld wrote: > > Why raw_cpu_ptr? include/linux/percpu-defs.h says about raw_*() operations: > > > > * Operations for contexts where we do not want to do any checks for > > * preemptions. Unless strictly necessary, always use [__]this_cpu_*() > > * instead. > > > > So when I see a raw_*() percpu thing, I read it as "it is expected > > that we can migrate after this point (or we're in some really weird > > context where the normal context check doesn't work)". Is that > > incorrect? > > If it says "contexts where we do not want to do any checks for > preemptions", then that would apply here I would think? We're taking a > local lock, which means afterwards there are no preemptions. For > context, the code that got committed after Sebastian's final review > is: > > local_lock_irqsave(&batched_entropy_u32.lock, flags); > batch = raw_cpu_ptr(&batched_entropy_u32); > > However, I think most other code uses this_cpu_ptr() instead? So not sure. It depends what you are looking for. raw_cpu_ptr(&batched_entropy_u32) give you the pointer to &batched_entropy_u32 of _this_ CPU - the CPU you are currently running. this_cpu_ptr() does the same except that it has smp_processor_id() in it. smp_processor_id() will yell at you (given you enabled CONFIG_DEBUG_PREEMPT) if the code can migrate to another CPU. So it will yell at you unless: - you disable preemption / migration so code can't migrate - you run on a per-CPU thread i.e. a thread which is bound to a single CPU and therefore can't migrate to another. The local_lock_irqsave() acquires a lock / disables interrupt and therefore can't migrate. So I suggested to use raw_cpu_ptr() as an optimisation in the debug-case. If you disable preemption before accessing a per-CPU variable with this_cpu_ptr() then you _need_ to ensure that nobody is accessing the variable from in-IRQ context. Nobody will yell at you. But if you use a local-lock then you get lockdep annotation and lockdep will complain if you use that variable always in process context and sometimes in-IRQ. > Jason Sebastian