Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp778890pxb; Thu, 17 Feb 2022 14:43:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJwSW01LClpbNVrnGEG3ofMcL21p/wwifla9jHb0cup8ipDSugfuPmxu82AV+Urb1zUSj0sV X-Received: by 2002:a05:6a00:b49:b0:4cf:432f:9cd9 with SMTP id p9-20020a056a000b4900b004cf432f9cd9mr4979846pfo.10.1645137799743; Thu, 17 Feb 2022 14:43:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645137799; cv=none; d=google.com; s=arc-20160816; b=eX8Z4nCCAQA27Qo2ZICoqvuK0hTR47eejR/ojfCyLAy6QSLgl1ACfNYyWyjeYPy7cD hI9Xkftk/bEfj13nm3kadEpqz1JSW58QKjtGoMA7iSwnLxMopY887AiLFJPZMd5xtKm5 t5FcVz+ZUj5XOE2eGpnBSgLOY5DLYItrUt9Mas73JTjvRNk3kfcrIiueRSiNlYpx+so+ o5U11+aRCkASo8Eo/q822Cwlbk+au2gsN4Bap11uqRWUEF00KajdEexZHK/UGfa6ILVh 6I0YNEVvfcrHWCeFv/4gWZP57h2gdRZpXmMuQ/yoD8JTKVgDZppDzm1XrMpH4IclsQBe E8TA== 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:date:dkim-signature; bh=cuPYf0BVKR4M8TjMWt+xlzIeV3CvKgxCupKj52NpG8E=; b=D5Y827iz3RhrJ1WL0+xKVMwmZfysn0Crrh1b3G20eeoqOfxMAUPnNJPeHEJxoC3OdU K2R2PXn3NEQClU7Flpav97U4R1qgOLCCvJAw1ul7RYgKgf0sfCXneAzLNimtXvo8WslQ 2u4ek9i/wc1ofiux8jFRi9tN35AmtsROob/9alK3kMCZQ6H6Om6MmKVsavBG6Sa5kWs0 hY2oBocgHQnqt79mXdcfpONTtbB1DvXU4nFIVoNfyC6DYsL7QsEUbQYLPzT4QOpUxoPd HUrZMAkgD7QmkZIt1BzgZ/9OUst1ovkgslXKHpMJo23E12pQp7zWQYdPZV46YoGdaO6G YTHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=O0uHw0H9; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 6si1846668pjn.90.2022.02.17.14.43.03; Thu, 17 Feb 2022 14:43:19 -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=@linuxfoundation.org header.s=korg header.b=O0uHw0H9; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245188AbiBQT3d (ORCPT + 99 others); Thu, 17 Feb 2022 14:29:33 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232139AbiBQT3c (ORCPT ); Thu, 17 Feb 2022 14:29:32 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D3DE129BB2; Thu, 17 Feb 2022 11:29:17 -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 CD70A61CFC; Thu, 17 Feb 2022 19:29:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A37C5C340E8; Thu, 17 Feb 2022 19:29:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645126156; bh=kHigbTssltdpECsVACXjUmCjoYc8nJe1TohCmnF/uDk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=O0uHw0H9e1BKIXKJ2/u7FYUc5Y24CS+0asyxuvN2f7vHivb+GQMOmznT82N8OROTl ciHE8TSSR3HCcxH1cGTveCtkMFi00xnkQIuCZGRvpvN4TDSGie2q4rtWDyUu6b8fyb CpZGKfeAyYH2IRlf3sjYb6ycq76erMuPHJklVVz4= Date: Thu, 17 Feb 2022 20:29:13 +0100 From: Greg Kroah-Hartman To: Jakob Koschel Cc: Linus Torvalds , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Thomas Gleixner , Arnd Bergman , Andy Shevchenko , Andrew Morton , Kees Cook , Mike Rapoport , "Gustavo A. R. Silva" , Brian Johannesmeyer , Cristiano Giuffrida , "Bos, H.J." Subject: Re: [RFC PATCH 01/13] list: introduce speculative safe list_for_each_entry() Message-ID: References: <20220217184829.1991035-1-jakobkoschel@gmail.com> <20220217184829.1991035-2-jakobkoschel@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220217184829.1991035-2-jakobkoschel@gmail.com> X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Thu, Feb 17, 2022 at 07:48:17PM +0100, Jakob Koschel wrote: > list_for_each_entry() selects either the correct value (pos) or a safe > value for the additional mispredicted iteration (NULL) for the list > iterator. > list_for_each_entry() calls select_nospec(), which performs > a branch-less select. > > On x86, this select is performed via a cmov. Otherwise, it's performed > via various shift/mask/etc. operations. > > Kasper Acknowledgements: Jakob Koschel, Brian Johannesmeyer, Kaveh > Razavi, Herbert Bos, Cristiano Giuffrida from the VUSec group at VU > Amsterdam. > > Co-developed-by: Brian Johannesmeyer > Signed-off-by: Brian Johannesmeyer > Signed-off-by: Jakob Koschel > --- > arch/x86/include/asm/barrier.h | 12 ++++++++++++ > include/linux/list.h | 3 ++- > include/linux/nospec.h | 16 ++++++++++++++++ > 3 files changed, 30 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h > index 35389b2af88e..722797ad74e2 100644 > --- a/arch/x86/include/asm/barrier.h > +++ b/arch/x86/include/asm/barrier.h > @@ -48,6 +48,18 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, > /* Override the default implementation from linux/nospec.h. */ > #define array_index_mask_nospec array_index_mask_nospec > > +/* Override the default implementation from linux/nospec.h. */ > +#define select_nospec(cond, exptrue, expfalse) \ > +({ \ > + typeof(exptrue) _out = (exptrue); \ > + \ > + asm volatile("test %1, %1\n\t" \ > + "cmove %2, %0" \ > + : "+r" (_out) \ > + : "r" (cond), "r" (expfalse)); \ > + _out; \ > +}) > + > /* Prevent speculative execution past this barrier. */ > #define barrier_nospec() alternative("", "lfence", X86_FEATURE_LFENCE_RDTSC) > > diff --git a/include/linux/list.h b/include/linux/list.h > index dd6c2041d09c..1a1b39fdd122 100644 > --- a/include/linux/list.h > +++ b/include/linux/list.h > @@ -636,7 +636,8 @@ static inline void list_splice_tail_init(struct list_head *list, > */ > #define list_for_each_entry(pos, head, member) \ > for (pos = list_first_entry(head, typeof(*pos), member); \ > - !list_entry_is_head(pos, head, member); \ > + ({ bool _cond = !list_entry_is_head(pos, head, member); \ > + pos = select_nospec(_cond, pos, NULL); _cond; }); \ > pos = list_next_entry(pos, member)) > You are not "introducing" a new macro for this, you are modifying the existing one such that all users of it now have the select_nospec() call in it. Is that intentional? This is going to hit a _lot_ of existing entries that probably do not need it at all. Why not just create list_for_each_entry_nospec()? thanks, greg k-h