Skip to content

Commit

Permalink
Fix possible inconsistencies for sys_memory mem stats report
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Jul 4, 2019
1 parent ad10eb3 commit d869844
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions rpcs3/Emu/Cell/lv2/sys_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

LOG_CHANNEL(sys_memory);

//
static shared_mutex s_memstats_mtx;

lv2_memory_alloca::lv2_memory_alloca(u32 size, u32 align, u64 flags, const std::shared_ptr<lv2_memory_container>& ct)
: size(size)
, align(align)
Expand Down Expand Up @@ -202,6 +205,8 @@ error_code sys_memory_get_page_attribute(u32 addr, vm::ptr<sys_page_attr_t> attr

sys_memory.trace("sys_memory_get_page_attribute(addr=0x%x, attr=*0x%x)", addr, attr);

vm::reader_lock rlock;

if (!vm::check_addr(addr))
{
return CELL_EINVAL;
Expand Down Expand Up @@ -240,6 +245,8 @@ error_code sys_memory_get_user_memory_size(vm::ptr<sys_memory_info_t> mem_info)
// Get "default" memory container
const auto dct = fxm::get<lv2_memory_container>();

::reader_lock lock(s_memstats_mtx);

mem_info->total_user_memory = dct->size;
mem_info->available_user_memory = dct->size - dct->used;

Expand Down Expand Up @@ -268,6 +275,8 @@ error_code sys_memory_container_create(vm::ptr<u32> cid, u32 size)

const auto dct = fxm::get<lv2_memory_container>();

std::lock_guard lock(s_memstats_mtx);

// Try to obtain "physical memory" from the default container
if (!dct->take(size))
{
Expand All @@ -286,6 +295,8 @@ error_code sys_memory_container_destroy(u32 cid)

sys_memory.warning("sys_memory_container_destroy(cid=0x%x)", cid);

std::lock_guard lock(s_memstats_mtx);

const auto ct = idm::withdraw<lv2_memory_container>(cid, [](lv2_memory_container& ct) -> CellError
{
// Check if some memory is not deallocated (the container cannot be destroyed in this case)
Expand Down
9 changes: 7 additions & 2 deletions rpcs3/Emu/Memory/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,14 @@ namespace vm

bool check_addr(u32 addr, u32 size, u8 flags)
{
for (u32 i = addr / 4096; i <= (addr + size - 1) / 4096; i++)
for (u32 i = addr / 4096, max = static_cast<u32>((addr + u64{size} - 1) / 4096); i <= max; i++)
{
if (UNLIKELY((g_pages[i % g_pages.size()].flags & flags) != flags))
if (UNLIKELY(i >= g_pages.size()))
{
return false;
}

if (UNLIKELY((g_pages[i].flags & flags) != flags))
{
return false;
}
Expand Down

0 comments on commit d869844

Please sign in to comment.