-
Notifications
You must be signed in to change notification settings - Fork 20
Home work #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ruslansukhov
wants to merge
12
commits into
Kernel-GL-HRK:Ruslan.Sukhov
Choose a base branch
from
ruslansukhov:master
base: Ruslan.Sukhov
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Home work #9
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e17e666
Homework for Lesson-04: kmalloc memory allocation added
a010b14
Homework for Lesson-05
dee62db
three ways of dynamic memory allocation added: kmalloc, vmalloc, kmem…
dbfff23
Time management examples using timer and hrtimer added
72c49fe
Mpu6050 initial project added
1353e20
Simplified show interface for sysfs added
f36b8e7
Mpu6050 devfs driver added. Mpu6050 read procedure is initiated by re…
1eedc44
Dynamic memory management implemented
113561e
Timer+shedulework mechanism
f2dae4d
Merge branch 'Lesson-04-"Memory_Management"'
be00538
Merge branch 'Lesson-05-''Time_Management''
e3c5ee1
Merge branch 'mpu6050_driver'
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /Makefile~ | ||
| *.c~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| ifneq ($(KERNELRELEASE),) | ||
|
|
||
| obj-m += xxx.o | ||
| obj-m += xxx_dynamic.o | ||
|
|
||
| else | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| #include <linux/module.h> | ||
| #include <linux/fs.h> | ||
| #include <linux/cdev.h> | ||
| #include <asm/uaccess.h> | ||
| #include <linux/pci.h> | ||
| #include <linux/version.h> | ||
| #include <linux/init.h> | ||
| #include <linux/slab.h> | ||
|
|
||
| #define BUF_SIZE_MAX 256 | ||
|
|
||
| static enum { ALLOC_TYPE_KMALLOC = 0, ALLOC_TYPE_KVMALLOC, ALLOC_TYPE_KCACHE} alloc_type = ALLOC_TYPE_KMALLOC; | ||
| static struct kmem_cache* pmem_cache = 0; | ||
| static size_t buf_size = 0; | ||
| struct KCACHE_STRUCT { | ||
| char buf[BUF_SIZE_MAX]; | ||
| }; | ||
|
|
||
| static char* buf_msg = 0; | ||
|
|
||
| const char* get_alloc_type(int type) | ||
| { | ||
| switch (type) { | ||
| case ALLOC_TYPE_KMALLOC: | ||
| return "ALLOC_TYPE_KMALLOC"; | ||
| case ALLOC_TYPE_KVMALLOC: | ||
| return "ALLOC_TYPE_KVMALLOC"; | ||
| case ALLOC_TYPE_KCACHE: | ||
| return "ALLOC_TYPE_KCACHE"; | ||
| } | ||
| return "ALLOC_TYPE_UNKNOWN"; | ||
| } | ||
|
|
||
| static void* mem_alloc(int type, size_t size) | ||
| { | ||
| void* buf = 0; | ||
| switch (type) { | ||
| case ALLOC_TYPE_KMALLOC: | ||
| buf = kmalloc(size, GFP_KERNEL); | ||
| break; | ||
| case ALLOC_TYPE_KVMALLOC: | ||
| buf = vmalloc(size); | ||
| break; | ||
| case ALLOC_TYPE_KCACHE: | ||
| buf = kmem_cache_alloc( pmem_cache, GFP_KERNEL ); | ||
| break; | ||
| } | ||
|
|
||
| if (buf) buf_size = size; | ||
|
|
||
| printk(KERN_INFO "[%s:%s] {%s} %zu bytes --> %p\n", THIS_MODULE->name, __FUNCTION__, get_alloc_type(type), size, buf); | ||
| return buf; | ||
| } | ||
|
|
||
| static void mem_free(int type, void* buf) | ||
| { | ||
| if (!buf) { | ||
| printk(KERN_INFO "[%s:%s] Wrong parameter %p\n", THIS_MODULE->name, __FUNCTION__, buf); | ||
| return; | ||
| } | ||
| switch (type) { | ||
| case ALLOC_TYPE_KMALLOC: | ||
| kfree(buf); | ||
| break; | ||
| case ALLOC_TYPE_KVMALLOC: | ||
| vfree(buf); | ||
| break; | ||
| case ALLOC_TYPE_KCACHE: | ||
| kmem_cache_free( pmem_cache, buf ); | ||
| break; | ||
| } | ||
| printk(KERN_INFO "[%s:%s] {%s} bytes --> %p\n", THIS_MODULE->name, __FUNCTION__, get_alloc_type(type), buf); | ||
| } | ||
|
|
||
| void init_kcache(void) | ||
| { | ||
| if (alloc_type != ALLOC_TYPE_KCACHE) return; | ||
| pmem_cache = KMEM_CACHE(KCACHE_STRUCT, 0); | ||
| printk(KERN_INFO "[%s:%s] pmem_cache: %p\n", THIS_MODULE->name, __FUNCTION__, pmem_cache); | ||
| } | ||
|
|
||
| void deinit_kcache(void) | ||
| { | ||
| if (alloc_type != ALLOC_TYPE_KCACHE) { | ||
| mem_free(alloc_type, buf_msg); | ||
| return; | ||
| } | ||
| kmem_cache_destroy( pmem_cache ); | ||
| } | ||
|
|
||
| static ssize_t xxx_show( struct class *class, struct class_attribute *attr, char *buf ) { | ||
| if (!buf_msg) { | ||
| printk(KERN_INFO "[%s:%s] Buffer is empty\n", THIS_MODULE->name, __FUNCTION__); | ||
| return 0; | ||
| } | ||
| strcpy( buf, buf_msg ); | ||
| printk(KERN_INFO "[%s:%s] read %ld\n", THIS_MODULE->name, __FUNCTION__, (long)strlen( buf ) ); | ||
| return strlen( buf ); | ||
| } | ||
|
|
||
| static ssize_t xxx_store( struct class *class, struct class_attribute *attr, const char *buf, size_t count ) { | ||
| if (buf_msg) { | ||
| mem_free(alloc_type, buf_msg); | ||
| buf_msg = 0; | ||
| } | ||
|
|
||
| if (count >= BUF_SIZE_MAX) { | ||
| printk(KERN_INFO "[%s:%s] Buffer size exceeded! \n", THIS_MODULE->name, __FUNCTION__); | ||
| return 0; | ||
| } | ||
| buf_msg = mem_alloc(alloc_type, count + 1); | ||
| if (!buf_msg) { | ||
| printk(KERN_INFO "[%s:%s] Memory allocation error! \n", THIS_MODULE->name, __FUNCTION__); | ||
| return 0; | ||
| } | ||
| else { | ||
| printk(KERN_INFO "[%s:%s] write %ld\n", THIS_MODULE->name, __FUNCTION__, (long)count ); | ||
| strncpy( buf_msg, buf, count ); | ||
| buf_msg[ count ] = '\0'; | ||
| return count; | ||
| } | ||
| } | ||
|
|
||
| CLASS_ATTR_RW( xxx ); | ||
|
|
||
| static struct class *x_class; | ||
|
|
||
| int __init x_init(void) { | ||
| int res; | ||
| printk(KERN_INFO "alloc_type is %s\n", get_alloc_type(alloc_type)); | ||
| x_class = class_create( THIS_MODULE, "x-class" ); | ||
| if( IS_ERR( x_class ) ) printk(KERN_INFO "bad class create\n" ); | ||
| res = class_create_file( x_class, &class_attr_xxx ); | ||
| init_kcache(); | ||
| printk(KERN_INFO "'xxx' module initialized\n" ); | ||
| return res; | ||
| } | ||
|
|
||
| void x_cleanup(void) { | ||
| class_remove_file( x_class, &class_attr_xxx ); | ||
| class_destroy( x_class ); | ||
| deinit_kcache(); | ||
| printk(KERN_INFO "'xxx' module deinitialized\n" ); | ||
| return; | ||
| } | ||
|
|
||
| module_param( alloc_type, int, 0444 ); | ||
| MODULE_PARM_DESC(alloc_type, "Type of memory allocation: 0 - kmalloc; 1 - vmalloc; 2 - cache"); | ||
|
|
||
| module_init( x_init ); | ||
| module_exit( x_cleanup ); | ||
|
|
||
| MODULE_LICENSE( "GPL" ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #include <linux/module.h> | ||
| #include <linux/fs.h> | ||
| #include <linux/cdev.h> | ||
| #include <asm/uaccess.h> | ||
| #include <linux/pci.h> | ||
| #include <linux/version.h> | ||
| #include <linux/init.h> | ||
|
|
||
| #define LEN_MSG 160 | ||
| //static char buf_msg[ LEN_MSG + 1 ] = "Hello from module!\n"; | ||
| static char* buf_msg; | ||
|
|
||
|
|
||
| static ssize_t xxx_show( struct class *class, struct class_attribute *attr, char *buf ) { | ||
| strcpy( buf, buf_msg ); | ||
| printk( "read %ld\n", (long)strlen( buf ) ); | ||
| return strlen( buf ); | ||
| } | ||
|
|
||
| static ssize_t xxx_store( struct class *class, struct class_attribute *attr, const char *buf, size_t count ) { | ||
| printk( "write %ld\n", (long)count ); | ||
| strncpy( buf_msg, buf, count ); | ||
| buf_msg[ count ] = '\0'; | ||
| return count; | ||
| } | ||
|
|
||
| CLASS_ATTR_RW( xxx ); | ||
|
|
||
| static struct class *x_class; | ||
|
|
||
| int __init x_init(void) { | ||
| int res; | ||
| buf_msg = kmalloc(LEN_MSG+1,GFP_KERNEL | __GFP_ZERO); | ||
| x_class = class_create( THIS_MODULE, "x-class" ); | ||
| if( IS_ERR( x_class ) ) printk( "bad class create\n" ); | ||
| res = class_create_file( x_class, &class_attr_xxx ); | ||
| printk( "'xxx' module initialized\n" ); | ||
| return res; | ||
| } | ||
|
|
||
| void x_cleanup(void) { | ||
| class_remove_file( x_class, &class_attr_xxx ); | ||
| class_destroy( x_class ); | ||
| kfree(buf_msg); | ||
| return; | ||
| } | ||
|
|
||
| module_init( x_init ); | ||
| module_exit( x_cleanup ); | ||
|
|
||
| MODULE_LICENSE( "GPL" ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /Makefile~ | ||
| *.c~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # | ||
| # Memory management example | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to update comments |
||
| # | ||
|
|
||
| ifneq ($(KERNELRELEASE),) | ||
|
|
||
| obj-m += xxx_1.o | ||
| obj-m += xxx_2.o | ||
| obj-m += xxx_timer.o | ||
| obj-m += xxx_hrtimer.o | ||
|
|
||
| else | ||
|
|
||
| KERNELDIR := $(BUILD_KERNEL) | ||
|
|
||
| .PHONY: all clean | ||
| all: | ||
| $(MAKE) -C $(KERNELDIR) M=$(CURDIR) modules | ||
| clean: | ||
| $(MAKE) -C $(KERNELDIR) M=$(CURDIR) clean | ||
|
|
||
| endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include <linux/module.h> | ||
| #include <linux/fs.h> | ||
| #include <linux/cdev.h> | ||
| #include <asm/uaccess.h> | ||
| #include <linux/pci.h> | ||
| #include <linux/version.h> | ||
| #include <linux/init.h> | ||
| #include <linux/time.h> | ||
|
|
||
|
|
||
| #define LEN_MSG 160 | ||
| static char* buf_msg; | ||
| static struct timeval* tv; | ||
| static unsigned char first_read; | ||
|
|
||
|
|
||
| static ssize_t xxx_show( struct class *class, struct class_attribute *attr, char *buf ) { | ||
| struct timeval tv_cur; | ||
| strcpy( buf, buf_msg ); | ||
| do_gettimeofday(&tv_cur); | ||
| if (first_read == 1) | ||
| { | ||
| printk( "No previous read operations\n"); | ||
| first_read = 0; | ||
| } | ||
| else | ||
| { | ||
| printk( "previous read was done %ld s ago\n", tv_cur.tv_sec-tv->tv_sec); | ||
| } | ||
| printk( "read %ld\n", (long)strlen( buf ) ); | ||
| *tv = tv_cur; | ||
| return strlen( buf ); | ||
| } | ||
|
|
||
| static ssize_t xxx_store( struct class *class, struct class_attribute *attr, const char *buf, size_t count ) { | ||
| printk( "write %ld\n", (long)count ); | ||
| strncpy( buf_msg, buf, count ); | ||
| buf_msg[ count ] = '\0'; | ||
| return count; | ||
| } | ||
|
|
||
| CLASS_ATTR_RW( xxx ); | ||
|
|
||
| static struct class *x_class; | ||
|
|
||
| int __init x_init(void) { | ||
| int res; | ||
| tv = kmalloc(sizeof(*tv), GFP_KERNEL); | ||
| buf_msg = kmalloc(LEN_MSG+1,GFP_KERNEL | __GFP_ZERO); | ||
| first_read = 1; | ||
| tv->tv_sec = 0; | ||
| tv->tv_usec = 0; | ||
| x_class = class_create( THIS_MODULE, "x-class" ); | ||
| if( IS_ERR( x_class ) ) printk( "bad class create\n" ); | ||
| res = class_create_file( x_class, &class_attr_xxx ); | ||
| printk( "'xxx' module initialized\n" ); | ||
| return res; | ||
| } | ||
|
|
||
| void x_cleanup(void) { | ||
| class_remove_file( x_class, &class_attr_xxx ); | ||
| class_destroy( x_class ); | ||
| kfree(buf_msg); | ||
| kfree(tv); | ||
| return; | ||
| } | ||
|
|
||
| module_init( x_init ); | ||
| module_exit( x_cleanup ); | ||
|
|
||
| MODULE_LICENSE( "GPL" ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include <linux/module.h> | ||
| #include <linux/fs.h> | ||
| #include <linux/cdev.h> | ||
| #include <asm/uaccess.h> | ||
| #include <linux/pci.h> | ||
| #include <linux/version.h> | ||
| #include <linux/init.h> | ||
| #include <linux/time.h> | ||
|
|
||
|
|
||
| #define LEN_MSG 160 | ||
| static char* buf_msg; | ||
| static struct timeval* tv; | ||
|
|
||
|
|
||
| static ssize_t xxx_show( struct class *class, struct class_attribute *attr, char *buf ) { | ||
| strcpy( buf, buf_msg ); | ||
| printk( "previous absolute read time: %ld s %ld mks\n", tv->tv_sec, tv->tv_usec ); | ||
| printk( "read %ld\n", (long)strlen( buf ) ); | ||
| do_gettimeofday(tv); | ||
| return strlen( buf ); | ||
| } | ||
|
|
||
| static ssize_t xxx_store( struct class *class, struct class_attribute *attr, const char *buf, size_t count ) { | ||
| printk( "write %ld\n", (long)count ); | ||
| strncpy( buf_msg, buf, count ); | ||
| buf_msg[ count ] = '\0'; | ||
| return count; | ||
| } | ||
|
|
||
| CLASS_ATTR_RW( xxx ); | ||
|
|
||
| static struct class *x_class; | ||
|
|
||
| int __init x_init(void) { | ||
| int res; | ||
| tv = kmalloc(sizeof(*tv),GFP_KERNEL); | ||
| buf_msg = kmalloc(LEN_MSG+1,GFP_KERNEL | __GFP_ZERO); | ||
| tv->tv_sec = 0; | ||
| tv->tv_usec = 0; | ||
| x_class = class_create( THIS_MODULE, "x-class" ); | ||
| if( IS_ERR( x_class ) ) printk( "bad class create\n" ); | ||
| res = class_create_file( x_class, &class_attr_xxx ); | ||
| printk( "'xxx' module initialized\n" ); | ||
| return res; | ||
| } | ||
|
|
||
| void x_cleanup(void) { | ||
| class_remove_file( x_class, &class_attr_xxx ); | ||
| class_destroy( x_class ); | ||
| kfree(buf_msg); | ||
| kfree(tv); | ||
| return; | ||
| } | ||
|
|
||
| module_init( x_init ); | ||
| module_exit( x_cleanup ); | ||
|
|
||
| MODULE_LICENSE( "GPL" ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very straightforward replacement of static buffer.
But dynamic allocation allow more flexible memory management.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was my first attempt to make pull request. Not very successful attempt. Please see pull requests 39 and 40. They refer to dynamic memory allocation and time management respectively. P.S. How can I delete this pull request (№9)?