Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uB3Bo-005p8m-Eu for pgsql-hackers@arkaria.postgresql.org; Sat, 03 May 2025 03:05:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uB3Bl-00D2jQ-MW for pgsql-hackers@arkaria.postgresql.org; Sat, 03 May 2025 03:05:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uB3Bl-00D2jI-Cy for pgsql-hackers@lists.postgresql.org; Sat, 03 May 2025 03:05:18 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uB3Bk-000nsc-1Q for pgsql-hackers@postgresql.org; Sat, 03 May 2025 03:05:17 +0000 Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-2ff799d99dcso2802292a91.1 for ; Fri, 02 May 2025 20:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1746241514; x=1746846314; darn=postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=DiISZhIL/Lo3i4aSMqjuGcNQZd7zm9yNiUaP4F80+Ts=; b=ULijJV8b9mBmSZqQnyXDK2qMRYX8mh/Nnc0dffdDNycK8a70wmjdqWKzb8InsTLvKH JlHEEvDSUy/e/njUFaYplHelYlh2Pr7lvxt0fW3X/q4XSnCqYZOPOSpYxgElT0Msoi9H 5bJTuQuHh8RfArefb5BCV4GuS1T66Hja5Rltk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746241514; x=1746846314; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DiISZhIL/Lo3i4aSMqjuGcNQZd7zm9yNiUaP4F80+Ts=; b=vKkr/75G3C5ZLoSmoolAdrJMd1wx1Gfh42P5lrC6ER3XMmF76ocEqNThiXBe/w0n5u DjJI1P11Hv79Z17gYC/QMCPUR/cVFFFJf+Tf91NgmRXJUReyHmPFauJdCiUT/ssLFEBt l9sVC4XSkyjBT8Cq7I03O6Azl5Decsh7hUUVerL3iibJTpmC6p7/NATS2V3XjrNG9H5W K8jrY0uJyndwAK/kjUkVqu3Lfd6TRKf8Hi1mA0srq5IrO+uQ+b5lo/8GYSKAOLZbEEzm TY7YpFCsVYAUa5U3fBxYG/biwdZLk5nFwVAUzF3WDVIyQ7uVAxBLiZvPh9vTHpmGA3V8 Cv5A== X-Forwarded-Encrypted: i=1; AJvYcCX+t5x5by9sGO3LVmdBbiD+MQt73U5oBSinLgTj0oJCZcgKYa6Bs7KpgiHvdu0pQfdZqfki5xywYaEMU1e+@postgresql.org X-Gm-Message-State: AOJu0YweR/MJFBItvTQh950X1OduGnr0rHkK8FVhiZk30BjYELOnK5p1 QtU4Sg11wDgMYGl+IhLqlL5z8JDtsHwSnyamXRXY7OG0YIs9+Ad4YpPPB94uwQ== X-Gm-Gg: ASbGnctuA3g9qwT10GOwEVEFiPNklZVkG/iO0QqSVuc4CItlQPCabRZlwvlSvemJgwW ipVU//HVOt974CbN9gA7RJaeXabzOj3aABPpcCMBJp60kJ+mcn41XdHliowMt0f/jFrY+sF/e2n NnkkjdwV6sdMNlcNKriBb+4H0pkJkDlGE4YwzCw4RmOhnutoedWYG2efL/bn1Jk8kUgf5ZNukgK 8NbKZ25vLoT+aV6q4bnvrAcr0DUaZDq539a2hhgb+PjgGGZMw4hugeILyBYMM8fQXSMBBDBWxtL wlc9fV7T7Aj93pJeE/miO+3JK6JNYFVgVbnuGA== X-Google-Smtp-Source: AGHT+IFZmw3LeBqOX2AX/GpTcdkgbecZyOE4M9SkeKlw09oiRyfT3TEP0AVAQvOoP/L2slp9BV9WbA== X-Received: by 2002:a17:90b:5790:b0:301:6343:1626 with SMTP id 98e67ed59e1d1-30a4e5768eemr7567393a91.1.1746241514091; Fri, 02 May 2025 20:05:14 -0700 (PDT) Received: from google.com ([2601:647:5600:80d0::31cd]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30a3478093csm6573217a91.29.2025.05.02.20.05.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 May 2025 20:05:13 -0700 (PDT) Date: Fri, 2 May 2025 20:05:11 -0700 From: Noah Misch To: Andres Freund Cc: Alexander Lakhin , pgsql-hackers@postgresql.org, Thomas Munro , Heikki Linnakangas , Robert Haas , Jakub Wartak , Jelte Fennema-Nio , Antonin Houska Subject: Re: AIO v2.5 Message-ID: <20250503030511.9b.nmisch@google.com> References: <20250402001324.e4.nmisch@google.com> <3fhulsvzks4khqahl6tngcnuda7pyrrl7223tmjh2x2spralm3@iisldh6pcowy> <8160688e-db8e-4d7d-a7e7-ff366adf9717@gmail.com> <4nervqmqplfr23jrjvkp5tsumi6qgouhgjqlubf7ujrudw2epb@6mszddainl4u> <6a6756a0-bad5-4ee5-a591-c763af289940@gmail.com> <96abefe8-fa72-41f5-8840-0517125c24e3@gmail.com> <4qk3ehe6w7x7hfrldei2hefjcb7v7nfmj2owl2ir64craqcapz@kbrao22ljxeb> <062daca9-dfad-4750-9da8-b13388301ad9@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Apr 30, 2025 at 04:00:35PM -0400, Andres Freund wrote: > pgaio_io_wait_for_free() does what it says on the tin. For that, after a bunch > of other things, finds the oldest in-flight IO and waits for it. > > PgAioHandle *ioh = dclist_head_element(PgAioHandle, node, > &pgaio_my_backend->in_flight_ios); > > switch (ioh->state) > { > ... > case PGAIO_HS_COMPLETED_IO: > case PGAIO_HS_SUBMITTED: > pgaio_debug_io(DEBUG2, ioh, > "waiting for free io with %d in flight", > dclist_count(&pgaio_my_backend->in_flight_ios)); > ... > pgaio_io_wait(ioh, ioh->generation); > break; > > > The problem is that, if the log level is low enough, ereport() (which is > called by pgaio_debug_io()), processes interrupts. The interrupt processing > may end up execute ProcessBarrierSmgrRelease(), which in turn needs to wait > for all in-flight IOs before the IOs are closed. > > Which then leads to the > elog(PANIC, "waiting for own IO in wrong state: %d", > state); > > error. Printing state 0 (PGAIO_HS_IDLE), right? I think the chief problem is that pgaio_io_wait_for_free() is fetching ioh->state, then possibly processing interrupts in pgaio_debug_io(), then finally fetching ioh->generation. If it fetched ioh->generation to a local variable before pgaio_debug_io, I think that would resolve this one. Then the pgaio_io_was_recycled() would prevent the PANIC: if (pgaio_io_was_recycled(ioh, ref_generation, &state)) return; if (am_owner) { if (state != PGAIO_HS_SUBMITTED && state != PGAIO_HS_COMPLETED_IO && state != PGAIO_HS_COMPLETED_SHARED && state != PGAIO_HS_COMPLETED_LOCAL) { elog(PANIC, "waiting for own IO in wrong state: %d", state); } } Is that right? If that's the solution, pgaio_closing_fd() and pgaio_shutdown() would need similar care around fetching the generation before the pgaio_debug_io. Maybe there's an opportunity for a common inline function. Or at least a comment at the "generation" field on how to safely time a fetch thereof and any barrier required. > A similar set of steps can lead to the "no free IOs despite no in-flight IOs" > ERROR that Alexander also observed - if pgaio_submit_staged() triggers a debug > ereport that executes ProcessBarrierSmgrRelease() in an interrupt, we might > wait for all in-flight IOs during IO submission, triggering the error. That makes sense. > I'm not yet sure how to best fix it - locally I have done so by pgaio_debug() > do a HOLD_INTERRUPTS()/RESUME_INTERRUPTS() around the call to ereport. But > that doesn't really seem great - otoh requiring various pieces of code to know > that anything emitting debug messages needs to hold interrupts etc makes for > rare and hard to understand bugs. > > We could just make the relevant functions hold interrupts, and that might be > the best path forward, but we don't really need to hold all interrupts > (e.g. termination would be fine), so it's a bit coarse grained. It would need > to happen in a few places, which isn't great either. > > Other suggestions? For the "no free IOs despite no in-flight IOs" case, I'd replace the ereport(ERROR) with "return;" since we now know interrupt processing reclaimed an IO. Then decide what protection if any, we need against bugs causing an infinite loop in caller pgaio_io_acquire(). What's the case motivating the unbounded loop in pgaio_io_acquire(), as opposed to capping at two pgaio_io_acquire_nb() calls? If the theory is that pgaio_io_acquire() could be reentrant, what scenario would reach that reentrancy? > Thanks again for finding and reporting this Alexander! +1!