SharedBuffer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. dbg() << what << " sanity -- SharedBuffer{" << this << "} id: " << m_shbuf_id << " has total refs " << m_total_refs << " but we found " << found_refs;
  44. for (const auto& ref : m_refs) {
  45. dbg() << " ref from pid " << ref.pid.value() << ": refcnt " << ref.count;
  46. }
  47. ASSERT_NOT_REACHED();
  48. }
  49. }
  50. bool SharedBuffer::is_shared_with(ProcessID peer_pid) const
  51. {
  52. LOCKER(shared_buffers().lock(), Lock::Mode::Shared);
  53. if (m_global)
  54. return true;
  55. for (auto& ref : m_refs) {
  56. if (ref.pid == peer_pid) {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. void* SharedBuffer::ref_for_process_and_get_address(Process& process)
  63. {
  64. LOCKER(shared_buffers().lock());
  65. ASSERT(is_shared_with(process.pid()));
  66. if (m_global) {
  67. bool found = false;
  68. for (auto& ref : m_refs) {
  69. if (ref.pid == process.pid()) {
  70. found = true;
  71. break;
  72. }
  73. }
  74. if (!found)
  75. m_refs.append(Reference(process.pid()));
  76. }
  77. for (auto& ref : m_refs) {
  78. if (ref.pid == process.pid()) {
  79. if (!ref.region) {
  80. auto* region = process.allocate_region_with_vmobject(VirtualAddress(), size(), m_vmobject, 0, "SharedBuffer", PROT_READ | (m_writable ? PROT_WRITE : 0));
  81. if (!region)
  82. return (void*)-ENOMEM;
  83. ref.region = region;
  84. region->set_shared(true);
  85. }
  86. ref.count++;
  87. m_total_refs++;
  88. sanity_check("ref_for_process_and_get_address");
  89. return ref.region.unsafe_ptr()->vaddr().as_ptr(); // TODO: Region needs to be RefCounted!
  90. }
  91. }
  92. ASSERT_NOT_REACHED();
  93. }
  94. void SharedBuffer::share_with(ProcessID peer_pid)
  95. {
  96. LOCKER(shared_buffers().lock());
  97. if (m_global)
  98. return;
  99. for (auto& ref : m_refs) {
  100. if (ref.pid == peer_pid) {
  101. // don't increment the reference count yet; let them shbuf_get it first.
  102. sanity_check("share_with (old ref)");
  103. return;
  104. }
  105. }
  106. m_refs.append(Reference(peer_pid));
  107. sanity_check("share_with (new ref)");
  108. }
  109. void SharedBuffer::share_all_shared_buffers(Process& from_process, Process& with_process)
  110. {
  111. LOCKER(shared_buffers().lock());
  112. for (auto& shbuf : shared_buffers().resource()) {
  113. auto& shared_buffer = *shbuf.value;
  114. if (shared_buffer.m_global)
  115. continue;
  116. for (auto& ref : shared_buffer.m_refs) {
  117. if (ref.pid == from_process.pid()) {
  118. shared_buffer.share_with(with_process.pid());
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. void SharedBuffer::deref_for_process(Process& process)
  125. {
  126. LOCKER(shared_buffers().lock());
  127. for (size_t i = 0; i < m_refs.size(); ++i) {
  128. auto& ref = m_refs[i];
  129. if (ref.pid == process.pid()) {
  130. ref.count--;
  131. m_total_refs--;
  132. if (ref.count == 0) {
  133. #ifdef SHARED_BUFFER_DEBUG
  134. dbg() << "Releasing shared buffer reference on " << m_shbuf_id << " of size " << size() << " by PID " << process.pid().value();
  135. #endif
  136. process.deallocate_region(*ref.region.unsafe_ptr()); // TODO: Region needs to be RefCounted!
  137. #ifdef SHARED_BUFFER_DEBUG
  138. dbg() << "Released shared buffer reference on " << m_shbuf_id << " of size " << size() << " by PID " << process.pid().value();
  139. #endif
  140. sanity_check("deref_for_process");
  141. destroy_if_unused();
  142. return;
  143. }
  144. return;
  145. }
  146. }
  147. ASSERT_NOT_REACHED();
  148. }
  149. void SharedBuffer::disown(ProcessID pid)
  150. {
  151. LOCKER(shared_buffers().lock());
  152. for (size_t i = 0; i < m_refs.size(); ++i) {
  153. auto& ref = m_refs[i];
  154. if (ref.pid == pid) {
  155. #ifdef SHARED_BUFFER_DEBUG
  156. dbg() << "Disowning shared buffer " << m_shbuf_id << " of size " << size() << " by PID " << pid.value();
  157. #endif
  158. m_total_refs -= ref.count;
  159. m_refs.unstable_take(i);
  160. #ifdef SHARED_BUFFER_DEBUG
  161. dbg() << "Disowned shared buffer " << m_shbuf_id << " of size " << size() << " by PID " << pid.value();
  162. #endif
  163. destroy_if_unused();
  164. return;
  165. }
  166. }
  167. }
  168. void SharedBuffer::destroy_if_unused()
  169. {
  170. LOCKER(shared_buffers().lock());
  171. sanity_check("destroy_if_unused");
  172. if (m_total_refs == 0) {
  173. #ifdef SHARED_BUFFER_DEBUG
  174. dbg() << "Destroying unused SharedBuffer{" << this << "} id: " << m_shbuf_id;
  175. #endif
  176. auto count_before = shared_buffers().resource().size();
  177. shared_buffers().resource().remove(m_shbuf_id);
  178. ASSERT(count_before != shared_buffers().resource().size());
  179. }
  180. }
  181. void SharedBuffer::seal()
  182. {
  183. LOCKER(shared_buffers().lock());
  184. m_writable = false;
  185. for (auto& ref : m_refs) {
  186. // TODO: Region needs to be RefCounted!
  187. if (auto* region = ref.region.unsafe_ptr()) {
  188. region->set_writable(false);
  189. region->remap();
  190. }
  191. }
  192. }
  193. }