From nobody Fri Feb 24 20:41:48 2023 X-Original-To: freebsd-fs@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 4PNhdl62qbz3tvST for ; Fri, 24 Feb 2023 20:42:03 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4PNhdl2SX5z3r9k for ; Fri, 24 Feb 2023 20:42:03 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.17.1/8.17.1) with ESMTPS id 31OKfmmW099820 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Fri, 24 Feb 2023 22:41:51 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 31OKfmmW099820 Received: (from kostik@localhost) by tom.home (8.17.1/8.17.1/Submit) id 31OKfmxE099819; Fri, 24 Feb 2023 22:41:48 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Fri, 24 Feb 2023 22:41:48 +0200 From: Konstantin Belousov To: Alexander Lochmann Cc: freebsd-fs@freebsd.org Subject: Re: Understanding locking for buf Message-ID: References: <45d84dae-0ca9-95ed-f6fd-8243797453ff@tu-dortmund.de> List-Id: Filesystems List-Archive: https://lists.freebsd.org/archives/freebsd-fs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-fs@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <45d84dae-0ca9-95ed-f6fd-8243797453ff@tu-dortmund.de> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.0 X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-14) on tom.home X-Rspamd-Queue-Id: 4PNhdl2SX5z3r9k X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N On Fri, Feb 24, 2023 at 03:00:57PM +0100, Alexander Lochmann wrote: > Hi Konstantin! > > I'm sorry. I'm still struggling to understand locking of struct buf. > As far as I know, a struct buf is (mostly) protected by b_lock. > You already explained the concept of LK_KERNPROC. > > I, however, have an instance of struct buf. That particular one is mostly > accessed from just one context (aka thread) -- assuming synchronous IO. > The other, unprotected accesses come from just one other context. Those > accesses originate from 'g_vfs_done' [1]. > Why don't you release b_lock when buf goes further down the io stack, and > acquire it again in g_vfs_done? > This way the context of g_vfs_done would own the b_lock. Unlocking the buffer means that other thread might take a lock. This gives the new owner a permission to modify the buffer state and data, causing the io operation in flight to corrupt data. Or the buffer might be reclaimed with brelse(), corrupting the kernel state. > > Viewing it from a different angle: Are accesses in g_vfs_done safe because > the buf instance is already locked from a global perspective? > Hence, other code paths would block on BUF_LOCK(). geom completion code is the only code that allowed to touch the buffer after the ownership was relinguished. I believe I already tell that to you: consider the buffer lock after LK_KERNPROC as a semaphore and not lock.