From nobody Fri Jun 23 20:53:20 2023 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QnqG56Hp8z4gH3t for ; Fri, 23 Jun 2023 20:53:33 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-vk1-f177.google.com (mail-vk1-f177.google.com [209.85.221.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QnqG53MhRz41kV for ; Fri, 23 Jun 2023 20:53:33 +0000 (UTC) (envelope-from asomers@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-vk1-f177.google.com with SMTP id 71dfb90a1353d-4718ddce780so487558e0c.1 for ; Fri, 23 Jun 2023 13:53:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687553612; x=1690145612; 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=2Z01/vlrmXYpwwtqMi5qhHYBcgdP8OILRwxIcZtwo1E=; b=bzHJFB9uxjFDOUd/2AOWb2yF9mVAaAAxUKVedLYeJQaDMFkjHOBhycxyormXmZRB2W at9p73Bn/OnoFOLzNkmoA1D5DFgiKcqlJLAIgsi50XxI1kbzklmlwA+9WlqV6YVUdnpw sLYM9g/kE0lM4f1oWeICtJ54RANXthhN4AwEoW5M5sHtwlyOMmLDmaF2wOGHDlQKfas4 fYucbx19RcNNHdnX4K/nAqlPumK2OIyCFa9ymlJRdvMtbcr+//o5h6eZV1nnDskx+Ftz 3lZoj5rRJcUKWxhGm6QQ+l7jydz8DYR4Me5xUkRUzGSMrQOOajQkGkS2csQkLER08mlK 3OZw== X-Gm-Message-State: AC+VfDwVq3IiRVQ/I8mOhUF8doToDmporwJIxVPV9cP5OVcFAZsdSZRE 7nrIB6Ic5g9j00qSBc/yDrzbF7MJL4iv7g6pXR4= X-Google-Smtp-Source: ACHHUZ4Waovmszv3o+aZf30pZJP8hqWfsOHR9yX7z7DKhEXB/1+RehhEKSZ2jz4+BTI036XqAQ0zpTV6rOMhPKxTtsQ= X-Received: by 2002:a1f:bfd3:0:b0:471:6b1d:378c with SMTP id p202-20020a1fbfd3000000b004716b1d378cmr12197298vkf.0.1687553612159; Fri, 23 Jun 2023 13:53:32 -0700 (PDT) List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 References: In-Reply-To: From: Alan Somers Date: Fri, 23 Jun 2023 13:53:20 -0700 Message-ID: Subject: Re: Should close() release locks atomically? To: Konstantin Belousov Cc: FreeBSD Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4QnqG53MhRz41kV X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N On Fri, Jun 23, 2023 at 1:48=E2=80=AFPM Konstantin Belousov wrote: > > On Fri, Jun 23, 2023 at 01:11:34PM -0700, Alan Somers wrote: > > On Fri, Jun 23, 2023 at 1:03=E2=80=AFPM Konstantin Belousov wrote: > > > > > > On Fri, Jun 23, 2023 at 12:00:36PM -0700, Alan Somers wrote: > > > > The close() syscall automatically releases locks. Should it do so > > > > atomically or is a delay permitted? I can't find anything in our m= an > > > > pages or the open group specification that says. > > > > > > > > The distinction matters when using O_NONBLOCK. For example: > > > > > > > > fd =3D open(..., O_DIRECT | O_EXLOCK | O_NONBLOCK); //succeeds > > > > // do some I/O > > > > close(fd); > > > > fd =3D open(..., O_DIRECT | O_EXLOCK | O_NONBLOCK); //fails with EA= GAIN! > > > > > > > > I see this error frequently on a heavily loaded system. It isn't a > > > > typical thread race though; ktrace shows that only one thread tries= to > > > > open the file in question. From the ktrace, I can see that the fin= al > > > > open() comes immediately after the close(), with no intervening > > > > syscalls from that thread. It seems that close() doesn't release t= he > > > > lock right away. I wouldn't notice if I weren't using O_NONBLOCK. > > > > > > > > Should this be considered a bug? If so I could try to come up with= a > > > > minimal test case. But it's somewhat academic, since I plan to > > > > refactor the code in a way that will eliminate the duplicate open()= . > > > What type of the object is behind fd? O_NONBLOCK affects open itself= . > > > We release flock after object close method, but before close(2) retur= ns. > > > > This is a plain file on ZFS. > > Can you write a self-contained example, and check the same issue e.g. on > tmpfs? I just reproduced it on tmpfs. A minimal test case will take some more tim= e...