Skip to content

Conversation

@lianux-mm
Copy link

@lianux-mm lianux-mm commented Jan 9, 2026

Dependencies

This PR depends on the API changes in ArceOS.
I have submitted a corresponding PR to Starry-OS/arceos to expose the necessary memory interfaces.

Upstream PR:

feat(mm): expose memory interfaces for procfs support (maps & meminfo) #39
Starry-OS/arceos#39

(Note: CI will fail with a fetch error until #39 is merged and the submodule pointer is updated.)


Summary

This PR implements the standard Linux interfaces /proc/pid/maps and /proc/meminfo for Starry.

Crucially, it also includes significant improvements to the underlying SeqFile infrastructure to correctly handle multi-page content generation, resolving issues where output would be truncated at 4KB.

Implementation Details

1. /proc/pid/maps

  • Standard Compliance: Aligned output format strictly with Linux AArch64 standards (12-digit hex addresses, path padding to column 73).
  • Region Identification: Added logic to automatically identify and label special regions like [heap] and [stack].

2. /proc/meminfo

  • Implemented global memory statistics reporting (Total, Free, Available).
  • Connects to the new global memory statistics API exposed in axmm.

3. SeqFile Infrastructure Improvements (Critical)

  • Large Output Support: Updated metadata to report a large virtual file size (1MB) instead of the physical buffer size. This bypasses the VFS layer's EOF check, allowing cat to read files larger than 4KB (e.g., a process with hundreds of VMAs).
  • Safety: Added output.fill(0) in the read path to prevent dirty data leakage.
  • State Machine: Refined offset tracking to ensure seamless multi-page reads.

Test result

1. /proc/pid/maps Validation

  • Used a custom C program to allocate a large number of VMAs (via mmap) to stress-test the SeqFile pagination logic.
  • Result: The system successfully handled the large output (> 4KB), correctly generating the maps file across multiple reads without truncation or duplication.

2. /proc/meminfo Validation

  • Verified that /proc/meminfo is implemented and correctly reports system memory
image

This commit implements the standard `/proc/pid/maps` interface and
significantly improves the underlying `SeqFile` infrastructure to support
large, multi-page content.

Key changes:

1. /proc/pid/maps:
   - Implemented VMA traversal to expose memory layout, permissions, and
     backing file paths.
   - Aligned output format strictly with Linux AArch64 standards (12-digit
     hex addresses, path padding to column 73).
   - Added automatic identification for [heap] and [stack] regions.

2. SeqFile Infrastructure Improvements:
   - VFS Size Bypass: Updated `metadata` to report a large virtual file size
     (1MB) instead of the physical buffer size (4KB). This is a critical fix
     that prevents the VFS layer from intercepting valid reads as EOF when
     the offset exceeds the initial buffer size, enabling correct multi-page
     reading.
   - Safety: Added `output.fill(0)` in the read path to prevent dirty data
     leakage into user buffers.
   - Buffering: Refined the state machine to handle buffer refills and
     offset tracking robustly.

Tested with a VMA stress test (120+ VMAs), confirming that data larger
than 4KB can be read sequentially without truncation or corruption.
@lianux-mm
Copy link
Author

lianux-mm commented Jan 9, 2026

Test program

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#define N_MAPS 4096
#define READ_BUF_SIZE 1024 

int main() {
    printf("=== Step 1: Spawning VMAs ===\n");
    void *addrs[N_MAPS];
    for (int i = 0; i < N_MAPS; i++) {
        int prot = (i % 2 == 0) ? PROT_READ : (PROT_READ | PROT_WRITE);
        addrs[i] = mmap(NULL, 4096, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (addrs[i] == MAP_FAILED) {
            perror("mmap failed");
            return 1;
        }
    }
    printf("Allocated %d VMAs.\n", N_MAPS);

    printf("\n=== Step 2: Reading /proc/self/maps ===\n");
    
    int fd = open("/proc/self/maps", O_RDONLY);
    if (fd < 0) {
        perror("Failed to open /proc/self/maps");
        return 1;
    }

    char buf[READ_BUF_SIZE];
    int total_bytes = 0;
    int n;
    

    while ((n = read(fd, buf, READ_BUF_SIZE - 1)) > 0) {
        buf[n] = '\0'; 
        printf("%s", buf);
        total_bytes += n;
    }

    if (n < 0) {
        perror("\nError reading file");
    }

    close(fd);

    printf("\n=== Step 3: Verification ===\n");
    printf("Total bytes read: %d\n", total_bytes);

    if (total_bytes > 4096) {
        printf("SUCCESS: File size > 4096 bytes. Multi-page reading works!\n");
    } else {
        printf("WARNING: File size <= 4096. Did not trigger multi-page logic.\n");
    }


    for (int i = 0; i < N_MAPS; i++) munmap(addrs[i], 4096);

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant