SharedBuffer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Singleton.h>
  27. #include <Kernel/Process.h>
  28. #include <Kernel/SharedBuffer.h>
  29. //#define SHARED_BUFFER_DEBUG
  30. namespace Kernel {
  31. static AK::Singleton<Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>> s_map;
  32. Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers()
  33. {
  34. return *s_map;
  35. }
  36. void SharedBuffer::sanity_check(const char* what)
  37. {
  38. LOCKER(shared_buffers().lock(), Lock::Mode::Shared);
  39. unsigned found_refs = 0;
  40. for (const auto& ref : m_refs)
  41. found_refs += ref.count;
  42. if (found_refs != m_total_refs) {
  43. dbgln("{} sanity -- SharedBuffer({}) id: {} has total refs {} but we found {}",
  44. what,
  45. this,
  46. m_shbuf_id,
  47. m_total_refs,
  48. found_refs);
  49. for (const auto& ref : m_refs)
  50. dbgln(" ref from pid {}: reference count {}", ref.pid.value(), ref.count);
  51. ASSERT_NOT_REACHED();
  52. }
  53. }
  54. bool SharedBuffer::is_shared_with(ProcessID peer_pid) const
  55. {
  56. LOCKER(shared_buffers().lock(), Lock::Mode::Shared);
  57. if (m_global)
  58. return true;
  59. for (auto& ref : m_refs) {
  60. if (ref.pid == peer_pid) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. void* SharedBuffer::ref_for_process_and_get_address(Process& process)
  67. {
  68. LOCKER(shared_buffers().lock());
  69. ASSERT(is_shared_with(process.pid()));
  70. if (m_global) {
  71. bool found = false;
  72. for (auto& ref : m_refs) {
  73. if (ref.pid == process.pid()) {
  74. found = true;
  75. break;
  76. }
  77. }
  78. if (!found)
  79. m_refs.append(Reference(process.pid()));
  80. }
  81. for (auto& ref : m_refs) {
  82. if (ref.pid == process.pid()) {
  83. if (!ref.region) {
  84. auto region_or_error = process.allocate_region_with_vmobject(VirtualAddress(), size(), m_vmobject, 0, "SharedBuffer", PROT_READ | (m_writable ? PROT_WRITE : 0), true);
  85. if (region_or_error.is_error())
  86. return (void*)region_or_error.error().error();
  87. ref.region = region_or_error.value();
  88. }
  89. ref.count++;
  90. m_total_refs++;
  91. sanity_check("ref_for_process_and_get_address");
  92. return ref.region.unsafe_ptr()->vaddr().as_ptr(); // TODO: Region needs to be RefCounted!
  93. }
  94. }
  95. ASSERT_NOT_REACHED();
  96. }
  97. void SharedBuffer::share_with(ProcessID peer_pid)
  98. {
  99. LOCKER(shared_buffers().lock());
  100. if (m_global)
  101. return;
  102. for (auto& ref : m_refs) {
  103. if (ref.pid == peer_pid) {
  104. // don't increment the reference count yet; let them shbuf_get it first.
  105. sanity_check("share_with (old ref)");
  106. return;
  107. }
  108. }
  109. m_refs.append(Reference(peer_pid));
  110. sanity_check("share_with (new ref)");
  111. }
  112. void SharedBuffer::share_all_shared_buffers(Process& from_process, Process& with_process)
  113. {
  114. LOCKER(shared_buffers().lock());
  115. for (auto& shbuf : shared_buffers().resource()) {
  116. auto& shared_buffer = *shbuf.value;
  117. // We need to clone all references (including for global shared buffers),
  118. // and the reference counts as well.
  119. for (auto& ref : shared_buffer.m_refs) {
  120. if (ref.pid == from_process.pid()) {
  121. auto ref_count = ref.count;
  122. shared_buffer.m_refs.append(Reference(with_process.pid(), ref_count));
  123. // NOTE: ref may become invalid after we appended!
  124. shared_buffer.m_total_refs += ref_count;
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. void SharedBuffer::deref_for_process(Process& process)
  131. {
  132. LOCKER(shared_buffers().lock());
  133. for (size_t i = 0; i < m_refs.size(); ++i) {
  134. auto& ref = m_refs[i];
  135. if (ref.pid == process.pid()) {
  136. ASSERT(ref.count > 0);
  137. ref.count--;
  138. ASSERT(m_total_refs > 0);
  139. m_total_refs--;
  140. if (ref.count == 0) {
  141. #ifdef SHARED_BUFFER_DEBUG
  142. dbg() << "Releasing shared buffer reference on " << m_shbuf_id << " of size " << size() << " by PID " << process.pid().value();
  143. #endif
  144. process.deallocate_region(*ref.region.unsafe_ptr()); // TODO: Region needs to be RefCounted!
  145. #ifdef SHARED_BUFFER_DEBUG
  146. dbg() << "Released shared buffer reference on " << m_shbuf_id << " of size " << size() << " by PID " << process.pid().value();
  147. #endif
  148. sanity_check("deref_for_process");
  149. destroy_if_unused();
  150. return;
  151. }
  152. return;
  153. }
  154. }
  155. ASSERT_NOT_REACHED();
  156. }
  157. bool SharedBuffer::disown(ProcessID pid)
  158. {
  159. LOCKER(shared_buffers().lock());
  160. for (size_t i = 0; i < m_refs.size(); ++i) {
  161. auto& ref = m_refs[i];
  162. if (ref.pid == pid) {
  163. #ifdef SHARED_BUFFER_DEBUG
  164. dbg() << "Disowning shared buffer " << m_shbuf_id << " of size " << size() << " by PID " << pid.value();
  165. #endif
  166. ASSERT(m_total_refs >= ref.count);
  167. m_total_refs -= ref.count;
  168. m_refs.unstable_take(i);
  169. #ifdef SHARED_BUFFER_DEBUG
  170. dbg() << "Disowned shared buffer " << m_shbuf_id << " of size " << size() << " by PID " << pid.value();
  171. #endif
  172. destroy_if_unused();
  173. break;
  174. }
  175. }
  176. return m_total_refs == 0;
  177. }
  178. void SharedBuffer::destroy_if_unused()
  179. {
  180. LOCKER(shared_buffers().lock());
  181. sanity_check("destroy_if_unused");
  182. if (m_total_refs == 0) {
  183. #ifdef SHARED_BUFFER_DEBUG
  184. dbg() << "Destroying unused SharedBuffer{" << this << "} id: " << m_shbuf_id;
  185. #endif
  186. auto count_before = shared_buffers().resource().size();
  187. shared_buffers().resource().remove(m_shbuf_id);
  188. ASSERT(count_before != shared_buffers().resource().size());
  189. }
  190. }
  191. void SharedBuffer::seal()
  192. {
  193. LOCKER(shared_buffers().lock());
  194. m_writable = false;
  195. for (auto& ref : m_refs) {
  196. // TODO: Region needs to be RefCounted!
  197. if (auto* region = ref.region.unsafe_ptr()) {
  198. region->set_writable(false);
  199. region->remap();
  200. }
  201. }
  202. }
  203. auto SharedBuffer::set_volatile_all(bool is_volatile, bool& was_purged) -> SetVolatileError
  204. {
  205. was_purged = false;
  206. auto pid = Process::current()->pid();
  207. LOCKER(shared_buffers().lock());
  208. for (size_t i = 0; i < m_refs.size(); ++i) {
  209. auto& ref = m_refs[i];
  210. if (ref.pid == pid) {
  211. if (Region* region = ref.region.unsafe_ptr()) {
  212. switch (region->set_volatile(region->vaddr(), region->size(), is_volatile, was_purged)) {
  213. case Region::SetVolatileError::Success:
  214. return SetVolatileError::Success;
  215. case Region::SetVolatileError::NotPurgeable:
  216. return SetVolatileError::NotPurgeable;
  217. case Region::SetVolatileError::OutOfMemory:
  218. return SetVolatileError::OutOfMemory;
  219. }
  220. }
  221. }
  222. }
  223. return SetVolatileError::NotMapped;
  224. }
  225. }