1*3a85e55fSvisa.\" $OpenBSD: smr_call.9,v 1.4 2022/06/22 14:10:49 visa Exp $ 21d1f0a92Svisa.\" 31d1f0a92Svisa.\" Copyright (c) 2019 Visa Hankala 41d1f0a92Svisa.\" 51d1f0a92Svisa.\" Permission to use, copy, modify, and distribute this software for any 61d1f0a92Svisa.\" purpose with or without fee is hereby granted, provided that the above 71d1f0a92Svisa.\" copyright notice and this permission notice appear in all copies. 81d1f0a92Svisa.\" 91d1f0a92Svisa.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 101d1f0a92Svisa.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 111d1f0a92Svisa.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 121d1f0a92Svisa.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 131d1f0a92Svisa.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 141d1f0a92Svisa.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 151d1f0a92Svisa.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 161d1f0a92Svisa.\" 17*3a85e55fSvisa.Dd $Mdocdate: June 22 2022 $ 181d1f0a92Svisa.Dt SMR_CALL 9 191d1f0a92Svisa.Os 201d1f0a92Svisa.Sh NAME 211d1f0a92Svisa.Nm smr_read_enter , 221d1f0a92Svisa.Nm smr_read_leave , 231d1f0a92Svisa.Nm smr_init , 241d1f0a92Svisa.Nm smr_call , 251d1f0a92Svisa.Nm smr_barrier , 261d1f0a92Svisa.Nm smr_flush , 271d1f0a92Svisa.Nm SMR_ASSERT_CRITICAL , 281d1f0a92Svisa.Nm SMR_ASSERT_NONCRITICAL 291d1f0a92Svisa.Nd safe memory reclamation 301d1f0a92Svisa.Sh SYNOPSIS 311d1f0a92Svisa.In sys/smr.h 321d1f0a92Svisa.Ft void 331d1f0a92Svisa.Fn smr_read_enter 341d1f0a92Svisa.Ft void 351d1f0a92Svisa.Fn smr_read_leave 361d1f0a92Svisa.Ft void 371d1f0a92Svisa.Fn smr_init "struct smr_entry *smr" 381d1f0a92Svisa.Ft void 391d1f0a92Svisa.Fn smr_call "struct smr_entry *smr" "void (*fn)(void *)" "void *arg" 401d1f0a92Svisa.Ft void 411d1f0a92Svisa.Fn smr_barrier 421d1f0a92Svisa.Ft void 431d1f0a92Svisa.Fn smr_flush 441d1f0a92Svisa.Ft void 451d1f0a92Svisa.Fn SMR_ASSERT_CRITICAL 461d1f0a92Svisa.Ft void 471d1f0a92Svisa.Fn SMR_ASSERT_NONCRITICAL 481d1f0a92Svisa.Sh DESCRIPTION 491d1f0a92SvisaThe safe memory reclamation API provides a mechanism for reclaiming 501d1f0a92Svisashared objects that readers can access without locking. 511d1f0a92SvisaObjects that are reclaimed through SMR are called SMR-protected. 521d1f0a92SvisaThe mechanism guarantees that SMR-protected objects are not destroyed 531d1f0a92Svisawhile readers are using them. 541d1f0a92SvisaHowever, it does not control how these objects are modified. 551d1f0a92Svisa.Pp 561d393350SdlgReaders access SMR-protected objects inside an SMR read-side critical section 571d393350Sdlgusing 581d393350Sdlg.Xr SMR_PTR_GET 9 . 591d393350Sdlg.\" or with a higher level API built on top of it. 601d1f0a92SvisaThe section is entered with 611d1f0a92Svisa.Fn smr_read_enter , 621d1f0a92Svisaand exited with 631d1f0a92Svisa.Fn smr_read_leave . 641d1f0a92SvisaThese routines never block. 651d1f0a92SvisaSleeping is not allowed within SMR read-side critical section. 661d1f0a92Svisa.Pp 671d1f0a92Svisa.Fn smr_init 681d1f0a92Svisainitializes the entry 691d1f0a92Svisa.Fa smr 701d1f0a92Svisafor use with 711d1f0a92Svisa.Fn smr_call . 721d1f0a92Svisa.Pp 731d1f0a92Svisa.Fn smr_call 741d1f0a92Svisaschedules a callback to be invoked after the entry 751d1f0a92Svisa.Fa smr 761d1f0a92Svisacannot be referenced by a reader in SMR read-side critical section. 771d1f0a92SvisaOn invocation, the system calls function 781d1f0a92Svisa.Fa fn 791d1f0a92Svisawith argument 801d1f0a92Svisa.Fa arg 811d1f0a92Svisain process context without any locks held. 821d1f0a92SvisaThe implementation may delay the call in order to reduce 831d1f0a92Svisaoverall system overhead by amortization. 841d1f0a92Svisa.Pp 851d1f0a92Svisa.Fn smr_barrier 861d1f0a92Svisasleeps until any SMR read-side critical sections that are active on other CPUs 871d1f0a92Svisaat the time of invocation have ended. 881d1f0a92SvisaLike with 891d1f0a92Svisa.Fn smr_call , 901d1f0a92Svisathe processing of the request may be delayed. 911d1f0a92Svisa.Pp 921d1f0a92Svisa.Fn smr_flush 931d1f0a92Svisais like 941d1f0a92Svisa.Fn smr_barrier 951d1f0a92Svisabut the system is forced to process the request as soon as possible. 961d1f0a92SvisaThe use of this function is discouraged because of the heavy impact 971d1f0a92Svisaon system performance. 981d1f0a92Svisa.Pp 99*3a85e55fSvisaTo avoid deadlocks, the caller of 100*3a85e55fSvisa.Fn smr_barrier 101*3a85e55fSvisaor 102*3a85e55fSvisa.Fn smr_flush 103*3a85e55fSvisamust not hold locks that can block the processing of SMR callbacks. 104*3a85e55fSvisa.Pp 1051d1f0a92SvisaThe SMR implementation does not limit the number of deferred calls. 1061d1f0a92SvisaIt is important to prevent arbitrary call rate of 1071d1f0a92Svisa.Fn smr_call . 1081d1f0a92SvisaOtherwise, it might be possible to exhaust system resources 1091d1f0a92Svisaif the system is not able to invoke callbacks quickly enough. 1101d1f0a92Svisa.Pp 1111d1f0a92Svisa.Fn SMR_ASSERT_CRITICAL 1121d1f0a92Svisaand 1131d1f0a92Svisa.Fn SMR_ASSERT_NONCRITICAL 1141d1f0a92Svisacan be used to assert that the current CPU is or is not 1151d1f0a92Svisain SMR read-side critical section. 1161d1f0a92Svisa.Sh CONTEXT 1171d1f0a92Svisa.Fn smr_read_enter , 1181d1f0a92Svisa.Fn smr_read_leave , 1191d1f0a92Svisa.Fn smr_call 1201d1f0a92Svisaand 1211d1f0a92Svisa.Fn smr_init 1221d1f0a92Svisacan be called during autoconf, from process context, or from interrupt context. 1231d1f0a92Svisa.Pp 1241d1f0a92Svisa.Fn smr_barrier 1251d1f0a92Svisaand 1261d1f0a92Svisa.Fn smr_flush 12716820413Svisacan be called from process context. 1281d1f0a92Svisa.Sh SEE ALSO 1291d1f0a92Svisa.Xr mutex 9 , 1301d393350Sdlg.Xr rwlock 9 , 1311d393350Sdlg.Xr SMR_LIST_INIT 9 , 1321d393350Sdlg.Xr SMR_PTR_GET 9 1331d1f0a92Svisa.Sh HISTORY 1341d1f0a92SvisaThe SMR API first appeared in 1351d1f0a92Svisa.Ox 6.5 . 136