SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Memory Management 
Feng Qin 
CSE Dept., The Ohio State University
Group Discussion 
• Applications of Information Flow 
Checking Mechanisms?
Outline 
• Background 
– Linux Kernel Memory Management 
• ION Memory Management
Kernel Memory Management 
• Physical memory 
• It is not easy: 
– Often kernel cannot sleep 
– Not easy to handle memory errors 
4/25/2012 CSE662: Operating Systems Laboratory 4
Pages 
• <linux/mm.h> 
struct page { 
page_flags_t flags; 
atomic_t _count; 
atomic_t _mapcount; 
unsigned long private; 
struct address_space *mapping; 
pgoff_t index; 
struct list_head lru; 
void *virtual; 
} 
4/25/2012 CSE662: Operating Systems Laboratory 5
Zones 
• Reasons: 
– Some devices are capable of performing DMA to 
only certain memory addresses 
– Some architectures are capable of physically 
addressing larger amounts of memory than they 
can virtually address 
• Zones: ( for x86) 
– ZONE_DMA (< 16 MB) 
– ZONE_NORMAL (16-896 MB) 
– ZONE_HIGHMEM(> 896 MB) 
4/25/2012 CSE662: Operating Systems Laboratory 6
Getting Memory (I) 
• Contiguous physical memory pages: 
– struct page *alloc_pages (unsigned int 
gfp_mask, unsigned int order) 
– unsigned long __get_free_pages(…) 
– unsigned long get_zeroed_pages(…) 
– void free_pages(…) 
– void __free_pages(…) 
4/25/2012 CSE662: Operating Systems Laboratory 7
Getting Memory (II) 
• Contiguous physical memory in bytes: 
– void *kmalloc(size_t size, int flags) 
– void kfree(const void *ptr) 
• Contiguous virtual memory in bytes: 
– void *vmalloc(unsigned long size) 
– void vfree(void *addr) 
– Performance may not be good (TLB threshing) 
– E.g. when loading modules 
4/25/2012 CSE662: Operating Systems Laboratory 8
Getting Memory (III) 
• gfp_mask flags: 
– GFP_ATOMIC (high priority and must not 
sleep) 
– GFP_KERNEL (normal allocation and 
might sleep) 
– GFP_USER (allocate memory for user-space 
processes) 
– GFP_DMA (allocate memory for device 
drivers) 
– …. 
4/25/2012 CSE662: Operating Systems Laboratory 9
High Memory Mappings 
• Permanent mappings (might sleep) 
– void *kmap(struct page *page) 
– void kunmap(struct page *page) 
• Temporary mappings (not sleep) 
– void *kmap_atomic(…) 
– void *kunmap_atomic(…) 
4/25/2012 CSE662: Operating Systems Laboratory 10
Slab Layer (I) 
• Cache objects that are frequently allocated 
and deallocated 
Cache 
Slab 
Slab 
obj 
obj 
obj 
obj 
4/25/2012 CSE662: Operating Systems Laboratory 11
Slab Layer (II) 
struct slab { 
struct list_head list; 
unsigned long colouroff; 
void *s_mem; 
unsigned int inuse; 
kmem_bufctl_t free; 
}; 
4/25/2012 CSE662: Operating Systems Laboratory 12
Statically Allocating on the 
Stack 
• Per-process kernel stack 
• Keep stack usage to a minimum 
– The size is really small ( 4 KB option in 2.6 
kernels) 
4/25/2012 CSE662: Operating Systems Laboratory 13
Per-CPU Allocations 
• In modern SMP architectures 
• Benefits: 
– Reduce locking 
– Avoid cache invalidation 
• Usage example: 
unsigned long my_percpu[NR_CPUS]; 
cpu = get_cpu(); 
my_percpu[cpu]; 
put_cpu(); 
4/25/2012 CSE662: Operating Systems Laboratory 14
ION Memory Allocator 
• Manage one or more memory pools, i.e., 
ION heaps 
– Each device can have a different set of ION 
heaps 
• Combat fragmentation 
• Serve special hardware needs: 
– E.g., GPU, display controller, cameras
Providing an ION Heap 
struct ion_heap_ops { 
int (*allocate) (struct ion_heap *heap, 
struct ion_buffer *buffer, unsigned long len, 
unsigned long align, unsigned long flags); 
void (*free) (struct ion_buffer *buffer); 
int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, 
ion_phys_addr_t *addr, size_t *len); 
struct scatterlist *(*map_dma) (struct ion_heap *heap, 
struct ion_buffer *buffer); 
void (*unmap_dma) (struct ion_heap *heap, 
struct ion_buffer *buffer); 
void * (*map_kernel) (struct ion_heap *heap, 
struct ion_buffer *buffer); 
void (*unmap_kernel) (struct ion_heap *heap, 
struct ion_buffer *buffer); 
int (*map_user) (struct ion_heap *heap, struct ion_buffer *buffer, 
struct vm_area_struct *vma); 
};
Default ION driver 
• ION_HEAP_TYPE_SYSTEM: memory 
allocated via vmalloc_user(). 
• ION_HEAP_TYPE_SYSTEM_CONTIG: 
memory allocated via kzalloc. 
• ION_HEAP_TYPE_CARVEOUT: carveout 
memory is physically contiguous and set aside 
at boot.
Using ION From User Space 
• Who uses ION? 
– User space device access libraries 
• Why needs ION? 
– Allocate large contiguous media buffers 
– Sharing between two processes or processes 
and kernel 
– E.g., still camera library, when buffer is filled 
with data, it will pass the buffer to the kernel 
to be processed by JPEG encoder hardware
Allocate Buffer from ION 
• Require Access permission to device /dev/ion 
• Retrieve a file descriptor (one client per 
user process) 
client_fd = open (“/dev/ion”, O_RDONLY) 
• Fill the following data structure 
struct ion_allocation_data { 
size_t len; 
size_t align; 
unsigned int flags; // specify which ION heap the buffer is from 
(i.e., ION_HEAP_TYPE_CARVEOUT | ION_HEAP_TYPE_CONTIG) 
struct ion_handle *handle; // output parameter, not CPU-accessible buffer pointer 
} 
• Use ioctl system call to interact with ION 
int ioctl(int client_fd, ION_IOC_ALLOC, struct ion_allocation_data *allocation_data)
ION Buffer Sharing in User 
Space 
• Convert ion_handle to a file descriptor 
int ioctl(int client_fd, ION_IOC_SHARE, struct ion_fd_data *fd_data); 
struct ion_fd_data { 
struct ion_handle *handle; 
int fd; 
} 
• BINDER IPC mechanism can be used to 
send fd to another process for sharing 
• To obtain the shared buffer, the second process 
needs to retrieve the file descriptor via 
client_fd = open (“/dev/ion”, O_RDONLY)
Free ION Buffer 
• Go through ioctl again 
int ioctl(int client_fd, ION_IOC_FREE, struct ion_handle_data *handle_data); 
struct ion_handle_data { 
struct ion_handle *handle; 
}
ION Buffers in Kernel 
• ION supports multiple clients in the kernel 
– One for each driver that uses ION 
• A kernel driver calls the following function 
to obtain an ION client handle 
struct ion_client *ion_client_create( 
struct ion_device *dev, // /dev/ion 
unsigned int heap_mask, // selects ION heaps 
const char *debug_name 
)
Sharing ION Buffer between 
User Space and Kernel (I) 
• User process 
– Allocates the ION buffer 
– Obtains a file descriptor using 
ION_IOC_SHARE 
– Pass the file descriptor to a kernel driver
Sharing ION Buffer between 
User Space and Kernel (II) 
• Kernel driver 
– Converts the file descriptor to an ion_handle 
struct ion_handle *ion_import_fd(struct ion_client *client, 
int fd_from_user); 
// based on the physical address of the buffer 
– Some hardware blocks need physically-contiguous 
buffers with physical addresses 
int ion_phys(struct ion_client *client, struct ion_handle 
*handle, ion_phys_addr_t *addr, size_t *len)
Source Code Cross References 
• Linux kernel 
– http://lxr.linux.no/ 
• Android 
– http://androidxref.com/source/xref/

