Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp211194ybn; Tue, 24 Sep 2019 21:33:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqzrdvhuUste0Ebky71CRKgFxCxdR6tk05MfGSYWFKsLdlO7c+eQiREPbHeMak9fIkgAoXFD X-Received: by 2002:a05:600c:295b:: with SMTP id n27mr4799163wmd.128.1569385997927; Tue, 24 Sep 2019 21:33:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569385997; cv=none; d=google.com; s=arc-20160816; b=mgWQPb0XUHkHXz4YSrm+/1n0LMYm8aaFm3vpHpXbmuQuFpDeyJKfy6GZ6GmQt7KO9a sE1LJHycSeBMwVHAlkgzUnqvkvgvAyJVzmNjoLD4RQ17AMmOLgO+YA6U/hLTL0O7dxi5 5n6ArNRwZRNTPBqcPODh8Wq2Ns93X3yQerCNqRrIM9YRqiJYbUl/rct9l34VJPN87mxt O7Qrlmrh1HpMQ0HFCeb6PFgQ4ffYpe6KHx2spBZXFSuOPmi2I0+5gJj+LRX/UBtWfEIZ nESg3zpT9GGwlMrQcwPpieTl972e+Q2XGHIm0yXjjgsCKHmVAnjM+ntrdLjdUPbMqoKp cecg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=N4oZOlVdiq1iUNu2ipKdKrBjjBpOUecRmSvPVqDDXZc=; b=sn6tRHOeI4IL5y/iNygx7ajGB7Qfe4kKkwLr5WWI0HUexe6ywi0cgqPZmYX2rwhrSO wUYonimyAAqWZw8wRsF5YL9MBmlqU9Eb6N8TlcpuRcUmhu06b3GjEwMjHdgXyXd1eU1b NHB4LExIQLC4bgca9pzbvsO1Sr2ary4zSoFTh6Y14Dq0vWqMWhp7Aijy8efvv/w+zdwR 18M7U96VqRpbKvkSey7MqYgc7i4vmYGnGjcq6OS3dV62UpXZnTNHDDKh9EqGt/knNC1X 7/g9Gh4FRFXFBIoWWXvbAnsUS62U3TdwIiIVfUJ44NyfjwfzZEsnMajQIV4NRccBYGFj lXHg== 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f29si2149749ejj.110.2019.09.24.21.32.54; Tue, 24 Sep 2019 21:33:17 -0700 (PDT) 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406586AbfIWE0x (ORCPT + 99 others); Mon, 23 Sep 2019 00:26:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34770 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405826AbfIWE0x (ORCPT ); Mon, 23 Sep 2019 00:26:53 -0400 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6A9B63B71F for ; Mon, 23 Sep 2019 04:26:52 +0000 (UTC) Received: by mail-pl1-f200.google.com with SMTP id f8so175833plj.10 for ; Sun, 22 Sep 2019 21:26:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N4oZOlVdiq1iUNu2ipKdKrBjjBpOUecRmSvPVqDDXZc=; b=Oq/7zhuZwAz0bwKWpXkx9GKeYzfIjCbrpGX6d3F2LwpOwahJjeSqoBcfIna//oxwWw GwlaoVdav9S7vcWBgIXOr1l8b2fvHYt+FwDc+IJX8FmeGjplFIAFLPzKCvk9UVR31PQH VtbBNKrHIJudAlobDVGzrGKWUfuNDdgkwoudxiQ9hIKDZ4FQWKBWJLytUB16673PbWZv GmPvi+l51EItSvkIjgwQjh7uurgudDKCYnh2rgYUKBFvgwCSeV++R5bXdY9WxHgX4wui aKBKJzfly1ooLSmB1jFWVANyn1c6ghl/0/6P3LqpjfdZ9rfZD6AScV8XJR2U/Nd+G2li YhrA== X-Gm-Message-State: APjAAAUBEWn4R8NWmH5hZCW47ofSYfKbgYsHeh2N0xF68sLB30k/V/2w PHWjqjznqnELkiqL0HvKMYb/MRYISodfmhoe9ykQifrtMClUs4ZFZPlVAY4X3JpTuN/oAIEzYwY 7T1kVrrLM1wXpkB7Kx8Huoo4z X-Received: by 2002:a17:902:166:: with SMTP id 93mr30567156plb.195.1569212811912; Sun, 22 Sep 2019 21:26:51 -0700 (PDT) X-Received: by 2002:a17:902:166:: with SMTP id 93mr30567137plb.195.1569212811739; Sun, 22 Sep 2019 21:26:51 -0700 (PDT) Received: from xz-x1.redhat.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id x20sm11781867pfp.120.2019.09.22.21.26.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Sep 2019 21:26:51 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Hugh Dickins , Maya Gokhale , Jerome Glisse , Pavel Emelyanov , Johannes Weiner , peterx@redhat.com, Martin Cracauer , Marty McFadden , Shaohua Li , Andrea Arcangeli , Mike Kravetz , Denis Plotnikov , Mike Rapoport , Linus Torvalds , Mel Gorman , "Kirill A . Shutemov" , "Dr . David Alan Gilbert" Subject: [PATCH v4 10/10] mm/userfaultfd: Honor FAULT_FLAG_KILLABLE in fault path Date: Mon, 23 Sep 2019 12:25:23 +0800 Message-Id: <20190923042523.10027-11-peterx@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190923042523.10027-1-peterx@redhat.com> References: <20190923042523.10027-1-peterx@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Userfaultfd fault path was by default killable even if the caller does not have FAULT_FLAG_KILLABLE. That makes sense before in that when with gup we don't have FAULT_FLAG_KILLABLE properly set before. Now after previous patch we've got FAULT_FLAG_KILLABLE applied even for gup code so it should also make sense to let userfaultfd to honor the FAULT_FLAG_KILLABLE. Because we're unconditionally setting FAULT_FLAG_KILLABLE in gup code right now, this patch should have no functional change. It also cleaned the code a little bit by introducing some helpers. Signed-off-by: Peter Xu --- fs/userfaultfd.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 2b3b48e94ae4..8c5863ccbf0e 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -334,6 +334,30 @@ static inline bool userfaultfd_must_wait(struct userfaultfd_ctx *ctx, return ret; } +/* Should pair with userfaultfd_signal_pending() */ +static inline long userfaultfd_get_blocking_state(unsigned int flags) +{ + if (flags & FAULT_FLAG_INTERRUPTIBLE) + return TASK_INTERRUPTIBLE; + + if (flags & FAULT_FLAG_KILLABLE) + return TASK_KILLABLE; + + return TASK_UNINTERRUPTIBLE; +} + +/* Should pair with userfaultfd_get_blocking_state() */ +static inline bool userfaultfd_signal_pending(unsigned int flags) +{ + if (flags & FAULT_FLAG_INTERRUPTIBLE) + return signal_pending(current); + + if (flags & FAULT_FLAG_KILLABLE) + return fatal_signal_pending(current); + + return false; +} + /* * The locking rules involved in returning VM_FAULT_RETRY depending on * FAULT_FLAG_ALLOW_RETRY, FAULT_FLAG_RETRY_NOWAIT and @@ -355,7 +379,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) struct userfaultfd_ctx *ctx; struct userfaultfd_wait_queue uwq; vm_fault_t ret = VM_FAULT_SIGBUS; - bool must_wait, return_to_userland; + bool must_wait; long blocking_state; /* @@ -462,9 +486,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) uwq.ctx = ctx; uwq.waken = false; - return_to_userland = vmf->flags & FAULT_FLAG_INTERRUPTIBLE; - blocking_state = return_to_userland ? TASK_INTERRUPTIBLE : - TASK_KILLABLE; + blocking_state = userfaultfd_get_blocking_state(vmf->flags); spin_lock_irq(&ctx->fault_pending_wqh.lock); /* @@ -490,8 +512,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) up_read(&mm->mmap_sem); if (likely(must_wait && !READ_ONCE(ctx->released) && - (return_to_userland ? !signal_pending(current) : - !fatal_signal_pending(current)))) { + userfaultfd_signal_pending(vmf->flags))) { wake_up_poll(&ctx->fd_wqh, EPOLLIN); schedule(); ret |= VM_FAULT_MAJOR; @@ -513,8 +534,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) set_current_state(blocking_state); if (READ_ONCE(uwq.waken) || READ_ONCE(ctx->released) || - (return_to_userland ? signal_pending(current) : - fatal_signal_pending(current))) + userfaultfd_signal_pending(vmf->flags)) break; schedule(); } -- 2.21.0