1 /* $NetBSD: rf_revent.c,v 1.30 2026/10/24 00:20:24 tls Exp $ */ 2 /* 3 * Copyright (c) 1995 Carnegie-Mellon University. 4 * All rights reserved. 5 * 6 * Author: 7 * 8 * Permission to use, copy, modify and distribute this software and 9 * its documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 /* 29 * revent.c -- reconstruction event handling code 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: rf_revent.c,v 1.30 2026/10/24 00:20:24 tls Exp $"); 34 35 #include <sys/errno.h> 36 37 #include "rf_raid.h" 38 #include "rf_revent.h" 39 #include "rf_etimer.h" 40 #include "rf_general.h" 41 #include "rf_desc.h" 42 #include "rf_shutdown.h" 43 44 #define RF_EVENTQ_WAIT 5000 45 46 #include <sys/proc.h> 47 #include <sys/kernel.h> 48 49 static void rf_ShutdownReconEvent(void *); 50 51 static RF_ReconEvent_t * 52 GetReconEventDesc(RF_Raid_t *raidPtr, RF_RowCol_t col, void *arg, RF_Revent_t type); 53 54 static void rf_ShutdownReconEvent(void *arg) 55 { 56 RF_Raid_t *raidPtr; 57 58 raidPtr = (RF_Raid_t *) arg; 59 60 pool_destroy(&raidPtr->pools.revent); 61 } 62 63 int 64 rf_ConfigureReconEvent(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr, 65 RF_Config_t *cfgPtr) 66 { 67 size_t stripe_data_bytes, min_revent, max_revent; 68 u_int max_stripes_per_bp, c; 69 70 if (raidPtr->Layout.map->SubmitReconBuffer == NULL) { 71 min_revent = 0; 72 max_revent = 0; 73 } else { 74 c = raidPtr->numCol; 75 stripe_data_bytes = 76 (size_t)raidPtr->Layout.dataSectorsPerStripe << 77 raidPtr->logBytesPerSector; 78 max_stripes_per_bp = howmany(MAXPHYS, stripe_data_bytes) + 1; 79 /* 1 event/rbuf + c-1 blockclear/forced events per active stripe. */ 80 min_revent = 2 * (c - 1) + raidPtr->numFloatingReconBufs + 81 raidPtr->openings * max_stripes_per_bp * (c - 1); 82 max_revent = 2 * min_revent; 83 } 84 85 rf_pool_init(raidPtr, raidPtr->poolNames.revent, &raidPtr->pools.revent, sizeof(RF_ReconEvent_t), 86 "revent", min_revent, max_revent); 87 rf_ShutdownCreate(listp, rf_ShutdownReconEvent, raidPtr); 88 89 return (0); 90 } 91 92 /* returns the next reconstruction event, blocking the calling thread 93 * until one becomes available. will now return null if it is blocked 94 * or will return an event if it is not */ 95 96 RF_ReconEvent_t * 97 rf_GetNextReconEvent(RF_RaidReconDesc_t *reconDesc) 98 { 99 RF_Raid_t *raidPtr = reconDesc->raidPtr; 100 RF_ReconCtrl_t *rctrl = raidPtr->reconControl; 101 RF_ReconEvent_t *event; 102 int stall_count; 103 104 rf_lock_mutex2(rctrl->eq_mutex); 105 /* q null and count==0 must be equivalent conditions */ 106 RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0)); 107 108 /* mpsleep timeout value: secs = timo_val/hz. 'ticks' here is 109 defined as cycle-counter ticks, not softclock ticks */ 110 111 #define MAX_RECON_EXEC_USECS (100 * 1000) /* 100 ms */ 112 #define RECON_DELAY_MS 25 113 #define RECON_TIMO ((RECON_DELAY_MS * hz) / 1000) 114 115 /* we are not pre-emptible in the kernel, but we don't want to run 116 * forever. If we run w/o blocking for more than MAX_RECON_EXEC_TICKS 117 * ticks of the cycle counter, delay for RECON_DELAY before 118 * continuing. this may murder us with context switches, so we may 119 * need to increase both the MAX...TICKS and the RECON_DELAY_MS. */ 120 if (reconDesc->reconExecTimerRunning) { 121 int status; 122 123 RF_ETIMER_STOP(reconDesc->recon_exec_timer); 124 RF_ETIMER_EVAL(reconDesc->recon_exec_timer); 125 reconDesc->reconExecTicks += 126 RF_ETIMER_VAL_US(reconDesc->recon_exec_timer); 127 if (reconDesc->reconExecTicks > reconDesc->maxReconExecTicks) 128 reconDesc->maxReconExecTicks = 129 reconDesc->reconExecTicks; 130 if (reconDesc->reconExecTicks >= MAX_RECON_EXEC_USECS) { 131 /* we've been running too long. delay for 132 * RECON_DELAY_MS */ 133 #if RF_RECON_STATS > 0 134 reconDesc->numReconExecDelays++; 135 #endif /* RF_RECON_STATS > 0 */ 136 137 status = rf_sleep("rfrecond", RECON_TIMO, 138 rctrl->eq_mutex); 139 RF_ASSERT(status == EWOULDBLOCK); 140 reconDesc->reconExecTicks = 0; 141 } 142 } 143 144 stall_count = 0; 145 while (!rctrl->eventQueue) { 146 #if RF_RECON_STATS > 0 147 reconDesc->numReconEventWaits++; 148 #endif /* RF_RECON_STATS > 0 */ 149 150 rf_timedwait_cond2(rctrl->eq_cv, rctrl->eq_mutex, 151 RF_EVENTQ_WAIT); 152 153 stall_count++; 154 155 if ((stall_count > 10) && 156 rctrl->headSepCBList) { 157 /* There is work to do on the callback list, and 158 we've waited long enough... */ 159 rf_WakeupHeadSepCBWaiters(raidPtr); 160 stall_count = 0; 161 } 162 reconDesc->reconExecTicks = 0; /* we've just waited */ 163 } 164 165 reconDesc->reconExecTimerRunning = 1; 166 if (RF_ETIMER_VAL_US(reconDesc->recon_exec_timer)!=0) { 167 /* it moved!! reset the timer. */ 168 RF_ETIMER_START(reconDesc->recon_exec_timer); 169 } 170 event = rctrl->eventQueue; 171 rctrl->eventQueue = event->next; 172 event->next = NULL; 173 rctrl->eq_count--; 174 175 /* q null and count==0 must be equivalent conditions */ 176 RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0)); 177 rf_unlock_mutex2(rctrl->eq_mutex); 178 return (event); 179 } 180 /* enqueues a reconstruction event on the indicated queue */ 181 void 182 rf_CauseReconEvent(RF_Raid_t *raidPtr, RF_RowCol_t col, void *arg, 183 RF_Revent_t type) 184 { 185 RF_ReconCtrl_t *rctrl = raidPtr->reconControl; 186 RF_ReconEvent_t *event = GetReconEventDesc(raidPtr, col, arg, type); 187 188 if (type == RF_REVENT_BUFCLEAR) { 189 RF_ASSERT(col != rctrl->fcol); 190 } 191 RF_ASSERT(col >= 0 && col <= raidPtr->numCol); 192 rf_lock_mutex2(rctrl->eq_mutex); 193 /* q null and count==0 must be equivalent conditions */ 194 RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0)); 195 event->next = rctrl->eventQueue; 196 rctrl->eventQueue = event; 197 rctrl->eq_count++; 198 rf_broadcast_cond2(rctrl->eq_cv); 199 rf_unlock_mutex2(rctrl->eq_mutex); 200 } 201 /* allocates and initializes a recon event descriptor */ 202 static RF_ReconEvent_t * 203 GetReconEventDesc(RF_Raid_t *raidPtr, RF_RowCol_t col, void *arg, RF_Revent_t type) 204 { 205 RF_ReconEvent_t *t; 206 207 t = pool_get(&raidPtr->pools.revent, PR_WAITOK); 208 t->col = col; 209 t->arg = arg; 210 t->type = type; 211 t->next = NULL; 212 return (t); 213 } 214 215 /* 216 rf_DrainReconEventQueue() -- used in the event of a reconstruction 217 problem, this function simply drains all pending events from the 218 reconstruct event queue. 219 */ 220 221 void 222 rf_DrainReconEventQueue(RF_RaidReconDesc_t *reconDesc) 223 { 224 RF_ReconCtrl_t *rctrl = reconDesc->raidPtr->reconControl; 225 RF_ReconEvent_t *event; 226 227 rf_lock_mutex2(rctrl->eq_mutex); 228 while (rctrl->eventQueue!=NULL) { 229 230 event = rctrl->eventQueue; 231 rctrl->eventQueue = event->next; 232 event->next = NULL; 233 rctrl->eq_count--; 234 /* dump it */ 235 rf_FreeReconEventDesc(reconDesc->raidPtr, event); 236 } 237 rf_unlock_mutex2(rctrl->eq_mutex); 238 } 239 240 void 241 rf_FreeReconEventDesc(RF_Raid_t *raidPtr, RF_ReconEvent_t *event) 242 { 243 pool_put(&raidPtr->pools.revent, event); 244 } 245