Contenu connexe

Tendances

Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)shimosawa
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device DriversNEEVEE Technologies
 
Linux Device Driver parallelism using SMP and Kernel Pre-emption
Linux Device Driver parallelism using SMP and Kernel Pre-emptionLinux Device Driver parallelism using SMP and Kernel Pre-emption
Linux Device Driver parallelism using SMP and Kernel Pre-emptionHemanth Venkatesh
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
Kernel init
Kernel initKernel init
Kernel initgowell
 
Linux Porting
Linux PortingLinux Porting
Linux PortingChamp Yen
 
The structure of process
The structure of processThe structure of process
The structure of processAbhaysinh Surve
 
Chapter 8 memory-updated
Chapter 8 memory-updatedChapter 8 memory-updated
Chapter 8 memory-updatedDelowar hossain
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSESUSE Labs Taipei
 
Linux Kernel Startup Code In Embedded Linux
Linux    Kernel    Startup  Code In  Embedded  LinuxLinux    Kernel    Startup  Code In  Embedded  Linux
Linux Kernel Startup Code In Embedded LinuxEmanuele Bonanni
 
AIX Advanced Administration Knowledge Share
AIX Advanced Administration Knowledge ShareAIX Advanced Administration Knowledge Share
AIX Advanced Administration Knowledge Share.Gastón. .Bx.
 

