Received: by 10.213.65.68 with SMTP id h4csp1741781imn; Sun, 1 Apr 2018 13:44:11 -0700 (PDT) X-Google-Smtp-Source: AIpwx49H1SgVUlUbnp+ALT+O/cODPGpkXG075K06bLlvvq06qK2FClzm/KBVYISlBWjXCggFE+Eh X-Received: by 10.99.94.197 with SMTP id s188mr4617509pgb.21.1522615451145; Sun, 01 Apr 2018 13:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522615451; cv=none; d=google.com; s=arc-20160816; b=seuTjwSWpu/HapIPusKuSSedOmiBGqgQ0b24+b5BiFowvt8m2ufHgzZsZchPw+tOCu C7q8Homz034d/fmld3IhyL0lBO/JP7F9ys1XF5oTtjYCM9FTUhn41WWv1ofNaZ4EF2Lw ufWjoG6YTxFgtNa0DwwuA0i0XWdoZn2eDF0vBL9e/UiTcesTXfRYC5K7p6bsRazTZK0l +OIWm7tt+h4TJUxdokPR+3MvctVAb/tzQle9nCgFeevy0TFMK/hCReoHyovMvNuhdfkO +uRQ5H+POXtIIDm+L7xzf7KxG7AdyE2ZqBmU7Uq3znf6rTKMRVuuuGFgcrnak0l08B05 YirQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:to:from:subject :organization:arc-authentication-results; bh=F9zp7tNzxTv2wrhAyZLtHD6a+pwCax4AHivHYwTAEsg=; b=ffyYMfJrtMtARv+W8OW9R9kzhvbxhQ7/53j4fh9c5Edz42mbMtF1BDUhdJPFts0NBZ cvUluf8tPtQlsKbd/S6DoJ+OmBC48+NXC4eKNZfuWMyWeYKeP2M75O8m9rNU/Uc0WABH +5HiZQZEPFTcmGvYAwZTQKRVvrO3iynFb0UVI77Bq1KTtGauI3WVRMbkbFGNGu4MtKAR /j7cg553sUkhBXbN10Y2izbLInsfSZsmelJuVXB2XSYGih+0ifyP1hfhlavNgoTQtRkJ fzbLCAv1t5R9kYXvYj5rJL0OeooJ5D47y8k4+3Q00CDJ0tDUe6JY1rXRotfYxjQr7Xg3 vtfQ== 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a23-v6si875321pls.423.2018.04.01.13.43.57; Sun, 01 Apr 2018 13:44:11 -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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754335AbeDAUmH (ORCPT + 99 others); Sun, 1 Apr 2018 16:42:07 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:42142 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754295AbeDAUmF (ORCPT ); Sun, 1 Apr 2018 16:42:05 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B98EC4023141 for ; Sun, 1 Apr 2018 20:42:04 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-120-59.rdu2.redhat.com [10.10.120.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 52312111DCF0; Sun, 1 Apr 2018 20:42:04 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 18/45] C++: Turn RCU accessors into inline template functions From: David Howells To: linux-kernel@vger.kernel.org Date: Sun, 01 Apr 2018 21:42:03 +0100 Message-ID: <152261532385.30503.14346830829867907595.stgit@warthog.procyon.org.uk> In-Reply-To: <152261521484.30503.16131389653845029164.stgit@warthog.procyon.org.uk> References: <152261521484.30503.16131389653845029164.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Sun, 01 Apr 2018 20:42:04 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Sun, 01 Apr 2018 20:42:04 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'dhowells@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Simplify the RCU accessor functions by turning them into inline template functions. There's then no need for the use of casts built around typeof. Signed-off-by: David Howells --- include/linux/rcupdate.h | 124 +++++++++++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 46 deletions(-) diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h index 043d04784675..328b312f09f0 100644 --- a/include/linux/rcupdate.h +++ b/include/linux/rcupdate.h @@ -1,4 +1,4 @@ -/* +/* -*- c++ -*- * Read-Copy Update mechanism for mutual exclusion * * This program is free software; you can redistribute it and/or modify @@ -337,32 +337,50 @@ static inline void rcu_preempt_sleep_check(void) { } #define rcu_dereference_sparse(p, space) #endif /* #else #ifdef __CHECKER__ */ -#define __rcu_access_pointer(p, space) \ -({ \ - typeof(*p) *_________p1 = (typeof(*p) *__force)READ_ONCE(p); \ - rcu_dereference_sparse(p, space); \ - ((typeof(*p) __force __kernel *)(_________p1)); \ -}) -#define __rcu_dereference_check(p, c, space) \ -({ \ - /* Dependency order vs. p above. */ \ - typeof(*p) *________p1 = (typeof(*p) *__force)READ_ONCE(p); \ - RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \ - rcu_dereference_sparse(p, space); \ - ((typeof(*p) __force __kernel *)(________p1)); \ -}) -#define __rcu_dereference_protected(p, c, space) \ -({ \ - RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \ - rcu_dereference_sparse(p, space); \ - ((typeof(*p) __force __kernel *)(p)); \ -}) -#define rcu_dereference_raw(p) \ -({ \ - /* Dependency order vs. p above. */ \ - typeof(p) ________p1 = READ_ONCE(p); \ - ((typeof(*p) __force __kernel *)(________p1)); \ -}) +template +T *__rcu_access_pointer(T *&p) +{ + /* Dependency order vs. p above. */ + T *p1 = (T *__force)READ_ONCE(p); + rcu_dereference_sparse(p, __rcu); + return (T __force __kernel *)p1; +} + +template +T *__rcu_dereference_check(T *&p, bool c) +{ + /* Dependency order vs. p above. */ + T *p1 = (T *__force)READ_ONCE(p); + RCU_LOCKDEP_WARN(!c, "suspicious rcu_dereference_check() usage"); + rcu_dereference_sparse(p, __rcu); + return (T __force __kernel *)p1; +} + +template +T *__rcu_dereference_check(T *const &p, bool c) +{ + /* Dependency order vs. p above. */ + T *p1 = (T *__force)READ_ONCE(p); + RCU_LOCKDEP_WARN(!c, "suspicious rcu_dereference_check() usage"); + rcu_dereference_sparse(p, __rcu); + return (T __force __kernel *)p1; +} + +template +T *__rcu_dereference_protected(T *&p, bool c) +{ + RCU_LOCKDEP_WARN(!c, "suspicious rcu_dereference_protected() usage"); + rcu_dereference_sparse(p, __rcu); + return (T __force __kernel *)p; +} + +template +T *rcu_dereference_raw(T *&p) +{ + /* Dependency order vs. p above. */ + T *p1 = READ_ONCE(p); + return (T __force __kernel *)p1; +} /** * RCU_INITIALIZER() - statically initialize an RCU-protected global variable @@ -401,16 +419,14 @@ static inline void rcu_preempt_sleep_check(void) { } * please be careful when making changes to rcu_assign_pointer() and the * other macros that it invokes. */ -#define rcu_assign_pointer(p, v) \ -({ \ - uintptr_t _r_a_p__v = (uintptr_t)(v); \ - \ - if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \ - WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \ - else \ - smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \ - _r_a_p__v; \ -}) +template +static inline void rcu_assign_pointer(T *&p, T *v) +{ + if (__builtin_constant_p(v) && !v) + WRITE_ONCE((p), v); + else + smp_store_release(&p, (T __force __rcu *)v); +} /** * rcu_swap_protected() - swap an RCU and a regular pointer @@ -482,8 +498,17 @@ static inline void rcu_preempt_sleep_check(void) { } * which pointers are protected by RCU and checks that the pointer is * annotated as __rcu. */ -#define rcu_dereference_check(p, c) \ - __rcu_dereference_check((p), (c) || rcu_read_lock_held(), __rcu) +template +T *rcu_dereference_check(T *&p, bool c) +{ + return __rcu_dereference_check(p, c || rcu_read_lock_held()); +} + +template +T *rcu_dereference_check(T *const &p, bool c) +{ + return __rcu_dereference_check(p, c || rcu_read_lock_held()); +} /** * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking @@ -493,7 +518,7 @@ static inline void rcu_preempt_sleep_check(void) { } * This is the RCU-bh counterpart to rcu_dereference_check(). */ #define rcu_dereference_bh_check(p, c) \ - __rcu_dereference_check((p), (c) || rcu_read_lock_bh_held(), __rcu) + __rcu_dereference_check((p), (c) || rcu_read_lock_bh_held()) /** * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking @@ -532,7 +557,7 @@ static inline void rcu_preempt_sleep_check(void) { } * but very ugly failures. */ #define rcu_dereference_protected(p, c) \ - __rcu_dereference_protected((p), (c), __rcu) + __rcu_dereference_protected((p), (c)) /** @@ -814,11 +839,18 @@ static inline notrace void rcu_read_unlock_sched_notrace(void) * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no * ordering guarantees for either the CPU or the compiler. */ -#define RCU_INIT_POINTER(p, v) \ - do { \ - rcu_dereference_sparse(p, __rcu); \ - WRITE_ONCE(p, RCU_INITIALIZER(v)); \ - } while (0) +template +static inline void RCU_INIT_POINTER(T *&p, T *v) +{ + rcu_dereference_sparse(p, __rcu); + WRITE_ONCE(p, (T __force __rcu *)v); +} + +template +static inline void RCU_INIT_POINTER(T *&p, nullptr_t v) +{ + WRITE_ONCE(p, (T __force __rcu *)v); +} /** * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer