Questions tagged [kmalloc]

The kmalloc function performs fast dynamic memory allocation and is part of the Linux Kernel API.

The kmalloc() function performs dynamic memory allocation and is part of the Linux Kernel API. In contrast to malloc, this function allocates memory faster in regions of contiguous physical memory, and it doesn't clear the memory it obtains.

More information can be found here

51 questions
0
votes
0 answers

kmalloc and slab relationship

My understanding about kmalloc() is that it uses generic slabs of kernel to allocate memory e.g. kmalloc-8, kmalloc-16, .. kmalloc-8k. But after inserting below module I'm not seeing any change to active_objs parameter of any kmalloc-xxx or…
MankPan
  • 79
  • 6
0
votes
1 answer

How to correctly implement kmalloc using C?

I have implemented kmalloc in the Makefile, defs.h, kmalloc.c, sysproc.c, usys.S, syscall.h, and syscall.c. I have a test case called test_1.c to test my implementation of kmalloc. I took the source code from xv6, I applied my implementations and…
0
votes
0 answers

Why my program is failing with "dereferencing pointer to incomplete type ‘struct kmem_cache’

Trying to understand Linux slab allocator internals. But unable to dereference struct kmem_cache structure. Dont know what wrong I am doing here. I might be very silly coding bug (sorry for that) or some deep concept which I dont know. I thought it…
MankPan
  • 79
  • 6
0
votes
0 answers

How to pin and get kernel pages in Linux?

I am allocating memory to the buffer in kernel space. I want to pin the pages and get the pinned kernel pages. But I did not find any APIs to do that. There is one API called get_kernel_page(unsigned long start, int write, struct ** pages) which…
0
votes
0 answers

Will mmu be used when cpu access virtual memory allocated via kmalloc?

I'm aware that memory allocated by kmalloc is physically continuous and virtual memory it returned has just an offset from its physical memory. But if CPU tries to access the virtual memory it returned, will MMU and page table be used? I heard all…
Chen Li
  • 4,824
  • 3
  • 28
  • 55
0
votes
1 answer

Dynamic Array in Linux Kernel Module with kmalloc

I am writing a small program which prints the time it took to allocate the memory. I want to free the memory later so I want to save it in a array, but since I can loop it as many times as I want I want to make a Dynamic array to store all the…
Chrissi
  • 15
  • 8
0
votes
0 answers

error @ sk_prot_alloc+0x9a/0x150 in linux kernel

I'm debugging a module kernel I've written, and I get the following stack trace. 37.700861] RIP: 0010:__kmalloc+0x9e/0x270 [ 37.700864] Code: 7f 01 00 00 4d 8b 01 65 49 8b 50 08 65 4c 03 05 30 7d b6 6e 4d 8b 20 4d 85 e4 0f 84 8d 01 00 00 41 8b…
Mr P
  • 113
  • 8
0
votes
1 answer

How to comprehend "You can use the slab cache allocator(i.e. kmem_cache_create or kmem_cache_create_usercopy) to allocate many identical objects"?

As per the documentation(https://www.kernel.org/doc/html/latest/core-api/memory-allocation.html), which says[emphasis mine]: If you need to allocate many identical objects you can use the slab cache allocator. The cache should be set up with…
John
  • 2,963
  • 11
  • 33
0
votes
0 answers

Freeing platform driver device struct

In the probe routine of platform drivers, I see the private device structures are being allocated via devm_kzalloc(). Before setting the driver data via platform_set_drvdata() there are error conditions which return without freeing the allocated…
foo_l
  • 591
  • 2
  • 10
  • 28
0
votes
1 answer

Contiguous physical memory using kmalloc on Guest

I am implementing a device driver in guest OS. For this I need to allocate a buffer space which is required to be contiguous physical memory. Does allocating buffer using kmalloc in guest OS guarantee contiguous physical address? If not, how can I…
Proy
  • 336
  • 2
  • 13
0
votes
0 answers

Way to know amount of continuous memory available in system

I have a device driver which requires 500MB of continuous kmalloc memory during init. The driver loads during the boot time and I have verified using free command that there is enough memory available at that time. But, sometimes I am not able to…
lokesharo
  • 305
  • 2
  • 11
0
votes
0 answers

Hi. I am trying to allocate memory in the linux kernel using kmalloc

I am trying to allocate memory in the Linux kernel using kmalloc. I have a structure designed as below: struct st_fetch_point { struct sk_buff *end_pkt ; struct sk_buff *start_pkt ; struct sk_buff *current_pkt ; struct st_fetch_point…
0
votes
0 answers

When do we use vmalloc and kmalloc?

I know that kmalloc allocates contiguous memory in physical memory as well as virtual memory, vmalloc allocates contiguous memory in virtual memory but it doesn't guarantee that memory allocated in physical memory will be contiguous. Therefore, it…
nguyennd
  • 55
  • 1
  • 11
0
votes
0 answers

How can I get strings which are located at a specific virtual address in Ubuntu?

I have a pointer to an array with symbols. char *myArr = kmalloc(100, GFP_KERNEL); //put message in kernel for(int i = 0; i < 100; ++i) { myArr[i] = x30; //put symbols here } printk(KERN_INFO "The string is located at virtual address %p,…
0
votes
1 answer

Does kmalloc call type constructor?

It is known that memory allocation with new calls respective type constructor and memory allocation with malloc does not. But what about kmalloc? I am trying to develop some system calls and I need to assign memory to a structure below. struct…
user3508953
  • 427
  • 6
  • 15