Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp831304ybv; Thu, 20 Feb 2020 08:04:24 -0800 (PST) X-Google-Smtp-Source: APXvYqx71GUX88PLWMToLyunsfGdHIjuEmZqvejUcLLxw7dqHO0YGFp04+Qf6HNUduOuZneJnKvM X-Received: by 2002:aca:2118:: with SMTP id 24mr2617362oiz.28.1582214664207; Thu, 20 Feb 2020 08:04:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582214664; cv=none; d=google.com; s=arc-20160816; b=cb2ymM2IQMR+C6sEIKzpaB8lIerZJvZ1nQC2ut8eYececYYR7qKbz8SjKUAzHSFmwn Mt+jMySyET1GZzBzbQ5oR4yTj8hw5IUX8g72w4enUfKD7kuCZok98h3Vsn6ZaOhUP7ic IePjx5ivGxasA7aBBxljEKjaCVhyDvUdDKUskeOPCXq4JEUDXcV1w8zApWvSdjde6zRg xjwEaMx8ZPAmIPdkkchdCbJvs8DLE6mqoYqhKOj5c5ZP8ecMmuvJzdYR6nVIYmGUkipM scgcmN99Cq/7iz75frt5LAX6NH/a9CUiFEfX+WntWHlE1gtRQExduJ9BZACU2Q/y6WjH tlTA== 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 :dkim-signature; bh=lwNlsBs8NIb6Y77atButveT5DrFFM9+Zoa3o5H5cFjw=; b=iDsZUK18NvnIB3YPQqByamN9CaeeiVfcFsdAPWujPY/h8zmoudLpIClMfGYZlnEMOd DZEtzCrJHbg48o52Lqn0pOqaMHbf8MLV4rHvJPIvHmTciQbSct5aR8j4L2Nl7HABVWAm bY5dKvt9MmYpg7I4vT67IAeBy03IFIaRuLZjJg4jfvy9pMebzwYT+scd6dgZGs34f3Xq Tw3FQWqlezDEajasA9e6lV6IMrY2WW3VV/knVLhg9MzP4wcm3UOwd2+3fbU5zSGlHmhe e4EexWbcCKVYWESh02AkQs61DzXNBxvSJ6xm6pim2cUIV6aLv6eMOxBpuXGCzPgSJP8a TVBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YGCOgway; 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=pass (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 185si11749239oie.52.2020.02.20.08.04.05; Thu, 20 Feb 2020 08:04:24 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YGCOgway; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728804AbgBTQDJ (ORCPT + 99 others); Thu, 20 Feb 2020 11:03:09 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:53802 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728798AbgBTQDI (ORCPT ); Thu, 20 Feb 2020 11:03:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582214586; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lwNlsBs8NIb6Y77atButveT5DrFFM9+Zoa3o5H5cFjw=; b=YGCOgwayF1eIniQUiuMWQrwnb7zw4PewZ7uDvVbwX7M1skSHBKDi00aXpPYla5g8E/yFGh BiDvsj2244nRors6aALOJ1cJQTeKSVKs5y8K9N6vitjD3wSSEZgaBc+9YOPh+l0L6ze7dH BL/PhWdMWF1JpmHNjLr6+P2R5ncheSo= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-414-95rLSdHiNVmGtj-_hRhBNw-1; Thu, 20 Feb 2020 11:03:03 -0500 X-MC-Unique: 95rLSdHiNVmGtj-_hRhBNw-1 Received: by mail-qv1-f70.google.com with SMTP id s5so2855714qvr.15 for ; Thu, 20 Feb 2020 08:03:03 -0800 (PST) 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=lwNlsBs8NIb6Y77atButveT5DrFFM9+Zoa3o5H5cFjw=; b=kWtPrmA0tUuAYOipXZ/G1LK05nXfMg01ajl/+JeeEPcfe11ZKFRZ08YB02UVRgDRyz OFgfXMBwtj/VPczwqPZRPLrKxdk4riTsLlSNSmrW4FG9Dzs/Bu7QoBK1Fo45LdeXYWi2 iq/nA7nxJ9xNZ2TbSfR3VLcTnrYsvzifJA7L6LMeUP5FjS8s8bhg0E/U1JeV2PhU7jab 99qImn2WcAgnWl0Y96EIo3LfR0lFfoAnWjh5zq/M6V0vFX5NMsLS5rYvIzN6sMjxlGq0 LdcyXlg9JQ1v4CtnvYEs/bcCUhmEZgvkIQ9TZYtVaw0laHhV8/nG0qJK2e2y5bdOpGWw WFnw== X-Gm-Message-State: APjAAAUBqLS4bezDn7nhfc6sSgtNsC+vc3RIMrOJDNFZsGE/yiMepkdw qch7gxv+P0xM88dzNAADGqlW3U5fA3ypf18YTllKIsvf2UHs3NW2sEmq7ZoVuaewUN6d5yNjkN1 oFTKDmIgOBDBs0RC+caFX1T9P X-Received: by 2002:a37:6343:: with SMTP id x64mr12490176qkb.469.1582214582493; Thu, 20 Feb 2020 08:03:02 -0800 (PST) X-Received: by 2002:a37:6343:: with SMTP id x64mr12490151qkb.469.1582214582267; Thu, 20 Feb 2020 08:03:02 -0800 (PST) Received: from xz-x1.redhat.com ([104.156.64.75]) by smtp.gmail.com with ESMTPSA id n123sm1857635qke.58.2020.02.20.08.03.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2020 08:03:01 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Peter Xu , Martin Cracauer , Mike Rapoport , Hugh Dickins , Jerome Glisse , "Kirill A . Shutemov" , Matthew Wilcox , Pavel Emelyanov , Brian Geffon , Maya Gokhale , Denis Plotnikov , Andrea Arcangeli , Johannes Weiner , "Dr . David Alan Gilbert" , Linus Torvalds , Mike Kravetz , Marty McFadden , David Hildenbrand , Bobby Powers , Mel Gorman Subject: [PATCH RESEND v6 16/16] mm/userfaultfd: Honor FAULT_FLAG_KILLABLE in fault path Date: Thu, 20 Feb 2020 11:03:00 -0500 Message-Id: <20200220160300.9941-1-peterx@redhat.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200220155353.8676-1-peterx@redhat.com> References: 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 c076d3295958..703c1c3faa6e 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.24.1