Received: by 10.223.185.116 with SMTP id b49csp4684026wrg; Tue, 27 Feb 2018 00:22:13 -0800 (PST) X-Google-Smtp-Source: AG47ELuBPcFMnJM/T4kzaczlQ1bX4fFxvTdmaCWDMcgbQznGubXLISNy6d9TmUNyBGDk/4SNvHnn X-Received: by 2002:a17:902:2845:: with SMTP id e63-v6mr4531982plb.389.1519719732975; Tue, 27 Feb 2018 00:22:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519719732; cv=none; d=google.com; s=arc-20160816; b=up7pJIONQpSuiSk/kIGuHcR5Jj21h4RG+ben+jov3AQoMJlhGzP3PH4oVVe4SKK8CQ 7MbdP2Zi2ToW5mr7SruCYUJkJSxNMKq/7Qb1Fcmgjb2ibb4QsBcXs0Uf+S4e6cqxd5tB pyE2+cjcPLkckItSSObf2pGzchEn+3j0VWFmZkWBhGynwDBXp2Bgrp1ZJBoS0iBOdnBL dIgZeiSJesAq10w75hv6qICx5G0lLqVwtbahGI4OQRrKFzEYkkyh/MqIv5Wrrha63IAS AiAkahoZpzQoWzQnxpyjE80A6RaTKclTcLRFikVAkklQskK5vWjgs1/LwvL8UVJKdhTB yDUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:references:in-reply-to:date :subject:cc:to:from:arc-authentication-results; bh=DTrzxu60HyeeNeOemdXxuirPToAhNpJkaKIJlAqEQpE=; b=O7laPZy8Q1lNTlv7reYsFHR/w4tmc+dis6Tyfht7VK/srB5Xhigpt4UqJmZgc6/NSe xGwRbbOakeJhnArCGfKeVc/ZFJZ7P4cF7JV9ORnKM7MHxiW4IfpXAXXRpndHgzlo0/e7 dkol/BE6vgSy+QyjVCEVram9hd4c/QS4boZg77gzKzLHFEvSpGqUyPLciQir6VYSFnwP /roxnXac/06GQ5ovjsE0HoKTGDqRa/1J+lmaBgst0/nJkb0IOYZTrx8V/K9KjzpqeuaY dzeHPp4bS15QHUuDgGruiBN3kpJlr2u7lL8hnhpmX7LMJM0qIDheU+xnZO3yTqu/yUw8 ES3g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f11-v6si6084067plj.470.2018.02.27.00.21.58; Tue, 27 Feb 2018 00:22:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752238AbeB0IUJ (ORCPT + 99 others); Tue, 27 Feb 2018 03:20:09 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:60318 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752206AbeB0IUH (ORCPT ); Tue, 27 Feb 2018 03:20:07 -0500 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w1R8JL11092852 for ; Tue, 27 Feb 2018 03:20:07 -0500 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0a-001b2d01.pphosted.com with ESMTP id 2gd1yav3nd-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 27 Feb 2018 03:20:06 -0500 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 27 Feb 2018 08:20:03 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp14.uk.ibm.com (192.168.101.144) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 27 Feb 2018 08:20:00 -0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w1R8K0q856098826; Tue, 27 Feb 2018 08:20:00 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D56224C046; Tue, 27 Feb 2018 08:13:30 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 436934C04A; Tue, 27 Feb 2018 08:13:29 +0000 (GMT) Received: from rapoport-lnx (unknown [9.148.8.77]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Tue, 27 Feb 2018 08:13:29 +0000 (GMT) Received: by rapoport-lnx (sSMTP sendmail emulation); Tue, 27 Feb 2018 10:19:58 +0200 From: Mike Rapoport To: Andrew Morton , Andrea Arcangeli Cc: Pavel Emelyanov , linux-mm , linux-api , lkml , crml , Mike Rapoport Subject: [PATCH 2/3] userfaultfd: non-cooperative: generalize wake key structure Date: Tue, 27 Feb 2018 10:19:51 +0200 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519719592-22668-1-git-send-email-rppt@linux.vnet.ibm.com> References: <1519719592-22668-1-git-send-email-rppt@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18022708-0016-0000-0000-0000052A4335 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18022708-0017-0000-0000-000028666E74 Message-Id: <1519719592-22668-3-git-send-email-rppt@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-02-27_04:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1802270100 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Upcoming support for synchronous non-page-fault events will require userfaultfd_wake_function to be able to differentiate between the event types. Depending on the event type, different parameters will define if the wait queue element should be awaken. This requires more general structure than userfaultfd_wake_range to be used as the "key" parameter for userfaultfd_wake_function. This patch introduces userfaultfd_wake_key that is used for waking up threads waiting on page-fault and non-cooperative events. Signed-off-by: Mike Rapoport --- fs/userfaultfd.c | 114 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 42 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index b32c7aaeca6b..d9f74b389706 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -91,21 +91,44 @@ struct userfaultfd_wake_range { unsigned long len; }; +struct userfaultfd_wake_key { + u8 event; + union { + struct userfaultfd_wake_range range; + } arg; +}; + +static bool userfaultfd_should_wake(struct userfaultfd_wait_queue *uwq, + struct userfaultfd_wake_key *key) +{ + /* key->event == 0 means wake all */ + if (key->event && key->event != uwq->msg.event) + return false; + + if (key->event == UFFD_EVENT_PAGEFAULT) { + unsigned long start, len, address; + + /* len == 0 means wake all threads waiting on page fault */ + address = uwq->msg.arg.pagefault.address; + start = key->arg.range.start; + len = key->arg.range.len; + if (len && (start > address || start + len <= address)) + return false; + } + + return true; +} + static int userfaultfd_wake_function(wait_queue_entry_t *wq, unsigned mode, - int wake_flags, void *key) + int wake_flags, void *_key) { - struct userfaultfd_wake_range *range = key; + struct userfaultfd_wake_key *key = _key; int ret; struct userfaultfd_wait_queue *uwq; - unsigned long start, len; uwq = container_of(wq, struct userfaultfd_wait_queue, wq); ret = 0; - /* len == 0 means wake all */ - start = range->start; - len = range->len; - if (len && (start > uwq->msg.arg.pagefault.address || - start + len <= uwq->msg.arg.pagefault.address)) + if (!userfaultfd_should_wake(uwq, key)) goto out; WRITE_ONCE(uwq->waken, true); /* @@ -585,7 +608,7 @@ static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx, goto out; ewq->ctx = ctx; - init_waitqueue_entry(&ewq->wq, current); + userfaultfd_init_waitqueue(ctx, ewq); release_new_ctx = NULL; spin_lock(&ctx->event_wqh.lock); @@ -596,7 +619,7 @@ static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx, __add_wait_queue(&ctx->event_wqh, &ewq->wq); for (;;) { set_current_state(TASK_KILLABLE); - if (ewq->msg.event == 0) + if (READ_ONCE(ewq->waken)) break; if (READ_ONCE(ctx->released) || fatal_signal_pending(current)) { @@ -653,9 +676,10 @@ static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx, static void userfaultfd_event_complete(struct userfaultfd_ctx *ctx, struct userfaultfd_wait_queue *ewq) { - ewq->msg.event = 0; - wake_up_locked(&ctx->event_wqh); - __remove_wait_queue(&ctx->event_wqh, &ewq->wq); + struct userfaultfd_wake_key key = { 0 }; + + key.event = ewq->msg.event; + __wake_up_locked_key(&ctx->event_wqh, TASK_NORMAL, &key); } int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs) @@ -854,8 +878,10 @@ static int userfaultfd_release(struct inode *inode, struct file *file) struct userfaultfd_ctx *ctx = file->private_data; struct mm_struct *mm = ctx->mm; struct vm_area_struct *vma, *prev; - /* len == 0 means wake all */ - struct userfaultfd_wake_range range = { .len = 0, }; + /* event == 0 means wake all */ + struct userfaultfd_wake_key key = { + .event = 0, + }; unsigned long new_flags; WRITE_ONCE(ctx->released, true); @@ -903,12 +929,12 @@ static int userfaultfd_release(struct inode *inode, struct file *file) * the fault_*wqh. */ spin_lock(&ctx->fault_pending_wqh.lock); - __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range); - __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &range); + __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &key); + __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, &key); spin_unlock(&ctx->fault_pending_wqh.lock); /* Flush pending events that may still wait on event_wqh */ - wake_up_all(&ctx->event_wqh); + __wake_up(&ctx->event_wqh, TASK_NORMAL, 0, &key); wake_up_poll(&ctx->fd_wqh, EPOLLHUP); userfaultfd_ctx_put(ctx); @@ -1201,20 +1227,20 @@ static ssize_t userfaultfd_read(struct file *file, char __user *buf, } static void __wake_userfault(struct userfaultfd_ctx *ctx, - struct userfaultfd_wake_range *range) + struct userfaultfd_wake_key *key) { spin_lock(&ctx->fault_pending_wqh.lock); /* wake all in the range and autoremove */ if (waitqueue_active(&ctx->fault_pending_wqh)) __wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, - range); + key); if (waitqueue_active(&ctx->fault_wqh)) - __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, range); + __wake_up_locked_key(&ctx->fault_wqh, TASK_NORMAL, key); spin_unlock(&ctx->fault_pending_wqh.lock); } static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx, - struct userfaultfd_wake_range *range) + struct userfaultfd_wake_key *key) { unsigned seq; bool need_wakeup; @@ -1241,7 +1267,7 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx, cond_resched(); } while (read_seqcount_retry(&ctx->refile_seq, seq)); if (need_wakeup) - __wake_userfault(ctx, range); + __wake_userfault(ctx, key); } static __always_inline int validate_range(struct mm_struct *mm, @@ -1567,10 +1593,11 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, * permanently and it avoids userland to call * UFFDIO_WAKE explicitly. */ - struct userfaultfd_wake_range range; - range.start = start; - range.len = vma_end - start; - wake_userfault(vma->vm_userfaultfd_ctx.ctx, &range); + struct userfaultfd_wake_key key; + key.event = UFFD_EVENT_PAGEFAULT; + key.arg.range.start = start; + key.arg.range.len = vma_end - start; + wake_userfault(vma->vm_userfaultfd_ctx.ctx, &key); } new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); @@ -1622,7 +1649,7 @@ static int userfaultfd_wake(struct userfaultfd_ctx *ctx, { int ret; struct uffdio_range uffdio_wake; - struct userfaultfd_wake_range range; + struct userfaultfd_wake_key key; const void __user *buf = (void __user *)arg; ret = -EFAULT; @@ -1633,16 +1660,17 @@ static int userfaultfd_wake(struct userfaultfd_ctx *ctx, if (ret) goto out; - range.start = uffdio_wake.start; - range.len = uffdio_wake.len; + key.event = UFFD_EVENT_PAGEFAULT; + key.arg.range.start = uffdio_wake.start; + key.arg.range.len = uffdio_wake.len; /* * len == 0 means wake all and we don't want to wake all here, * so check it again to be sure. */ - VM_BUG_ON(!range.len); + VM_BUG_ON(!key.arg.range.len); - wake_userfault(ctx, &range); + wake_userfault(ctx, &key); ret = 0; out: @@ -1655,7 +1683,7 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, __s64 ret; struct uffdio_copy uffdio_copy; struct uffdio_copy __user *user_uffdio_copy; - struct userfaultfd_wake_range range; + struct userfaultfd_wake_key key; user_uffdio_copy = (struct uffdio_copy __user *) arg; @@ -1691,12 +1719,13 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx, goto out; BUG_ON(!ret); /* len == 0 would wake all */ - range.len = ret; + key.event = UFFD_EVENT_PAGEFAULT; + key.arg.range.len = ret; if (!(uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE)) { - range.start = uffdio_copy.dst; - wake_userfault(ctx, &range); + key.arg.range.start = uffdio_copy.dst; + wake_userfault(ctx, &key); } - ret = range.len == uffdio_copy.len ? 0 : -EAGAIN; + ret = key.arg.range.len == uffdio_copy.len ? 0 : -EAGAIN; out: return ret; } @@ -1707,7 +1736,7 @@ static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx, __s64 ret; struct uffdio_zeropage uffdio_zeropage; struct uffdio_zeropage __user *user_uffdio_zeropage; - struct userfaultfd_wake_range range; + struct userfaultfd_wake_key key; user_uffdio_zeropage = (struct uffdio_zeropage __user *) arg; @@ -1738,12 +1767,13 @@ static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx, goto out; /* len == 0 would wake all */ BUG_ON(!ret); - range.len = ret; + key.event = UFFD_EVENT_PAGEFAULT; + key.arg.range.len = ret; if (!(uffdio_zeropage.mode & UFFDIO_ZEROPAGE_MODE_DONTWAKE)) { - range.start = uffdio_zeropage.range.start; - wake_userfault(ctx, &range); + key.arg.range.start = uffdio_zeropage.range.start; + wake_userfault(ctx, &key); } - ret = range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN; + ret = key.arg.range.len == uffdio_zeropage.range.len ? 0 : -EAGAIN; out: return ret; } -- 2.7.4