Skip to content

Commit a91ae4f

Browse files
Steve Sistarejfvogel
Steve Sistare
authored andcommitted
mm: introduce MADV_DOEXEC
madvise MADV_DOEXEC preserves a memory range across exec. Initially only supported for non-executable, non-stack, anonymous memory. MADV_DOEXEC is single-use and after exec madvise must done again to preserve memory for a subsequent exec. MADV_DONTEXEC reverts the effect of a previous MADV_DOEXEC call and undoes the preservation of the range. Orabug: 32387884 Signed-off-by: Steve Sistare <steven.sistare@oracle.com> Signed-off-by: Anthony Yznaga <anthony.yznaga@oracle.com> Reviewed-by: Khalid Aziz <khalid.aziz@oracle.com> (cherry picked from commit 5495dfe) Signed-off-by: Jack Vogel <jack.vogel@oracle.com>
1 parent 637e8db commit a91ae4f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

include/uapi/asm-generic/mman-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
#define MADV_COLD 20 /* deactivate these pages */
7272
#define MADV_PAGEOUT 21 /* reclaim these pages */
7373

74+
#define MADV_DOEXEC 22 /* do inherit across exec */
75+
#define MADV_DONTEXEC 23 /* don't inherit across exec */
76+
7477
/* compatibility flags */
7578
#define MAP_FILE 0
7679

mm/madvise.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ static long madvise_behavior(struct vm_area_struct *vma,
102102
case MADV_KEEPONFORK:
103103
new_flags &= ~VM_WIPEONFORK;
104104
break;
105+
case MADV_DOEXEC:
106+
/*
107+
* MADV_DOEXEC is only supported on private, non-executable,
108+
* non-stack anonymous memory and if the VM_EXEC_KEEP flag
109+
* is available.
110+
*/
111+
if (!VM_EXEC_KEEP || !vma_is_anonymous(vma) || vma->vm_flags & (VM_EXEC|VM_SHARED|VM_STACK)) {
112+
error = -EINVAL;
113+
goto out;
114+
}
115+
new_flags |= (new_flags & ~VM_MAYEXEC) | VM_EXEC_KEEP;
116+
break;
117+
case MADV_DONTEXEC:
118+
if (!VM_EXEC_KEEP) {
119+
error = -EINVAL;
120+
goto out;
121+
}
122+
if (new_flags & VM_EXEC_KEEP)
123+
new_flags |= (new_flags & ~VM_EXEC_KEEP) | VM_MAYEXEC;
124+
break;
105125
case MADV_DONTDUMP:
106126
new_flags |= VM_DONTDUMP;
107127
break;
@@ -982,6 +1002,8 @@ madvise_behavior_valid(int behavior)
9821002
case MADV_SOFT_OFFLINE:
9831003
case MADV_HWPOISON:
9841004
#endif
1005+
case MADV_DOEXEC:
1006+
case MADV_DONTEXEC:
9851007
return true;
9861008

9871009
default:
@@ -1036,6 +1058,9 @@ madvise_behavior_valid(int behavior)
10361058
* MADV_DONTDUMP - the application wants to prevent pages in the given range
10371059
* from being included in its core dump.
10381060
* MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1061+
* MADV_DOEXEC - On exec, preserve and duplicate this area in the new process
1062+
* if the new process allows it.
1063+
* MADV_DONTEXEC - Undo the effect of MADV_DOEXEC.
10391064
*
10401065
* return values:
10411066
* zero - success

0 commit comments

Comments
 (0)