Two questions about copy_pte_range() in kernel
I've been trying to understand how fork()
works and finally arrived at copy_pte_range()
. Most of the functions are understandable but few are quite questionable.
Kernel: 4.14.84
static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
unsigned long addr, unsigned long end)
{
pte_t *orig_src_pte, *orig_dst_pte;
pte_t *src_pte, *dst_pte;
spinlock_t *src_ptl, *dst_ptl;
int progress = 0;
int rss[NR_MM_COUNTERS];
swp_entry_t entry = (swp_entry_t){0};
again:
init_rss_vec(rss);
dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
if (!dst_pte)
return -ENOMEM;
src_pte = pte_offset_map(src_pmd, addr);
src_ptl = pte_lockptr(src_mm, src_pmd);
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
orig_src_pte = src_pte;
orig_dst_pte = dst_pte;
arch_enter_lazy_mmu_mode();
do {
/*
* We are holding two locks at this point - either of them
* could generate latencies in another task on another CPU.
*/
if (progress >= 32) {
progress = 0;
if (need_resched() ||
spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
break;
}
if (pte_none(*src_pte)) {
progress++;
continue;
}
entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
vma, addr, rss);
if (entry.val)
break;
progress += 8;
} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
arch_leave_lazy_mmu_mode();
spin_unlock(src_ptl);
pte_unmap(orig_src_pte);
add_mm_rss_vec(dst_mm, rss);
pte_unmap_unlock(orig_dst_pte, dst_ptl);
cond_resched();
if (entry.val) {
if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
return -ENOMEM;
progress = 0;
}
if (addr != end)
goto again;
return 0;
}
Questions
1. In the do {} while()
, what is the purpose of progress
variable?
2. After do {} while()
, there is pte_unmap(orig_src_pte);
Why is it needed? This is the process of fork()
. Based on my knowledge, the parent pte(orig_src_pte)
should still be mapped because the process is based on Copy-on-Write so I guess it doesn't have to be unmapped.
linux kernel x86
add a comment |
I've been trying to understand how fork()
works and finally arrived at copy_pte_range()
. Most of the functions are understandable but few are quite questionable.
Kernel: 4.14.84
static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
unsigned long addr, unsigned long end)
{
pte_t *orig_src_pte, *orig_dst_pte;
pte_t *src_pte, *dst_pte;
spinlock_t *src_ptl, *dst_ptl;
int progress = 0;
int rss[NR_MM_COUNTERS];
swp_entry_t entry = (swp_entry_t){0};
again:
init_rss_vec(rss);
dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
if (!dst_pte)
return -ENOMEM;
src_pte = pte_offset_map(src_pmd, addr);
src_ptl = pte_lockptr(src_mm, src_pmd);
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
orig_src_pte = src_pte;
orig_dst_pte = dst_pte;
arch_enter_lazy_mmu_mode();
do {
/*
* We are holding two locks at this point - either of them
* could generate latencies in another task on another CPU.
*/
if (progress >= 32) {
progress = 0;
if (need_resched() ||
spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
break;
}
if (pte_none(*src_pte)) {
progress++;
continue;
}
entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
vma, addr, rss);
if (entry.val)
break;
progress += 8;
} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
arch_leave_lazy_mmu_mode();
spin_unlock(src_ptl);
pte_unmap(orig_src_pte);
add_mm_rss_vec(dst_mm, rss);
pte_unmap_unlock(orig_dst_pte, dst_ptl);
cond_resched();
if (entry.val) {
if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
return -ENOMEM;
progress = 0;
}
if (addr != end)
goto again;
return 0;
}
Questions
1. In the do {} while()
, what is the purpose of progress
variable?
2. After do {} while()
, there is pte_unmap(orig_src_pte);
Why is it needed? This is the process of fork()
. Based on my knowledge, the parent pte(orig_src_pte)
should still be mapped because the process is based on Copy-on-Write so I guess it doesn't have to be unmapped.
linux kernel x86
add a comment |
I've been trying to understand how fork()
works and finally arrived at copy_pte_range()
. Most of the functions are understandable but few are quite questionable.
Kernel: 4.14.84
static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
unsigned long addr, unsigned long end)
{
pte_t *orig_src_pte, *orig_dst_pte;
pte_t *src_pte, *dst_pte;
spinlock_t *src_ptl, *dst_ptl;
int progress = 0;
int rss[NR_MM_COUNTERS];
swp_entry_t entry = (swp_entry_t){0};
again:
init_rss_vec(rss);
dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
if (!dst_pte)
return -ENOMEM;
src_pte = pte_offset_map(src_pmd, addr);
src_ptl = pte_lockptr(src_mm, src_pmd);
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
orig_src_pte = src_pte;
orig_dst_pte = dst_pte;
arch_enter_lazy_mmu_mode();
do {
/*
* We are holding two locks at this point - either of them
* could generate latencies in another task on another CPU.
*/
if (progress >= 32) {
progress = 0;
if (need_resched() ||
spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
break;
}
if (pte_none(*src_pte)) {
progress++;
continue;
}
entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
vma, addr, rss);
if (entry.val)
break;
progress += 8;
} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
arch_leave_lazy_mmu_mode();
spin_unlock(src_ptl);
pte_unmap(orig_src_pte);
add_mm_rss_vec(dst_mm, rss);
pte_unmap_unlock(orig_dst_pte, dst_ptl);
cond_resched();
if (entry.val) {
if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
return -ENOMEM;
progress = 0;
}
if (addr != end)
goto again;
return 0;
}
Questions
1. In the do {} while()
, what is the purpose of progress
variable?
2. After do {} while()
, there is pte_unmap(orig_src_pte);
Why is it needed? This is the process of fork()
. Based on my knowledge, the parent pte(orig_src_pte)
should still be mapped because the process is based on Copy-on-Write so I guess it doesn't have to be unmapped.
linux kernel x86
I've been trying to understand how fork()
works and finally arrived at copy_pte_range()
. Most of the functions are understandable but few are quite questionable.
Kernel: 4.14.84
static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
unsigned long addr, unsigned long end)
{
pte_t *orig_src_pte, *orig_dst_pte;
pte_t *src_pte, *dst_pte;
spinlock_t *src_ptl, *dst_ptl;
int progress = 0;
int rss[NR_MM_COUNTERS];
swp_entry_t entry = (swp_entry_t){0};
again:
init_rss_vec(rss);
dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
if (!dst_pte)
return -ENOMEM;
src_pte = pte_offset_map(src_pmd, addr);
src_ptl = pte_lockptr(src_mm, src_pmd);
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
orig_src_pte = src_pte;
orig_dst_pte = dst_pte;
arch_enter_lazy_mmu_mode();
do {
/*
* We are holding two locks at this point - either of them
* could generate latencies in another task on another CPU.
*/
if (progress >= 32) {
progress = 0;
if (need_resched() ||
spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
break;
}
if (pte_none(*src_pte)) {
progress++;
continue;
}
entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
vma, addr, rss);
if (entry.val)
break;
progress += 8;
} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
arch_leave_lazy_mmu_mode();
spin_unlock(src_ptl);
pte_unmap(orig_src_pte);
add_mm_rss_vec(dst_mm, rss);
pte_unmap_unlock(orig_dst_pte, dst_ptl);
cond_resched();
if (entry.val) {
if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
return -ENOMEM;
progress = 0;
}
if (addr != end)
goto again;
return 0;
}
Questions
1. In the do {} while()
, what is the purpose of progress
variable?
2. After do {} while()
, there is pte_unmap(orig_src_pte);
Why is it needed? This is the process of fork()
. Based on my knowledge, the parent pte(orig_src_pte)
should still be mapped because the process is based on Copy-on-Write so I guess it doesn't have to be unmapped.
linux kernel x86
linux kernel x86
edited 1 hour ago
Stephen Kitt
177k24402480
177k24402480
asked 3 hours ago
Mr.NobodyMr.Nobody
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The
progress
variable measures the cost of the operations performed under locks, and avoids holding those locks for too long. At most every 32 calls topte_none
, or 4 calls tocopy_one_pte
(which is expensive), or a combination thereof, the function checks whether a reschedule is needed, or if the locks are requested elsewhere; if so, it releases the locks and allows a reschedule. The function continues where it left off, thanks to the jump toagain
.The unmap call doesn’t unmap the original PTE in the source process, it undoes the effects of the
src_pte = pte_offset_map(src_pmd, addr);
line at the start of the function.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507868%2ftwo-questions-about-copy-pte-range-in-kernel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The
progress
variable measures the cost of the operations performed under locks, and avoids holding those locks for too long. At most every 32 calls topte_none
, or 4 calls tocopy_one_pte
(which is expensive), or a combination thereof, the function checks whether a reschedule is needed, or if the locks are requested elsewhere; if so, it releases the locks and allows a reschedule. The function continues where it left off, thanks to the jump toagain
.The unmap call doesn’t unmap the original PTE in the source process, it undoes the effects of the
src_pte = pte_offset_map(src_pmd, addr);
line at the start of the function.
add a comment |
The
progress
variable measures the cost of the operations performed under locks, and avoids holding those locks for too long. At most every 32 calls topte_none
, or 4 calls tocopy_one_pte
(which is expensive), or a combination thereof, the function checks whether a reschedule is needed, or if the locks are requested elsewhere; if so, it releases the locks and allows a reschedule. The function continues where it left off, thanks to the jump toagain
.The unmap call doesn’t unmap the original PTE in the source process, it undoes the effects of the
src_pte = pte_offset_map(src_pmd, addr);
line at the start of the function.
add a comment |
The
progress
variable measures the cost of the operations performed under locks, and avoids holding those locks for too long. At most every 32 calls topte_none
, or 4 calls tocopy_one_pte
(which is expensive), or a combination thereof, the function checks whether a reschedule is needed, or if the locks are requested elsewhere; if so, it releases the locks and allows a reschedule. The function continues where it left off, thanks to the jump toagain
.The unmap call doesn’t unmap the original PTE in the source process, it undoes the effects of the
src_pte = pte_offset_map(src_pmd, addr);
line at the start of the function.
The
progress
variable measures the cost of the operations performed under locks, and avoids holding those locks for too long. At most every 32 calls topte_none
, or 4 calls tocopy_one_pte
(which is expensive), or a combination thereof, the function checks whether a reschedule is needed, or if the locks are requested elsewhere; if so, it releases the locks and allows a reschedule. The function continues where it left off, thanks to the jump toagain
.The unmap call doesn’t unmap the original PTE in the source process, it undoes the effects of the
src_pte = pte_offset_map(src_pmd, addr);
line at the start of the function.
answered 1 hour ago
Stephen KittStephen Kitt
177k24402480
177k24402480
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507868%2ftwo-questions-about-copy-pte-range-in-kernel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown