Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3083790imu; Sun, 9 Dec 2018 16:59:27 -0800 (PST) X-Google-Smtp-Source: AFSGD/VUThhBSG3shTVkgZ8UOfXLAJAsUSCR7eJwwlTCIwUOUIvNH0fV37SjsUb5Mk6W6xxg9qfc X-Received: by 2002:a17:902:28aa:: with SMTP id f39mr10330698plb.297.1544403567612; Sun, 09 Dec 2018 16:59:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544403567; cv=none; d=google.com; s=arc-20160816; b=qJiLnpXGr1Z6mEuIF6Rbdu6sIBCJ5TXWogX1CciaR3v7NCIsk0KbcZza1Xki20QnTp uGI435Gj9CeE4gXgIKwaN3Nn6LXiDvHIFLQ45XJ1xn2c6RtinePGzfNLskmYtDArJw9r dkrs+1P+5J2G9WtmQlFzkrHW1Nn/am8DwQVJdyTB9zZja6WSmPb5UggYbxdISgWskMyw BRhjQO/Bcc6YR9pgnlMvRL3c41zb82FqS3kZXgI7R6mnmWuqNOxb0qmAY50j5eN/mKxU h+3wtyCV/K/nJa8cyZDslsxszltnaLfRx1iqOyV7vldhN9FshjMkvsanuqcdZnkVSUKQ ZuiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=q7/lCL9FJdyhz/T1CmCRiZjq32YE0nsxJB+Y/sBlV3g=; b=0VHZP9VoV+Ep/QkQW55Jd5ov2X07cydgbEmbRvmc+LOt7R7/q16i9u3fMXCE2iXkTX 0w2cHyS/a4e7LcWLYigA8MMHYZ7Ukzq6WoUxB1Wct38ZsZleQXJCnRJEDyFzss9hq1zN BtUzNjk6QI59O0eovgeMaD9ZmogSpaoLvpo79pXMohyBXYWeNKEbxeEHPo0YwCCaBD+O uekP8SRcKjjlWmNvFvxKbwWw0mZrfYO0RJVT8WBVMq8p2tdt+ZMni+/gRqs7Vus3Q7nW H5D7LN5NpjjKbSk9ekN9a639BeWWot00Sb70WsDP6Gi7qeWk5Num1kSYc2Q8ot4Rk2nl AruQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZXRIHmUQ; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r14si8962335pfh.229.2018.12.09.16.59.11; Sun, 09 Dec 2018 16:59:27 -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=pass header.i=@gmail.com header.s=20161025 header.b=ZXRIHmUQ; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726389AbeLJA5t (ORCPT + 99 others); Sun, 9 Dec 2018 19:57:49 -0500 Received: from mail-pf1-f176.google.com ([209.85.210.176]:36494 "EHLO mail-pf1-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726317AbeLJA5s (ORCPT ); Sun, 9 Dec 2018 19:57:48 -0500 Received: by mail-pf1-f176.google.com with SMTP id b85so4535314pfc.3 for ; Sun, 09 Dec 2018 16:57:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=q7/lCL9FJdyhz/T1CmCRiZjq32YE0nsxJB+Y/sBlV3g=; b=ZXRIHmUQ5TiGwj3GnTIu3I6rlLVW++ahpF2g0aGsK/0vHZMAwGPNlltIxQLNzhcfEw AD5anQSWRvtsakjUzd9A7muAP68PZK7ggIds88hHmE8+iiskHhWN+NMmzGjGLWSNuFkW 1089qZdBXaa7vQf+T3D+oRhtovmZh4eOiyMMfNzPqTHkoczEU+EaM0WXWcambTwxixY/ 3Kr8/TkpnwvscB4OLn1RpjeQorKeKUJrvrioqBux/J7T18zQL+kSBdwYS/diR4f0cMP5 aD0u1oVo6hnGxMojiaJkc5gDYqCdSt2G724VAUg4BnvMpkDV1ODpfsGaigwQ0wc5bgYc GXtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=q7/lCL9FJdyhz/T1CmCRiZjq32YE0nsxJB+Y/sBlV3g=; b=FErNvGJg1V0CBy5eZl06bMLUP3OIMdeIxSWNTLVS1hXcGPTp3F+w4c005l+RLKRWlV ctZNsAP8zaE8BkB19wnYJpu9moxVkWWrjeolnZuE2uPPTw0ymdJPQLdcySW70Mv5mT5U kRYIFqq4WC2s1Pi+bhXkSiMLwUsgb2NphtTqyAJjFS0kbBPhjFDo5cZsbxpc+Km41hSo RsWYC67tLUdTnIxA0ZFbiiYq1TX9rhLMR3LwVYOQZm5Le+loiqtcrODer99DEQIh+o42 qeW6H5Nphz2Ql/91MZFQIdrHvshJVgsDRcQlCxpVJwBhMZc7Ob7v5M660mANLyiLv/W8 QMeQ== X-Gm-Message-State: AA+aEWY0UzNkqda6TKdnR5AY29Bv4Drm7+PifRFwRqIsqMv6VRcnj6zk eTc5/tByX8Yyvgmgnlcto5I= X-Received: by 2002:a62:7892:: with SMTP id t140mr10370777pfc.237.1544403466252; Sun, 09 Dec 2018 16:57:46 -0800 (PST) Received: from [10.2.19.70] ([208.91.2.1]) by smtp.gmail.com with ESMTPSA id k137sm17297319pfd.56.2018.12.09.16.57.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Dec 2018 16:57:45 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.1 \(3445.101.1\)) Subject: Re: Should this_cpu_read() be volatile? From: Nadav Amit In-Reply-To: <20181208105220.GF5289@hirez.programming.kicks-ass.net> Date: Sun, 9 Dec 2018 16:57:43 -0800 Cc: Matthew Wilcox , Vlastimil Babka , Linux-MM , LKML , X86 ML , Ingo Molnar , Thomas Gleixner , Andy Lutomirski Content-Transfer-Encoding: quoted-printable Message-Id: <5DE00B41-835C-4E68-B192-2A3C7ACB4392@gmail.com> References: <20181203161352.GP10377@bombadil.infradead.org> <4F09425C-C9AB-452F-899C-3CF3D4B737E1@gmail.com> <20181203224920.GQ10377@bombadil.infradead.org> <20181206102559.GG13538@hirez.programming.kicks-ass.net> <55B665E1-3F64-4D87-B779-D1B4AFE719A9@gmail.com> <20181207084550.GA2237@hirez.programming.kicks-ass.net> <20181208105220.GF5289@hirez.programming.kicks-ass.net> To: Peter Zijlstra X-Mailer: Apple Mail (2.3445.101.1) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Dec 8, 2018, at 2:52 AM, Peter Zijlstra = wrote: >=20 > On Fri, Dec 07, 2018 at 04:40:52PM -0800, Nadav Amit wrote: >=20 >>> I'm actually having difficulty finding the this_cpu_read() in any of = the >>> functions you mention, so I cannot make any concrete suggestions = other >>> than pointing at the alternative functions available. >>=20 >>=20 >> So I got deeper into the code to understand a couple of differences. = In the >> case of select_idle_sibling(), the patch (Peter=E2=80=99s) increase = the function >> code size by 123 bytes (over the baseline of 986). The per-cpu = variable is >> called through the following call chain: >>=20 >> select_idle_sibling() >> =3D> select_idle_cpu() >> =3D> local_clock() >> =3D> raw_smp_processor_id() >>=20 >> And results in 2 more calls to sched_clock_cpu(), as the compiler = assumes >> the processor id changes in between (which obviously wouldn=E2=80=99t = happen). >=20 > That is the thing with raw_smp_processor_id(), it is allowed to be = used > in preemptible context, and there it _obviously_ can change between > subsequent invocations. >=20 > So again, this change is actually good. >=20 > If we want to fix select_idle_cpu(), we should maybe not use > local_clock() there but use sched_clock_cpu() with a stable argument, > this code runs with IRQs disabled and therefore the CPU number is = stable > for us here. >=20 >> There may be more changes around, which I didn=E2=80=99t fully = analyze. But >> the very least reading the processor id should not get = =E2=80=9Cvolatile=E2=80=9D. >>=20 >> As for finish_task_switch(), the impact is only few bytes, but still >> unnecessary. It appears that with your patch preempt_count() causes = multiple >> reads of __preempt_count in this code: >>=20 >> if (WARN_ONCE(preempt_count() !=3D 2*PREEMPT_DISABLE_OFFSET, >> "corrupted preempt_count: %s/%d/0x%x\n", >> current->comm, current->pid, preempt_count())) >> preempt_count_set(FORK_PREEMPT_COUNT); >=20 > My patch proposed here: >=20 > https://marc.info/?l=3Dlinux-mm&m=3D154409548410209 >=20 > would actually fix that one I think, preempt_count() uses > raw_cpu_read_4() which will loose the volatile with that patch. Sorry for the spam from yesterday. That what happens when I try to write emails on my phone while I=E2=80=99m distracted. I tested the patch you referenced, and it certainly improves the = situation for reads, but there are still small and big issues lying around. The biggest one is that (I think) smp_processor_id() should apparently = use __this_cpu_read(). Anyhow, when preemption checks are on, it is = validated that smp_processor_id() is not used while preemption is enabled, and = IRQs are not supposed to change its value. Otherwise the generated code of = many functions is affected. There are all kind of other smaller issues, such as set_irq_regs() and get_irq_regs(), which should run with disabled interrupts. They affect = the generated code in do_IRQ() and others. But beyond that, there are so many places in the code that use this_cpu_read() while IRQs are guaranteed to be disabled. For example arch/x86/mm/tlb.c is full with this_cpu_read/write() and almost(?) all should be running with interrupts disabled. Having said that, in my = build only flush_tlb_func_common() was affected.