The patch titled proc: fix pagemap_read() error case has been added to the -mm tree. Its filename is proc-fix-pagemap_read-error-case.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/SubmitChecklist when testing your code *** See http://userweb.kernel.org/~akpm/stuff/added-to-mm.txt to find out what to do about this The current -mm tree may be found at http://userweb.kernel.org/~akpm/mmotm/ ------------------------------------------------------ Subject: proc: fix pagemap_read() error case From: KOSAKI Motohiro <kosaki.motohiro@xxxxxxxxxxxxxx> Currently, pagemap_read() has three error and/or corner case handling mistake. (1) If ppos parameter is wrong, mm refcount will be leak. (2) If count parameter is 0, mm refcount will be leak too. (3) If the current task is sleeping in kmalloc() and the system is out of memory and oom-killer kill the proc associated task, mm_refcount prevent the task free its memory. then system may hang up. <Quote Hugh's explain why we shold call kmalloc() before get_mm()> check_mem_permission gets a reference to the mm. If we __get_free_page after check_mem_permission, imagine what happens if the system is out of memory, and the mm we're looking at is selected for killing by the OOM killer: while we wait in __get_free_page for more memory, no memory is freed from the selected mm because it cannot reach exit_mmap while we hold that reference. This patch fixes the above three. Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@xxxxxxxxxxxxxx> Cc: Hugh Dickins <hughd@xxxxxxxxxx> Cc: Jovi Zhang <bookjovi@xxxxxxxxx> Acked-by: Hugh Dickins <hughd@xxxxxxxxxx> Cc: Stephen Wilson <wilsons@xxxxxxxx> Cc: Alexey Dobriyan <adobriyan@xxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- fs/proc/task_mmu.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff -puN fs/proc/task_mmu.c~proc-fix-pagemap_read-error-case fs/proc/task_mmu.c --- a/fs/proc/task_mmu.c~proc-fix-pagemap_read-error-case +++ a/fs/proc/task_mmu.c @@ -769,18 +769,12 @@ static ssize_t pagemap_read(struct file if (!task) goto out; - mm = mm_for_maps(task); - ret = PTR_ERR(mm); - if (!mm || IS_ERR(mm)) - goto out_task; - ret = -EINVAL; /* file position must be aligned */ if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES)) goto out_task; ret = 0; - if (!count) goto out_task; @@ -788,7 +782,12 @@ static ssize_t pagemap_read(struct file pm.buffer = kmalloc(pm.len, GFP_TEMPORARY); ret = -ENOMEM; if (!pm.buffer) - goto out_mm; + goto out_task; + + mm = mm_for_maps(task); + ret = PTR_ERR(mm); + if (!mm || IS_ERR(mm)) + goto out_free; pagemap_walk.pmd_entry = pagemap_pte_range; pagemap_walk.pte_hole = pagemap_pte_hole; @@ -831,7 +830,7 @@ static ssize_t pagemap_read(struct file len = min(count, PM_ENTRY_BYTES * pm.pos); if (copy_to_user(buf, pm.buffer, len)) { ret = -EFAULT; - goto out_free; + goto out_mm; } copied += len; buf += len; @@ -841,10 +840,10 @@ static ssize_t pagemap_read(struct file if (!ret || ret == PM_END_OF_BUFFER) ret = copied; -out_free: - kfree(pm.buffer); out_mm: mmput(mm); +out_free: + kfree(pm.buffer); out_task: put_task_struct(task); out: _ Patches currently in -mm which might be from kosaki.motohiro@xxxxxxxxxxxxxx are origin.patch oom-use-pte-pages-in-oom-score.patch mm-per-node-vmstat-show-proper-vmstats.patch mm-per-node-vmstat-show-proper-vmstats-fix.patch mm-increase-reclaim_distance-to-30.patch mm-introduce-wait_on_page_locked_killable.patch x86mm-make-pagefault-killable.patch mm-mem-hotplug-fix-section-mismatch-setup_per_zone_inactive_ratio-should-be-__meminit.patch mm-mem-hotplug-recalculate-lowmem_reserve-when-memory-hotplug-occur.patch mm-mem-hotplug-update-pcp-stat_threshold-when-memory-hotplug-occur.patch mm-mem-hotplug-update-pcp-stat_threshold-when-memory-hotplug-occur-fix.patch mm-convert-vma-vm_flags-to-64-bit.patch mm-add-__nocast-attribute-to-vm_flags.patch fremap-convert-vm_flags-to-unsigned-long-long.patch procfs-convert-vm_flags-to-unsigned-long-long.patch oom-replace-pf_oom_origin-with-toggling-oom_score_adj.patch oom-replace-pf_oom_origin-with-toggling-oom_score_adj-update.patch mm-mmu_gather-rework.patch powerpc-mmu_gather-rework.patch sparc-mmu_gather-rework.patch s390-mmu_gather-rework.patch arm-mmu_gather-rework.patch sh-mmu_gather-rework.patch ia64-mmu_gather-rework.patch um-mmu_gather-rework.patch mm-now-that-all-old-mmu_gather-code-is-gone-remove-the-storage.patch mm-powerpc-move-the-rcu-page-table-freeing-into-generic-code.patch mm-extended-batches-for-generic-mmu_gather.patch lockdep-mutex-provide-mutex_lock_nest_lock.patch mm-remove-i_mmap_lock-lockbreak.patch mm-convert-i_mmap_lock-to-a-mutex.patch mm-revert-page_lock_anon_vma-lock-annotation.patch mm-improve-page_lock_anon_vma-comment.patch mm-use-refcounts-for-page_lock_anon_vma.patch mm-convert-anon_vma-lock-to-a-mutex.patch mm-optimize-page_lock_anon_vma-fast-path.patch mn10300-replace-mm-cpu_vm_mask-with-mm_cpumask.patch tile-replace-mm-cpu_vm_mask-with-mm_cpumask.patch mm-convert-mm-cpu_vm_cpumask-into-cpumask_var_t.patch mm-convert-mm-cpu_vm_cpumask-into-cpumask_var_t-checkpatch-fixes.patch mem-hotplug-call-isolate_lru_page-with-elevated-refcount.patch mem-hwpoison-fix-page-refcount-around-isolate_lru_page.patch mm-strictly-require-elevated-page-refcount-in-isolate_lru_page.patch mm-check-if-any-page-in-a-pageblock-is-reserved-before-marking-it-migrate_reserve.patch mm-check-if-any-page-in-a-pageblock-is-reserved-before-marking-it-migrate_reserve-fix.patch readahead-readahead-page-allocations-are-ok-to-fail.patch vmscan-change-shrink_slab-interfaces-by-passing-shrink_control.patch vmscan-change-shrinker-api-by-passing-shrink_control-struct.patch mm-batch-activate_page-to-reduce-lock-contention.patch alpha-replace-with-new-cpumask-apis.patch m32r-convert-cpumask-api.patch m32r-fix-spin_lock_irqsave-misuse.patch m32r-remove-redundant-declaration.patch sparse-define-dummy-build_bug_on-definition-for-sparse.patch sparse-define-__must_be_array-for-__checker__.patch sparse-undef-__compiletime_warningerror-if-__checker__-is-defined.patch mm-move-enum-vm_event_item-into-a-standalone-header-file.patch memcg-count-the-soft_limit-reclaim-in-global-background-reclaim.patch memcg-add-stats-to-monitor-soft_limit-reclaim.patch add-the-pagefault-count-into-memcg-stats.patch add-the-pagefault-count-into-memcg-stats-fix.patch vmscanmemcg-memcg-aware-swap-token.patch vmscanmemcg-memcg-aware-swap-token-fix.patch cpusets-randomize-node-rotor-used-in-cpuset_mem_spread_node.patch cpusets-randomize-node-rotor-used-in-cpuset_mem_spread_node-cpusets-initialize-spread-rotor-lazily.patch proc-put-check_mem_permission-after-__get_free_page-in-mem_write.patch proc-fix-pagemap_read-error-case.patch cpumask-convert-for_each_cpumask-with-for_each_cpu.patch cpumask-convert-cpumask_of_cpu-to-cpumask_of.patch kexec-remove-kmsg_dump_kexec.patch kexec-remove-kmsg_dump_kexec-fix.patch -- To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html