SharedBuffer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <Kernel/Process.h>
  27. #include <Kernel/SharedBuffer.h>
  28. namespace Kernel {
  29. Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers()
  30. {
  31. static Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>* map;
  32. if (!map)
  33. map = new Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>;
  34. return *map;
  35. }
  36. void SharedBuffer::sanity_check(const char* what)
  37. {
  38. LOCKER(shared_buffers().lock());
  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 << ": refcnt " << ref.count;
  46. }
  47. ASSERT_NOT_REACHED();
  48. }
  49. }
  50. bool SharedBuffer::is_shared_with(pid_t peer_pid)
  51. {
  52. LOCKER(shared_buffers().lock());
  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->make_weak_ptr();
  84. ref.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->vaddr().as_ptr();
  90. }
  91. }
  92. ASSERT_NOT_REACHED();
  93. }
  94. void SharedBuffer::share_with(pid_t 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::deref_for_process(Process& process)
  110. {
  111. LOCKER(shared_buffers().lock());
  112. for (size_t i = 0; i < m_refs.size(); ++i) {
  113. auto& ref = m_refs[i];
  114. if (ref.pid == process.pid()) {
  115. ref.count--;
  116. m_total_refs--;
  117. if (ref.count == 0) {
  118. #ifdef SHARED_BUFFER_DEBUG
  119. dbg() << "Releasing shared buffer reference on " << m_shbuf_id << " of size " << size() << " by PID " << process.pid();
  120. #endif
  121. process.deallocate_region(*ref.region);
  122. m_refs.unstable_remove(i);
  123. #ifdef SHARED_BUFFER_DEBUG
  124. dbg() << "Released shared buffer reference on " << m_shbuf_id << " of size " << size() << " by PID " << process.pid();
  125. #endif
  126. sanity_check("deref_for_process");
  127. destroy_if_unused();
  128. return;
  129. }
  130. return;
  131. }
  132. }
  133. ASSERT_NOT_REACHED();
  134. }
  135. void SharedBuffer::disown(pid_t pid)
  136. {
  137. LOCKER(shared_buffers().lock());
  138. for (size_t i = 0; i < m_refs.size(); ++i) {
  139. auto& ref = m_refs[i];
  140. if (ref.pid == pid) {
  141. #ifdef SHARED_BUFFER_DEBUG
  142. dbg() << "Disowning shared buffer " << m_shbuf_id << " of size " << size() << " by PID " << pid;
  143. #endif
  144. m_total_refs -= ref.count;
  145. m_refs.unstable_remove(i);
  146. #ifdef SHARED_BUFFER_DEBUG
  147. dbg() << "Disowned shared buffer " << m_shbuf_id << " of size " << size() << " by PID " << pid;
  148. #endif
  149. destroy_if_unused();
  150. return;
  151. }
  152. }
  153. }
  154. void SharedBuffer::destroy_if_unused()
  155. {
  156. LOCKER(shared_buffers().lock());
  157. sanity_check("destroy_if_unused");
  158. if (m_total_refs == 0) {
  159. #ifdef SHARED_BUFFER_DEBUG
  160. dbg() << "Destroying unused SharedBuffer{" << this << "} id: " << m_shbuf_id;
  161. #endif
  162. auto count_before = shared_buffers().resource().size();
  163. shared_buffers().resource().remove(m_shbuf_id);
  164. ASSERT(count_before != shared_buffers().resource().size());
  165. }
  166. }
  167. void SharedBuffer::seal()
  168. {
  169. LOCKER(shared_buffers().lock());
  170. m_writable = false;
  171. for (auto& ref : m_refs) {
  172. if (ref.region) {
  173. ref.region->set_writable(false);
  174. ref.region->remap();
  175. }
  176. }
  177. }
  178. }