From patchwork Tue Sep 5 21:42:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13375092 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB0C0CA100D for ; Tue, 5 Sep 2023 21:42:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE87B8E000A; Tue, 5 Sep 2023 17:42:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D97AD8D0001; Tue, 5 Sep 2023 17:42:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C38238E000A; Tue, 5 Sep 2023 17:42:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B4E8A8D0001 for ; Tue, 5 Sep 2023 17:42:43 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 88FB7A071A for ; Tue, 5 Sep 2023 21:42:43 +0000 (UTC) X-FDA: 81203868606.17.93880CD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 7EB3C16001B for ; Tue, 5 Sep 2023 21:42:41 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cl1++Ayt; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693950161; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yL/LhjyJgEaE1ERsjh3smTILDBi2p+nbzGXe8vQHohc=; b=Fo6DGQMXNp+v0c057XB9c00EFG2IjrwivQO4+hKrjT5IvwQGa/OdMdan+319PQyMExAFLK GuE6Vy+6wSBwt/tPWslUnNJpU0/cDS3ZtsRKwE19q54Q+NWSOH6OYGdztVEc0Q77KyFbdO gr8Y6Oo0e6XsaBVwaGv6lOphTZOa/xE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cl1++Ayt; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693950161; a=rsa-sha256; cv=none; b=Z6ciEasAPVfYMFWzzbYwGn2QNcb5xZPo9Yy+orbThY/aXaz8XDCxms4loyTi74km1MMieT 8QNZmmY7Jtg5r5jLUwy001N+Pd+BeMhl5ACwTzhzzmzA9TUlDnjTFAzXT00PURjjn1cVN4 ZYgUCrgfePW0x8T1+fhIVSDdiT82tM4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693950160; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yL/LhjyJgEaE1ERsjh3smTILDBi2p+nbzGXe8vQHohc=; b=cl1++AytMnm7VxG1lbJ1+L8bKs+Kn0XKnFNCOvAOu3JmBK8sKUm2NoL1tTl/MJXjvlWgTs VMYYPqJG5U1CFS5CgisYRYfI8pLThXVvwEDSjPDr2vHqSF6foJCK5kibYBnsaK/HSCoS+i ndANBZ3WWyR615H8PNJwI4Wvop67uJs= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-84-1UXV2iZSPDu7-PCLodrrNw-1; Tue, 05 Sep 2023 17:42:39 -0400 X-MC-Unique: 1UXV2iZSPDu7-PCLodrrNw-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-76f0d1c4045so44101785a.1 for ; Tue, 05 Sep 2023 14:42:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693950159; x=1694554959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yL/LhjyJgEaE1ERsjh3smTILDBi2p+nbzGXe8vQHohc=; b=BZ5dqTJpHBbVIEXPWTOAkZ/73/VRLBuUZEylsQYAlHWHRlZic1QzavEMywBXVMYmBi j02UeD+yEzA9RE3pv+zAumHyZa0tyTs/S874uJ4MkjBI0LiJPazriW8XaC9WHZNLF6wi fuT20FgQ6HkuxReuSErFWjl75L8dd4km3YDHKJVKLHcteLzd3P6KzCALVu7eG/53bQCT BhxoJCckTmNm8bMzp+Z6P+rMo4tiIoCnRRaMy+OLy04qZ/30nOSjRkipYUPJ2ulO41co mQCaXbl0G6qwjIHvRcnwV3jk4VoJ1FffpzngHYnrw2AwHsId1yUFyZw0wvR4O9/1CpaH Ww+A== X-Gm-Message-State: AOJu0YwYaDNf/FwvxYc9c1U1agwbl+7s5/A1MYdG+BPxdBHmnTIiftp5 HbUBZoL9nV0SQb8Kpm4N/VFLkYtchW8yIh7z7tzMUCEDgmP+ScQZVinH19FZdi9cw17FKSgLc4m yfvFJ87Zb5Jo= X-Received: by 2002:a05:620a:1aa4:b0:76f:1614:577d with SMTP id bl36-20020a05620a1aa400b0076f1614577dmr16479345qkb.4.1693950158927; Tue, 05 Sep 2023 14:42:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEdEZxCLbQuYXO7d4fPer9qvq4VofEooWqnv//Ff0/KOa/Ui0Gkdskd8CCG0Sg9WRZVsAXIfA== X-Received: by 2002:a05:620a:1aa4:b0:76f:1614:577d with SMTP id bl36-20020a05620a1aa400b0076f1614577dmr16479333qkb.4.1693950158658; Tue, 05 Sep 2023 14:42:38 -0700 (PDT) Received: from x1n.redhat.com (cpe5c7695f3aee0-cm5c7695f3aede.cpe.net.cable.rogers.com. [99.254.144.39]) by smtp.gmail.com with ESMTPSA id i2-20020a37c202000000b007682af2c8aasm4396938qkm.126.2023.09.05.14.42.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 14:42:38 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Anish Moorthy , Axel Rasmussen , Alexander Viro , Mike Kravetz , Peter Zijlstra , Andrew Morton , Mike Rapoport , Christian Brauner , peterx@redhat.com, linux-fsdevel@vger.kernel.org, Andrea Arcangeli , Ingo Molnar , James Houghton , Nadav Amit Subject: [PATCH 1/7] mm/userfaultfd: Make uffd read() wait event exclusive Date: Tue, 5 Sep 2023 17:42:29 -0400 Message-ID: <20230905214235.320571-2-peterx@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230905214235.320571-1-peterx@redhat.com> References: <20230905214235.320571-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 7EB3C16001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 5k5cn64ecmued98rh8whds4udkhc8cca X-HE-Tag: 1693950161-817748 X-HE-Meta: U2FsdGVkX18YwF11UeZ51HqeNIr54NgjvBFwClulVMOPRLXIthD9NnWp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2b3plYuFxG+/DgufQSuqnIYtzGaet5npAiRDoUiMOTl4VfHCJytpXEeWIe0kkglUav1Ib29qeqtFK3tBYKDjpVJ/w3pYyamqEScI3RbdJHK5hCr8Z/XBpi5h68r2XItnV+Cay/7CO+LzzDQxZQhf2a4hG61FoDdqYNGj/sZWGNyGUWVcX3BH8jJFTU3V43wZ4vrxCYL4az1I6tAf8v7UXWGRJWNbMwPyRg0JI1h0HmCWFSYmKEf51oAIJaX59ivEbmZnRlRGydjL3qWr5fFKp9/zlrap6FYZJgXvf3E0gPPsDj1Vj/Z912uZCS2i/OWn176K489B9uRqU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrea Arcangeli When a new message is generated for an userfaultfd, instead of waking up all the readers, we can wake up only one exclusive reader to process the event. Waking up >1 readers for 1 message will be a waste of resource, where the rest readers will see nothing again and re-queue. This should make userfaultfd read() O(1) on wakeups. Note that queuing on head is intended (rather than tail) to make sure the readers are waked up in LIFO fashion; fairness doesn't matter much here, but caching does. Signed-off-by: Andrea Arcangeli [peterx: modified subjects / commit message] Signed-off-by: Peter Xu --- fs/userfaultfd.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 56eaae9dac1a..f7fda7d0c994 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1061,7 +1061,11 @@ static ssize_t userfaultfd_ctx_read(struct userfaultfd_ctx *ctx, int no_wait, /* always take the fd_wqh lock before the fault_pending_wqh lock */ spin_lock_irq(&ctx->fd_wqh.lock); - __add_wait_queue(&ctx->fd_wqh, &wait); + /* + * Only wake up one exclusive reader each time there's an event. + * Paired with wake_up_poll() when e.g. a new page fault msg generated. + */ + __add_wait_queue_exclusive(&ctx->fd_wqh, &wait); for (;;) { set_current_state(TASK_INTERRUPTIBLE); spin_lock(&ctx->fault_pending_wqh.lock);