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

Single makefile that compiles kernel driver and .c file

$
0
0

I have a hello.c file that prints Hello World, and I also have another hello_driver.c file that prints Hello World to the kernellog.

I can compile hello_driver.c file and it prints out Hello World to the kernel log but I can't compile those 2 .c files in same Makefile program.

I've tried this Makefile but it is not working:

obj-o += hello.o hello_driver.o

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

clean:
     make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Why can low priority processes affect the latency of an high priority process?

$
0
0

I have tested the rt process's latency by cyclictest. And found that when I launch a few lower priority rt processes(priority is 40) the high priority rt process's latency will get bigger. I can't explain this.

cyclictest is one of the programs of rt-tests. https://github.com/jlelli/rt-tests/tree/master/src/cyclictest

Cyclictest measures the latency between when a timer expires and when the thread which set the timer actually runs. It does this by taking a time snapshot just prior to waiting for a specific time interval (t1), then taking another time snapshot after the timer finishes (t2), then comparing the theoretical wakeup time with the actual wakeup time (t2 -(t1 + sleep_time)). This value is the latency for that timer wakeup.

The cyclictest's fake code:

clock_gettime(CLOCK_MONOTONIC, &t1);
next = t1 + 1000;
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL);
clock_gettime(CLOCK_MONOTONIC, &t2);
latency = t2 - next;

