Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible inconsistencies for sys_memory mem stats report #6159

Merged
merged 1 commit into from Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions rpcs3/Emu/Cell/lv2/sys_memory.cpp
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
10 changes: 8 additions & 2 deletions rpcs3/Emu/Memory/vm.cpp
Expand Up @@ -514,9 +514,15 @@ namespace vm

bool check_addr(u32 addr, u32 size, u8 flags)
{
for (u32 i = addr / 4096; i <= (addr + size - 1) / 4096; i++)
// Overflow checking
if (addr + size < addr && (addr + size) != 0)
{
return false;
}

for (u32 i = addr / 4096, max = (addr + size - 1) / 4096; i <= max; i++)
{
if (UNLIKELY((g_pages[i % g_pages.size()].flags & flags) != flags))
if (UNLIKELY((g_pages[i].flags & flags) != flags))
{
return false;
}
Expand Down