Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1385390ybi; Sat, 8 Jun 2019 08:34:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3UhWlAzMGPbugDrRMpXh25YbCvyEmthZaEOIlkkVgPybTttPJ6FzQoBRJq5DcfrFDg0Mc X-Received: by 2002:aa7:8193:: with SMTP id g19mr58301690pfi.162.1560008044186; Sat, 08 Jun 2019 08:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560008044; cv=none; d=google.com; s=arc-20160816; b=CskuaMr7bwTZJFhJO3y2BmP9WfNhkFGlie5c9CYvcy/VoMkCAHNlC6h8kBPBw3CJz5 MjY1yCKjP2kGjgRtVDRrK1mNigSIwamMRv/lL4G8A4tiwJQArAyiwiupqxShN+ThnJgV H7El3bdzSAhmCxKOZPeXnx9L57Fek0FFrSXBwHRZUBMudsVO/u+7kulpXGjKzJqjBHYc ymLccTdk9I/+taTUB/1RH9aPwaw0/pJ/Dgr4CXOco7RxKpkrm0RjpRDt5EJqeWK11wgV LNdbXRVPYdITzGVS1HR8mRh5KpVaD9aGOuE0e7VHjbL1ijeVqte7NOCfA3WIJcLF+EaV D1/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:subject:cc:to :from:date; bh=hYJ4EyuH1V3vQfFzJkcaq8lEvLvjelboaRi0XFeB8Vs=; b=vbghijIsxxZOQf7M2pS5QaD+sM1ncvG61x9lKvDhF101RxImCUmfLp1PcyEEvl5+PD HJxXc6I3FT7dj3XJVSomWmCswfByJ9wpfsOCXf+IUwTfhVBaqyj3R7MfLVnqma+A9aIi NG6Qw8Wxb738Q7Etnt1eOl1h+7DvdevEdt3NkiAkOnHeroCA7F02Spq/khxlwp7hFtT8 0Iwb/FGqOr7YFicxlBDpejwOuGueX1LMGG0yaCvpWsnFkKB4tidH/0GYM+aUSX9/OdLA n3RSpjFKIkAw0swLgR15FlyNAd61PTVIr4t4z/oQCIHMnuZAR9gCnpAWDAcpMVbKN4Ko JHIw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 99si5249511pla.397.2019.06.08.08.33.48; Sat, 08 Jun 2019 08:34:04 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727209AbfFHPck (ORCPT + 99 others); Sat, 8 Jun 2019 11:32:40 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:42120 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727015AbfFHPcg (ORCPT ); Sat, 8 Jun 2019 11:32:36 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x58FQnb1134572 for ; Sat, 8 Jun 2019 11:32:35 -0400 Received: from e14.ny.us.ibm.com (e14.ny.us.ibm.com [129.33.205.204]) by mx0b-001b2d01.pphosted.com with ESMTP id 2t05xwq0a2-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 08 Jun 2019 11:32:35 -0400 Received: from localhost by e14.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 8 Jun 2019 16:32:34 +0100 Received: from b01cxnp23034.gho.pok.ibm.com (9.57.198.29) by e14.ny.us.ibm.com (146.89.104.201) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Sat, 8 Jun 2019 16:32:30 +0100 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x58FWT7739125490 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 8 Jun 2019 15:32:29 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CE3F2B2064; Sat, 8 Jun 2019 15:32:29 +0000 (GMT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BCA16B2066; Sat, 8 Jun 2019 15:32:29 +0000 (GMT) Received: from paulmck-ThinkPad-W541 (unknown [9.85.180.36]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Sat, 8 Jun 2019 15:32:29 +0000 (GMT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id E91A416C5D98; Sat, 8 Jun 2019 08:19:43 -0700 (PDT) Date: Sat, 8 Jun 2019 08:19:43 -0700 From: "Paul E. McKenney" To: Alan Stern Cc: Andrea Parri , Boqun Feng , Herbert Xu , Linus Torvalds , Frederic Weisbecker , Fengguang Wu , LKP , LKML , Netdev , "David S. Miller" , Luc Maranget , Jade Alglave Subject: Re: rcu_read_lock lost its compiler barrier Reply-To: paulmck@linux.ibm.com References: <20190606081657.GA4249@andrea> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-GCONF: 00 x-cbid: 19060815-0052-0000-0000-000003CD34DB X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00011234; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000286; SDB=6.01215054; UDB=6.00638754; IPR=6.00996145; MB=3.00027235; MTD=3.00000008; XFM=3.00000015; UTC=2019-06-08 15:32:34 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19060815-0053-0000-0000-0000613D2590 Message-Id: <20190608151943.GD28207@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-06-08_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1906080116 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 06, 2019 at 10:19:43AM -0400, Alan Stern wrote: > On Thu, 6 Jun 2019, Andrea Parri wrote: > > > This seems a sensible change to me: looking forward to seeing a patch, > > on top of -rcu/dev, for further review and testing! > > > > We could also add (to LKMM) the barrier() for rcu_read_{lock,unlock}() > > discussed in this thread (maybe once the RCU code and the informal doc > > will have settled in such direction). > > Yes. Also for SRCU. That point had not escaped me. And it does seem pretty settled. There are quite a few examples where there are normal accesses at either end of the RCU read-side critical sections, for example, the one in the requirements diffs below. For SRCU, srcu_read_lock() and srcu_read_unlock() have implied compiler barriers since 2006. ;-) Thanx, Paul ------------------------------------------------------------------------ diff --git a/Documentation/RCU/Design/Requirements/Requirements.html b/Documentation/RCU/Design/Requirements/Requirements.html index 5a9238a2883c..080b39cc1dbb 100644 --- a/Documentation/RCU/Design/Requirements/Requirements.html +++ b/Documentation/RCU/Design/Requirements/Requirements.html @@ -2129,6 +2129,8 @@ Some of the relevant points of interest are as follows:
  • Hotplug CPU.
  • Scheduler and RCU.
  • Tracing and RCU. +
  • +Accesses to User Mamory and RCU.
  • Energy Efficiency.
  • Scheduling-Clock Interrupts and RCU. @@ -2521,6 +2523,75 @@ cannot be used. The tracing folks both located the requirement and provided the needed fix, so this surprise requirement was relatively painless. +

    +Accesses to User Mamory and RCU

    + +

    +The kernel needs to access user-space memory, for example, to access +data referenced by system-call parameters. +The get_user() macro does this job. + +

    +However, user-space memory might well be paged out, which means +that get_user() might well page-fault and thus block while +waiting for the resulting I/O to complete. +It would be a very bad thing for the compiler to reorder +a get_user() invocation into an RCU read-side critical +section. +For example, suppose that the source code looked like this: + +

    +
    + 1 rcu_read_lock();
    + 2 p = rcu_dereference(gp);
    + 3 v = p->value;
    + 4 rcu_read_unlock();
    + 5 get_user(user_v, user_p);
    + 6 do_something_with(v, user_v);
    +
    +
    + +

    +The compiler must not be permitted to transform this source code into +the following: + +

    +
    + 1 rcu_read_lock();
    + 2 p = rcu_dereference(gp);
    + 3 get_user(user_v, user_p); // BUG: POSSIBLE PAGE FAULT!!!
    + 4 v = p->value;
    + 5 rcu_read_unlock();
    + 6 do_something_with(v, user_v);
    +
    +
    + +

    +If the compiler did make this transformation in a +CONFIG_PREEMPT=n kernel build, and if get_user() did +page fault, the result would be a quiescent state in the middle +of an RCU read-side critical section. +This misplaced quiescent state could result in line 4 being +a use-after-free access, which could be bad for your kernel's +actuarial statistics. +Similar examples can be constructed with the call to get_user() +preceding the rcu_read_lock(). + +

    +Unfortunately, get_user() doesn't have any particular +ordering properties, and in some architectures the underlying asm +isn't even marked volatile. +And even if it was marked volatile, the above access to +p->value is not volatile, so the compiler would not have any +reason to keep those two accesses in order. + +

    +Therefore, the Linux-kernel definitions of rcu_read_lock() +and rcu_read_unlock() must act as compiler barriers, +at least for outermost instances of rcu_read_lock() and +rcu_read_unlock() within a nested set of RCU read-side critical +sections. +

    Energy Efficiency