1 / 79

Linux

Code Slides. Linux. → File Systems Hui Liu, Pengpeng Qi Device Drivers Andrew Henderson. File System. Hui Liu && Pengpeng Qi. Related code super_block. struct super_block{ struct list_head s_list; usigned long s_blocksize; struct file_system_tpye *s_type;

mura
Télécharger la présentation

Linux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Code Slides Linux → File Systems Hui Liu, Pengpeng Qi Device Drivers Andrew Henderson

  2. File System Hui Liu && Pengpeng Qi

  3. Related codesuper_block struct super_block{ struct list_head s_list; usigned long s_blocksize; struct file_system_tpye *s_type; struct super_operations *s_op; struct semaphore s_lock; int s_need_sync_fs; struct list_head s_dirty; struct block_device *s_bdev; ... }

  4. Related codesuper_operation struct super_operations{ struct inode *(*alloc_inode)(struct super_block *sb); void (*destory_inode)()struct inode *; void (*read_inode)(struct inode *, int); int (*sync_fs)(struct super_block *sb, int wait); ... };

  5. Related codeinode struct inode{ unsigned long i_ino; unode_t i_node; uid_t i_uid; struct timespec i_atime; struct timespec i_btime; struct timespec i_ctime; unsigned short i_bytes; struct inode_operations *i_op; struct file_operations *i_fop; struct super_block *i_sb; ...

  6. Related codeinode_operation & file_operation struct inode_operations{ int (*create)(struct inode *, struct dentry *, struct nameidata *); struct dentry *(*lookup)(struct inode *, struct dentry *, struct nameidata *); int (*skdir)(struct inode *, struct dentry *, int); int (*rename)(struct inode *, struct dentry *, struct inode *,struct dentry *); ... } struct file_operations{ struct module *owner; ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); ssize_t (*write)(struct file*, const char __user *, size_t, loff_t *); int (*open)(struct inode *, struct file *); ... }

  7. Related codeMFT Typedef struct tag_MFTHEAD{ BYTE hHeadID[4]; //MFT symbol, must be file USHORT usAtteOffset; // offset of first attribute WORD wResident; // file attribute symbol ULONG ulMFTSize; // length of the file record ULONG ulMFTAllocSize; // record the allocation size ULONG ullMainMFT; // record file index ……. }MFTHEAD, *LPMFTHEAD;

  8. Related codeMFT Typedef struct tag_MFTATTR{ DWORD dwAttrType; //attr type USHORT usAttrSize; // attr size WORD wReservel; BYTE bISResident; BYTE bLenName; //attr name length USHORT usDataOffset; // attr beginning offset WORD wISCompr; WORD wAttrID; // attr ID ……. };

  9. Related codeExt4 struct ext4_super_block { __le32 s_inodes_count; /* Inodes count */ __le32 s_blocks_count; /* Blocks count */ __le32 s_r_blocks_count; /* Reserved blocks count */ __le32 s_free_blocks_count; /* Free blocks count */ __le32 s_free_inodes_count; /* Free inodes count */ __le32 s_first_data_block; /* First Data Block */ __le32 s_log_block_size; /* Block size */ /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */ __le32 s_blocks_count_hi; /* Blocks count */ __le32 s_r_blocks_count_hi; /* Reserved blocks count */ __le32 s_free_blocks_count_hi; /* Free blocks count */ };

  10. Related codeExt4 struct ext4_group_desc { __le32 bg_block_bitmap; /* Blocks bitmap block */ __le32 bg_inode_bitmap; /* Inodes bitmap block */ __le32 bg_inode_table; /* Inodes table block */ __le16 bg_free_blocks_count; /* Free blocks count */ __le16 bg_free_inodes_count; /* Free inodes count */ __le16 bg_used_dirs_count; /* Directories count */ __u16 bg_flags; __u32 bg_reserved[3]; __le32 bg_block_bitmap_hi; /* Blocks bitmap block MSB */ __le32 bg_inode_bitmap_hi; /* Inodes bitmap block MSB */ __le32 bg_inode_table_hi; /* Inodes table block MSB */ };

  11. Code Slides Linux File Systems Hui Liu, Pengpeng Qi → Device Drivers Andrew Henderson

  12. By: Andrew Henderson CIS 657 – Operating Systems Basics of Device Driver Modules In The 2.6 Linux Kernel

  13. Example Of Using insmod, rmmod, And modprobe • Prompt$ insmod mymodule param=50 • Prompt$ rmmod mymodule • Prompt$ modprobe mymodule

  14. Example of Using lsmod • Prompt$ lsmod • Module Size Used by • af_packet 27392 2 • 8139too 30592 0 • snd_cs46xx 96872 3 • snd_pcm_oss 55808 1 • snd_mixer_oss 21760 2 snd_pcm_oss

  15. Module Code: Init/Exit • #include <linux/init.h> • #include <linux/module.h> • static int init_func(void) { printk(KERN_ALERT “Module loaded\n”); return 0; } • static void exit_func(void) { printk(KERN_ALERT “Module unloaded\n”); } • module_init(init_func); • module_exit(exit_func);

  16. Module Code: Parameters • #include <linux/moduleparam.h> • Static char *someText = “test”; • Static int count = 1; • Static int init_func(void) { • module_param(count, int, S_IRUGO); • module_param(someText, charp, S_IRUGO); • } • module_init(init_func); • Prompt$ insmod module someText=”mytext” count=5

  17. ioctl() in the module • int (*ioctl) (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg); • #define OUR_MAGIC_NUMBER 50 • #define OUR_IOCRESET _IO(OUR_MAGIC_NUMBER, 0) • #define OUR_IOCSETCOUNT _IOW(OUR_MAGIC_NUMBER, 1, int) • #define OUR_IOCGETCOUNT _IOR(OUR_MAGIC_NUMBER, 2, int) • ioctl() function implementation in driver is a large switch statement that handles each of our defined IO functions

  18. Code Slides Android → ashmem (Shared Mem) A. Shetty, K. Patil Lowmem, OOM Killer Zeyu Liu, Shudan Shi Wake Locks A. Iyer, A. Ananthanarayanan Alarm Timers Xiao Zhang, C. Wang

  19. ASHMEMPart-2 Code(By AbhishekShetty And KaustubhPatil)

  20. ASHMEM AREA struct ashmem_area { char name[ASHMEM_FULL_NAME_LEN]; /* optional name for /proc/pid/maps */ struct list_head unpinned_list; /* list of all ashmem areas */ struct file *file; /* the shmem-based backing file */ size_t size; /* size of the mapping, in bytes */ unsignedlong prot_mask; /* allowed prot bits, as vm_flags */ };

  21. ASHMEM RANGE structashmem_range { structlist_headlru; /* entry in LRU list */ structlist_head unpinned; /* entry in its area's unpinned list */ structashmem_area*asma; /* associated area */ size_tpgstart; /* starting page, inclusive */ size_tpgend; /* ending page, inclusive */ unsignedint purged; /* ASHMEM_NOT or ASHMEM_WAS_PURGED */ };

  22. AshmemInput/Output Control staticlongashmem_ioctl(struct file *file, unsignedint cmd, unsignedlong arg){ struct ashmem_area *asma = file->private_data; long ret =-ENOTTY; switch (cmd) { case ASHMEM_SET_NAME: ret = set_name(asma, (void __user *) arg);break; case ASHMEM_GET_NAME: ret = get_name(asma, (void __user *) arg);break; case ASHMEM_SET_SIZE: ret =-EINVAL; if (!asma->file) { ret = 0;asma->size = (size_t) arg; }break; case ASHMEM_GET_SIZE: ret = asma->size;break;

  23. AshmemInput/Output Control(Contd..) case ASHMEM_SET_PROT_MASK: ret = set_prot_mask(asma, arg);break; case ASHMEM_GET_PROT_MASK: ret = asma->prot_mask;break; case ASHMEM_PIN: case ASHMEM_UNPIN: case ASHMEM_GET_PIN_STATUS: ret = ashmem_pin_unpin(asma, cmd, (void __user *) arg); break; case ASHMEM_PURGE_ALL_CACHES: ret =-EPERM; if (capable(CAP_SYS_ADMIN)) { ret = ashmem_shrink(&ashmem_shrinker, 0, GFP_KERNEL); ashmem_shrink(&ashmem_shrinker, ret, GFP_KERNEL); }break; }return ret; }

  24. Ashmem Region Allocator int ashmem_create_region(const char *name, size_t size) { int fd, ret; fd = open(ASHMEM_DEVICE, O_RDWR); if (fd < 0) return fd; if (name) { char buf[ASHMEM_NAME_LEN]; strlcpy(buf, name, sizeof(buf)); ret = ioctl(fd, ASHMEM_SET_NAME, buf); if (ret < 0) goto error; }ret = ioctl(fd, ASHMEM_SET_SIZE, size); if (ret < 0) goto error; return fd; error: close(fd); return ret; }

  25. Ashmem Useful Function Calls Ashmem_set_prot_region intashmem_set_prot_region(intfd, intprot) { return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); } Ashmem_get_size_region intashmem_get_size_region(intfd) { return ioctl(fd, ASHMEM_GET_SIZE, NULL); }

  26. Ashmem Useful Function Calls(Contd..) Ashmem_pin_region intashmem_pin_region(intfd, size_t offset, size_tlen) { structashmem_pin pin = { offset, len }; return ioctl(fd, ASHMEM_PIN, &pin); } Ashmem_unpin_region intashmem_unpin_region(intfd, size_t offset, size_tlen) { structashmem_pin pin = { offset, len }; return ioctl(fd, ASHMEM_UNPIN, &pin); }

  27. Example To create an ashmem region fd= ashmem_create_region("ShareRegion", size); if(fd== 0) { data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(data != MAP_FAILED) { ...your code } }

  28. PROBLEM The above code does not provide solution to share memory area: Reason: Memory Pointer ‘data’ ,Memory area name ‘ShareRegion’ and File Descriptor ‘fd’ cannot be shared with other process.

  29. SOLUTION Share the file descriptor with the binder. Binder has special functions to transfer file descriptors over its interface. The class ‘MemoryHeapBase’ can be used to help with ashmem handling and file descriptor sharing.

  30. Questions??? Please ask us some questions! We’d love a challenge!

  31. Code Slides Android ashmem (Shared Mem) A. Shetty, K. Patil → Lowmem, OOM Killer Zeyu Liu, Shudan Shi Wake Locks A. Iyer, A. Ananthanarayanan Alarm Timers Xiao Zhang, C. Wang

  32. Code of OOM Implement of OOM in Linux, android and FreeBSD

  33. OOM in FreeBSD FreeBSD will kill the biggest process in page Replacement algorithm

  34. Prototype of OOM Linux

  35. Give OOM a Last Chance

  36. Start Killing

  37. Body of OOM Kill Process

  38. Choose a Victim

  39. Calculate Who Will Been Killed

  40. Killing the Bad Task(Victim)

  41. Send Signal to Kill

  42. Android’s Low Memory Shrinker

  43. Definition of Process Category

  44. Low Memory Shrink

  45. Decide a Thresthold

  46. Choose the Effect Less Process

  47. Lower Then Threshold? Return;

  48. Send Signal to Kill

  49. Android Application’s OOM

More Related