Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3669843rwb; Tue, 16 Aug 2022 07:03:38 -0700 (PDT) X-Google-Smtp-Source: AA6agR60QssebzgRu32qqIiJsS/NlUsZu3QWnhlpR/M8zxfp4WYBdJ/IrtGsBPjaKT7bFvSA2oTf X-Received: by 2002:a05:6402:1bcf:b0:443:1c7:cd07 with SMTP id ch15-20020a0564021bcf00b0044301c7cd07mr19404209edb.365.1660658618234; Tue, 16 Aug 2022 07:03:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660658618; cv=none; d=google.com; s=arc-20160816; b=ZNTz/i5f5QMr32K/QEO5nY3uRAc12PX3AkH3jH2yN/4GqrxP1m8yJ88UGTtw/cXr+b HdLYPPwu2fFJOrvfZzI2kosC5uXzWKd5XVIZXhUz8C4Ig+kpv3tZi8mBJHm+qRWcX2wp XKslXBtKCa4PmIql1khVWmloVOY7SjiQufJ+zzuOj/S57NyXW7FYEgPi4Yl209sW7Xlx Iv0WqteJZ39kV9vYEI/vY5dYftBTpiNjpZSbIqNXZULiF1HTslvFnvIKa5g2B3v85xhQ 175E1ldeDctgN5/YmPLgJn8INd93L2CeVcL4ZaKsa0DJ35csC8zh5vzmITZxQcEashXY cSUQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=VnKI2p9F7lsB7NxKiuJl2dToVpYX6lGSZuife1T99VQ=; b=TooqbxNUyc5CWcvqpws3ZtiZbytHZmxMxGU2I7OCmsYz8nyBRp36zmnW6tUQKzWzFc TGhpH+jZUa6srZlv0/xQqt1gpimBjRMa/uBaVMmO5Z4xoHiMeOFGkYDvc1vZh3C9MEZx Ud8YIEqiIFg1SU1+zVtVtc/8NvX3dG0k/h/SLTbDaxJf7tD2pdRbrivePY+W/OmkTZFT bOUOmI3ZOP4e2ARgCc2caV3UXwZB1vaWpJv/tcCN6f8gsB0qPr+hyiAAmUZDPK9U/+Rk u9kZGcCMred+hnwwuwzcv74OiRHV00Jz+jhPr3ZITzFi4mmwwnnwFkLR2b1uJtLsN5po 7ZOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=HlILdw51; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id md12-20020a170906ae8c00b00730a3c9aeeasi8236902ejb.549.2022.08.16.07.03.10; Tue, 16 Aug 2022 07:03:38 -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=@fb.com header.s=facebook header.b=HlILdw51; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235314AbiHPOAm (ORCPT + 99 others); Tue, 16 Aug 2022 10:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235585AbiHPOAa (ORCPT ); Tue, 16 Aug 2022 10:00:30 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E7BC4DB35 for ; Tue, 16 Aug 2022 07:00:29 -0700 (PDT) Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27GBWBfW021035 for ; Tue, 16 Aug 2022 07:00:29 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=facebook; bh=VnKI2p9F7lsB7NxKiuJl2dToVpYX6lGSZuife1T99VQ=; b=HlILdw51Pn+QHWMYcgawEmOimsCw62pJKk5G0gCyg+UOiHd7v/lhq+c+/tBpwlLfzTzv y2K/99DB+ZRJJKAFl4VpjPgw0PAabI+1cW8/nFBdKZh6Sfz7tIp+QLnGHI9zPQzMbw4p LUJc7ElkmLznlsBQTc7Fgh/jtP1gdP0Ceac= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3j0aek8tdc-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 16 Aug 2022 07:00:29 -0700 Received: from twshared7556.02.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 16 Aug 2022 07:00:27 -0700 Received: by devbig038.lla2.facebook.com (Postfix, from userid 572232) id 786BA4A9B6F3; Tue, 16 Aug 2022 07:00:17 -0700 (PDT) From: Dylan Yudaken To: , CC: , , , Jens Axboe , , Dylan Yudaken Subject: [PATCH] eventfd: guard wake_up in eventfd fs calls as well Date: Tue, 16 Aug 2022 06:59:59 -0700 Message-ID: <20220816135959.1490641-1-dylany@fb.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: uK_9Q0eaiKIVeORzYeS0aWTUqPBNxBDI X-Proofpoint-ORIG-GUID: uK_9Q0eaiKIVeORzYeS0aWTUqPBNxBDI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-16_08,2022-08-16_02,2022-06-22_01 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 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 approac= h 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 =3D NULL; struct iocb iocb; struct iocb *iocbs[] =3D { &iocb }; int evfd; uint64_t val =3D 1; evfd =3D eventfd(0, EFD_CLOEXEC); assert(!io_setup(2, &ctx)); io_prep_poll(&iocb, evfd, POLLIN); io_set_eventfd(&iocb, evfd); assert(1 =3D=3D io_submit(ctx, 1, iocbs)); write(evfd, &val, 8); } Signed-off-by: Dylan Yudaken --- fs/eventfd.c | 10 +++++++--- include/linux/eventfd.h | 2 +- include/linux/sched.h | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/fs/eventfd.c b/fs/eventfd.c index 3627dd7d25db..c0ffee99ad23 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -69,17 +69,17 @@ __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n= ) * 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; =20 spin_lock_irqsave(&ctx->wqh.lock, flags); - current->in_eventfd_signal =3D 1; + current->in_eventfd =3D 1; if (ULLONG_MAX - ctx->count < n) n =3D ULLONG_MAX - ctx->count; ctx->count +=3D n; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLIN); - current->in_eventfd_signal =3D 0; + current->in_eventfd =3D 0; spin_unlock_irqrestore(&ctx->wqh.lock, flags); =20 return n; @@ -253,8 +253,10 @@ static ssize_t eventfd_read(struct kiocb *iocb, stru= ct iov_iter *to) __set_current_state(TASK_RUNNING); } eventfd_ctx_do_read(ctx, &ucnt); + current->in_eventfd =3D 1; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLOUT); + current->in_eventfd =3D 0; spin_unlock_irq(&ctx->wqh.lock); if (unlikely(copy_to_iter(&ucnt, sizeof(ucnt), to) !=3D sizeof(ucnt))) return -EFAULT; @@ -301,8 +303,10 @@ static ssize_t eventfd_write(struct file *file, cons= t char __user *buf, size_t c } if (likely(res > 0)) { ctx->count +=3D ucnt; + current->in_eventfd =3D 1; if (waitqueue_active(&ctx->wqh)) wake_up_locked_poll(&ctx->wqh, EPOLLIN); + current->in_eventfd =3D 0; } spin_unlock_irq(&ctx->wqh.lock); =20 diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h index 305d5f19093b..30eb30d6909b 100644 --- a/include/linux/eventfd.h +++ b/include/linux/eventfd.h @@ -46,7 +46,7 @@ void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64= *cnt); =20 static inline bool eventfd_signal_allowed(void) { - return !current->in_eventfd_signal; + return !current->in_eventfd; } =20 #else /* CONFIG_EVENTFD */ diff --git a/include/linux/sched.h b/include/linux/sched.h index e7b2f8a5c711..8d82d6d32670 100644 --- 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; --=20 2.30.2