Tendances (20)

Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
Linux Device Driver parallelism using SMP and Kernel Pre-emption
Linux Device Driver parallelism using SMP and Kernel Pre-emptionLinux Device Driver parallelism using SMP and Kernel Pre-emption
Linux Device Driver parallelism using SMP and Kernel Pre-emption
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
Kernel init
Kernel initKernel init
Kernel init
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
The structure of process
The structure of processThe structure of process
The structure of process
 
System Calls
System CallsSystem Calls
System Calls
 
Chapter 8 memory-updated
Chapter 8 memory-updatedChapter 8 memory-updated
Chapter 8 memory-updated
 
Metadata/Time-Date Tools (Toolkit_MTD)
Metadata/Time-Date Tools (Toolkit_MTD)Metadata/Time-Date Tools (Toolkit_MTD)
Metadata/Time-Date Tools (Toolkit_MTD)
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSE
 
Linux Kernel Startup Code In Embedded Linux
Linux    Kernel    Startup  Code In  Embedded  LinuxLinux    Kernel    Startup  Code In  Embedded  Linux
Linux Kernel Startup Code In Embedded Linux
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Embedded Storage Management
Embedded Storage ManagementEmbedded Storage Management
Embedded Storage Management
 
AIX Advanced Administration Knowledge Share
AIX Advanced Administration Knowledge ShareAIX Advanced Administration Knowledge Share
AIX Advanced Administration Knowledge Share
 
Linux Memory
Linux MemoryLinux Memory
Linux Memory
 
Architecture Porting
Architecture PortingArchitecture Porting
Architecture Porting
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 

En vedette

バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1
バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1
バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1Hirotaka Kawata
 
CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料SECCON Beginners
 
SyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IP
SyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IPSyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IP
SyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IPStefan Esser
 
C++でできる!OS自作入門
C++でできる!OS自作入門C++でできる!OS自作入門
C++でできる!OS自作入門uchan_nos
 
ハッキング実演
ハッキング実演ハッキング実演
ハッキング実演Ken Ogura
 
BlackHat USA 2011 - Stefan Esser - iOS Kernel Exploitation
BlackHat USA 2011 - Stefan Esser - iOS Kernel ExploitationBlackHat USA 2011 - Stefan Esser - iOS Kernel Exploitation
BlackHat USA 2011 - Stefan Esser - iOS Kernel ExploitationStefan Esser
 

En vedette (7)

バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1
バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1
バイナリより低レイヤな話 (プロセッサの心を読み解く) - カーネル/VM探検隊@北陸1
 
CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料
 
Unix executable buffer overflow
Unix executable buffer overflowUnix executable buffer overflow
Unix executable buffer overflow
 
SyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IP
SyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IPSyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IP
SyScan360 - Stefan Esser - OS X El Capitan sinking the S\H/IP
 
C++でできる!OS自作入門
C++でできる!OS自作入門C++でできる!OS自作入門
C++でできる!OS自作入門
 
ハッキング実演
ハッキング実演ハッキング実演
ハッキング実演
 
BlackHat USA 2011 - Stefan Esser - iOS Kernel Exploitation
BlackHat USA 2011 - Stefan Esser - iOS Kernel ExploitationBlackHat USA 2011 - Stefan Esser - iOS Kernel Exploitation
BlackHat USA 2011 - Stefan Esser - iOS Kernel Exploitation
 

Similaire à MEMORY MANAGEMENT TITLE

Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesYourHelper1
 
Open CL For Speedup Workshop
Open CL For Speedup WorkshopOpen CL For Speedup Workshop
Open CL For Speedup WorkshopOfer Rosenberg
 
Us 18-chen-keen lab-ios-jailbreak-internals
Us 18-chen-keen lab-ios-jailbreak-internalsUs 18-chen-keen lab-ios-jailbreak-internals
Us 18-chen-keen lab-ios-jailbreak-internalsLiang Chen
 
KeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be Dangerous
KeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be DangerousKeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be Dangerous
KeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be DangerousPriyanka Aash
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux DevelopersOpersys inc.
 
Lec 10-linux-review
Lec 10-linux-reviewLec 10-linux-review
Lec 10-linux-reviewabinaya m
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and InsightsGlobalLogic Ukraine
 
Study on Android Emulator
Study on Android EmulatorStudy on Android Emulator
Study on Android EmulatorSamael Wang
 
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh Naik
 
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsnullowaspmumbai
 
Android Mind Reading: Android Live Memory Analysis with LiME and Volatility
Android Mind Reading: Android Live Memory Analysis with LiME and VolatilityAndroid Mind Reading: Android Live Memory Analysis with LiME and Volatility
Android Mind Reading: Android Live Memory Analysis with LiME and VolatilityJoe Sylve
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Opersys inc.
 
Container-relevant Upstream Kernel Developments
Container-relevant Upstream Kernel DevelopmentsContainer-relevant Upstream Kernel Developments
Container-relevant Upstream Kernel DevelopmentsDocker, Inc.
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Javamalduarte
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modulesdibyajyotig
 
Linux Memory Analysis with Volatility
Linux Memory Analysis with VolatilityLinux Memory Analysis with Volatility
Linux Memory Analysis with VolatilityAndrew Case
 
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012DefCamp
 

Similaire à MEMORY MANAGEMENT TITLE (20)

Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
Open CL For Speedup Workshop
Open CL For Speedup WorkshopOpen CL For Speedup Workshop
Open CL For Speedup Workshop
 
Us 18-chen-keen lab-ios-jailbreak-internals
Us 18-chen-keen lab-ios-jailbreak-internalsUs 18-chen-keen lab-ios-jailbreak-internals
Us 18-chen-keen lab-ios-jailbreak-internals
 
KeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be Dangerous
KeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be DangerousKeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be Dangerous
KeenLab iOS Jailbreak Internals: Userland Read-Only Memory can be Dangerous
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
 
Lec 10-linux-review
Lec 10-linux-reviewLec 10-linux-review
Lec 10-linux-review
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and Insights
 
Study on Android Emulator
Study on Android EmulatorStudy on Android Emulator
Study on Android Emulator
 
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh naik linux_kernel_internals
Ganesh naik linux_kernel_internals
 
Ganesh naik linux_kernel_internals
Ganesh naik linux_kernel_internalsGanesh naik linux_kernel_internals
Ganesh naik linux_kernel_internals
 
