I am to pass data between a kernel module and a user space program, through a specific way (for a POC).
I have written a module which allocates a struct page using alloc_page() function, and writes some data on it. my module code is below
#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>      // included for __init and __exit macros
#include<linux/kthread.h>
#include<linux/sched.h>
#include<linux/delay.h>
#include<linux/slab.h>
#include <linux/mm_types.h>
#include <linux/mm.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("POC");
struct task_struct *task1;
struct page * p1;
int cpu, data_pass;
int thread_function_one(void *data_old)
{
    char * data;
    int i;
    p1 = alloc_page(GFP_KERNEL);
    data = (char *)page_address(p1);
    for(i = 0;i < 5; i++)
        data[i] = 'x';
    while(!kthread_should_stop()){
        printk("Thread 1\n");
        printk("Virt = %p\n", page_to_virt(p1));
        printk("Phys %llx\n", page_to_phys(p1));
        printk("page_address %p\n", page_address(p1));
        printk("offset_in_page %ld\n", offset_in_page(p1));
        for(i = 0;i < 5; i++)
            printk("data %c", data[i]);
        printk("\n");
       msleep(5000);
    }
    printk(KERN_INFO "EXIT from thread function 1\n");
    return 0;
}
static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    task1 = kthread_create(&thread_function_one,(void *)&data_pass,"one");
    wake_up_process(task1);
    return 0;    // Non-zero return means that the module couldn't be loaded.
}
static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
    kthread_stop(task1);
}
module_init(hello_init);
module_exit(hello_cleanup);
I am getting the output,
[  201.976198] Phys 3c183c000
[  201.976200] page_address 0000000066f018d8
[  201.976202] offset_in_page 3840
[  201.976204] data x
[  201.976206] data x
[  201.976207] data x
[  201.976209] data x
[  201.976210] data x
On the user side, I am trying to mmap that physical memory and read the data. I refereed this question for the user side code.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: %s <phys_addr> <offset>\n", argv[0]);
        return 0;
    }
    off_t offset = strtoul(argv[1], NULL, 0);
    size_t len = strtoul(argv[2], NULL, 0);
    // Truncate offset to a multiple of the page size, or mmap will fail.
    size_t pagesize = sysconf(_SC_PAGE_SIZE);
    off_t page_base = (offset / pagesize) * pagesize;
    off_t page_offset = offset - page_base;
    int fd = open("/dev/mem", O_SYNC);
    unsigned char *mem = mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
    if (mem == MAP_FAILED) {
        perror("Can't map memory");
        return -1;
    }
    size_t i;
    for (i = 0; i < 10; ++i)
    {
        printf("%02x \n", (int)mem[page_offset + i]);
        printf("char 1 %c\n", mem[page_offset + i]);
        printf("char 2 %c\n", mem[i]);
        printf("\n\n");
    }
    return 0;
}
But, the output on the user side is garbage.
f0
char 1 ▒
char 2 S
53
char 1 S
char 2 ▒
ff
char 1 ▒
char 2
00
char 1
char 2 ▒
f0
char 1 ▒
char 2 S
What am I doing wrong?
