SharedBuffer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/Debug.h>
  27. #include <AK/Singleton.h>
  28. #include <Kernel/Process.h>
  29. #include <Kernel/SharedBuffer.h>
  30. //#define SHARED_BUFFER_DEBUG
  31. namespace Kernel {
  32. static AK::Singleton<Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>> s_map;
  33. Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers()
  34. {
  35. return *s_map;
  36. }
  37. void SharedBuffer::sanity_check(const char* what)
  38. {
  39. LOCKER(shared_buffers().lock(), Lock::Mode::Shared);
  40. unsigned found_refs = 0;
  41. for (const auto& ref : m_refs)
  42. found_refs += ref.count;
  43. if (found_refs != m_total_refs) {
  44. dbgln("{} sanity -- SharedBuffer({}) id: {} has total refs {} but we found {}",
  45. what,
  46. this,
  47. m_shbuf_id,
  48. m_total_refs,
  49. found_refs);
  50. for (const auto& ref : m_refs)
  51. dbgln(" ref from pid {}: reference count {}", ref.pid.value(), ref.count);
  52. ASSERT_NOT_REACHED();
  53. }
  54. }
  55. bool SharedBuffer::is_shared_with(ProcessID peer_pid) const
  56. {
  57. LOCKER(shared_buffers().lock(), Lock::Mode::Shared);
  58. for (auto& ref : m_refs) {
  59. if (ref.pid == peer_pid) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. void* SharedBuffer::ref_for_process_and_get_address(Process& process)
  66. {
  67. LOCKER(shared_buffers().lock());
  68. ASSERT(is_shared_with(process.pid()));
  69. for (auto& ref : m_refs) {
  70. if (ref.pid == process.pid()) {
  71. if (!ref.region) {
  72. auto region_or_error = process.allocate_region_with_vmobject(VirtualAddress(), size(), m_vmobject, 0, "SharedBuffer", PROT_READ | (m_writable ? PROT_WRITE : 0), true);
  73. if (region_or_error.is_error())
  74. return (void*)region_or_error.error().error();
  75. ref.region = region_or_error.value();
  76. }
  77. ref.count++;
  78. m_total_refs++;
  79. sanity_check("ref_for_process_and_get_address");
  80. return ref.region.unsafe_ptr()->vaddr().as_ptr(); // TODO: Region needs to be RefCounted!
  81. }
  82. }
  83. ASSERT_NOT_REACHED();
  84. }
  85. void SharedBuffer::share_with(ProcessID peer_pid)
  86. {
  87. LOCKER(shared_buffers().lock());
  88. for (auto& ref : m_refs) {
  89. if (ref.pid == peer_pid) {
  90. // don't increment the reference count yet; let them shbuf_get it first.
  91. sanity_check("share_with (old ref)");
  92. return;
  93. }
  94. }
  95. m_refs.append(Reference(peer_pid));
  96. sanity_check("share_with (new ref)");
  97. }
  98. void SharedBuffer::share_all_shared_buffers(Process& from_process, Process& with_process)
  99. {
  100. LOCKER(shared_buffers().lock());
  101. for (auto& shbuf : shared_buffers().resource()) {
  102. auto& shared_buffer = *shbuf.value;
  103. // We need to clone all references (including for global shared buffers),
  104. // and the reference counts as well.
  105. for (auto& ref : shared_buffer.m_refs) {
  106. if (ref.pid == from_process.pid()) {
  107. auto ref_count = ref.count;
  108. shared_buffer.m_refs.append(Reference(with_process.pid(), ref_count));
  109. // NOTE: ref may become invalid after we appended!
  110. shared_buffer.m_total_refs += ref_count;
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. void SharedBuffer::deref_for_process(Process& process)
  117. {
  118. LOCKER(shared_buffers().lock());
  119. for (size_t i = 0; i < m_refs.size(); ++i) {
  120. auto& ref = m_refs[i];
  121. if (ref.pid == process.pid()) {
  122. ASSERT(ref.count > 0);
  123. ref.count--;
  124. ASSERT(m_total_refs > 0);
  125. m_total_refs--;
  126. if (ref.count == 0) {
  127. dbgln<debug_shared_buffer>("Releasing shared buffer reference on {} of size {} by PID {}", m_shbuf_id, size(), process.pid().value());
  128. process.deallocate_region(*ref.region.unsafe_ptr()); // TODO: Region needs to be RefCounted!
  129. dbgln<debug_shared_buffer>("Released shared buffer reference on {} of size {} by PID {}", m_shbuf_id, size(), process.pid().value());
  130. sanity_check("deref_for_process");
  131. destroy_if_unused();
  132. return;
  133. }
  134. return;
  135. }
  136. }
  137. ASSERT_NOT_REACHED();
  138. }
  139. bool SharedBuffer::disown(ProcessID pid)
  140. {
  141. LOCKER(shared_buffers().lock());
  142. for (size_t i = 0; i < m_refs.size(); ++i) {
  143. auto& ref = m_refs[i];
  144. if (ref.pid == pid) {
  145. dbgln<debug_shared_buffer>("Disowning shared buffer {} of size {} by PID {}", m_shbuf_id, size(), pid.value());
  146. ASSERT(m_total_refs >= ref.count);
  147. m_total_refs -= ref.count;
  148. m_refs.unstable_take(i);
  149. dbgln<debug_shared_buffer>("Disowned shared buffer {} of size {} by PID {}", m_shbuf_id, size(), pid.value());
  150. destroy_if_unused();
  151. break;
  152. }
  153. }
  154. return m_total_refs == 0;
  155. }
  156. void SharedBuffer::destroy_if_unused()
  157. {
  158. LOCKER(shared_buffers().lock());
  159. sanity_check("destroy_if_unused");
  160. if (m_total_refs == 0) {
  161. dbgln<debug_shared_buffer>("Destroying unused SharedBuffer({}) id={}", this, m_shbuf_id);
  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. }