From nobody Fri Jun 23 20:11:34 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 4QnpKw30WPz4gmj5 for ; Fri, 23 Jun 2023 20:11:48 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-ua1-f47.google.com (mail-ua1-f47.google.com [209.85.222.47]) (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 4QnpKv5s6dz3hwv for ; Fri, 23 Jun 2023 20:11:47 +0000 (UTC) (envelope-from asomers@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-ua1-f47.google.com with SMTP id a1e0cc1a2514c-78cee27c08aso415682241.2 for ; Fri, 23 Jun 2023 13:11:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687551106; x=1690143106; 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=c5XSsh7658/ln6TELE1WbwFESaD587/kgAs/zv+bIhs=; b=NTr9xEIYY9DruLKeJ9IRwDNWwNHpvgtsH+hstjJRhW1pdb5pkMCmxefrDPeczNie76 1EEuRIIKPn1zgFhD7CRdhD0ijLqOapIJKe+miJfDg84pcONKd+Ca7bge2+YpiwvzdWQk D75qGwugXdHWgslMrN8M7EIxuzzK1Lhr6ztGnExWrceEUc6jh47dOT6N5kpwSHH4GXm8 VT4gIleHotrPTcdvknPjArdKF/I7qO4YP5nkQ7fRZC5tCk+F5IHg2lhIWw0diqLX6FKr UeAGTHqL5/tCTFgxca38GzfIxa/e0JMfh6js6X4zMiZPjfC06XYpsV33Z8W9NEQadkgj 0w5Q== X-Gm-Message-State: AC+VfDz1JGUMSHPQn/vPLXJukwzTxH3pAF+hnTKyR3rr66+yjmI6LT8P WU0Gohd7zQtlube4RZ95piGFMy6EKSoOb7EfP7cevS19y+Y= X-Google-Smtp-Source: ACHHUZ44qbct5vdVWnn52CDkKRRjbkUFG2P7Dt9qj9GUnLiKPm7k613jzE/gJsk4lQxe6tjq0kntl+aVKCDjTV1pz14= X-Received: by 2002:a67:e8d4:0:b0:440:a6fb:909f with SMTP id y20-20020a67e8d4000000b00440a6fb909fmr10010316vsn.32.1687551106260; Fri, 23 Jun 2023 13:11:46 -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:11:34 -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: 4QnpKv5s6dz3hwv 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: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 man > > 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 EAGAIN= ! > > > > 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 final > > open() comes immediately after the close(), with no intervening > > syscalls from that thread. It seems that close() doesn't release the > > 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) returns. This is a plain file on ZFS.