臨時內核映射
臨時內核映射和永久內核映射相比,其最大的特點就是不會阻塞請求映射頁框的進程,因此臨時內核映射請求可以發生在中斷和可延遲函數中。系統中的每個CPU都有自己的臨時內核映射窗口,根據不同的需求,選擇不同的窗口來創建映射,這些窗口都以枚舉類型定義在km_type中
- enum km_type {
- KMAP_D(0) KM_BOUNCE_READ,
- KMAP_D(1) KM_SKB_SUNRPC_DATA,
- KMAP_D(2) KM_SKB_DATA_SOFTIRQ,
- KMAP_D(3) KM_USER0,
- KMAP_D(4) KM_USER1,
- KMAP_D(5) KM_BIO_SRC_IRQ,
- KMAP_D(6) KM_BIO_DST_IRQ,
- KMAP_D(7) KM_PTE0,
- KMAP_D(8) KM_PTE1,
- KMAP_D(9) KM_IRQ0,
- KMAP_D(10) KM_IRQ1,
- KMAP_D(11) KM_SOFTIRQ0,
- KMAP_D(12) KM_SOFTIRQ1,
- KMAP_D(13) KM_SYNC_ICACHE,
- KMAP_D(14) KM_SYNC_DCACHE,
- /* UML specific, for copy_*_user - used in do_op_one_page */
- KMAP_D(15) KM_UML_USERCOPY,
- KMAP_D(16) KM_IRQ_PTE,
- KMAP_D(17) KM_NMI,
- KMAP_D(18) KM_NMI_PTE,
- KMAP_D(19) KM_TYPE_NR
- };
其中KM_TYPE_NR標志了一個CPU可以擁有多少個頁表項窗口來建立映射。
臨時內核映射的實現也比永久內核映射要簡單,當一個進程申請在某個窗口創建映射,即使這個窗口已經在之前就建立了映射,新的映射也會建立並且覆蓋之前的映射,所以說這種映射機制是臨時的,並且不會阻塞當前進程。
- void *kmap_atomic(struct page *page, enum km_type type)
- {
- return kmap_atomic_prot(page, type, kmap_prot);
- }
- /*
- * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because
- * no global lock is needed and because the kmap code must perform a global TLB
- * invalidation when the kmap pool wraps.
- *
- * However when holding an atomic kmap it is not legal to sleep, so atomic
- * kmaps are appropriate for short, tight code paths only.
- */
- void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)
- {
- enum fixed_addresses idx;
- unsigned long vaddr;
-
- /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
- /*為了保證函數的原子性,禁止page fault handler*/
- pagefault_disable();
-
- if (!PageHighMem(page))/*屬於低端內存則直接返回page的線性地址*/
- return page_address(page);
-
- debug_kmap_atomic(type);
-
- /*smp_processor_id()得到CPU的標識號,用KM_TYPE_NR乘以該標識號就得到了該CPU可用窗口的區段,
- 再加上type就得到了相應的屬於該CPU的窗口*/
- idx = type + KM_TYPE_NR*smp_processor_id();
- vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);/*得到對應頁表項的虛擬地址*/
- BUG_ON(!pte_none(*(kmap_pte-idx)));
- /*將頁表項與page進行關聯,用kmap_pte-idx而不是用kmap_pte+idx,因為固定映射區是逆向生長的,
- 也就是說枚舉項越靠前的部分的虛擬地址越靠後*/
- set_pte(kmap_pte-idx, mk_pte(page, prot));
-
- return (void *)vaddr;
- }
若要手動撤銷當前的臨時內核映射,則可調用kunmap_atomic()函數
- void kunmap_atomic(void *kvaddr, enum km_type type)
- {
- unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
- enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();
-
- /*
- * Force other mappings to Oops if they'll try to access this pte
- * without first remap it. Keeping stale mappings around is a bad idea
- * also, in case the page changes cacheability attributes or becomes
- * a protected page in a hypervisor.
- */
- /*如果建立了映射則清除頁表項的內容,並且淸刷相應的TLB項*/
- if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx))
- kpte_clear_flush(kmap_pte-idx, vaddr);
- else {
- #ifdef CONFIG_DEBUG_HIGHMEM
- BUG_ON(vaddr < PAGE_OFFSET);
- BUG_ON(vaddr >= (unsigned long)high_memory);
- #endif
- }
-
- pagefault_enable();/*重新使能page fault handler*/
- }
相關閱讀:
Linux高端內存映射(上) http://www.linuxidc.com/Linux/2012-05/60627.htm
Linux高端內存映射(中) http://www.linuxidc.com/Linux/2012-05/60628.htm
Linux高端內存映射(中) http://www.linuxidc.com/Linux/2012-05/60902.htm