From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7D6882899 for ; Thu, 2 Jan 2025 16:33:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735835636; cv=none; b=CLEk7XX0Zf68k3zhvxCfCs5ABwxg/buL4eelQyFIClkmAnqRLwq1ADqWCDHSAF9vQfcOaaxtTkznaOIlBmqEqsjvkYS/CuXZSK79PaB2jG9pG9y337U3LT78p+eMI9GQRhfGa7VefEn7cgJUmGtbBaAWTymi2ZSmyCcmWlCHjk0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735835636; c=relaxed/simple; bh=GJyV6RC1G9scYkTqmswNWIYnRS9BXqsVCCgr0G/MrlM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PWpRJxQeeyw+ftPB4tPTk6ALruf9+wno/zc+MKFX2KpJlu/MCWJRpOjnTmY1nTjkt5jJSyMgFa66nD0iwCOHeDuJcOkklXrwFLcYMgmVgvkbEmRtQ9jl+NHgUyiDRFniPA3lwOhQt1RWYRqh9XwXmaCz4LMbi5ch2Ruk0nBx/YQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=O7f+x0EB; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O7f+x0EB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735835633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=pChq+YBV4R1ZsMZryOabHA5VhHF+4Dc111SPJ8z0saY=; b=O7f+x0EBtB3YFrDelRT2PlzR3eOCJep7bxEKPoL5sQ+n4k+5kM0OiyeVCUmlPUZ0+OcIzR wSFzL24bC6ZZarq9bMJA/epccuP8VYfT5qt9Wsmi3f6CV1Upr//B8YaTJN8/H3Y7+BOnIt KZbbVBC33dvxot8i8FYdC/MMP4IjF18= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-497-a3YmlZkTPuema5eqLEGOfA-1; Thu, 02 Jan 2025 11:33:50 -0500 X-MC-Unique: a3YmlZkTPuema5eqLEGOfA-1 X-Mimecast-MFC-AGG-ID: a3YmlZkTPuema5eqLEGOfA Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F100A1955F08; Thu, 2 Jan 2025 16:33:48 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.145]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id D03473000197; Thu, 2 Jan 2025 16:33:45 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Thu, 2 Jan 2025 17:33:24 +0100 (CET) Date: Thu, 2 Jan 2025 17:33:20 +0100 From: Oleg Nesterov To: Manfred Spraul , Linus Torvalds , Christian Brauner , David Howells Cc: WangYuli , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: wakeup_pipe_readers/writers() && pipe_poll() Message-ID: <20250102163320.GA17691@redhat.com> References: <20241229135737.GA3293@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241229135737.GA3293@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 I was going to send a one-liner patch which adds mb() into pipe_poll() but then I decided to make even more spam and ask some questions first. static void wakeup_pipe_readers(struct pipe_inode_info *pipe) { smp_mb(); if (waitqueue_active(&pipe->rd_wait)) wake_up_interruptible(&pipe->rd_wait); kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); } I think that wq_has_sleeper() + wake_up_interruptible_poll(POLLIN) make more sense but this is minor. Either way the waitqueue_active() check is only correct if the waiter has a barrier between __add_wait_queue() and "check the condition". wait_event() is fine, but pipe_poll() does: // poll_wait() __pollwait() -> add_wait_queue(pipe->rd_wait) -> list_add() READ_ONCE(pipe->head); READ_ONCE(pipe->tail); In theory these LOAD's can leak into the critical section in add_wait_queue() and they can happen before list_add(entry, rd_wait.head). So I think we need the trivial --- a/fs/pipe.c +++ b/fs/pipe.c @@ -680,6 +680,7 @@ pipe_poll(struct file *filp, poll_table *wait) * if something changes and you got it wrong, the poll * table entry will wake you up and fix it. */ + smp_mb(); head = READ_ONCE(pipe->head); tail = READ_ONCE(pipe->tail); and after that pipe_read/pipe_write can use the wq_has_sleeper() check too (this is what the patch from WangYuli did). ------------------------------------------------------------------------------- But perhaps this mb() should go into __pollwait() ? We can have more waitqueue_active() users which do not take .poll() into account... The are more init_poll_funcptr()'s, but at least epoll looks fine, epi_fget() in ep_item_poll() provides a full barrier before vfs_poll(). ------------------------------------------------------------------------------- Or really add mb() into __add_wait_queue/__add_wait_queue_entry_tail as Manfred suggests? Somehow I am not sure about this change. Oleg.