Read and write the file system Sysfs--sysfs file for the device service

Source: Internet
Author: User
Tags call back

4.3 Sysfs file reads and writes

SYSFS is a file system that exists in memory, and its files exist only in memory. So read and write to the file is actually read and write to the memory, do not involve the operation of the hard disk

Analysis of the process of 4.3.1 reading files

A
A

To request a memory page function:

Fastcall unsigned long get_zeroed_page (gfp_t gfp_mask)
{
	struct page * page;

	/
	 * * Get_zeroed_page () returns a 32-bit address, which cannot represent
	 * a highmem page */
	bug_on ((gfp_m Ask & __gfp_highmem)! = 0);

	page = alloc_pages (gfp_mask | __gfp_zero, 0);
	if (page)
		return (unsigned long) page_address (page);
	return 0;
}

Assign a page space function Alloc_pages:

#ifdef config_numa
extern struct page *alloc_pages_current (gfp_t gfp_mask, unsigned order);

Static inline struct page *
alloc_pages (gfp_t gfp_mask, unsigned int order)
{
	if (unlikely (order >= Max_or DER))
		return NULL;

	Return Alloc_pages_current (gfp_mask, order);
}
extern struct page *alloc_page_vma (gfp_t gfp_mask,
			struct vm_area_struct *vma, unsigned long addr);
#else
#define Alloc_pages (gfp_mask, order) \
		Alloc_pages_node (numa_node_id (), gfp_mask, order)
# Define ALLOC_PAGE_VMA (Gfp_mask, VMA, addr) alloc_pages (gfp_mask, 0)
#endif

Mm/mempolicy.h:

/** * alloc_pages_current-allocate pages. * * @gfp: *%gfp_user USER allocation, *%gfp_kernel KERNEL allocation, *%gfp_highmem Highmem Allocati
 ON, *%gfp_fs-T call back into a file system.
 *%gfp_atomic don ' t sleep. * @order: Power of allocation size in pages.
 0 is a single page.  * * Allocate a page from the kernel page pool.
 When not in * interrupt context and apply the current process NUMA policy.
 * Returns NULL when the no page can be allocated.  * * Don ' t call Cpuset_update_task_memory_state () unless * 1) It's OK to take Cpuset_sem (can WAIT), and * 2) allocating
 For current task (not interrupt).

	*/struct page *alloc_pages_current (gfp_t GFP, unsigned order) {struct Mempolicy *pol = current->mempolicy;
	if (GFP & __gfp_wait) &&!in_interrupt ()) cpuset_update_task_memory_state ();
	if (!pol | | in_interrupt ()) Pol = &default_policy; if (Pol->policy = = Mpol_interleave) return Alloc_page_interlEave (GFP, Order, Interleave_nodes (POL));
Return __alloc_pages (GFP, Order, Zonelist_policy (GFP, Pol)); } export_symbol (Alloc_pages_current);

Include/linux/gfp.h

Static inline struct page *alloc_pages_node (int nid, gfp_t gfp_mask,
						unsigned int order)
{
	if (unlikely ( Order >= Max_order))
		return NULL;

	/* Unknown node is current node *
	/if (Nid < 0)
		nid = numa_node_id ();

	Return __alloc_pages (Gfp_mask, order,
		Node_data (nid)->node_zonelists + gfp_zone (gfp_mask));
}

A

4.3.2 Process Analysis of writing files

The write function of the normal file is Sysfs_write_file, and its code is as follows:


A

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.