Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2130890iof; Tue, 7 Jun 2022 20:46:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQh7Q4oa84SQwx5J5PVurGoPhnlfgeCW80wQ3N9M2RDSNwiPp9V55ZKwQPnPzowJNWyZDj X-Received: by 2002:a17:902:f641:b0:15f:21f8:92a1 with SMTP id m1-20020a170902f64100b0015f21f892a1mr33256486plg.56.1654659993951; Tue, 07 Jun 2022 20:46:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654659993; cv=none; d=google.com; s=arc-20160816; b=Vjgrnwywuezr1V3RT6VsrA0hqP9zlpQm2OYietD1YxBfaDZxdhOopyW8Lp7AVuoPMS xoqU+lLMeVWMouOYM/1IWfRec7eLaAQxXS+wauvVO5PAp29UVoKprq/2I+hPRRiGsTSQ 4txjLmQMiqAJQBy1Is1ou0oF1ejO7SDDt1PRa7VluQPsOPjt+l5CRtZ7OOu4ySW5Ggym tLlOy49hDajEu08kGUhz0GjXjYErKwm3pIrJoFmtolna9qk73RlXq4gNY81PdHWe4fuL 9YA7YcV2Yy6j/49/1LjeZiSSxDXvxL5B1ub9z8gKHimJLR13fhhbTrghBdwnD6bPy105 b0Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=x4IJbeSPwqHNNzv96I29/orNZTpYQPQuyaBORkJUAzg=; b=iqFIw7K2SbUe+acUTLaheiUYMABNTKYCHpAYikP3fijmFjHLE44D3mTBvsk5AjPFTM ryrhwvRfSJeg10TD19SXHcfgDK5oXOdqNRt4AlMTJcgEYDF+unPrZ8gEbo5IB4cyOTWH TSwTbTs8yJ7OGDS4jOmXGTz2XiElkeRX0ogInmcqp+xWjCllMa2djjmQYmrMCJM/0+UE 19KIbT6S8VuhLpsko1kLqKtNcOfdfdgFhdF6LcunMWLMG2BlpTjZEZkIVWYzqiQPxNK3 UfI1fGQt5neWGNaz5uEJpeodPyiRkGOlNaq6s0qPEVnntMmvr/0cXRnV1iaw/gHm+KOL r3QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XlTRtdAk; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id k3-20020a17090a3e8300b001e070f1ddabsi26661925pjc.65.2022.06.07.20.46.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 20:46:33 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XlTRtdAk; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6CEA136E7B5; Tue, 7 Jun 2022 20:07:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244882AbiFGNoI (ORCPT + 99 others); Tue, 7 Jun 2022 09:44:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244875AbiFGNoA (ORCPT ); Tue, 7 Jun 2022 09:44:00 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5D24AE25E for ; Tue, 7 Jun 2022 06:43:57 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id a15so15722678wrh.2 for ; Tue, 07 Jun 2022 06:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=x4IJbeSPwqHNNzv96I29/orNZTpYQPQuyaBORkJUAzg=; b=XlTRtdAks6FyNUJd2ckhu1c5pagq2qIDjS5WoYMfF8F/GzNkTOGIRee+NWovA3egqG FLKHASNpirn/UlLG3ZAyDRDYJRzLIoqPa2VOSQsO/pnUIRKTo2FatbwU5AN+8a98G+bI rILYCsPCDfbhcAaQTDUz6Z0bpVEwrjjd678IOahbAX1ktBr2VBSVvT4Q22Zq/oJ0gIvB gLFBcHndCp7mXuXnrTrJ5ol0TaqYaNK1yM+q+L+MLEFH7U9tjlgO9MMK7/IQqfI6vNk3 XKf5sJfrUYjASIsJfQuT+NOtyQD4MTgpt1oQl38IcOdgvDLq5htBD62P6yCDZtdjrpI/ Nk9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=x4IJbeSPwqHNNzv96I29/orNZTpYQPQuyaBORkJUAzg=; b=dyF+772QfauW6lIvE6G0kO5ZZRPIcpZWopSmSbL5bl+lWh70MfaaSRz1+QBBHZKAqW 1mAMaJxmcDi0jtiOQ9cFOLQRvmheRCyBO0niRywmNh4Cjy7sdwoMMhe4cAOTku95XZwo rDScZhZt2ucjK9DNFPSw1ofQdvjQaA40gPPe4QmMczWcFebpyI5mkCv7sz8sN+wHvPcU KC9OMpTINBHh2bHTJvjW+AspOSbVfT44BS1lsj5Qm0TXguttsaOm1D/+CYrb5A8xAfkb XCgDB4YVKwaW3IJfmEobXilCAYpsS98QKA/E77RW2HhbGHVLIoZXjKTtro3DA+IPmEoA Te0A== X-Gm-Message-State: AOAM532pbBABljZQeEqj7PC3neM+vOJNog3CDcKyoFnR3zatqhwfNsvg 9Rk0cjC6wUP0CB/552DKue6ofg== X-Received: by 2002:adf:eb42:0:b0:20f:ebc5:cb0f with SMTP id u2-20020adfeb42000000b0020febc5cb0fmr27945814wrn.355.1654609436139; Tue, 07 Jun 2022 06:43:56 -0700 (PDT) Received: from elver.google.com ([2a00:79e0:9c:201:cd40:f1bd:cd2c:953c]) by smtp.gmail.com with ESMTPSA id k2-20020a5d5182000000b0020c5253d8fcsm17787769wrv.72.2022.06.07.06.43.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 06:43:55 -0700 (PDT) Date: Tue, 7 Jun 2022 15:43:49 +0200 From: Marco Elver To: Alexander Lobakin Cc: Arnd Bergmann , Yury Norov , Andy Shevchenko , Richard Henderson , Matt Turner , Brian Cain , Geert Uytterhoeven , Yoshinori Sato , Rich Felker , "David S. Miller" , Kees Cook , "Peter Zijlstra (Intel)" , Borislav Petkov , Tony Luck , Greg Kroah-Hartman , linux-alpha@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/6] bitops: define gen_test_bit() the same way as the rest of functions Message-ID: References: <20220606114908.962562-1-alexandr.lobakin@intel.com> <20220606114908.962562-4-alexandr.lobakin@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220606114908.962562-4-alexandr.lobakin@intel.com> User-Agent: Mutt/2.1.4 (2021-12-11) X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 Mon, Jun 06, 2022 at 01:49PM +0200, Alexander Lobakin wrote: > Currently, the generic test_bit() function is defined as a one-liner > and in case with constant bitmaps the compiler is unable to optimize > it to a constant. At the same time, gen_test_and_*_bit() are being > optimized pretty good. > Define gen_test_bit() the same way as they are defined. > > Signed-off-by: Alexander Lobakin > --- > include/asm-generic/bitops/generic-non-atomic.h | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/include/asm-generic/bitops/generic-non-atomic.h b/include/asm-generic/bitops/generic-non-atomic.h > index 7a60adfa6e7d..202d8a3b40e1 100644 > --- a/include/asm-generic/bitops/generic-non-atomic.h > +++ b/include/asm-generic/bitops/generic-non-atomic.h > @@ -118,7 +118,11 @@ gen___test_and_change_bit(unsigned int nr, volatile unsigned long *addr) > static __always_inline int > gen_test_bit(unsigned int nr, const volatile unsigned long *addr) > { > - return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); > + const unsigned long *p = (const unsigned long *)addr + BIT_WORD(nr); > + unsigned long mask = BIT_MASK(nr); > + unsigned long val = *p; > + > + return !!(val & mask); Unfortunately this makes the dereference of 'addr' non-volatile, and effectively weakens test_bit() to the point where I'd no longer consider it atomic. Per atomic_bitops.txt, test_bit() is atomic. The generic version has been using a volatile access to make it atomic (akin to generic READ_ONCE() casting to volatile). The volatile is also the reason the compiler can't optimize much, because volatile forces a real memory access. Yes, confusingly, test_bit() lives in non-atomic.h, and this had caused confusion before, but the decision was made that moving it will cause headaches for ppc so it was left alone: https://lore.kernel.org/all/87a78xgu8o.fsf@dja-thinkpad.axtens.net/T/#u As for how to make test_bit() more compiler-optimization friendly, I'm guessing that test_bit() needs some special casing where even the generic arch_test_bit() is different from the gen_test_bit(). gen_test_bit() should probably assert that whatever it is called with can actually be evaluated at compile-time so it is never accidentally used otherwise. I would also propose adding a comment close to the deref that test_bit() is atomic and the deref needs to remain volatile, so future people will not try to do the same optimization. Thanks, -- Marco