Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speculative page fault #359

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ endif # $(dot-config)
# Defaults to vmlinux, but the arch makefile usually adds further targets
all: vmlinux

# force no-pie for distro compilers that enable pie by default
KBUILD_CFLAGS += $(call cc-option, -fno-pie)
KBUILD_CFLAGS += $(call cc-option, -no-pie)
KBUILD_AFLAGS += $(call cc-option, -fno-pie)
KBUILD_CPPFLAGS += $(call cc-option, -fno-pie)

# The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
# values of the respective KBUILD_* variables
ARCH_CPPFLAGS :=
Expand Down
18 changes: 18 additions & 0 deletions arch/x86/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,16 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
if (error_code & PF_INSTR)
flags |= FAULT_FLAG_INSTRUCTION;

if (error_code & PF_USER) {
fault = handle_speculative_fault(mm, address,
flags & ~FAULT_FLAG_ALLOW_RETRY);

if (fault & VM_FAULT_RETRY)
goto retry;

goto done;
}

/*
* When running in the kernel we expect faults to occur only to
* addresses in user space. All other faults represent errors in
Expand Down Expand Up @@ -1379,7 +1389,15 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
return;
}

if (unlikely(fault & VM_FAULT_RETRY)) {
if (fatal_signal_pending(current))
return;

goto done;
}

up_read(&mm->mmap_sem);
done:
if (unlikely(fault & VM_FAULT_ERROR)) {
mm_fault_error(regs, error_code, address, vma, fault);
return;
Expand Down
4 changes: 4 additions & 0 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ extern pgprot_t protection_map[16];
#define FAULT_FLAG_USER 0x40 /* The fault originated in userspace */
#define FAULT_FLAG_REMOTE 0x80 /* faulting for non current tsk/mm */
#define FAULT_FLAG_INSTRUCTION 0x100 /* The fault was during an instruction fetch */
#define FAULT_FLAG_SPECULATIVE 0x200 /* Speculative fault, not holding mmap_sem */

/*
* vm_fault is filled by the the pagefault handler and passed to the vma's
Expand Down Expand Up @@ -319,6 +320,7 @@ struct fault_env {
struct vm_area_struct *vma; /* Target VMA */
unsigned long address; /* Faulting virtual address */
unsigned int flags; /* FAULT_FLAG_xxx flags */
unsigned int sequence;
pmd_t *pmd; /* Pointer to pmd entry matching
* the 'address'
*/
Expand Down Expand Up @@ -1257,6 +1259,8 @@ int invalidate_inode_page(struct page *page);
#ifdef CONFIG_MMU
extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
unsigned int flags);
extern int handle_speculative_fault(struct mm_struct *mm,
unsigned long address, unsigned int flags);
extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
unsigned long address, unsigned int fault_flags,
bool *unlocked);
Expand Down
3 changes: 3 additions & 0 deletions include/linux/mm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ struct vm_area_struct {
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
#endif
struct vm_userfaultfd_ctx vm_userfaultfd_ctx;
seqcount_t vm_sequence;
struct rcu_head vm_rcu_head;
};

struct core_thread {
Expand Down Expand Up @@ -396,6 +398,7 @@ struct kioctx_table;
struct mm_struct {
struct vm_area_struct *mmap; /* list of VMAs */
struct rb_root mm_rb;
seqlock_t mm_seq;
u32 vmacache_seqnum; /* per-thread vmacache */
#ifdef CONFIG_MMU
unsigned long (*get_unmapped_area) (struct file *filp,
Expand Down
1 change: 1 addition & 0 deletions kernel/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
mm->mmap = NULL;
mm->mm_rb = RB_ROOT;
mm->vmacache_seqnum = 0;
seqlock_init(&mm->mm_seq);
atomic_set(&mm->mm_users, 1);
atomic_set(&mm->mm_count, 1);
init_rwsem(&mm->mmap_sem);
Expand Down
1 change: 1 addition & 0 deletions mm/init-mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

struct mm_struct init_mm = {
.mm_rb = RB_ROOT,
.mm_seq = __SEQLOCK_UNLOCKED(init_mm.mm_seq),
.pgd = swapper_pg_dir,
.mm_users = ATOMIC_INIT(2),
.mm_count = ATOMIC_INIT(1),
Expand Down
18 changes: 18 additions & 0 deletions mm/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@

int do_swap_page(struct fault_env *fe, pte_t orig_pte);

extern struct srcu_struct vma_srcu;

extern struct vm_area_struct *find_vma_srcu(struct mm_struct *mm, unsigned long addr);

static inline bool vma_is_dead(struct vm_area_struct *vma, unsigned int sequence)
{
int ret = RB_EMPTY_NODE(&vma->vm_rb);
unsigned seq = ACCESS_ONCE(vma->vm_sequence.sequence);

/*
* Matches both the wmb in write_seqlock_{begin,end}() and
* the wmb in vma_rb_erase().
*/
smp_rmb();

return ret || seq != sequence;
}

void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
unsigned long floor, unsigned long ceiling);

Expand Down
Loading