From patchwork Wed Oct 19 08:30:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5359 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp298745wrs; Wed, 19 Oct 2022 05:30:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Ujn21fcb00srqojGftzAdiFu1/tPaH2NjHV8TXqZW2G2FTEo1J8qjsMKvLtD/gLjIsvuK X-Received: by 2002:a17:907:320c:b0:741:1e55:7a69 with SMTP id xg12-20020a170907320c00b007411e557a69mr6804766ejb.740.1666182657426; Wed, 19 Oct 2022 05:30:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666182657; cv=none; d=google.com; s=arc-20160816; b=xyIJzs/BfdsbG5apvK2ppancBNp9gs71bJch7BkVOAPbBgPLLyJonMci0UD5dNsrxK QZECTvtLJbSPpKpFHhwzFWy0eHvp8knnO8y5wjDBAyJiXpJ3fOvVLmPjySZ08EFeugTB Ei7nLHF+pHIwGgzz044cNf0d6xhgGDVA4bTWl/qlepFYZMDhazf9FazaopGZ/wjNdhWx OMbzdFFHot9kgYNnKwtyhZ0k9SY3b+cVyxDwIeGHtQ4LBDZzvhygMbQXTthMpCX2Lsn5 zvysSB1oJgSvCerKauA99gj6zOzpPmLwlLQXLW6M3BktC3onfapA1h1rlUnaRwKrFPBo nmkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fNX9SJsAlJENB0ThRhQpv2LbpU6v/Kgr1vQWd8eYhTs=; b=I4JAVISeC6kLwrM7RhNqz3wmwJN7Cm+8anmyLBZvz1AKyQgEOKqq31QZ2JRwFuuh43 HRVRfQ/hB/8w6WQiCflgxbOX7rUqQcZNIwG1D8GOeUFYoVTlyq9BVSKSX4/c3Nzi5Lo+ VQZdQm/ajI1wZHa7gne9WZFkrRO+R1sSqUFxxVv6FJLxwyHEqZETQqczrOkSWm1m4LIr iRif/URcZnVp9Z7YGXuyRqHqigBIwfd4085Zr6xzLIvhPd7NRrxirNKMU/hNr5CinxhW n9VTxyTrxQ5mbEEH0TfpHXyAsja1xhGAsS4kjcUV/ytsX8jE0cpplV3iOl6dVwPRp4r3 7i2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=abVJlzuz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z9-20020a05640235c900b00459060fce1bsi15347504edc.574.2022.10.19.05.30.24; Wed, 19 Oct 2022 05:30:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=abVJlzuz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233026AbiJSMTj (ORCPT + 99 others); Wed, 19 Oct 2022 08:19:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231894AbiJSMSo (ORCPT ); Wed, 19 Oct 2022 08:18:44 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1289136403; Wed, 19 Oct 2022 04:54:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 00F99B822BB; Wed, 19 Oct 2022 09:02:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54F77C433D6; Wed, 19 Oct 2022 09:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666170148; bh=6A4G5ttVug3XAxhzsOqGeSmvV1mp2WWrcAMaD8Bz/k8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=abVJlzuzJx3hexX5PeEqOSdur8KO1npIUlD2kkvBdT4nNB2RMvXxK3n5sM1EU9tzV 3aXQEuVsHqI+QNyRgk7xhsmgzSf8FpTT70JI5R8Qwpz30JCwJGrUkljC4RiRkIVnIO RnP9OG1SsVGQbhS8T92ldDxxIHdJ1VgCtcWH+kNk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dylan Yudaken , Jens Axboe , Sasha Levin Subject: [PATCH 6.0 545/862] eventfd: guard wake_up in eventfd fs calls as well Date: Wed, 19 Oct 2022 10:30:32 +0200 Message-Id: <20221019083314.035666345@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747119146265298493?= X-GMAIL-MSGID: =?utf-8?q?1747119146265298493?= From: Dylan Yudaken [ Upstream commit 9f0deaa12d832f488500a5afe9b912e9b3cfc432 ] Guard wakeups that the user can trigger, and that may end up triggering a call back into eventfd_signal. This is in addition to the current approach that only guards in eventfd_signal. Rename in_eventfd_signal -> in_eventfd at the same time to reflect this. Without this there would be a deadlock in the following code using libaio: int main() { struct io_context *ctx = NULL; struct iocb iocb; struct iocb *iocbs[] = { &iocb }; int evfd; uint64_t val = 1; evfd = eventfd(0, EFD_CLOEXEC); assert(!io_setup(2, &ctx)); io_prep_poll(&iocb, evfd, POLLIN); io_set_eventfd(&iocb, evfd); assert(1 == io_submit(ctx, 1, iocbs)); write(evfd, &val, 8); } Signed-off-by: Dylan Yudaken Reviewed-by: Jens Axboe Link: https://lore.kernel.org/r/20220816135959.1490641-1-dylany@fb.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/eventfd.c | 10 +++++++--- include/linux/eventfd.h | 2 +- include/linux/sched.h | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -69,17 +69,17 @@ __u64 eventfd_signal(struct eventfd_ctx * it returns false, the eventfd_signal() call should be deferred to a * safe context. */ - if (WARN_ON_ONCE(current->in_eventfd_signal)) + if (WARN_ON_ONCE(current->in_eventfd)) return 0; spin_lock_irqsave(&ctx->wqh.lock, flags); - current->in_eventfd_signal = 1; + current->in_eventfd = 1; if (ULLONG_MAX - ctx->count < n) n = ULLONG_MAX - ctx->count; ctx->count += n; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLIN); - current->in_eventfd_signal = 0; + current->in_eventfd = 0; spin_unlock_irqrestore(&ctx->wqh.lock, flags); return n; @@ -253,8 +253,10 @@ static ssize_t eventfd_read(struct kiocb __set_current_state(TASK_RUNNING); } eventfd_ctx_do_read(ctx, &ucnt); + current->in_eventfd = 1; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLOUT); + current->in_eventfd = 0; spin_unlock_irq(&ctx->wqh.lock); if (unlikely(copy_to_iter(&ucnt, sizeof(ucnt), to) != sizeof(ucnt))) return -EFAULT; @@ -301,8 +303,10 @@ static ssize_t eventfd_write(struct file } if (likely(res > 0)) { ctx->count += ucnt; + current->in_eventfd = 1; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLIN); + current->in_eventfd = 0; } spin_unlock_irq(&ctx->wqh.lock); --- a/include/linux/eventfd.h +++ b/include/linux/eventfd.h @@ -46,7 +46,7 @@ void eventfd_ctx_do_read(struct eventfd_ static inline bool eventfd_signal_allowed(void) { - return !current->in_eventfd_signal; + return !current->in_eventfd; } #else /* CONFIG_EVENTFD */ --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -936,7 +936,7 @@ struct task_struct { #endif #ifdef CONFIG_EVENTFD /* Recursion prevention for eventfd_signal() */ - unsigned in_eventfd_signal:1; + unsigned in_eventfd:1; #endif #ifdef CONFIG_IOMMU_SVA unsigned pasid_activated:1;