14102adabSPaul E. McKenney /* 24102adabSPaul E. McKenney * Read-Copy Update definitions shared among RCU implementations. 34102adabSPaul E. McKenney * 44102adabSPaul E. McKenney * This program is free software; you can redistribute it and/or modify 54102adabSPaul E. McKenney * it under the terms of the GNU General Public License as published by 64102adabSPaul E. McKenney * the Free Software Foundation; either version 2 of the License, or 74102adabSPaul E. McKenney * (at your option) any later version. 84102adabSPaul E. McKenney * 94102adabSPaul E. McKenney * This program is distributed in the hope that it will be useful, 104102adabSPaul E. McKenney * but WITHOUT ANY WARRANTY; without even the implied warranty of 114102adabSPaul E. McKenney * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 124102adabSPaul E. McKenney * GNU General Public License for more details. 134102adabSPaul E. McKenney * 144102adabSPaul E. McKenney * You should have received a copy of the GNU General Public License 1587de1cfdSPaul E. McKenney * along with this program; if not, you can access it online at 1687de1cfdSPaul E. McKenney * http://www.gnu.org/licenses/gpl-2.0.html. 174102adabSPaul E. McKenney * 184102adabSPaul E. McKenney * Copyright IBM Corporation, 2011 194102adabSPaul E. McKenney * 204102adabSPaul E. McKenney * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com> 214102adabSPaul E. McKenney */ 224102adabSPaul E. McKenney 234102adabSPaul E. McKenney #ifndef __LINUX_RCU_H 244102adabSPaul E. McKenney #define __LINUX_RCU_H 254102adabSPaul E. McKenney 265cb5c6e1SPaul Gortmaker #include <trace/events/rcu.h> 274102adabSPaul E. McKenney #ifdef CONFIG_RCU_TRACE 284102adabSPaul E. McKenney #define RCU_TRACE(stmt) stmt 294102adabSPaul E. McKenney #else /* #ifdef CONFIG_RCU_TRACE */ 304102adabSPaul E. McKenney #define RCU_TRACE(stmt) 314102adabSPaul E. McKenney #endif /* #else #ifdef CONFIG_RCU_TRACE */ 324102adabSPaul E. McKenney 334102adabSPaul E. McKenney /* 344102adabSPaul E. McKenney * Process-level increment to ->dynticks_nesting field. This allows for 354102adabSPaul E. McKenney * architectures that use half-interrupts and half-exceptions from 364102adabSPaul E. McKenney * process context. 374102adabSPaul E. McKenney * 384102adabSPaul E. McKenney * DYNTICK_TASK_NEST_MASK defines a field of width DYNTICK_TASK_NEST_WIDTH 394102adabSPaul E. McKenney * that counts the number of process-based reasons why RCU cannot 404102adabSPaul E. McKenney * consider the corresponding CPU to be idle, and DYNTICK_TASK_NEST_VALUE 414102adabSPaul E. McKenney * is the value used to increment or decrement this field. 424102adabSPaul E. McKenney * 434102adabSPaul E. McKenney * The rest of the bits could in principle be used to count interrupts, 444102adabSPaul E. McKenney * but this would mean that a negative-one value in the interrupt 454102adabSPaul E. McKenney * field could incorrectly zero out the DYNTICK_TASK_NEST_MASK field. 464102adabSPaul E. McKenney * We therefore provide a two-bit guard field defined by DYNTICK_TASK_MASK 474102adabSPaul E. McKenney * that is set to DYNTICK_TASK_FLAG upon initial exit from idle. 484102adabSPaul E. McKenney * The DYNTICK_TASK_EXIT_IDLE value is thus the combined value used upon 494102adabSPaul E. McKenney * initial exit from idle. 504102adabSPaul E. McKenney */ 514102adabSPaul E. McKenney #define DYNTICK_TASK_NEST_WIDTH 7 524102adabSPaul E. McKenney #define DYNTICK_TASK_NEST_VALUE ((LLONG_MAX >> DYNTICK_TASK_NEST_WIDTH) + 1) 534102adabSPaul E. McKenney #define DYNTICK_TASK_NEST_MASK (LLONG_MAX - DYNTICK_TASK_NEST_VALUE + 1) 544102adabSPaul E. McKenney #define DYNTICK_TASK_FLAG ((DYNTICK_TASK_NEST_VALUE / 8) * 2) 554102adabSPaul E. McKenney #define DYNTICK_TASK_MASK ((DYNTICK_TASK_NEST_VALUE / 8) * 3) 564102adabSPaul E. McKenney #define DYNTICK_TASK_EXIT_IDLE (DYNTICK_TASK_NEST_VALUE + \ 574102adabSPaul E. McKenney DYNTICK_TASK_FLAG) 584102adabSPaul E. McKenney 59*2e8c28c2SPaul E. McKenney 60*2e8c28c2SPaul E. McKenney /* 61*2e8c28c2SPaul E. McKenney * Grace-period counter management. 62*2e8c28c2SPaul E. McKenney */ 63*2e8c28c2SPaul E. McKenney 64*2e8c28c2SPaul E. McKenney /* Adjust sequence number for start of update-side operation. */ 65*2e8c28c2SPaul E. McKenney static inline void rcu_seq_start(unsigned long *sp) 66*2e8c28c2SPaul E. McKenney { 67*2e8c28c2SPaul E. McKenney WRITE_ONCE(*sp, *sp + 1); 68*2e8c28c2SPaul E. McKenney smp_mb(); /* Ensure update-side operation after counter increment. */ 69*2e8c28c2SPaul E. McKenney WARN_ON_ONCE(!(*sp & 0x1)); 70*2e8c28c2SPaul E. McKenney } 71*2e8c28c2SPaul E. McKenney 72*2e8c28c2SPaul E. McKenney /* Adjust sequence number for end of update-side operation. */ 73*2e8c28c2SPaul E. McKenney static inline void rcu_seq_end(unsigned long *sp) 74*2e8c28c2SPaul E. McKenney { 75*2e8c28c2SPaul E. McKenney smp_mb(); /* Ensure update-side operation before counter increment. */ 76*2e8c28c2SPaul E. McKenney WRITE_ONCE(*sp, *sp + 1); 77*2e8c28c2SPaul E. McKenney WARN_ON_ONCE(*sp & 0x1); 78*2e8c28c2SPaul E. McKenney } 79*2e8c28c2SPaul E. McKenney 80*2e8c28c2SPaul E. McKenney /* Take a snapshot of the update side's sequence number. */ 81*2e8c28c2SPaul E. McKenney static inline unsigned long rcu_seq_snap(unsigned long *sp) 82*2e8c28c2SPaul E. McKenney { 83*2e8c28c2SPaul E. McKenney unsigned long s; 84*2e8c28c2SPaul E. McKenney 85*2e8c28c2SPaul E. McKenney s = (READ_ONCE(*sp) + 3) & ~0x1; 86*2e8c28c2SPaul E. McKenney smp_mb(); /* Above access must not bleed into critical section. */ 87*2e8c28c2SPaul E. McKenney return s; 88*2e8c28c2SPaul E. McKenney } 89*2e8c28c2SPaul E. McKenney 90*2e8c28c2SPaul E. McKenney /* 91*2e8c28c2SPaul E. McKenney * Given a snapshot from rcu_seq_snap(), determine whether or not a 92*2e8c28c2SPaul E. McKenney * full update-side operation has occurred. 93*2e8c28c2SPaul E. McKenney */ 94*2e8c28c2SPaul E. McKenney static inline bool rcu_seq_done(unsigned long *sp, unsigned long s) 95*2e8c28c2SPaul E. McKenney { 96*2e8c28c2SPaul E. McKenney return ULONG_CMP_GE(READ_ONCE(*sp), s); 97*2e8c28c2SPaul E. McKenney } 98*2e8c28c2SPaul E. McKenney 994102adabSPaul E. McKenney /* 1004102adabSPaul E. McKenney * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally 1014102adabSPaul E. McKenney * by call_rcu() and rcu callback execution, and are therefore not part of the 1024102adabSPaul E. McKenney * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors. 1034102adabSPaul E. McKenney */ 1044102adabSPaul E. McKenney 1054102adabSPaul E. McKenney #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 1064102adabSPaul E. McKenney # define STATE_RCU_HEAD_READY 0 1074102adabSPaul E. McKenney # define STATE_RCU_HEAD_QUEUED 1 1084102adabSPaul E. McKenney 1094102adabSPaul E. McKenney extern struct debug_obj_descr rcuhead_debug_descr; 1104102adabSPaul E. McKenney 1114102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head) 1124102adabSPaul E. McKenney { 1134102adabSPaul E. McKenney int r1; 1144102adabSPaul E. McKenney 1154102adabSPaul E. McKenney r1 = debug_object_activate(head, &rcuhead_debug_descr); 1164102adabSPaul E. McKenney debug_object_active_state(head, &rcuhead_debug_descr, 1174102adabSPaul E. McKenney STATE_RCU_HEAD_READY, 1184102adabSPaul E. McKenney STATE_RCU_HEAD_QUEUED); 1194102adabSPaul E. McKenney return r1; 1204102adabSPaul E. McKenney } 1214102adabSPaul E. McKenney 1224102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head) 1234102adabSPaul E. McKenney { 1244102adabSPaul E. McKenney debug_object_active_state(head, &rcuhead_debug_descr, 1254102adabSPaul E. McKenney STATE_RCU_HEAD_QUEUED, 1264102adabSPaul E. McKenney STATE_RCU_HEAD_READY); 1274102adabSPaul E. McKenney debug_object_deactivate(head, &rcuhead_debug_descr); 1284102adabSPaul E. McKenney } 1294102adabSPaul E. McKenney #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 1304102adabSPaul E. McKenney static inline int debug_rcu_head_queue(struct rcu_head *head) 1314102adabSPaul E. McKenney { 1324102adabSPaul E. McKenney return 0; 1334102adabSPaul E. McKenney } 1344102adabSPaul E. McKenney 1354102adabSPaul E. McKenney static inline void debug_rcu_head_unqueue(struct rcu_head *head) 1364102adabSPaul E. McKenney { 1374102adabSPaul E. McKenney } 1384102adabSPaul E. McKenney #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 1394102adabSPaul E. McKenney 140bd73a7f5STeodora Baluta void kfree(const void *); 1414102adabSPaul E. McKenney 142406e3e53SPaul E. McKenney /* 143406e3e53SPaul E. McKenney * Reclaim the specified callback, either by invoking it (non-lazy case) 144406e3e53SPaul E. McKenney * or freeing it directly (lazy case). Return true if lazy, false otherwise. 145406e3e53SPaul E. McKenney */ 1464102adabSPaul E. McKenney static inline bool __rcu_reclaim(const char *rn, struct rcu_head *head) 1474102adabSPaul E. McKenney { 1484102adabSPaul E. McKenney unsigned long offset = (unsigned long)head->func; 1494102adabSPaul E. McKenney 15024ef659aSPaul E. McKenney rcu_lock_acquire(&rcu_callback_map); 1514102adabSPaul E. McKenney if (__is_kfree_rcu_offset(offset)) { 152dffd06a7SPaul E. McKenney RCU_TRACE(trace_rcu_invoke_kfree_callback(rn, head, offset);) 1534102adabSPaul E. McKenney kfree((void *)head - offset); 15424ef659aSPaul E. McKenney rcu_lock_release(&rcu_callback_map); 155406e3e53SPaul E. McKenney return true; 1564102adabSPaul E. McKenney } else { 157dffd06a7SPaul E. McKenney RCU_TRACE(trace_rcu_invoke_callback(rn, head);) 1584102adabSPaul E. McKenney head->func(head); 15924ef659aSPaul E. McKenney rcu_lock_release(&rcu_callback_map); 160406e3e53SPaul E. McKenney return false; 1614102adabSPaul E. McKenney } 1624102adabSPaul E. McKenney } 1634102adabSPaul E. McKenney 1644102adabSPaul E. McKenney #ifdef CONFIG_RCU_STALL_COMMON 1654102adabSPaul E. McKenney 1664102adabSPaul E. McKenney extern int rcu_cpu_stall_suppress; 1674102adabSPaul E. McKenney int rcu_jiffies_till_stall_check(void); 1684102adabSPaul E. McKenney 1694102adabSPaul E. McKenney #endif /* #ifdef CONFIG_RCU_STALL_COMMON */ 1704102adabSPaul E. McKenney 1714102adabSPaul E. McKenney /* 1724102adabSPaul E. McKenney * Strings used in tracepoints need to be exported via the 1734102adabSPaul E. McKenney * tracing system such that tools like perf and trace-cmd can 1744102adabSPaul E. McKenney * translate the string address pointers to actual text. 1754102adabSPaul E. McKenney */ 1764102adabSPaul E. McKenney #define TPS(x) tracepoint_string(x) 1774102adabSPaul E. McKenney 178aa23c6fbSPranith Kumar void rcu_early_boot_tests(void); 17952d7e48bSPaul E. McKenney void rcu_test_sync_prims(void); 180aa23c6fbSPranith Kumar 1815f6130faSLai Jiangshan /* 1825f6130faSLai Jiangshan * This function really isn't for public consumption, but RCU is special in 1835f6130faSLai Jiangshan * that context switches can allow the state machine to make progress. 1845f6130faSLai Jiangshan */ 1855f6130faSLai Jiangshan extern void resched_cpu(int cpu); 1865f6130faSLai Jiangshan 1874102adabSPaul E. McKenney #endif /* __LINUX_RCU_H */ 188