linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Yznaga <anthony.yznaga@oracle.com>
To: Yongting Lin <linyongting@bytedance.com>
Cc: khalid@kernel.org, shuah@kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	akpm@linux-foundation.org, linux-mm@kvack.org
Subject: Re: [External] Re: [PATCH 4/8] mshare: selftests: Add test case shared memory
Date: Tue, 2 Sep 2025 14:34:37 -0700	[thread overview]
Message-ID: <7d61c6b9-fef7-4b70-9172-effdd7e69457@oracle.com> (raw)
In-Reply-To: <CAFuXZ_UY6RrOVmTayW-DdxQX9TE978LPx7ad=Pzr01j1cZ4o2Q@mail.gmail.com>



On 9/1/25 5:50 AM, Yongting Lin wrote:
> On Fri, Aug 29, 2025 at 9:00 AM Anthony Yznaga
> <anthony.yznaga@oracle.com> wrote:
>>
>> Hi Yongting,
>>
>> Thank you for doing this. This is a great start for testing mshare.
>> I do have some comments below.
>>
>> On 8/25/25 7:57 AM, Yongting Lin wrote:
>>> This test case aims to verify the basic functionalities of mshare.
>>>
>>> Create a mshare file and use ioctl to create mapping for host mm
>>> with supportive flags, then create two processes to map mshare file
>>> to their memory spaces, and eventually verify the correctiness
>>> of sharing memory.
>>>
>>> Signed-off-by: Yongting Lin <linyongting@bytedance.com>
>>> ---
>>>    tools/testing/selftests/mshare/basic.c | 81 +++++++++++++++++++++++++-
>>>    1 file changed, 79 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/tools/testing/selftests/mshare/basic.c b/tools/testing/selftests/mshare/basic.c
>>> index 35739b1133f7..2347d30adfee 100644
>>> --- a/tools/testing/selftests/mshare/basic.c
>>> +++ b/tools/testing/selftests/mshare/basic.c
>>> @@ -3,9 +3,86 @@
>>>    #include "../kselftest_harness.h"
>>>    #include "util.c"
>>>
>>> -TEST(basic)
>>> +#define STRING "I am Msharefs"
>>> +
>>> +FIXTURE(basic)
>>> +{
>>> +     char filename[128];
>>> +     size_t align_size;
>>> +     size_t allocate_size;
>>> +};
>>> +
>>> +FIXTURE_VARIANT(basic) {
>>> +     /* decide the time of real mapping size besed on align_size */
>>> +     size_t map_size_time;
>>> +     /* flags for ioctl */
>>> +     int map_flags;
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(basic, ANON_512G) {
>>> +     .map_size_time = 1,
>>> +     .map_flags = MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED,
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(basic, HUGETLB_512G) {
>>> +     .map_size_time = 1,
>>> +     .map_flags = MAP_ANONYMOUS | MAP_HUGETLB | MAP_SHARED | MAP_FIXED,
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(basic, ANON_1T) {
>>> +     .map_size_time = 2,
>>> +     .map_flags = MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED,
>>> +};
>>> +
>>> +FIXTURE_VARIANT_ADD(basic, HUGETLB_1T) {
>>> +     .map_size_time = 2,
>>> +     .map_flags = MAP_ANONYMOUS | MAP_HUGETLB | MAP_SHARED | MAP_FIXED,
>>> +};
>>> +
>>> +FIXTURE_SETUP(basic)
>>>    {
>>> -     printf("Hello mshare\n");
>>> +     int fd;
>>> +
>>> +     self->align_size = mshare_get_info();
>>> +     self->allocate_size = self->align_size * variant->map_size_time;
>>> +
>>> +     fd = create_mshare_file(self->filename, sizeof(self->filename));
>>> +     ftruncate(fd, self->allocate_size);
>>> +
>>> +     ASSERT_EQ(mshare_ioctl_mapping(fd, self->allocate_size, variant->map_flags), 0);
>>
>> The tests should differentiate between how much VA space is allocated to
>> an mshare region (i.e with ftruncate()) and how much memory is allocated
>> within an mshare region through the ioctl. While the bounds of an mshare
>> region need to be aligned to 512 GB, the memory allocated within it does
>> not. Right now the tests will try to map 512 GB or 1 TB of anon or
>> hugetlb memory in an mshare region which will fail on smaller systems to
>> due to insufficient memory. Better to allocate smaller amounts so the
>> tests can run on more systems.
>>
>> Anthony
> 
> I Changed my code to allocate a smaller chunk of memory (i.e.
> 4K/8K/2M/4M), and these tests are passed.
> 
> But I found something different:
> step1:  ftruncate a mshare file to 512G
> step2:  ioctl map 8K
> step3: but after that, I am going to mmap 8K to a process but it
> fails, then I have up to mmap 512G memory to process.

This is correct. For page table sharing to work correctly the entire 
range of the mshare region must be mmap'd into the process even though 
the amount of memory actually mapped within the region via ioctl() may 
be much smaller.

> step4: Accessing the memory within the 8K boundary is fine but get
> segfault after exceling the boundary (as the vma of host mm only holds
> a memory region of 8K)
> 
> Should the mmap region keep consistent with the ioctl map region in
> size? (currently, ioctl map region is 8K, but mmap region is 512G)

It might help to think of the 512G mmap region as a window that gives 
the process visbility or access to the actual memory that is mapped 
within the mshare region.

Anthony


> 
> Yongting
>>
>>> +     close(fd);
>>> +}
>>> +
>>> +FIXTURE_TEARDOWN(basic)
>>> +{
>>> +     ASSERT_EQ(unlink(self->filename), 0);
>>> +}
>>> +
>>> +TEST_F(basic, shared_mem)
>>> +{
>>> +     int fd;
>>> +     void *addr;
>>> +     pid_t pid = fork();
>>> +
>>> +     ASSERT_NE(pid, -1);
>>> +
>>> +     fd = open(self->filename, O_RDWR, 0600);
>>> +     ASSERT_NE(fd, -1);
>>> +
>>> +     addr = mmap(NULL, self->allocate_size, PROT_READ | PROT_WRITE,
>>> +                    MAP_SHARED, fd, 0);
>>> +     ASSERT_NE(addr, MAP_FAILED);
>>> +
>>> +     if (pid == 0) {
>>> +             /* Child process write date the shared memory */
>>> +             memcpy(addr, STRING, sizeof(STRING));
>>> +             exit(0);
>>> +     }
>>> +
>>> +     ASSERT_NE(waitpid(pid, NULL, 0), -1);
>>> +
>>> +     /* Parent process should retrieve the data from the shared memory */
>>> +     ASSERT_EQ(memcmp(addr, STRING, sizeof(STRING)), 0);
>>>    }
>>>
>>>    TEST_HARNESS_MAIN
>>
> 


  reply	other threads:[~2025-09-02 21:35 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25 14:57 [PATCH 0/8] Add selftests for mshare Yongting Lin
2025-08-25 14:57 ` [PATCH 1/8] mshare: Add selftests Yongting Lin
2025-08-25 14:57 ` [PATCH 2/8] mshare: selftests: Adding config fragment Yongting Lin
2025-08-25 14:57 ` [PATCH 3/8] mshare: selftests: Add some helper function for mshare filesystem Yongting Lin
2025-08-25 14:57 ` [PATCH 4/8] mshare: selftests: Add test case shared memory Yongting Lin
2025-08-29  0:59   ` Anthony Yznaga
2025-09-01 12:50     ` [External] " Yongting Lin
2025-09-02 21:34       ` Anthony Yznaga [this message]
2025-08-25 14:57 ` [PATCH 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-08-25 14:57 ` [PATCH 6/8] mshare: selftests: Add some helper functions for reading and controlling cgroup Yongting Lin
2025-08-25 14:57 ` [PATCH 7/8] mshare: selftests: Add test case to demostrate the swaping of mshare memory Yongting Lin
2025-08-25 14:57 ` [PATCH 8/8] mshare: selftests: Add test case to demostrate that mshare doesn't support THP Yongting Lin
2025-08-29  1:04   ` Anthony Yznaga
2025-08-25 14:57 ` [PATCH 1/8] mshare: Add selftests Yongting Lin
2025-08-25 14:57 ` [PATCH 2/8] mshare: selftests: Adding config fragment Yongting Lin
2025-08-25 14:57 ` [PATCH 3/8] mshare: selftests: Add some helper function for mshare filesystem Yongting Lin
2025-08-25 14:57 ` [PATCH 4/8] mshare: selftests: Add test case shared memory Yongting Lin
2025-08-25 14:57 ` [PATCH 5/8] mshare: selftests: Add test case ioctl unmap Yongting Lin
2025-08-25 14:57 ` [PATCH 6/8] mshare: selftests: Add some helper functions for reading and controlling cgroup Yongting Lin
2025-08-25 14:57 ` [PATCH 7/8] mshare: selftests: Add test case to demostrate the swaping of mshare memory Yongting Lin
2025-08-25 14:57 ` [PATCH 8/8] mshare: selftests: Add test case to demostrate that mshare doesn't support THP Yongting Lin
2025-08-26 11:16 ` [PATCH 0/8] Add selftests for mshare Yongting Lin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7d61c6b9-fef7-4b70-9172-effdd7e69457@oracle.com \
    --to=anthony.yznaga@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=khalid@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linyongting@bytedance.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).