The test command is(-p80 means the test thread's priority is 80, the -n means useing clock_nanosleep to waiting for a specific time interval): ./cyclictest -p80 -n

The disturbing process is simple:

struct sched_param schedp;
schedp.sched_priority = 40;
sched_set_scheduler(0, SHED_FIFO, &schedp);
while(1) {
    srand(time(0));
    while(j++ < rand()%1000000);
    usleep(10);
}

In Linux kernel, no matter how many lower priority processes are there, as long as the high priority process is runnable, it will be executed immediately.

Can anyone explain why launching a few low priority processes can affect high priority process's latency?

What causes apic_timer_interrupt()?

$
0
0

I am on CentOS 7 (kernel 3.10.0-1062.1.2.el7.x86_64).

For some reasons, my application with tight loop (on an isolated core) has apic_timer_interrupt about once every 1 sec. This isn't the local timer interrupt.

My dummy application:

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstdint>

int main(int argc, char** argv) {
        srand(time(nullptr));
        int32_t i = 0;
        while (i != rand()) i = rand() * -1;
        printf("%d", i);
}

Any idea?

Right way to delete kthread waiting while semaphore will be upped

$
0
0

I write a kernel module that uses kernel threads and semaphores.

I call up(...) function for semaphore from interrupt handler and then my kthread starts to execute.

static int interrupt_handler_thread(void *data)
{
    /* duty cycle */
    while (!kthread_should_stop()) {
        /*
         * If semaphore has been uped in the interrupt, we will
         * acquire it here, else thread will go to sleep.
         */
        if (!down_interruptible(mysem)) {
            /* proccess  gpio interrupt */
            dev_info(dev, "gpio interrupt detected\n"); 
        }
    }

    do_exit(0);

    return 0;
}

The semaphore and thread are initializated into module_init function. Error checking was omitted.

...
sema_init(mysem, 0);

thread = kthread_create(interrupt_handler_thread,client,"my_int_handler");
wake_up_process(thread);
...

And during unloading a module the semaphore and the thread are removed:

    /*
     * After this call kthread_should_stop() in the thread will return TRUE.
     * See https://lwn.net/Articles/118935/
     */
    kthread_stop(thread);

    /*
     * Release the semaphore to return
     * from down_interruptible() function
     */
    up(mysem);

When I try to unload my module the one frozes into thread in down_interruptible() function, because it waits while the semaphore ups in interrupt handler. And my code never returns from kthread_stop().

It seems, I need to disable the interrupt from my gpio, up the semaphore by hand and call kthread_stop() function. But it is a potential bug, because after the semaphore is uped by hand, the thread starts executing and the one can again down_interruptible() after its duty cycle.

Could anyone help me, please?

PS: I know about this question, but, it seems, this is not my case.

modinfo: ERROR: could not get modinfo from 'hello_1': Exec format error

$
0
0

trying to insert a simple module to the kernel that prints hello world. but on inserting the module using insmod it gives the following error.

insmod: ERROR: could not insert module ./hello-1.ko: Invalid module format

the linux kernel module programming guide says it is due to vermagic difference. so , i tried using modinfo to see the vermagic of the module hello-1.ko and got the stated error.

I searched a lot but i am not getting any idea how to proceed further.

i am using linux mint tricia 19.3

kernel :5.0.0-32-generic

module -

#include <linux/module.h>       
#include <linux/kernel.h>       
int init_module(void)
{        
    printk(KERN_INFO "Hello world 1.\n");        
    return 0;
}
void cleanup_module(void)
{
        printk(KERN_INFO "Goodbye world 1.\n");
}

its makefile

obj−m += hello−1.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

Efficient way to find task_struct by pid

$
0
0

Is there an efficient way of finding the task_struct for a specified PID, without iterating through the task_struct list?

Loading Linux kernel module parameters [closed]

$
0
0

I have been trying to load the NVIDIA Linux kernel module with the NVreg_PreserveVideoMemoryAllocations=1 module parameter.

I have created a file "nvidia.conf" in the /etc/modprobe.d. folder and have the below line in the file.

options nvidia NVreg_PreserveVideoMemoryAllocations=1

I have also added a file in /etc/modules-load.d which simply has a line with "nvidia", even though I'm not sure if this is necessary in order to get my other file called.

However, when I look at the /proc/driver/nvidia/params file, the parameter above is never being set. When I execute "modinfo nvidia" the parameter in question shows up as valid. I would just like this parameter to be loaded into the driver each time I startup.

I have also tried to set this in the /etc/default/grub file as

GRUB_CMDLINE_LINUX="nvidia.NVreg_PreserveVideoMemoryAllocations=1"

The strange thing, is that the paramter does not even show up in the /nvidia/params file.

I am using the 440 proprietary drivers on Ubuntu 19.19.

How does KVM/QEMU and guest OS handles page fault

$
0
0

For example, I have a host OS (say, Ubuntu) with KVM enabled. I start a virtual machine with QEMU to run a guest OS (say, CentOS). It is said that to the host OS, this VM is just a process. So in the host's point of view, it handles page fault as usual (e.g., allocate page frame as needed, swap pages based on active/inactive lists if necessary).

Here is the question and my understanding. Within the guest OS, as it's still a full-fledged OS, I assume it still has all mechanisms handling virtual memory. It sees some virtualized physical memory provided by QEMU. By virtualized physical memory I mean the guest OS doesn't know it is in a VM, and still works as it would on a real physical machine, but what it has are indeed an abstraction given by QEMU. So even if a page frame is allocated to it, if that's not in guest's page table, the guest OS will still trigger a page fault and then map some page to the frame. What's worse, there may be a double page fault, where the guest first allocate some page frames upon page fault, which triggers page fault at host OS.

However, I also heard something like shallow (or shadow) page table which seems could optimize this unnecessary double page fault and double page table issue. I also looked at some other kernel implementation, specifically unikernels, e.g., OSv, IncludeOS, etc. I didn't find anything related to page fault and page table mechanisms. I did see some symbols like page_fault_handler but not as huge as what I saw in Linux kernel code. It seems memory management is not a big deal in these unikernel implementations. So I assume QEMU/KVM and some Intel's virtualization technologies have already handled that.

Any ideas in this topic? Or if you have some good references/papers/resources to this problem, or some hints would be very helpful.


trying to understand other methods to prevent attacks into system then kernel

$
0
0

If you were an antivirus designer or maker, what other methods than installing software into kernel do you utilize to prevent viruses?

Linux network device watchdog timer mechanism

$
0
0

I'm studying Linux network device driver with Linux 4.12.14, and here is what I've learnt:

  1. In device driver, it sets some variables when the device is opened:
     dev->watchdog_timeo = TX_TIMEOUT;
     ...
     .ndo_tx_timeout    = el3_tx_timeout,
  1. Linux kernel activates a timer when the device is opened:
    static void dev_watchdog(unsigned long arg)
    {
        struct net_device *dev = (struct net_device *)arg;

        netif_tx_lock(dev);
        if (!qdisc_tx_is_noop(dev)) {
            if (netif_device_present(dev) &&
                netif_running(dev) &&
                netif_carrier_ok(dev)) {
                int some_queue_timedout = 0;
                unsigned int i;
                unsigned long trans_start;

                for (i = 0; i < dev->num_tx_queues; i++) {
                    struct netdev_queue *txq;

                    txq = netdev_get_tx_queue(dev, i);
                    trans_start = txq->trans_start;
                    if (netif_xmit_stopped(txq) &&
                        time_after(jiffies, (trans_start +
                                 dev->watchdog_timeo))) {
                        some_queue_timedout = 1;
                        txq->trans_timeout++;
                        break;
                    }
                }

                if (some_queue_timedout) {
                    WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
                           dev->name, netdev_drivername(dev), i);
                    dev->netdev_ops->ndo_tx_timeout(dev);
                }
                if (!mod_timer(&dev->watchdog_timer,
                           round_jiffies(jiffies +
                                 dev->watchdog_timeo)))
                    dev_hold(dev);
            }
        }
        netif_tx_unlock(dev);

        dev_put(dev);
    }

