| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 |
|
| 14 | use crate::{
|
| 15 | bindings,
|
| 16 | sync::aref::{ARef, AlwaysRefCounted},
|
| 17 | types::{NotThreadSafe, Opaque},
|
| 18 | };
|
| 19 | use core::{ops::Deref, ptr::NonNull};
|
| 20 |
|
| 21 | pub mod virt;
|
| 22 | use virt::VmaRef;
|
| 23 |
|
| 24 | #[cfg(CONFIG_MMU)]
|
| 25 | pub use mmput_async::MmWithUserAsync;
|
| 26 | mod mmput_async;
|
| 27 |
|
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | #[repr(transparent)]
|
| 48 | pub struct Mm {
|
| 49 | mm: Opaque<bindings::mm_struct>,
|
| 50 | }
|
| 51 |
|
| 52 | |
| 53 | unsafe impl Send for Mm {}
|
| 54 | |
| 55 | unsafe impl Sync for Mm {}
|
| 56 |
|
| 57 | |
| 58 | unsafe impl AlwaysRefCounted for Mm {
|
| 59 | #[inline]
|
| 60 | fn inc_ref(&self) {
|
| 61 | |
| 62 | unsafe { bindings::mmgrab(self.as_raw()) };
|
| 63 | }
|
| 64 |
|
| 65 | #[inline]
|
| 66 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 67 | |
| 68 | unsafe { bindings::mmdrop(obj.cast().as_ptr()) };
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | #[repr(transparent)]
|
| 84 | pub struct MmWithUser {
|
| 85 | mm: Mm,
|
| 86 | }
|
| 87 |
|
| 88 | |
| 89 | unsafe impl Send for MmWithUser {}
|
| 90 | |
| 91 | unsafe impl Sync for MmWithUser {}
|
| 92 |
|
| 93 | |
| 94 | unsafe impl AlwaysRefCounted for MmWithUser {
|
| 95 | #[inline]
|
| 96 | fn inc_ref(&self) {
|
| 97 | |
| 98 | unsafe { bindings::mmget(self.as_raw()) };
|
| 99 | }
|
| 100 |
|
| 101 | #[inline]
|
| 102 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 103 | |
| 104 | unsafe { bindings::mmput(obj.cast().as_ptr()) };
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | |
| 109 | impl Deref for MmWithUser {
|
| 110 | type Target = Mm;
|
| 111 |
|
| 112 | #[inline]
|
| 113 | fn deref(&self) -> &Mm {
|
| 114 | &self.mm
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | |
| 119 | impl Mm {
|
| 120 | |
| 121 | #[inline]
|
| 122 | pub fn as_raw(&self) -> *mut bindings::mm_struct {
|
| 123 | self.mm.get()
|
| 124 | }
|
| 125 |
|
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | #[inline]
|
| 133 | pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a Mm {
|
| 134 | |
| 135 | |
| 136 | unsafe { &*ptr.cast() }
|
| 137 | }
|
| 138 |
|
| 139 | |
| 140 | #[inline]
|
| 141 | pub fn mmget_not_zero(&self) -> Option<ARef<MmWithUser>> {
|
| 142 | |
| 143 | let success = unsafe { bindings::mmget_not_zero(self.as_raw()) };
|
| 144 |
|
| 145 | if success {
|
| 146 | |
| 147 | Some(unsafe { ARef::from_raw(NonNull::new_unchecked(self.as_raw().cast())) })
|
| 148 | } else {
|
| 149 | None
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | |
| 155 | impl MmWithUser {
|
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | #[inline]
|
| 163 | pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser {
|
| 164 | |
| 165 | |
| 166 | unsafe { &*ptr.cast() }
|
| 167 | }
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | #[inline]
|
| 176 | pub fn lock_vma_under_rcu(&self, vma_addr: usize) -> Option<VmaReadGuard<'_>> {
|
| 177 | #[cfg(CONFIG_PER_VMA_LOCK)]
|
| 178 | {
|
| 179 | |
| 180 | |
| 181 | let vma = unsafe { bindings::lock_vma_under_rcu(self.as_raw(), vma_addr) };
|
| 182 | if !vma.is_null() {
|
| 183 | return Some(VmaReadGuard {
|
| 184 | |
| 185 | |
| 186 | vma: unsafe { VmaRef::from_raw(vma) },
|
| 187 | _nts: NotThreadSafe,
|
| 188 | });
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | |
| 193 | #[cfg(not(CONFIG_PER_VMA_LOCK))]
|
| 194 | let _ = vma_addr;
|
| 195 |
|
| 196 | None
|
| 197 | }
|
| 198 |
|
| 199 | |
| 200 | #[inline]
|
| 201 | pub fn mmap_read_lock(&self) -> MmapReadGuard<'_> {
|
| 202 | |
| 203 | unsafe { bindings::mmap_read_lock(self.as_raw()) };
|
| 204 |
|
| 205 | |
| 206 | MmapReadGuard {
|
| 207 | mm: self,
|
| 208 | _nts: NotThreadSafe,
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | |
| 213 | #[inline]
|
| 214 | pub fn mmap_read_trylock(&self) -> Option<MmapReadGuard<'_>> {
|
| 215 | |
| 216 | let success = unsafe { bindings::mmap_read_trylock(self.as_raw()) };
|
| 217 |
|
| 218 | if success {
|
| 219 | |
| 220 | Some(MmapReadGuard {
|
| 221 | mm: self,
|
| 222 | _nts: NotThreadSafe,
|
| 223 | })
|
| 224 | } else {
|
| 225 | None
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | pub struct MmapReadGuard<'a> {
|
| 236 | mm: &'a MmWithUser,
|
| 237 | |
| 238 | _nts: NotThreadSafe,
|
| 239 | }
|
| 240 |
|
| 241 | impl<'a> MmapReadGuard<'a> {
|
| 242 | |
| 243 | #[inline]
|
| 244 | pub fn vma_lookup(&self, vma_addr: usize) -> Option<&virt::VmaRef> {
|
| 245 | |
| 246 | |
| 247 | let vma = unsafe { bindings::vma_lookup(self.mm.as_raw(), vma_addr) };
|
| 248 |
|
| 249 | if vma.is_null() {
|
| 250 | None
|
| 251 | } else {
|
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | unsafe { Some(virt::VmaRef::from_raw(vma)) }
|
| 258 | }
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | impl Drop for MmapReadGuard<'_> {
|
| 263 | #[inline]
|
| 264 | fn drop(&mut self) {
|
| 265 | |
| 266 | unsafe { bindings::mmap_read_unlock(self.mm.as_raw()) };
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | pub struct VmaReadGuard<'a> {
|
| 276 | vma: &'a VmaRef,
|
| 277 | |
| 278 | _nts: NotThreadSafe,
|
| 279 | }
|
| 280 |
|
| 281 | |
| 282 | impl Deref for VmaReadGuard<'_> {
|
| 283 | type Target = VmaRef;
|
| 284 |
|
| 285 | #[inline]
|
| 286 | fn deref(&self) -> &VmaRef {
|
| 287 | self.vma
|
| 288 | }
|
| 289 | }
|
| 290 |
|
| 291 | impl Drop for VmaReadGuard<'_> {
|
| 292 | #[inline]
|
| 293 | fn drop(&mut self) {
|
| 294 | |
| 295 | unsafe { bindings::vma_end_read(self.vma.as_ptr()) };
|
| 296 | }
|
| 297 | }
|