Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all 12288 articles
Browse latest View live

linux get_user function unexpected behaviour

$
0
0

I am trying to implement a one-byte char device driver for the linux kernel, as part of my operating system module assignment. To implement the device driver, I have to define the read and write function. Below is my device write function

ssize_t onebyte_write(struct file *filep, const char *buf, size_t count, loff_t *f_pos)
{
    if (get_user(*onebyte_data, buf) != 0)
        return 0;
    if (count > 1)
        printk(KERN_ALERT "Error: No space left on device\n");
    return 1;
}

'onebyte_data' is a char pointer to a dynamically allocated one byte memory in the kernel space. From my understanding, 'get_user' is supposed to copy a simple variable (int or char) from user space to kernel space. But when I execute the following set of command

printf abc > /dev/onebyte
cat /dev/onebyte

The result is a 'c' instead of a 'a', meaning get_user has read the whole input string instead of just the first character, and store the last character 'c' instead. Is this the expected behaviour? What should I do if I just want to store the first character of the input in onebyte_data?


Linux kernel module fixed execution time

$
0
0

I'm developing kernel module where in probe function there is some chunk of code which consists of spi_writes/spi_reads... I need these chunk of code to have its execution time as fixed as possible (for example 200'000 ns straight)... What kernel hacks can I use to achieve these? (My kernel is 4.14.40 and it's not an RTLinux kernel).

Can I disable ext4 journaling for some specific files rather than the whole filesystem? [closed]

$
0
0

I know I can discard the "had_journal" feature for a ext4 partition.

But is there a way I can disable journaling for some files and enable journaling for the other files at the same time?

Checking list_empty without a lock

$
0
0

I'm wondering about the thread safety of the linux kernel linked list. Suppose one thread adds items to a list while another thread reads the list. Of course while modifying the list I need to take a lock/mutex. But does checking if the list is empty using list_empty already require a lock?

Looking at the source of list_empty:

static inline int list_empty(const struct list_head *head)
{
    return READ_ONCE(head->next) == head;
}

we see that it uses READ_ONCE. AFAIK this prevents certain compiler optimizations and is also atomic for properly aligned/sized variables. Because there is no memory barrier I get no ordering with other memory accesses whatsoever, but the list will eventually become not empty after some other thread added an item. So I believe a lock is not strictly necessary for list_empty.

I'm asking because I want to use list_empty as a condition for wait_event_interruptible.

Writing to a "application/octet-stream" file on linux

$
0
0

I am working on application which should block some USB devices.

I have found a way how could be blocking done. Problem is, as it's written here, that I need to write some string into /sys/bus/usb/drivers_probe file. Mentioned file is application/octet-stream and I can't find a way how to read or write to this file.

I have tried vim, echo, hexdump with sudo or as root, but every time I get "Permission denied" or "No such device" message. I did not tried it in C/C++, which is my app using, but I guess it would bring same result.

Can anyone help me understand how kernel developers meant writing to that file?

What is the use of vmlinux file generated when we compile linux kernel

$
0
0

I am compiling Linux Kernel to my ARM board. I have seen file called vmlinux generated in kernel root folder. Can someone give good explanation about this file and it's use.

How to trace a fix process’s wakeup latency?

$
0
0

I want to use ftrace to trace a fix process’s wakeup latency. But, ftrace will only record the max latency. And, set_ftrace_pid is useless.

Does anybody know how to do that?

Thank you very much.

How to invoke platform specific driver in host machine?

$
0
0

I am writing a dummy phy driver as a LKM. This is a platform driver and I can't call the driver probe since it depends on the compatible string. How can I make my driver invoke probe in my host machine, which is running Ubuntu 18.04, kernel version - 5.3.0-40-generic.

Here is my code

static int phy_platform_probe(struct platform_device *pdev) { 
  struct custom_port_phy_platform_local *lp;
  int ret;
  printk(KERN_ALERT "abc..phy_platform_probe..Invoked\r\n");
  lp  = devm_kzalloc(&pdev->dev,sizeof(struct custom_port_phy_platform_local),GFP_KERNEL);
  if (!lp) {
      dev_err(&(pdev->dev),"Failed to allocatate platform_local\n");

        return -ENOMEM;
    }
    platform_set_drvdata(pdev, lp);
    lp->dev = &pdev->dev;
    ret = custom_port_phy_mdio_setup(lp, pdev->dev.of_node);
    if (ret < 0) {
      dev_err(&(pdev->dev),"Failed to setup MDIO bus\n");
      return ret;
    }
    return 0;
}
static int phy_platform_remove(struct platform_device *pdev) { 
  struct custom_port_phy_platform_local *lp = platform_get_drvdata(pdev);
  dev_info(&pdev->dev,"%s Enter\n",__func__);
  custom_port_phy_mdio_teardown(lp);
    return 0;
}
static struct phy_driver custom_phy_driver = {
  .phy_id = 0x00000002,
  .phy_id_mask = 0xffffffff,
  .name = "custom_port_phy",
};

static struct platform_driver custom_phy_platform_driver = {
 .probe = phy_platform_probe,
 .remove = phy_platform_remove,
 #if 1
 .driver = {
    .name = "custom_port_phy"
    .of_match_table = port_phy_of_match,
 }
 #endif
};

#if 1
/* Match table for of_platform binding */
static struct of_device_id port_phy_of_match[] = {
    { .compatible = "custom,custom-port-phy", },
    {},
};
#endif

static int __init phy_init(void)
{
   int ret = 0;
   printk(KERN_ALERT "abc >>> phy_driver_register started\r\n");
   ret = phy_driver_register(&custom_phy_driver, THIS_MODULE);
   printk(KERN_ALERT "abc >>> phy_driver_register END\r\n");
   if(ret < 0) {
       printk(KERN_ALERT "custom phy driver registration failed\r\n");
       return ret;
   }
   ret = platform_driver_register(&custom_phy_platform_driver);
   if(ret < 0) {
       phy_driver_unregister(&custom_phy_driver);
       printk("%s: Failed to register as Platform Driver\r\n", __func__);
       return ret;
   }
   return ret;
}

static void __exit phy_exit(void)
{
    pr_info("Goodbye phy_driver .\n");
    phy_driver_unregister(&custom_phy_driver);
    platform_driver_unregister(&custom_phy_platform_driver);
}
module_init(phy_init);
module_exit(phy_exit);
MODULE_LICENSE("GPL");

My code doesn't do probe since it can't find compatible flag. How can I make it work on my host machine? I know I need dtb file. Even if I have dtb file, how do I supply it and how to write it that way.


Increasing time slices for a particular process via implementing a system call in xv6

$
0
0

I am trying to implement a system call in xv6 OS Increase_time( int n) which when executed will increase the timeslice of a program that calls it by n times. The default xv6 scheduler uses a simple FCFS and RR policies with each process having the same time slice. My implementation of Increase_time() will allow different processes to have different amount of time slices.

Can you please tell me a way how I can work around this? I know how to add a system call in xv6. I just need an idea as to how I can code my system call and what files to change in xv6.

The calculation of process descriptor address in kernel stack

$
0
0

In this book (understanding Linux kernel),

the kernel can easily obtain the address of the thread_info structure of the process currently running on a CPU from the value of the esp register. In fact, if the thread_union structure is 8 KB (213 bytes) long, the kernel masks out the 13 least significant bits of esp to obtain the base address of the thread_info structure; on the other hand, if the thread_union struc- ture is 4 KB long, the kernel masks out the 12 least significant bits of esp. This is done by the current_thread_info() function, which produces assembly language instructions like the following:

 movl $0xffffe000,%ecx or 0xfffff000 for 4KB stacks 
 andl %esp,%ecx
 movl %ecx,p

Why is stack pointer & 0xffffe000 equal to the process descriptor base address?

That mean the base address always is 0xXYZ...000, right? It is weird.

gbuild: failed to run on target setarch x86_64 bash -x

Run script in every day in specified time in linux

$
0
0

I'm newer to linux operating system. I want to run the script every day at 12 AM. If system is shutdown. It should execute when boot the system. There are some scenario system can boot many times in one day(with in 12AM). This time system should not execute script.

there are some scenario system is down till next day. That time it should check weather script executed previous day or not.

Add New Kernel Parameter To Custom Linux Image Generated By Yocto

$
0
0

I am experimenting with Yocto project for generating custom Linux images for my embedded devices.

I have a requirement to add a persistent custom kernel parameter to /etc/sysctl.conf of the generated image.

i.e.

kernel.core_pipe_limit = 1

/etc/sysctl.conf is generated by procps package that comes with Yocto base system (meta/recipes-extended/procps/procps/sysctl.conf). However, I believe editing the sysctl.conf in the base system is not the recommended approach.

I am using a new layer for defining my custom configurations. I hope there is a way to apply a patch to a base package via a custom layer after deploying the base layer.

How can I do this?


I am aware how to persistently change a kernel variable by updating /etc/sysctl.conf (or, preferably, /etc/sysctl.d/xxx.conf). My question is, how to generate the Linux image with the necessary update applied?

Finding Physical page number from the virtual address of kernel BSS section

$
0
0

I am using 32-bit x86 architecture, I have taken one global variable in kernel space, so memory for that variable will be assigned in the BSS section of kernel's virtual memory. now I have a virtual address of that global variable and using that virtual address I want to read physical page number. Is it possible to read a physical page number? If yes, How?

Can anyone please explain what are a drambase and kas_physbase?

Keyboard interrupt handler that outputs custom scancodes doesn't work - linux

$
0
0

I want to create a kernel module that registers an interrupt handler for the keyboard. The interrupt handler will output custom scancodes instead of the ones the user actually typed. It doeesn't work - I see with debug prints that the scancode I output is the one I want, but the real characters are outputed. Even if I make a handler that doesn't output anything, the real characters are outputted.

Here is my code:

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */
#include <linux/interrupt.h>
#include <asm/io.h>

MODULE_LICENSE("GPL");

#define KBD_IRQ             1       /* IRQ number for keyboard (i8042) */
#define KBD_DATA_REG        0x60    /* I/O port for keyboard data */
#define KBD_SCANCODE_MASK   0x7f

static int current_index = 0;
static int text_len = 2;
static int scancodes[2] = {0x15, 0x1e};

static irqreturn_t keyboard_intr_handler(int irq, void *dev)
{
    printk(KERN_INFO "in interrupt handler\n");
    char scancode = scancodes[current_index];
    printk(KERN_INFO "got scancode %x\n", scancode);
    current_index++;
    if (text_len == current_index)
        current_index = 0;
    printk(KERN_INFO "gonna output scancode\n");
    outb(KBD_DATA_REG, scancode);
    printk(KERN_INFO "outputed scancode\n");
    return IRQ_HANDLED;
}

static int __init my_register_interrupt(void)
{
    printk(KERN_INFO "in init\n");
    if (request_irq(KBD_IRQ, keyboard_intr_handler, IRQF_SHARED,
        "i8042", (void*) keyboard_intr_handler)) {
        printk(KERN_ERR "my_device: cannot register IRQ 1\n");
        return -EIO;
    }
    return 0;
}

static void __exit my_unregister_interrupt(void)
{
    free_irq(1, (void*) keyboard_intr_handler);
}

module_init(my_register_interrupt);
module_exit(my_unregister_interrupt);

Thanks in advance!


Device fails to boot when build with binutils 2.31 or higer

$
0
0

I am trying to upgrade my current Buildroot build from 2019.11 to the newest 2020.02 release. With 2019.11 everything builds and produces a bootable image. With the 2020.02 release everything still builds, but the device no longer boots and does not output any booting messages (no log data and no out via serial).

The Issue seems to be, the binutils version. In 2020.02 binutils 2.30 is no longer supported and the min version is 2.31.1 (this is the only legacy option in my config when upgrading).

Is this a known issue? are there any workarounds?

When I try building the 4.14 Kernel the build aborts during Kernel compilation. This post ecnoutered the same issues I had during the compilation of the 4.14 Kernel. Si I guess a Kernel upgrade is not a solution.

Graphics: Can the frame buffer be allocated anywhere in memory?

$
0
0

I'm trying to figure out if the frame buffer - not some software concept, but the actual piece of memory holding the final video frame that is scanned out by the hardware to output it on a display - is in a special memory location.

Or is it the case that some memory is allocated at boot time somewhat randomly (maybe with some constraints applied), this memory gets designated as the frame buffer, and then the scan-out hardware is told at what address it resides?

Call to request_module() does not return in Linux Kernel

$
0
0

I am currently struggling with signing files with IMA/EVM signatures.

Similar things were already done on an embedded Linux system based on 3.14.79. Now I am dealing with a system based on Linux 4.14.98. The problem is not the signing itself but the trouble starts when the new signatures are verified.

The system is currently in non-enforcing state, i.e. a missing or incorrect signature does not cause program termination.

In error case, the system is simply stuck during signature verification. If it happens after entering a command on the shell, I can break with Ctrl-C, but if it happens during boot, that's it.

During my bug hunt I sorted out a few bugs in the kernel related to keyring search and integrity keyring cache. But now I am stuck.

I placed a few printk logs in the code and saw that the problem is in this code:

struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
{
...
    alg = crypto_alg_lookup(name, type, mask);
    if (!alg && !(mask & CRYPTO_NOLOAD)) {
        request_module("crypto-%s", name);
...

Where name holds "pkcs1pad(rsa,sha256)"

In some cases this function call returns, in other cases it does not return until I enter ´Ctrl-C` or not at all.

I am not sure where to look for further information, wrong configuration, missing flags or any other hint how to solve the issue.

phandle kernel not looking on right address

$
0
0

I'm facing an issue with the kernel at boot time for one driver especially. The phandle where he's looking for finding the node is not the one that I see on the dts file decompiled.

Here is the boot logs :

[   28.110920] From list_name:avdd-supply
[   28.110953] /soc/i2c@75b5000/pcm1864@4b: could not find phandle addr:0x3eb
[   28.116603] 7-004b supply avdd not found, using dummy regulator
[   28.124287] From list_name:dvdd-supply
[   28.129168] /soc/i2c@75b5000/pcm1864@4b: could not find phandle addr:0x3ec
[   28.135623] 7-004b supply dvdd not found, using dummy regulator
[   28.143282] From list_name:iovdd-supply
[   28.148178] /soc/i2c@75b5000/pcm1864@4b: could not find phandle addr:0x3ed
[   28.154621] 7-004b supply iovdd not found, using dummy regulator

The fullpath is correct for example /soc/i2c@75b5000/pcm1864@4b. He's looking for phandle of property : avdd-supply at the first line and he's looking at 0x3eb physical address I guess.

When I decompile the DTBO file I see an other phandle value, let me fully print it here:

    pcm1864@4b {
        compatible = "ti,pcm1864";
        reg = <0x4b>;
        avdd-supply = <0x22>;
        dvdd-supply = <0x23>;
        iovdd-supply = <0x24>;
        phandle = <0x1e>;
    };

As you can see the phandle is 0x22 for the first one for example

So why Kernel is looking for node at 0x3eb instead of 0x22 ?

Thanks for help

What makes `insmod` running infinite when trying syscall hook application in Ubuntu?

$
0
0

I'm struggling with my assignment which gives a taste of Linux system call hooking ops. What I mean is to attach a hook onto sys_rt_sigaction call using a kernel module approach. The module stuff looks as below:

#define __KERNEL__
#define MODULE

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/unistd.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/syscalls.h>
#include <asm/unistd.h>

void **sys_call_table;

MODULE_AUTHOR("author_name");
MODULE_DESCRIPTION("Linux kernel hook program");
MODULE_LICENSE("GPL");

asmlinkage int (*real_rt_sigaction)(int signum, const struct sigaction *act,
                                    struct sigaction *oldact, size_t sigsetsize);

static int __init buffer_init_module(void);
static void __exit buffer_exit_module(void);

asmlinkage int alter_rt_sigaction(int signum, const struct sigaction *act,
                       struct sigaction *oldact, size_t sigsetsize) {
    printk(KERN_INFO "Syscall function hooked - just what I want");
    return 0;
}

int buffer_init_module(void)
{
    printk(KERN_INFO
    "Device initializing in progress...");   

    real_rt_sigaction=sys_call_table[__NR_rt_sigaction];
    sys_call_table[__NR_rt_sigaction]=alter_rt_sigaction;

    return 0;
}

void buffer_exit_module(void)
{

    printk(KERN_INFO "Outside the module - exit successfully completed\n");
    sys_call_table[__NR_rt_sigaction]=real_rt_sigaction;
}

Makefile for that:

obj-m += buffer.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

It doesn't make any problem at first glance, however the program runs into infinite process once starting sudo insmod buffer.ko (make execution results into pretty smooth .ko file building). Local OS is Ubuntu 18.04. I've to provide with the code that attaches a hook to the specific syscall function, just nothing else. Seemingly, I miss some essential stuff in the actual program, most probably in makefile, but it lacks me an insight into whatever is wrong.

Viewing all 12288 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>