so, after kernel transfers a skb, it updates txq->trans_start:

static inline netdev_tx_t netdev_start_xmit(struct sk_buff *skb, struct net_device *dev,
                        struct netdev_queue *txq, bool more)
{
    const struct net_device_ops *ops = dev->netdev_ops;
    int rc;

    rc = __netdev_start_xmit(ops, skb, dev, more);
    if (rc == NETDEV_TX_OK)
        txq_trans_update(txq);

    return rc;
}

and the function to update txq->trans_start:

static inline void txq_trans_update(struct netdev_queue *txq)
{
    if (txq->xmit_lock_owner != -1)
        txq->trans_start = jiffies;
}

How ever, I didn't find anywhere updates the "txq->trans_start" again, so my question is: How a successful transmit doesn't make the watchdog timer timeout?

How do I make sure that a connected USB device gets enumerated before starting "init" process in Linux?

$
0
0

I am working on a Linux based embedded product that has a non-removable USB device. Sometimes during bootup the device gets enumerated after starting "init" process. I need to make sure that this device should get enumerated by Linux kernel before starting "init" process.

Please provide your views to handle such situation. Suggestions for changes in Kernel/User space are welcome.

Condition of do_group_exit() in get_signal() in Linux kernel

Getting linking error for i2c_smbus_read_byte()

$
0
0

I want to perform, read write operation on /dev/i2c-0 in sama5d27_som(arm_cortex_A5) board. Therfore I have used the i2c_smbus_read_byte()in my program. I have installed the following packages libi2c-dev and i2c-tools in my ubuntu18.04. But while cross_compiling, I got the following error.

gp@guru-hp:~/guru/projects/i2c$ arm-linux-gnueabihf-gcc main.c 
/tmp/ccW1Pk9X.o: In function `main':
main.c:(.text+0xf6): undefined reference to `i2c_smbus_read_byte'
collect2: error: ld returned 1 exit status
gp@guru-hp:~/guru/projects/i2c$

Here is the source code. Please have a look and help me on this. Thanks

