which kmalloc flag should be used
You want section Get Free Page (GFP) Flags of Understanding the Linux Virtual Memory Manager (pdf). It is for the 2.4 kernel but it should still apply.
original post:
From the 2.6.32 gfp.h:
/*
* Action modifiers - doesn't change the zoning
*
* __GFP_REPEAT: Try hard to allocate the memory, but the allocation attempt
* _might_ fail. This depends upon the particular VM implementation.
*
* __GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
* cannot handle allocation failures.
*
* __GFP_NORETRY: The VM implementation must not retry indefinitely.
*
* __GFP_MOVABLE: Flag that this page will be movable by the page migration
* mechanism or reclaimed
*/
#define __GFP_WAIT ((__force gfp_t)0x10u) /* Can wait and reschedule? */
#define __GFP_ ((__force gfp_t)0x20u) /* Should access emergency pools? */
#define __GFP_IO ((__force gfp_t)0x40u) /* Can start physical IO? */
#define __GFP_FS ((__force gfp_t)0x80u) /* Can call down to low-level FS? */
#define __GFP_COLD ((__force gfp_t)0x100u) /* Cache-cold page required */
#define __GFP_NOWARN ((__force gfp_t)0x200u) /* Suppress page allocation failure warning */
#define __GFP_REPEAT ((__force gfp_t)0x400u) /* See above */
#define __GFP_NOFAIL ((__force gfp_t)0x800u) /* See above */
#define __GFP_NORETRY ((__force gfp_t)0x1000u)/* See above */
#define __GFP_COMP ((__force gfp_t)0x4000u)/* Add compound page metadata */
#define __GFP_ZERO ((__force gfp_t)0x8000u)/* Return zeroed page on success */
#define __GFP_NOMEMALLOC ((__force gfp_t)0x10000u) /* Don't use emergency reserves */
#define __GFP_HARDWALL ((__force gfp_t)0x20000u) /* Enforce hardwall cpuset memory allocs */
#define __GFP_THISNODE ((__force gfp_t)0x40000u)/* No fallback, no policies */
#define __GFP_RECLAIMABLE ((__force gfp_t)0x80000u) /* Page is reclaimable */
Also, from kernel archives:
To make code easier to read, a set of
three GFP flags are added called
GFP_PAGECACHE, GFP_NOFS_PAGECACHE and
GFP_HIGHUSER_PAGECACHE.
Looking at the diff source on that page shows the flags you were asking about are a combination of existing flags:
#define GFP_HIGHUSER_MOVABLE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
__GFP_HARDWALL | __GFP_HIGHMEM | \
__GFP_MOVABLE)
+#define GFP_NOFS_PAGECACHE (__GFP_WAIT | __GFP_IO | __GFP_MOVABLE)
+#define GFP_USER_PAGECACHE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
+ __GFP_HARDWALL | __GFP_MOVABLE)
+#define GFP_HIGHUSER_PAGECACHE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
+ __GFP_HARDWALL | __GFP_HIGHMEM | \
+ __GFP_MOVABLE)
#ifdef CONFIG_NUMA
#define GFP_THISNODE (__GFP_THISNODE | __GFP_NOWARN | __GFP_NORETRY)
What is different functions: `malloc()` and `kmalloc()`?
I answer to the second question, assuming that you are using Linux OS. Regarding to the first one please have a look to my comment.
kmalloc
uses get_free_page
to get the memory. The way in which the pages are collected depends on the second parameter ( GFP_ATOMIC GFP_KERNEL ...
in which GFP means GET FREE PAGE). The advantage of kmalloc on the GFP is that it can fit multiple allocations into a single page.
some of the options for kmalloc are:
GFP_USER - Allocate memory on behalf of user. May sleep.
GFP_KERNEL - Allocate normal kernel ram. May sleep.
GFP_ATOMIC - Allocation will not sleep. May use emergency pools. For example, use this inside interrupt handlers.
GFP_HIGHUSER - Allocate pages from high memory.
GFP_NOIO - Do not do any I/O at all while trying to get memory.
GFP_NOFS - Do not make any fs calls while trying to get memory.
GFP_NOWAIT - Allocation will not sleep.
GFP_THISNODE - Allocate node-local memory only.
GFP_DMA - Allocation suitable for DMA. Should only be used for kmalloc caches. Otherwise, use a slab created with SLAB_DMA.
Apart from this get_free_page
and kmalloc
are very similar. _get_free_pages
differs from get_free_page
because it gives the pointer to the first byte of a memory area that is potentially several (physically contiguous) pages long.
Another function that is again very similar to get_free_page
is get_zeroed_page(unsigned int flags)
which gets a single page like get_free_page
but zeroes the memory
how to alloc user space memory in kernel space?
This is wrong on at least two accounts:
- syscall hijacking (let alone for something like open) is just a bad idea. the only sensible method to catch all possible open path is through using LSM hooks. it also happens to deal with the actual file being opened avoiding the race: you read the path in your routine, wrapped opens reads it again. but by that time malicious userspace could have changed it and you ended up looking at the wrong file.
- it should be clear getcwd has to have a method of resolving a name in order to put it into the userspace buffer. you should dig in into the call and see what can be changed to put it in a kernel buffer.
Why are you doing this to begin with?
GFP_KERNEL vs GFP_DMA and kmalloc() vs dma_alloc_coherent()
dma_alloc_coherent() returns address range for which proper memory attributes are already set so cache effect is handled naturally. We need not to do any cache operation for these addresses.
If we use address allocated by kmalloc() for DMA operation then we need to do extra cache operation like cache clean and cache invalidate based on direction of transfer.
GFP_DMA flag just allocates memory from DMA Zone on 32-bit machine.
allocate user-space memory from kernel
You can temporarily disable memory address validity checking by using set_fs
mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
/* Your syscall here */
set_fs(old_fs);
Related Topics
Icudt Error While Installing Stringi Package from R in Linux Offline
How to Concatenate Files with the Same Prefix (And Many Prefixes)
How to Enable Evp Functions in Openssl
Linux Cant Find Dynamically Linked Applications
Lazarus: How to List All the Available Network Connection on a System
Passing Arguments to a Script Invoked with Bash -C
Nasm: How to Create/Handle Basic Bmp File Using Intel 64 Bit Assembly
This Script Wont Sort Correctly by Age
Implement Custom U-Boot Command
Converting a Pcap Trace to Netflow Format
.Bashrc Syntax Error: Unexpected End of File
How to Break an Arbitrary Tcp/Ip Connection on Linux
Addresses of Thread Local Storage Variables
Bash: How to Pass in Arguments to an Alias: Cannot Use a Function - Syntax of Bash Conditionals