From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id 606A73858D37 for ; Thu, 27 Apr 2023 22:03:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 606A73858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-506b2a08877so15731892a12.2 for ; Thu, 27 Apr 2023 15:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682633019; x=1685225019; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=JvYvmenQMOruCZJuXPNeg3xyiBV+6LcVUeKjZNyIf/Q=; b=jsdBByEdW2btp3kcszS82yPPQJIHPpbwyVGWOThwYLa4EiwFOouj9YhtGcYIPfnrvc 00Ioq5ba6HRU+9Moz/YVktQDllSTlPNuJTB0i5IJdWEChXUkxxJtxzes3WO1GaSXUg3T y2lKVo2fhhCtVZewO4iblQ8mbD8ruCczPXr2T6xVgS5dJc8h9UltiGNPxHnQZ8/K/MoI /hc7L653qxVwfB4QskQ/4wfppHY/XAKXzatzqK0tUEpkrNfc61arsy9EgIu9vbQTOYdv X6xNVmByLbptT6Pl11CW9btBKd/IKufA5Oz3yKrzLXqm4/QqrNuRyvdLUF8u5AHfr9k2 qq9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682633019; x=1685225019; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JvYvmenQMOruCZJuXPNeg3xyiBV+6LcVUeKjZNyIf/Q=; b=IjuPhZlHSclK6WdOtHdp2GYhRaFx+EGUM/KwIZCYS1np2xlzH+NQC8GexBSnebyRtf P93/q+Eyh/i8NGLV71cUNF9FJwC2MBUaCO+dmDia9ZjD5ApsMatfRAJkfO+86BPyWaTO AeRkMgpcXYzAL7Uod5x4bWp2UMchyQy8UxVRjLmdqAQlVtM0ic90SNbs4xPtp86G5lQS Z7H3zQ3pTKGY1D/Xh8PA7kQooq6i0FO+X/a2RcV2kGOveOErvqH3iFzruLRHsv082h93 9IBN2jt3ucipy4WpSLuSxjuUeplQiBsN0JGIByi1QiSE23Dh8xGOgG3XzCgATcEqQpzY wjXQ== X-Gm-Message-State: AC+VfDz/2udU8Bh7FSMd7J2KnejcbI/wyK01fPZ966aqwwswM6vPn/l1 rPX9hZhLtc394fETk4IHV8dCB/zje0cr3ySMKHcvA0Cq X-Google-Smtp-Source: ACHHUZ6Jir0v4jCDdu7JVuNWJVzO8DEAxJy9lK+NR1dqJEZFedscns1DKxS4JkRqQKNVaJBbiqbeSiYQ1nJT+8zcRz8= X-Received: by 2002:a17:907:5c9:b0:94a:7da2:d339 with SMTP id wg9-20020a17090705c900b0094a7da2d339mr3303937ejb.26.1682633018641; Thu, 27 Apr 2023 15:03:38 -0700 (PDT) MIME-Version: 1.0 References: <20230427200615.1496059-1-hjl.tools@gmail.com> In-Reply-To: <20230427200615.1496059-1-hjl.tools@gmail.com> From: Noah Goldstein Date: Thu, 27 Apr 2023 17:03:27 -0500 Message-ID: Subject: Re: [PATCH] __check_pf: Add a cancellation cleanup handler [BZ #20975] To: "H.J. Lu" Cc: libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,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 server2.sourceware.org List-Id: On Thu, Apr 27, 2023 at 3:06=E2=80=AFPM H.J. Lu via Libc-alpha wrote: > > There are reports for hang in __check_pf: > > https://github.com/JoeDog/siege/issues/4 > > It is reproducible only under specific configurations: > > 1. Large number of cores (>=3D 64) and large number of threads (> 3X of > the number of cores) with long lived socket connection. > 2. Low power (frequency) mode. > 3. Power management is enabled. > > While holding lock, __check_pf calls make_request which calls __sendto > and __recvmsg. Since __sendto and __recvmsg are cancellation points, > lock held by __check_pf won't be released and can cause deadlock when > thread cancellation happens in __sendto or __recvmsg. Add a cancellation > cleanup handler for __check_pf to unlock the lock when cancelled by > another thread. This fixes BZ #20975 and the siege hang issue. > --- > sysdeps/unix/sysv/linux/Makefile | 2 ++ > sysdeps/unix/sysv/linux/check_pf.c | 15 +++++++++++++++ > 2 files changed, 17 insertions(+) > > diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/M= akefile > index aec7a94785..0160be8790 100644 > --- a/sysdeps/unix/sysv/linux/Makefile > +++ b/sysdeps/unix/sysv/linux/Makefile > @@ -529,6 +529,8 @@ sysdep_headers +=3D \ > sysdep_routines +=3D \ > netlink_assert_response \ > # sysdep_routines > + > +CFLAGS-check_pf.c +=3D -fexceptions > endif > > # Don't compile the ctype glue code, since there is no old non-GNU C lib= rary. > diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux= /check_pf.c > index b157c5126c..2b0b8b6368 100644 > --- a/sysdeps/unix/sysv/linux/check_pf.c > +++ b/sysdeps/unix/sysv/linux/check_pf.c > @@ -292,6 +292,14 @@ make_request (int fd, pid_t pid) > return NULL; > } > > +#ifdef __EXCEPTIONS > +static void > +cancel_handler (void *arg __attribute__((unused))) > +{ > + /* Release the lock. */ > + __libc_lock_unlock (lock); > +} > +#endif > > void > attribute_hidden > @@ -304,6 +312,10 @@ __check_pf (bool *seen_ipv4, bool *seen_ipv6, > struct cached_data *olddata =3D NULL; > struct cached_data *data =3D NULL; > > +#ifdef __EXCEPTIONS > + /* Make sure that lock is released when the thread is cancelled. */ > + __libc_cleanup_push (cancel_handler, NULL); > +#endif Should we wait until we have successfully taken the lock to push this? > __libc_lock_lock (lock); > > if (cache_valid_p ()) > @@ -338,6 +350,9 @@ __check_pf (bool *seen_ipv4, bool *seen_ipv6, > } > } > > +#ifdef __EXCEPTIONS > + __libc_cleanup_pop (0); > +#endif > __libc_lock_unlock (lock); > > if (data !=3D NULL) > -- > 2.40.0 >