Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1430451ybi; Sat, 8 Jun 2019 09:32:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqz9EVsDzj+mL6+UcmyG79xtytpzaVuLTaaN6cH6tg3JjryR3J1B6M+DrCWoOHDKgMq1gkek X-Received: by 2002:a63:1c16:: with SMTP id c22mr8466586pgc.333.1560011564006; Sat, 08 Jun 2019 09:32:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560011563; cv=none; d=google.com; s=arc-20160816; b=p3LM2CkevfpKxgpKAzhDpqI4/jLRexaMQ4I6VccciuXbDfXU6uEEWZSzhG8yfFPmTD BOskWbSJex3GdOoTGdZ/qKgETzwBH6nPLJbmORxrKpbxXIaecaJ32VOnlhFHkEYRxYy8 dq/xK9CoO+bMd3HXpQ9EDq2HApN9Rv86n7cGB9pO3XhYa0FTtnjiFYr5Evs0MeGT6Y8t 4+ImRKPBbVvVd3+n2xEzuaZZ1Lv9Uo6+1Sxnjk/XJmfuu0M28CCAmKCqIglEjnazhWul YSpP1zqUPAv3WGquakMJ4ra14MpOzIQ9+SQSkrq4x3N1Uc2IfBarvAEzFunY6mOnqzQJ EY5g== 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=6FAZQt8j94zDKBG8zo69NCtS8cPyuPj6ruLPDU/hP3g=; b=P2MkaC4YBRQy/gzgrGMp64r6mfbTphld+bnlGxL7bdwXOemCvi6Jv9mpo8vEo3NVs2 OJX8uQCyMYAYuQyxhEMJ4lIDV63555y2hXSLeFxue71SE2QkKMo0I0BOscB4BQyZa4// 15iYG+vyvM4BkwefqQVQK3pMBWWiWlQimrxsPeUBAKiMrYvCDFb+KeH6ckA2NflIJVnT TqV7g3UvheDHX0IS1Qbz6uC8666owHNGIZB8QXkOOAA61oMkoyQpWlP4wcxJJ2mvelMR 4KoKv1h28r5ZkKX1Y5MMHknEWHYk2wzIigzv6dyY/7+RRjJ6fcvLPhJgzbVXyn7uWJnI gRRA== 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 b38si1663558pla.137.2019.06.08.09.32.27; Sat, 08 Jun 2019 09:32:43 -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 S1727324AbfFHQbZ (ORCPT + 99 others); Sat, 8 Jun 2019 12:31:25 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:46308 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727203AbfFHQbZ (ORCPT ); Sat, 8 Jun 2019 12:31:25 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x58GQi77106467 for ; Sat, 8 Jun 2019 12:31:24 -0400 Received: from e12.ny.us.ibm.com (e12.ny.us.ibm.com [129.33.205.202]) by mx0a-001b2d01.pphosted.com with ESMTP id 2t093p43xs-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 08 Jun 2019 12:31:23 -0400 Received: from localhost by e12.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 8 Jun 2019 17:31:22 +0100 Received: from b01cxnp23034.gho.pok.ibm.com (9.57.198.29) by e12.ny.us.ibm.com (146.89.104.199) 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 17:31:18 +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 x58GVHWs37224912 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 8 Jun 2019 16:31:17 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 67075B2064; Sat, 8 Jun 2019 16:31:17 +0000 (GMT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 534D3B205F; Sat, 8 Jun 2019 16:31:17 +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 16:31:17 +0000 (GMT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id 12F5F16C21CA; Sat, 8 Jun 2019 09:31:19 -0700 (PDT) Date: Sat, 8 Jun 2019 09:31:19 -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: <20190608151943.GD28207@linux.ibm.com> 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: 19060816-0060-0000-0000-0000034DF921 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.01215073; UDB=6.00638766; IPR=6.00996165; MB=3.00027235; MTD=3.00000008; XFM=3.00000015; UTC=2019-06-08 16:31:21 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19060816-0061-0000-0000-000049B0F7F7 Message-Id: <20190608163119.GI28207@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-1906080124 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 08, 2019 at 11:56:04AM -0400, Alan Stern wrote: > On Sat, 8 Jun 2019, Paul E. McKenney wrote: > > > 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

    > ---------------------^ > > Are these issues especially notable for female programmers? :-) *Red face* Some days it just doesn't pay to get up in the morning... Well, those issues certainly seem a bit inconsiderate to non-mammalian programmers. :-/ How about the updated version shown below? Thanx, Paul ------------------------------------------------------------------------ diff --git a/Documentation/RCU/Design/Requirements/Requirements.html b/Documentation/RCU/Design/Requirements/Requirements.html index 5a9238a2883c..f04c467e55c5 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 Memory 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 Memory 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