#include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <linux/i2c-dev.h>
 #include <i2c/smbus.h>

 int main(void)
 {
     printf("Wellcome to device programming.\n");
     printf("Enter i2c port no : ");
     u_int8_t prt = 0;
     scanf("%c",&prt);
     char pth[18] = {0};
     sprintf(pth,"/dev/i2c-%c",prt);
     int fd = open(pth,O_RDWR);
     if(fd > 0)
     {
         printf("%s has been opened\n",pth);
         int rt_ctl = 0;
         for(int i=0;i<=60;i+=10)
         {
             if(!i)
             {
                 printf("");
                 for(int i=0;i<=0xf;i++)
                 {
                     printf("%x  ",i);
                 }
                 printf("\n");
             }
             printf("%.2d: ",i);
             for(int j=0x0;j<=0xf;j++)
             {

                 rt_ctl = ioctl(fd,I2C_SLAVE,i+j);
                 if(rt_ctl < 0)
                 {
                     printf("-- ");
                     //perror("ioctl() failed due to : ");
                 }
                 else
                 {
                     rt_ctl = i2c_smbus_read_byte(fd);
                     if(rt_ctl < 0)
                     {
                         printf("-- ");
                     }
                     else
                     {
                         printf("%02x ",j);
                     }
                 }
             }
             printf("\n");
         }
         close(fd);
     }

 }

Where is radeon(hd 7450)'s Interrupt handler in linux kernel?

$
0
0

I'm trying to learn radeon driver with my hd 7450. IIUC, it should have a interrupt handler in its driver, but I cannot find one.

wait_event and lock interaction

$
0
0

I'm new to linux kernel development and wonder how wait_event/wait_event_interruptible interacts with other locking primitives in the kernel. I'm used to C++ std::condition_variable::wait and want to achieve something similar in the kernel.

I have a queue of receive buffers which are filled using DMA transfers. The list is protected using a spin lock. Finished buffers are added by the DMA finished soft IRQ. I created a character device which allows reading of the finished buffers. So far this works as expected but I want to support blocking reads.

I've read about wait_event which seems like what I want, but I'm really confused how wait_event does not take any lock/mutex parameter. How is evaluating the condition without holding a lock safe? I suppose I can add the necessary locking to my condition, but then I need to lock again once wait_event returns to extract the data. Is this already a case for prepare_to_wait/schedule/finish_wait?


Linux Kernel function call flow

$
0
0

Is there a way, I get to know what kernel functions are called during a certain event.

For example, if I press any key on keyboard, I want to know what all kernel functions and device driver functions are getting called - before the character appears on the screen (the character corresponding to the key that I typed on keyboard).

I want to dump the complete flow somewhere and examine it later. I am all talking about kernel space functions - NOT user space functions.

Kindly illuminate.

Guest Kernel Crashes with Supervisor Mode Protection fault when monitored using Intel PEBS

$
0
0

I am monitoring my test program's memory access using Intel PEBS (Precise Event-Based Sampling). I don't want to use PEBS using the existing PERF infrastructure (for various reasons). So, I have written my own Kernel Module to for Intel PEBS and I handle all the NMI events inside my Kernel Module. I am able to successfully log memory access when the test program natively runs on the host system. Everything works fine.

But when I try to run the test program inside a VM (running over QEMU/KVM), my Guest Kernel crash with error following error. Only the Guest kernel crashes. Host Kernel is stable:

#PF: supervisor read access in user mode.

I have tried playing around disabling/enabling SMEP, SMAP, LAPIC and other options in both Guest and Host kernel. Still crash happens.

I am using Intel(R) Xeon(R) Gold 6230 CPU @ 2.10GHZ, Host Kernel Linux Version 5.4.0 (same as the Guest Kernel Version).

Logs from Guest Kernel:

