Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3664801imu; Tue, 18 Dec 2018 02:03:09 -0800 (PST) X-Google-Smtp-Source: AFSGD/X+m7WmR/WEuhv5SDzDIsyZXNvgwF1vmrwWCJLiqPvXrsM5Bfhlje7lAhwEG5m+rzP2E20w X-Received: by 2002:a62:220d:: with SMTP id i13mr15950040pfi.162.1545127389029; Tue, 18 Dec 2018 02:03:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545127389; cv=none; d=google.com; s=arc-20160816; b=KDchgV9iH9vLgRth2OwP5iXTxlu1f1uXUYeryTYAp/kzrvFNMbS3zQLaFJHSC1OGqD a68+GyYtbsR+GbxKugEnTM95FayiCMPjgg6q+33fifafxdmxnx/+NdLbWh3Cq9FD4jmT defrU7FBY3mcZ1qci/UnyV3zGbSk+oA0uGnEDp2uBJ0OXCMl8/jnu3MGsjS/WeNyo7+i 3FnVjepdY+0AeTt/4y4FN+2rq4Sa9cQq/PkzzRwTvYz5FHthfqdDQyu/vhiQmqNG9tit 2XyVc3ciOYUrvELJlHjEO72auxvAdp1mFRg8QWZcklblwjqTKy/USZ4/yWGayRLc2MAr xFkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=sAPOe0gM5iUG74dB+h9n+e8IQvrL8+xWqxnhfJm5eMg=; b=aItJjf6Ze4LyLg0PIo8dWqdwAbWdF6kkaeofsbb2GbYGQ/rBQtBZK+grckAx8oF3BJ BRBf2NzVrsXQxeYM6gWu/CjGtDh7O8CSnpmcD4BfMxNbr376xyFETp/aXd6V1XE284SV DZD1tWJAhoo8gdhyyu0GOP/sahel8LRgZjRYqmaqXd8ercEGdndRpGNd418cNC1TtAd3 0+It3U6KLMzabYQA3/aqZBF97rZkvBlAKFzrCNJJwCXjTNtL9XRq7W1OjuG+DrhFdD6j iX0MTaDx1EftSVNTYoaDQF37xMv6VEsIr96SIS1YdnxRqB208NWVyl7HCCK0rhuqtpua +31w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=hUZUNWu3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u28si12997696pgn.436.2018.12.18.02.02.53; Tue, 18 Dec 2018 02:03:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=hUZUNWu3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726516AbeLRKAf (ORCPT + 99 others); Tue, 18 Dec 2018 05:00:35 -0500 Received: from merlin.infradead.org ([205.233.59.134]:35996 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726476AbeLRKAd (ORCPT ); Tue, 18 Dec 2018 05:00:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=sAPOe0gM5iUG74dB+h9n+e8IQvrL8+xWqxnhfJm5eMg=; b=hUZUNWu33qeudHIzgIz9J5nEI w+AkybM2olseab72QSAhNbQzwpHIS/8xtvqBlhxubfLhqi8aZHfwONuriSaQ83aaefy8S6+zwALem DNDzaS5ahD9jkcN5Hq4d+jht6WKLOIvE+vnW6Sh4jpN/WOOmXN3QChX0WLAs+MYaQ2I8YwQcpFCqI 56LgRx9nc2Z+pSWFCd78vOJk5C5LfGan3/8+9we6oa0Jd6jFswh1Hwe71LJ2ES32gBdSHmiX9mw8W 7TCB1RXVBvVX+ox0LDH4eaFsjbUeVCW1rDX47kDczTBJnp0D8wfWU+ULZ8yaBD4Qlyj0wC0Vv9qjn LQMfsZkWg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1gZCAk-0007E3-EF; Tue, 18 Dec 2018 10:00:18 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6FE332027DA8E; Tue, 18 Dec 2018 11:00:14 +0100 (CET) Date: Tue, 18 Dec 2018 11:00:14 +0100 From: Peter Zijlstra To: Andi Kleen Cc: Arnd Bergmann , Nicolas Pitre , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Paul McKenney , Will Deacon , Ingo Molnar , Thomas Gleixner Subject: Re: [PATCH 0/7] ARM: hacks for link-time optimization Message-ID: <20181218100014.GA16284@hirez.programming.kicks-ass.net> References: <20180220215954.4092811-1-arnd@arndb.de> <20181217225020.GA16520@hirez.programming.kicks-ass.net> <20181218000800.GB25620@tassilo.jf.intel.com> <20181218091824.GI2218@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181218091824.GI2218@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 18, 2018 at 10:18:24AM +0100, Peter Zijlstra wrote: > In particular turning an address-dependency into a control-dependency, > which is something allowed by the C language, since it doesn't recognise > these concepts as such. > > The 'optimization' is allowed currently, but LTO will make it much more > likely since it will have a much wider view of things. Esp. when combined > with PGO. > > Specifically; if you have something like: > > int idx; > struct object objs[2]; > > the statement: > > val = objs[idx & 1].ponies; > > which you 'need' to be translated like: > > struct object *obj = objs; > obj += (idx & 1); > val = obj->ponies; > > Such that the load of obj->ponies depends on the load of idx. However > our dear compiler is allowed to make it: > > if (idx & 1) > obj = &objs[1]; > else > obj = &objs[0]; > > val = obj->ponies; > > Because C doesn't recognise this as being different. However this is > utterly broken, because in this translation we can speculate the load > of obj->ponies such that it no longer depends on the load of idx, which > breaks RCU. > > Note that further 'optimization' is possible and the compiler could even > make it: > > if (idx & 1) > val = objs[1].ponies; > else > val = objs[0].ponies; A variant that is actually broken on x86 too (due to issuing the loads in the 'wrong' order): val = objs[0].ponies; if (idx & 1) val = objs[1].ponies; Which is a translation that makes sense if we either marked unlikely(idx & 1) or if PGO found the same. > Now, granted, this is a fairly artificial example, but it does > illustrate the exact problem. > > The more the compiler can see of the complete program, the more likely > it can make inferrences like this, esp. when coupled with PGO. > > Now, we're (usually) very careful to wrap things in READ_ONCE() and > rcu_dereference() and the like, which makes it harder on the compiler > (because 'volatile' is special), but nothing really stops it from doing > this. > > Paul has been trying to beat clue into the language people, but given > he's been at it for 10 years now, and there's no resolution, I figure we > ought to get compiler implementations to give us a knob.