Android Mind Reading: Android Live Memory Analysis with LiME and Volatility
Android Mind Reading: Android Live Memory Analysis with LiME and VolatilityAndroid Mind Reading: Android Live Memory Analysis with LiME and Volatility
Android Mind Reading: Android Live Memory Analysis with LiME and Volatility
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
Container-relevant Upstream Kernel Developments
Container-relevant Upstream Kernel DevelopmentsContainer-relevant Upstream Kernel Developments
Container-relevant Upstream Kernel Developments
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Java
 
Virtual memory 20070222-en
Virtual memory 20070222-enVirtual memory 20070222-en
Virtual memory 20070222-en
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
 
Linux Memory Analysis with Volatility
Linux Memory Analysis with VolatilityLinux Memory Analysis with Volatility
Linux Memory Analysis with Volatility
 
General Purpose GPU Computing
General Purpose GPU ComputingGeneral Purpose GPU Computing
General Purpose GPU Computing
 
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
 

MEMORY MANAGEMENT TITLE

  • 1. Memory Management Feng Qin CSE Dept., The Ohio State University
  • 2. Group Discussion • Applications of Information Flow Checking Mechanisms?
  • 3. Outline • Background – Linux Kernel Memory Management • ION Memory Management
  • 4. Kernel Memory Management • Physical memory • It is not easy: – Often kernel cannot sleep – Not easy to handle memory errors 4/25/2012 CSE662: Operating Systems Laboratory 4
  • 5. Pages • <linux/mm.h> struct page { page_flags_t flags; atomic_t _count; atomic_t _mapcount; unsigned long private; struct address_space *mapping; pgoff_t index; struct list_head lru; void *virtual; } 4/25/2012 CSE662: Operating Systems Laboratory 5
  • 6. Zones • Reasons: – Some devices are capable of performing DMA to only certain memory addresses – Some architectures are capable of physically addressing larger amounts of memory than they can virtually address • Zones: ( for x86) – ZONE_DMA (< 16 MB) – ZONE_NORMAL (16-896 MB) – ZONE_HIGHMEM(> 896 MB) 4/25/2012 CSE662: Operating Systems Laboratory 6
  • 7. Getting Memory (I) • Contiguous physical memory pages: – struct page *alloc_pages (unsigned int gfp_mask, unsigned int order) – unsigned long __get_free_pages(…) – unsigned long get_zeroed_pages(…) – void free_pages(…) – void __free_pages(…) 4/25/2012 CSE662: Operating Systems Laboratory 7
  • 8. Getting Memory (II) • Contiguous physical memory in bytes: – void *kmalloc(size_t size, int flags) – void kfree(const void *ptr) • Contiguous virtual memory in bytes: – void *vmalloc(unsigned long size) – void vfree(void *addr) – Performance may not be good (TLB threshing) – E.g. when loading modules 4/25/2012 CSE662: Operating Systems Laboratory 8
  • 9. Getting Memory (III) • gfp_mask flags: – GFP_ATOMIC (high priority and must not sleep) – GFP_KERNEL (normal allocation and might sleep) – GFP_USER (allocate memory for user-space processes) – GFP_DMA (allocate memory for device drivers) – …. 4/25/2012 CSE662: Operating Systems Laboratory 9
  • 10. High Memory Mappings • Permanent mappings (might sleep) – void *kmap(struct page *page) – void kunmap(struct page *page) • Temporary mappings (not sleep) – void *kmap_atomic(…) – void *kunmap_atomic(…) 4/25/2012 CSE662: Operating Systems Laboratory 10
  • 11. Slab Layer (I) • Cache objects that are frequently allocated and deallocated Cache Slab Slab obj obj obj obj 4/25/2012 CSE662: Operating Systems Laboratory 11
  • 12. Slab Layer (II) struct slab { struct list_head list; unsigned long colouroff; void *s_mem; unsigned int inuse; kmem_bufctl_t free; }; 4/25/2012 CSE662: Operating Systems Laboratory 12
  • 13. Statically Allocating on the Stack • Per-process kernel stack • Keep stack usage to a minimum – The size is really small ( 4 KB option in 2.6 kernels) 4/25/2012 CSE662: Operating Systems Laboratory 13
  • 14. Per-CPU Allocations • In modern SMP architectures • Benefits: – Reduce locking – Avoid cache invalidation • Usage example: unsigned long my_percpu[NR_CPUS]; cpu = get_cpu(); my_percpu[cpu]; put_cpu(); 4/25/2012 CSE662: Operating Systems Laboratory 14
  • 15. ION Memory Allocator • Manage one or more memory pools, i.e., ION heaps – Each device can have a different set of ION heaps • Combat fragmentation • Serve special hardware needs: – E.g., GPU, display controller, cameras
  • 16. Providing an ION Heap struct ion_heap_ops { int (*allocate) (struct ion_heap *heap, struct ion_buffer *buffer, unsigned long len, unsigned long align, unsigned long flags); void (*free) (struct ion_buffer *buffer); int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, ion_phys_addr_t *addr, size_t *len); struct scatterlist *(*map_dma) (struct ion_heap *heap, struct ion_buffer *buffer); void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer); void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); int (*map_user) (struct ion_heap *heap, struct ion_buffer *buffer, struct vm_area_struct *vma); };
  • 17. Default ION driver • ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc_user(). • ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kzalloc. • ION_HEAP_TYPE_CARVEOUT: carveout memory is physically contiguous and set aside at boot.
  • 18. Using ION From User Space • Who uses ION? – User space device access libraries • Why needs ION? – Allocate large contiguous media buffers – Sharing between two processes or processes and kernel – E.g., still camera library, when buffer is filled with data, it will pass the buffer to the kernel to be processed by JPEG encoder hardware
  • 19. Allocate Buffer from ION • Require Access permission to device /dev/ion • Retrieve a file descriptor (one client per user process) client_fd = open (“/dev/ion”, O_RDONLY) • Fill the following data structure struct ion_allocation_data { size_t len; size_t align; unsigned int flags; // specify which ION heap the buffer is from (i.e., ION_HEAP_TYPE_CARVEOUT | ION_HEAP_TYPE_CONTIG) struct ion_handle *handle; // output parameter, not CPU-accessible buffer pointer } • Use ioctl system call to interact with ION int ioctl(int client_fd, ION_IOC_ALLOC, struct ion_allocation_data *allocation_data)
  • 20. ION Buffer Sharing in User Space • Convert ion_handle to a file descriptor int ioctl(int client_fd, ION_IOC_SHARE, struct ion_fd_data *fd_data); struct ion_fd_data { struct ion_handle *handle; int fd; } • BINDER IPC mechanism can be used to send fd to another process for sharing • To obtain the shared buffer, the second process needs to retrieve the file descriptor via client_fd = open (“/dev/ion”, O_RDONLY)
  • 21. Free ION Buffer • Go through ioctl again int ioctl(int client_fd, ION_IOC_FREE, struct ion_handle_data *handle_data); struct ion_handle_data { struct ion_handle *handle; }
  • 22. ION Buffers in Kernel • ION supports multiple clients in the kernel – One for each driver that uses ION • A kernel driver calls the following function to obtain an ION client handle struct ion_client *ion_client_create( struct ion_device *dev, // /dev/ion unsigned int heap_mask, // selects ION heaps const char *debug_name )
  • 23. Sharing ION Buffer between User Space and Kernel (I) • User process – Allocates the ION buffer – Obtains a file descriptor using ION_IOC_SHARE – Pass the file descriptor to a kernel driver
  • 24. Sharing ION Buffer between User Space and Kernel (II) • Kernel driver – Converts the file descriptor to an ion_handle struct ion_handle *ion_import_fd(struct ion_client *client, int fd_from_user); // based on the physical address of the buffer – Some hardware blocks need physically-contiguous buffers with physical addresses int ion_phys(struct ion_client *client, struct ion_handle *handle, ion_phys_addr_t *addr, size_t *len)
  • 25. Source Code Cross References • Linux kernel – http://lxr.linux.no/ • Android – http://androidxref.com/source/xref/