BUG: unable to handle page fault for address: ffff9c122e8a1038
[   76.011730] #PF: supervisor read access in user mode
[   76.012190] #PF: error_code(0x0000) - not-present page
[   76.012603] IDT: 0xfffffe0000000000 (limit=0xfff) GDT: 0xfffffe0000034000 (limit=0x7f)
[   76.013206] LDTR: NULL
[   76.013398] TR: 0x40 -- base=0xfffffe0000036000 limit=0x206f
[   76.013852] PGD 0 P4D 0
[   76.014041] Oops: 0000 [#1] SMP
[   76.014274] CPU: 1 PID: 953 Comm: one_page Not tainted 5.4.0-rc7 #4
[   76.014738] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
[   76.015394] RIP: 0033:0x55bd4855cd56
[   76.015661] Code: 85 5c ff ff ff 00 00 00 00 eb 26 48 8b 05 e2 12 20 00 8b 95 5c ff ff ff 48 63 d2 48 c1 e2 02 48 01 d0 8b 00 88
 85 5b ff ff ff <83> 85 5c ff ff ff 01 81 bd 5c ff ff ff ff 0f 00 00 7e ce 8b 05 a1
[   76.017081] RSP: 002b:00007fff48cf8810 EFLAGS: 00010202
[   76.017477] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
[   76.018038] RDX: 0000000000000b60 RSI: 00007fff48cf86d0 RDI: 0000000000000002
[   76.018556] RBP: 00007fff48cf88d0 R08: 0000000000000000 R09: 0000000000000000
[   76.019077] R10: 0000000000000008 R11: 0000000000000246 R12: 000055bd4855c8c0
[   76.019599] R13: 00007fff48cf89b0 R14: 0000000000000000 R15: 0000000000000000
[   76.020116] FS:  00007f2b93ce34c0 GS:  0000000000000000
[   76.020533] Modules linked in: nfsv3 nfs_acl rpcsec_gss_krb5 auth_rpcgss nfsv4 nfs lockd grace fscache intel_rapl_msr intel_rapl
_common crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel crypto_simd cryptd dax_pmem_compat device_dax nd_pmem glue_he
lper dax_pmem_core nd_btt ppdev joydev parport_pc input_leds nfit intel_rapl_perf mac_hid qemu_fw_cfg serio_raw sch_fq_codel sunrpc
 lp parport ip_tables x_tables psmouse virtio_blk virtio_net net_failover failover i2c_piix4 pata_acpi floppy
[   76.023751] CR2: ffff9c122e8a1038
[   76.023997] ---[ end trace 10450321a820a090 ]---

How can ip_nonlocal_bind=1 break applications?

$
0
0

I have two Linux VMs. They all start the sshd service on the same IP. When one machine is down, you can login on another VM with the same IP. However, the other VM will not be listening to that IP because it does not already exist.

I must restart the sshd service manually. However, I can not login the VM if sshd is not started. I found a solution by setting ip_nonlocal_bind to 1.

I googled the description of ip_nonlocal_bind in kernel.org:

  ip_nonlocal_bind - BOOLEAN
  If set, allows processes to bind() to non-local IPv6 addresses, 
  which can be quite useful - but may break some applications.
  Default: 0

Last line says but may break some applications, and I worry this could break the application I'm running on my VM.

My VM mainly act as a router.

Finally, I want to know: how can ip_nonlocal_bind break applications?

Linux reboots after few seconds on shutdown in USB live

$
0
0

I'm using Kali Linux in live persistence mode. Whenever I shutdown, the system reboots after 2 seconds of power off.

This happens not only with Kali Linux, but with Ubuntu and Arch Linux which I installed previously in USB.

Can anyone give solution to this issue?

How to use a seq_file in Linux kernel modules?

$
0
0

Hello all I'm new to Linux and wondering how to use a Linux sequence file in a module to traverse kernel objects.

What I know is I can use the command:

 cat /proc/kallsyms

to view the available symbols and from what I've read on google, the symbols in the list that have a 'D' or 'd' are pointers to data structures.

Though I know the basics of how to create a module, the examples on the internet on how to use seq operations are not uniform and I'm getting a little confused.

If someone knows of any good doco that will help me understand how to create a seq file to traverse kernel objects and could post a link (or a quick example), I would be greatly appreciative.

Viewing all 12279 articles
Browse latest View live