From patchwork Mon Aug 26 06:30:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777124 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A309A83CA0; Mon, 26 Aug 2024 06:37:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654247; cv=none; b=RGNBOGQKbABM7r8m4uOKl0rCrd/OEKzwfkgbk3RTBMa1EQf5aMTpwTbiJwcXwiYee0koYj6wsMKfJuIqsWlnC3ATyxMApdGw2JJ+AYMgLKWYy9mE+JlIwVfOu5Cc2Zf4ba4wAOB7W6Hw5D0XibwC4KAXpBrUjKmk2QRDZewIKXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654247; c=relaxed/simple; bh=UNa/mbC3OpcW1lOX4b6toj/OKSZTmT1AmounDSZjksA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JtUBMkzL39z7aYBdlP87YEvvDT8tLmX+4liBNWOG14nqE3O493Z0ncUZkqouA+6NiFREK8bFXmWaT5ekETmetTRlwSCX4g+fmlX3KmveI+K3kcciPyf9gwUCYvfFoM5ncWcip/OEwEGumNGEUwGPW8NoWrWJLFKc7f7TyPkmWuA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Iq3DZ8T0; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=ZnxHgty0; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=Iq3DZ8T0; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=ZnxHgty0; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Iq3DZ8T0"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ZnxHgty0"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="Iq3DZ8T0"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ZnxHgty0" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B034C1F83B; Mon, 26 Aug 2024 06:37:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654242; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zZ7qqMqYr+coZy8gdEZ03r7ExQ3wOPQZ6PCEnVK5cfk=; b=Iq3DZ8T0D8QI2VPD2ma24FBKYAsOiLhbEFr1q40IGm6ZxrSnY24NiW/oXHiEtSqJESe8ZA RDE4bT0UKzciqrOW9NUT3uqYw7606VFg6CgXHYvOYdw+AKx8YJVb9V/9LnFUbrk8BGYjFu bELJFgLV1JtXmbgcQJFA17g8qBxMNYc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654242; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zZ7qqMqYr+coZy8gdEZ03r7ExQ3wOPQZ6PCEnVK5cfk=; b=ZnxHgty0wfoXE/vEHhAXtciT6D5BdI45QeZqrzrlqfPsBS5HwP6lhiQ20o/pGobEo4IRXW /TqjJHqkG6enhPCA== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=Iq3DZ8T0; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=ZnxHgty0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654242; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zZ7qqMqYr+coZy8gdEZ03r7ExQ3wOPQZ6PCEnVK5cfk=; b=Iq3DZ8T0D8QI2VPD2ma24FBKYAsOiLhbEFr1q40IGm6ZxrSnY24NiW/oXHiEtSqJESe8ZA RDE4bT0UKzciqrOW9NUT3uqYw7606VFg6CgXHYvOYdw+AKx8YJVb9V/9LnFUbrk8BGYjFu bELJFgLV1JtXmbgcQJFA17g8qBxMNYc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654242; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zZ7qqMqYr+coZy8gdEZ03r7ExQ3wOPQZ6PCEnVK5cfk=; b=ZnxHgty0wfoXE/vEHhAXtciT6D5BdI45QeZqrzrlqfPsBS5HwP6lhiQ20o/pGobEo4IRXW /TqjJHqkG6enhPCA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 4DBEA13724; Mon, 26 Aug 2024 06:37:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 06rRAKAizGYnOAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:20 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 1/7] block: change wait on bd_claiming to use a var_waitqueue, not a bit_waitqueue Date: Mon, 26 Aug 2024 16:30:58 +1000 Message-ID: <20240826063659.15327-2-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Queue-Id: B034C1F83B X-Spam-Level: X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[99.99%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_TRACE(0.00)[suse.de:+]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; DWL_DNSWL_BLOCKED(0.00)[suse.de:dkim]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; RCPT_COUNT_SEVEN(0.00)[7]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.de:email,suse.de:dkim,suse.de:mid] X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: no action X-Spam-Score: -3.01 X-Spam-Flag: NO bd_prepare_to_claim() waits for a var to change, not for a bit to be cleared. So change from bit_waitqueue() to __var_waitqueue() and correspondingly use wake_up_var(). This will allow a future patch which change the "bit" function to expect an "unsigned long *" instead of "void *". Signed-off-by: NeilBrown --- block/bdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index c5507b6f63b8..21e688fb6449 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -548,7 +548,7 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder, /* if claiming is already in progress, wait for it to finish */ if (whole->bd_claiming) { - wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); + wait_queue_head_t *wq = __var_waitqueue(&whole->bd_claiming); DEFINE_WAIT(wait); prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); @@ -571,7 +571,7 @@ static void bd_clear_claiming(struct block_device *whole, void *holder) /* tell others that we're done */ BUG_ON(whole->bd_claiming != holder); whole->bd_claiming = NULL; - wake_up_bit(&whole->bd_claiming, 0); + wake_up_var(&whole->bd_claiming); } /** From patchwork Mon Aug 26 06:30:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777125 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFE9378281; Mon, 26 Aug 2024 06:37:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654254; cv=none; b=ul/AhMmQNjw53dXUBZXyTSpu3iDm50m45cW70OZA3zHBnSXFWrnhvkRezL9gOCP9bbgu6b0Ph6S2qjbUs4ZpPCUZyfr6M02UvvjXKAqYDlVW6Ge2kpjIj3ySbD9ahSSuhbFWyZAvJ5KkPPAvCKnHQSm1HdVmUAe/mHB93QQA3ok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654254; c=relaxed/simple; bh=dCfrj2kl/kt6Z0EursSTPCaFyKyHLEP3r1+tiEaiX/Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c34kEPxuesTAvB4d8aFViom2urmKKPP0l9fHvZEelge/QQCT/bxb2GxKkvueELlml6Udtzp79Ft+ToXx5s9tdjO5+1kSwJl2EjZPNwrprFsa6ZyoXJR5yZEwwBK0925VF7fNfLhdkU3sHaD4oO0xPzXqNZgTVUF2qgXnNElbQOQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=hKQI9g1a; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Plctls6E; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=feVj+ca3; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=5tXCOgQk; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="hKQI9g1a"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Plctls6E"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="feVj+ca3"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="5tXCOgQk" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 79C201F83C; Mon, 26 Aug 2024 06:37:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654249; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W0AazAjBEO6XKGRkZKBGDcwpbW2z3s1AfVJ8MobowfA=; b=hKQI9g1aKnqt6W8jKElhuNxWf2AZrKt13XWPlH03wdCs31IyCzGqNyHrVK/8IHsz+2sWmX 9KaNuovItl8riJAKx+haZr02bBDC27jmgfbut7boLFCtnec8ntZUuBkZJjHdHB7EVYGVnm 0Ewi3j2K1XUpy2xRQCVc/ZRMmpWP31A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654249; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W0AazAjBEO6XKGRkZKBGDcwpbW2z3s1AfVJ8MobowfA=; b=Plctls6EhleJATCQteKNd2mK21Q0OatyyZKBFJFHxqZDB5ijt/SzA2W/6VaEHLF9GqmVfe 4JET8VNsaYHYN2Dg== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=feVj+ca3; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=5tXCOgQk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654248; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W0AazAjBEO6XKGRkZKBGDcwpbW2z3s1AfVJ8MobowfA=; b=feVj+ca3cKLAPyHGMESo/vAKYcmbHxV8IBOIfDm50M/t0xlYVAFj0glhfOWTrs4Wh9YE2e qD4SHY5a3AqpfCHOpflVKii7W4GKXqyL7poU2Lp8JGrFfGtEjzXcmWx49k0tA3yxX68ESM I/WvKTFp3MnK3jZWUbmbeHSk6nPhQ0A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654248; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W0AazAjBEO6XKGRkZKBGDcwpbW2z3s1AfVJ8MobowfA=; b=5tXCOgQky77les9DRmk7JkoV0dAMj/S0kueQcU9ApE4D1w5ei7PhxcTNqdx4vTUJnHfIs5 sK1p+eQnB9WChCAg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1A1A613724; Mon, 26 Aug 2024 06:37:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id d0NvMKUizGYtOAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:25 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 2/7] sched: change wake_up_bit() and related function to expect unsigned long * Date: Mon, 26 Aug 2024 16:30:59 +1000 Message-ID: <20240826063659.15327-3-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Queue-Id: 79C201F83C X-Spam-Score: -3.01 X-Rspamd-Action: no action X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DWL_DNSWL_BLOCKED(0.00)[suse.de:dkim]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; RCPT_COUNT_SEVEN(0.00)[7]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,suse.de:dkim,suse.de:mid,suse.de:email] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Flag: NO X-Spam-Level: wake_up_bit() currently allows a "void *". While this isn't strictly a problem as the address is never dereferenced, it is inconsistent with the corresponding wait_var_event() which requires "unsigned long *" and does dereference the pointer. And code that needs to wait for a change in something other than an unsigned long would be better served by wake_up_var(). This patch changes all related "void *" to "unsigned long *". Reported-by: Linus Torvalds Signed-off-by: NeilBrown --- include/linux/wait_bit.h | 16 ++++++++-------- kernel/sched/wait_bit.c | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h index 7725b7579b78..48e123839892 100644 --- a/include/linux/wait_bit.h +++ b/include/linux/wait_bit.h @@ -8,7 +8,7 @@ #include struct wait_bit_key { - void *flags; + unsigned long *flags; int bit_nr; unsigned long timeout; }; @@ -23,14 +23,14 @@ struct wait_bit_queue_entry { typedef int wait_bit_action_f(struct wait_bit_key *key, int mode); -void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit); +void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit); int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode); int __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode); -void wake_up_bit(void *word, int bit); -int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode); -int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout); -int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode); -struct wait_queue_head *bit_waitqueue(void *word, int bit); +void wake_up_bit(unsigned long *word, int bit); +int out_of_line_wait_on_bit(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode); +int out_of_line_wait_on_bit_timeout(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout); +int out_of_line_wait_on_bit_lock(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode); +struct wait_queue_head *bit_waitqueue(unsigned long *word, int bit); extern void __init wait_bit_init(void); int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); @@ -327,7 +327,7 @@ do { \ * You can use this helper if bitflags are manipulated atomically rather than * non-atomically under a lock. */ -static inline void clear_and_wake_up_bit(int bit, void *word) +static inline void clear_and_wake_up_bit(int bit, unsigned long *word) { clear_bit_unlock(bit, word); /* See wake_up_bit() for which memory barrier you need to use. */ diff --git a/kernel/sched/wait_bit.c b/kernel/sched/wait_bit.c index 134d7112ef71..058b0e18727e 100644 --- a/kernel/sched/wait_bit.c +++ b/kernel/sched/wait_bit.c @@ -9,7 +9,7 @@ static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned; -wait_queue_head_t *bit_waitqueue(void *word, int bit) +wait_queue_head_t *bit_waitqueue(unsigned long *word, int bit) { const int shift = BITS_PER_LONG == 32 ? 5 : 6; unsigned long val = (unsigned long)word << shift | bit; @@ -55,7 +55,7 @@ __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_ } EXPORT_SYMBOL(__wait_on_bit); -int __sched out_of_line_wait_on_bit(void *word, int bit, +int __sched out_of_line_wait_on_bit(unsigned long *word, int bit, wait_bit_action_f *action, unsigned mode) { struct wait_queue_head *wq_head = bit_waitqueue(word, bit); @@ -66,7 +66,7 @@ int __sched out_of_line_wait_on_bit(void *word, int bit, EXPORT_SYMBOL(out_of_line_wait_on_bit); int __sched out_of_line_wait_on_bit_timeout( - void *word, int bit, wait_bit_action_f *action, + unsigned long *word, int bit, wait_bit_action_f *action, unsigned mode, unsigned long timeout) { struct wait_queue_head *wq_head = bit_waitqueue(word, bit); @@ -108,7 +108,7 @@ __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry } EXPORT_SYMBOL(__wait_on_bit_lock); -int __sched out_of_line_wait_on_bit_lock(void *word, int bit, +int __sched out_of_line_wait_on_bit_lock(unsigned long *word, int bit, wait_bit_action_f *action, unsigned mode) { struct wait_queue_head *wq_head = bit_waitqueue(word, bit); @@ -118,7 +118,7 @@ int __sched out_of_line_wait_on_bit_lock(void *word, int bit, } EXPORT_SYMBOL(out_of_line_wait_on_bit_lock); -void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit) +void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit) { struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit); @@ -144,7 +144,7 @@ EXPORT_SYMBOL(__wake_up_bit); * may need to use a less regular barrier, such fs/inode.c's smp_mb(), * because spin_unlock() does not guarantee a memory barrier. */ -void wake_up_bit(void *word, int bit) +void wake_up_bit(unsigned long *word, int bit) { __wake_up_bit(bit_waitqueue(word, bit), word, bit); } From patchwork Mon Aug 26 06:31:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777126 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D0C1127B57; Mon, 26 Aug 2024 06:37:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654258; cv=none; b=mnEmNdjt09qodlUjCbzel4YNpq9iy97jnhNqXmrk2Tc3kaYDviNyW03Wi/ulPebUJw/hrhXQkmJexQ/u4dU3DEcVUBa/yQZY9aMiXwK4x0R2dAcsYlnP+o9eviVVGEGFjzebOLaugvqrzPVm1L8e7ZRWn1idSndiEZcxkhDzjsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654258; c=relaxed/simple; bh=tqIW5ADrxqpprsFVkY4P7BaM5PA32jHUVg6V7x/C0sI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Dfc9eJPIqyD19b6mRWQ0OxYHgjssbyevWG61BNJlL/Vk3lpxnX7a5TDVtSCMrGx5tnup0IK5gGx/oGN7IGH/HBg41ISWgYkdi/aSu2anZZ2lDyaHLNhADpf2vLYd9CdiG0HeeMYhMrEOa2fCbGAyZTqViGs2PMpaxOXerHbPYms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=XAf0dvnq; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=IYIkUSL8; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=XAf0dvnq; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=IYIkUSL8; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="XAf0dvnq"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="IYIkUSL8"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="XAf0dvnq"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="IYIkUSL8" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 9B30D1F83B; Mon, 26 Aug 2024 06:37:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654254; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mYvuABek3Hw1EehHXYAISUiDFC3hNIfdi0ZgD0cyZSo=; b=XAf0dvnqWsbdNw6yQ9fxEyB/MiRfJuGrNXJsMZqG7bc/440LDw1UyJwwv0ZhiaV0CVexz4 GkAZAtPNtjzxekYM/Ob54wzUBDOExFnQczYT8zGZqVPRSLdj+C4w/zZXjKJQhWSFhvlHz0 OzHZSBGcCqOQg5zhx9sdJ9v2Sde1we4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654254; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mYvuABek3Hw1EehHXYAISUiDFC3hNIfdi0ZgD0cyZSo=; b=IYIkUSL8GMdEDsuk/+W/JDeuqvdfwlLUaWq6s6hpCFTWxVhoiz0X5+2KpKZ1KGC2FxrNiR XrIey972DLCVE+Dw== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654254; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mYvuABek3Hw1EehHXYAISUiDFC3hNIfdi0ZgD0cyZSo=; b=XAf0dvnqWsbdNw6yQ9fxEyB/MiRfJuGrNXJsMZqG7bc/440LDw1UyJwwv0ZhiaV0CVexz4 GkAZAtPNtjzxekYM/Ob54wzUBDOExFnQczYT8zGZqVPRSLdj+C4w/zZXjKJQhWSFhvlHz0 OzHZSBGcCqOQg5zhx9sdJ9v2Sde1we4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654254; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mYvuABek3Hw1EehHXYAISUiDFC3hNIfdi0ZgD0cyZSo=; b=IYIkUSL8GMdEDsuk/+W/JDeuqvdfwlLUaWq6s6hpCFTWxVhoiz0X5+2KpKZ1KGC2FxrNiR XrIey972DLCVE+Dw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E2EDE13724; Mon, 26 Aug 2024 06:37:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 6wyeJasizGY2OAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:31 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 3/7] sched: Improve documentation for wake_up_bit/wait_on_bit family of functions Date: Mon, 26 Aug 2024 16:31:00 +1000 Message-ID: <20240826063659.15327-4-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email,suse.de:mid,imap1.dmz-prg2.suse.org:helo] X-Spam-Score: -2.80 X-Spam-Flag: NO This patch revises the documention for wake_up_bit(), clear_and_wake_up_bit(), and all the wait_on_bit() family of functions. The new documentation places less emphasis on the pool of waitqueues used (an implementation details) and focuses instead on details of how the functions behave. The barriers included in the wait functions and clear_and_wake_up_bit() and those required for wake_up_bit() are spelled out more clearly. The error statuses returned are given explicitly. The fact that the wait_on_bit_lock function set the bit is made more obvious. Signed-off-by: NeilBrown --- include/linux/wait_bit.h | 159 +++++++++++++++++++++------------------ kernel/sched/wait_bit.c | 37 +++++---- 2 files changed, 110 insertions(+), 86 deletions(-) diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h index 48e123839892..b792a92a036e 100644 --- a/include/linux/wait_bit.h +++ b/include/linux/wait_bit.h @@ -53,19 +53,21 @@ extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode); /** * wait_on_bit - wait for a bit to be cleared - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * @word: the address containing the bit being waited on + * @bit: the bit at that address being waited on * @mode: the task state to sleep in * - * There is a standard hashed waitqueue table for generic use. This - * is the part of the hashtable's accessor API that waits on a bit. - * For instance, if one were to have waiters on a bitflag, one would - * call wait_on_bit() in threads waiting for the bit to clear. - * One uses wait_on_bit() where one is waiting for the bit to clear, - * but has no intention of setting it. - * Returned value will be zero if the bit was cleared, or non-zero - * if the process received a signal and the mode permitted wakeup - * on that signal. + * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) + * to be cleared. The clearing of the bit must be signalled with + * wake_up_bit(), often as clear_and_wake_up_bit(). + * + * The process will wait on a waitqueue selected by hash from a shared + * pool. It will only be woken on a wake_up for the target bit, even + * if other processed on the same queue are woken for other bits. + * + * Returned value will be zero if the bit was cleared in which case the + * call has ACQUIRE semantics, or %-EINTR if the process received a + * signal and the mode permitted wake up on that signal. */ static inline int wait_on_bit(unsigned long *word, int bit, unsigned mode) @@ -80,17 +82,20 @@ wait_on_bit(unsigned long *word, int bit, unsigned mode) /** * wait_on_bit_io - wait for a bit to be cleared - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * @word: the address containing the bit being waited on + * @bit: the bit at that address being waited on * @mode: the task state to sleep in * - * Use the standard hashed waitqueue table to wait for a bit - * to be cleared. This is similar to wait_on_bit(), but calls - * io_schedule() instead of schedule() for the actual waiting. + * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) + * to be cleared. The clearing of the bit must be signalled with + * wake_up_bit(), often as clear_and_wake_up_bit(). + * + * This is similar to wait_on_bit(), but calls io_schedule() instead of + * schedule() for the actual waiting. * - * Returned value will be zero if the bit was cleared, or non-zero - * if the process received a signal and the mode permitted wakeup - * on that signal. + * Returned value will be zero if the bit was cleared in which case the + * call has ACQUIRE semantics, or %-EINTR if the process received a + * signal and the mode permitted wake up on that signal. */ static inline int wait_on_bit_io(unsigned long *word, int bit, unsigned mode) @@ -104,19 +109,24 @@ wait_on_bit_io(unsigned long *word, int bit, unsigned mode) } /** - * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * wait_on_bit_timeout - wait for a bit to be cleared or a timeout to elapse + * @word: the address containing the bit being waited on + * @bit: the bit at that address being waited on * @mode: the task state to sleep in * @timeout: timeout, in jiffies * - * Use the standard hashed waitqueue table to wait for a bit - * to be cleared. This is similar to wait_on_bit(), except also takes a - * timeout parameter. + * Wait for the given bit in an unsigned long or bitmap (see + * DECLARE_BITMAP()) to be cleared, or for a timeout to expire. The + * clearing of the bit must be signalled with wake_up_bit(), often as + * clear_and_wake_up_bit(). * - * Returned value will be zero if the bit was cleared before the - * @timeout elapsed, or non-zero if the @timeout elapsed or process - * received a signal and the mode permitted wakeup on that signal. + * This is similar to wait_on_bit(), except it also takes a timeout + * parameter. + * + * Returned value will be zero if the bit was cleared in which case the + * call has ACQUIRE semantics, or %-EINTR if the process received a + * signal and the mode permitted wake up on that signal, or %-EAGAIN if the + * timeout elapsed. */ static inline int wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode, @@ -132,19 +142,21 @@ wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode, /** * wait_on_bit_action - wait for a bit to be cleared - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * @word: the address containing the bit waited on + * @bit: the bit at that address being waited on * @action: the function used to sleep, which may take special actions * @mode: the task state to sleep in * - * Use the standard hashed waitqueue table to wait for a bit - * to be cleared, and allow the waiting action to be specified. - * This is like wait_on_bit() but allows fine control of how the waiting - * is done. + * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP()) + * to be cleared. The clearing of the bit must be signalled with + * wake_up_bit(), often as clear_and_wake_up_bit(). + * + * This is similar to wait_on_bit(), but calls @action() instead of + * schedule() for the actual waiting. * - * Returned value will be zero if the bit was cleared, or non-zero - * if the process received a signal and the mode permitted wakeup - * on that signal. + * Returned value will be zero if the bit was cleared in which case the + * call has ACQUIRE semantics, or the error code returned by @action if + * that call returned non-zero. */ static inline int wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action, @@ -157,23 +169,22 @@ wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action, } /** - * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * wait_on_bit_lock - wait for a bit to be cleared, then set it + * @word: the address containing the bit being waited on + * @bit: the bit of the word being waited on and set * @mode: the task state to sleep in * - * There is a standard hashed waitqueue table for generic use. This - * is the part of the hashtable's accessor API that waits on a bit - * when one intends to set it, for instance, trying to lock bitflags. - * For instance, if one were to have waiters trying to set bitflag - * and waiting for it to clear before setting it, one would call - * wait_on_bit() in threads waiting to be able to set the bit. - * One uses wait_on_bit_lock() where one is waiting for the bit to - * clear with the intention of setting it, and when done, clearing it. + * Wait for the given bit in an unsigned long or bitmap (see + * DECLARE_BITMAP()) to be cleared. The clearing of the bit must be + * signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As + * soon as it is clear, atomically set it and return. * - * Returns zero if the bit was (eventually) found to be clear and was - * set. Returns non-zero if a signal was delivered to the process and - * the @mode allows that signal to wake the process. + * This is similar to wait_on_bit(), but sets the bit before returning. + * + * Returned value will be zero if the bit was successfully set in which + * case the call has the same memory sequencing semantics as + * test_and_clear_bit(), or %-EINTR if the process received a signal and + * the mode permitted wake up on that signal. */ static inline int wait_on_bit_lock(unsigned long *word, int bit, unsigned mode) @@ -185,15 +196,18 @@ wait_on_bit_lock(unsigned long *word, int bit, unsigned mode) } /** - * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * wait_on_bit_lock_io - wait for a bit to be cleared, then set it + * @word: the address containing the bit being waited on + * @bit: the bit of the word being waited on and set * @mode: the task state to sleep in * - * Use the standard hashed waitqueue table to wait for a bit - * to be cleared and then to atomically set it. This is similar - * to wait_on_bit(), but calls io_schedule() instead of schedule() - * for the actual waiting. + * Wait for the given bit in an unsigned long or bitmap (see + * DECLARE_BITMAP()) to be cleared. The clearing of the bit must be + * signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As + * soon as it is clear, atomically set it and return. + * + * This is similar to wait_on_bit_lock(), but calls io_schedule() instead + * of schedule(). * * Returns zero if the bit was (eventually) found to be clear and was * set. Returns non-zero if a signal was delivered to the process and @@ -209,21 +223,19 @@ wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode) } /** - * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * wait_on_bit_lock_action - wait for a bit to be cleared, then set it + * @word: the address containing the bit being waited on + * @bit: the bit of the word being waited on and set * @action: the function used to sleep, which may take special actions * @mode: the task state to sleep in * - * Use the standard hashed waitqueue table to wait for a bit - * to be cleared and then to set it, and allow the waiting action - * to be specified. - * This is like wait_on_bit() but allows fine control of how the waiting - * is done. + * This is similar to wait_on_bit_lock(), but calls @action() instead of + * schedule() for the actual waiting. * - * Returns zero if the bit was (eventually) found to be clear and was - * set. Returns non-zero if a signal was delivered to the process and - * the @mode allows that signal to wake the process. + * Returned value will be zero if the bit was successfully set in which + * case the call has the same memory sequencing semantics as + * test_and_clear_bit(), or the error code returned by @action if that + * call returned non-zero. */ static inline int wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action, @@ -320,12 +332,13 @@ do { \ /** * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit - * * @bit: the bit of the word being waited on - * @word: the word being waited on, a kernel virtual address + * @word: the address containing the bit being waited on * - * You can use this helper if bitflags are manipulated atomically rather than - * non-atomically under a lock. + * The designated bit is cleared and any tasks waiting in wait_on_bit() + * or similar will be woken. This call has RELEASE semantics so that + * any changes to memory made before this call are guaranteed to be visible + * after the corresponding wait_on_bit() completes. */ static inline void clear_and_wake_up_bit(int bit, unsigned long *word) { diff --git a/kernel/sched/wait_bit.c b/kernel/sched/wait_bit.c index 058b0e18727e..247997e1c9c4 100644 --- a/kernel/sched/wait_bit.c +++ b/kernel/sched/wait_bit.c @@ -128,21 +128,32 @@ void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit EXPORT_SYMBOL(__wake_up_bit); /** - * wake_up_bit - wake up a waiter on a bit - * @word: the word being waited on, a kernel virtual address - * @bit: the bit of the word being waited on + * wake_up_bit - wake up waiters on a bit + * @word: the address containing the bit being waited on + * @bit: the bit at that address being waited on * - * There is a standard hashed waitqueue table for generic use. This - * is the part of the hash-table's accessor API that wakes up waiters - * on a bit. For instance, if one were to have waiters on a bitflag, - * one would call wake_up_bit() after clearing the bit. + * Wake up any process waiting in wait_on_bit() or similar for the + * given bit to be cleared. * - * In order for this to function properly, as it uses waitqueue_active() - * internally, some kind of memory barrier must be done prior to calling - * this. Typically, this will be smp_mb__after_atomic(), but in some - * cases where bitflags are manipulated non-atomically under a lock, one - * may need to use a less regular barrier, such fs/inode.c's smp_mb(), - * because spin_unlock() does not guarantee a memory barrier. + * The wake-up is sent to tasks in a waitqueue selected by hash from a + * shared pool. Only those tasks on that queue which have requested + * wake_up on this specific address and bit will be woken, and only if the + * bit is clear. + * + * In order for this to function properly there must be a full memory + * barrier after the bit is cleared and before this function is called. + * If the bit was cleared atomically, such as a by clear_bit() then + * smb_mb__after_atomic() can be used, othwewise smb_mb() is needed. + * + * If, however, the wait_on_bit is performed under a lock, such as with + * wait_on_bit_action() where the action drops and reclaims the lock + * around schedule(), and if this wake_up_bit() call happens under the + * same lock, then no barrier is required. + * + * Normally the bit should be cleared by an operation with RELEASE + * semantics so that any changes to memory made before the bit is + * cleared are guaranteed to be visible after the matching wait_on_bit() + * completes. */ void wake_up_bit(unsigned long *word, int bit) { From patchwork Mon Aug 26 06:31:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777138 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EA2812D1FA; Mon, 26 Aug 2024 06:37:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654264; cv=none; b=VvZ2tdLeOhF59c7W0zJASbsTlHFAjhDdHfvviBZ/Ce+hlS/YjJcX7hlARTUIodmm64+J2EFWlTUMu/bjzlOer9irqxfnPVybpph5ancavMpIhNVHR490HFhwT6Q1wAGDyKmkYRSgU3bgC8EuILBnVg3wK6rmQEiF0/3OBkMgjD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654264; c=relaxed/simple; bh=ke81x9eako5vW+VuRFxe+EXuYh5rkIDZYI5NtrObd2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l7IdgDGbMOEO4WL7mbaKsS6o/AqESpoP/OAmMMYtnbvGHJNrP0al3scmIaR1ITSRaCb6D5vRu2wax8oWastW6bHpFVSmhECw+noUzJGL4p8YqmdvzMoGZ9D7JRasJ5Y06DrvsiAb77PwLjCHx4rfQv2XOhn95N0wR8R4x5xRLPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=BxyLyDUG; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=YiQ0NLlK; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=BxyLyDUG; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=YiQ0NLlK; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="BxyLyDUG"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="YiQ0NLlK"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="BxyLyDUG"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="YiQ0NLlK" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 723802191F; Mon, 26 Aug 2024 06:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654260; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2H+YqLG+AMKGCcjtLQ3+u471leznMZwzGQZIx8eoU+Y=; b=BxyLyDUGFHtpej19yaKifO4TO8wLR70iqAMrd3XgAShu8jGzDpQ03wXKqLxsJeAj0PVssV vI/Vf6jAlp5W0RRa6RCBa2i294CK0cA7F+VrcqTzrAU+MBQt71UD/s/62hTh1Fz2ubVree kacpQNOBn6lIjbS9MSTrNtM79cNuur4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654260; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2H+YqLG+AMKGCcjtLQ3+u471leznMZwzGQZIx8eoU+Y=; b=YiQ0NLlKId+1/ZU4PPYnmXZPbETyfpRbbJSXAUZC3kLzwXTH/Pc3MBFbWoqt3FdlsG+xnw //Ta0jyWMhAVePDA== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654260; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2H+YqLG+AMKGCcjtLQ3+u471leznMZwzGQZIx8eoU+Y=; b=BxyLyDUGFHtpej19yaKifO4TO8wLR70iqAMrd3XgAShu8jGzDpQ03wXKqLxsJeAj0PVssV vI/Vf6jAlp5W0RRa6RCBa2i294CK0cA7F+VrcqTzrAU+MBQt71UD/s/62hTh1Fz2ubVree kacpQNOBn6lIjbS9MSTrNtM79cNuur4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654260; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2H+YqLG+AMKGCcjtLQ3+u471leznMZwzGQZIx8eoU+Y=; b=YiQ0NLlKId+1/ZU4PPYnmXZPbETyfpRbbJSXAUZC3kLzwXTH/Pc3MBFbWoqt3FdlsG+xnw //Ta0jyWMhAVePDA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 0BC7C13724; Mon, 26 Aug 2024 06:37:37 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id FfKcLLEizGZHOAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:37 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 4/7] sched: Document wait_var_event() family of functions and wake_up_var() Date: Mon, 26 Aug 2024 16:31:01 +1000 Message-ID: <20240826063659.15327-5-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email,suse.de:mid,imap1.dmz-prg2.suse.org:helo]; RCVD_TLS_ALL(0.00)[] X-Spam-Score: -2.80 X-Spam-Flag: NO wake_up_var(), wait_var_event() and related interfaces are not documented but have important ordering requirements. This patch adds documentation and makes these requirements explicit. The return values for those wait_var_event_* functions which return a value are documented. Note that these are, perhaps surprisingly, sometimes different from comparable wait_on_bit() functions. Signed-off-by: NeilBrown --- include/linux/wait_bit.h | 71 ++++++++++++++++++++++++++++++++++++++++ kernel/sched/wait_bit.c | 30 +++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h index b792a92a036e..ca5c6e70f908 100644 --- a/include/linux/wait_bit.h +++ b/include/linux/wait_bit.h @@ -282,6 +282,22 @@ __out: __ret; \ ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ schedule()) +/** + * wait_var_event - wait for a variable to be updated and notified + * @var: the address of variable being waited on + * @condition: the condition to wait for + * + * Wait for a @condition to be true, only re-checking when a wake up is + * received for the given @var (an arbitrary kernel address which need + * not be directly related to the given condition, but usually is). + * + * The process will wait on a waitqueue selected by hash from a shared + * pool. It will only be woken on a wake_up for the given address. + * + * The condition should normally use smp_load_acquire() or a similarly + * ordered access to ensure that any changes to memory made before the + * condition became true will be visible after the wait completes. + */ #define wait_var_event(var, condition) \ do { \ might_sleep(); \ @@ -294,6 +310,24 @@ do { \ ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \ schedule()) +/** + * wait_var_event_killable - wait for a variable to be updated and notified + * @var: the address of variable being waited on + * @condition: the condition to wait for + * + * Wait for a @condition to be true or a fatal signal to be received, + * only re-checking the condition when a wake up is received for the given + * @var (an arbitrary kernel address which need not be directly related + * to the given condition, but usually is). + * + * This is similar to wait_var_event() but returns a value which is + * 0 if the condition became true, or %-ERESTARTSYS if a fatal signal + * was received. + * + * The condition should normally use smp_load_acquire() or a similarly + * ordered access to ensure that any changes to memory made before the + * condition became true will be visible after the wait completes. + */ #define wait_var_event_killable(var, condition) \ ({ \ int __ret = 0; \ @@ -308,6 +342,26 @@ do { \ TASK_UNINTERRUPTIBLE, 0, timeout, \ __ret = schedule_timeout(__ret)) +/** + * wait_var_event_timeout - wait for a variable to be updated or a timeout to expire + * @var: the address of variable being waited on + * @condition: the condition to wait for + * @timeout: maximum time to wait in jiffies + * + * Wait for a @condition to be true or a timeout to expire, only + * re-checking the condition when a wake up is received for the given + * @var (an arbitrary kernel address which need not be directly related + * to the given condition, but usually is). + * + * This is similar to wait_var_event() but returns a value which is 0 if + * the timeout expired and the condition was still false, or the + * remaining time left in the timeout (but at least 1) if the condition + * was found to be true. + * + * The condition should normally use smp_load_acquire() or a similarly + * ordered access to ensure that any changes to memory made before the + * condition became true will be visible after the wait completes. + */ #define wait_var_event_timeout(var, condition, timeout) \ ({ \ long __ret = timeout; \ @@ -321,6 +375,23 @@ do { \ ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \ schedule()) +/** + * wait_var_event_killable - wait for a variable to be updated and notified + * @var: the address of variable being waited on + * @condition: the condition to wait for + * + * Wait for a @condition to be true or a signal to be received, only + * re-checking the condition when a wake up is received for the given + * @var (an arbitrary kernel address which need not be directly related + * to the given condition, but usually is). + * + * This is similar to wait_var_event() but returns a value which is 0 if + * the condition became true, or %-ERESTARTSYS if a signal was received. + * + * The condition should normally use smp_load_acquire() or a similarly + * ordered access to ensure that any changes to memory made before the + * condition became true will be visible after the wait completes. + */ #define wait_var_event_interruptible(var, condition) \ ({ \ int __ret = 0; \ diff --git a/kernel/sched/wait_bit.c b/kernel/sched/wait_bit.c index 247997e1c9c4..d7ac2ec09f8f 100644 --- a/kernel/sched/wait_bit.c +++ b/kernel/sched/wait_bit.c @@ -199,6 +199,36 @@ void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int } EXPORT_SYMBOL(init_wait_var_entry); +/** + * wake_up_var - wake up waiters on a variable (kernel address) + * @var: the address of the variable being waited on + * + * Wake up any process waiting in wait_var_event() or similar for the + * given variable to change. wait_var_event() can be waiting for an + * arbitrary condition to be true and associates that condition with an + * address. Calling wake_up_var() suggests that the condition has been + * made true, but does not strictly require the condtion to use the + * address given. + * + * The wake-up is sent to tasks in a waitqueue selected by hash from a + * shared pool. Only those tasks on that queue which have requested + * wake_up on this specific address will be woken. + * + * In order for this to function properly there must be a full memory + * barrier after the variable is updated (or more accurately, after the + * condtion waited on has been made to be true) and before this function + * is called. If the variable was updated atomically, such as a by + * atomic_dec() then smb_mb__after_atomic() can be used. If the + * variable was updated by a fully ordered operation such as + * atomic_dec_and_test() then no extra barrier is required. Othwewise + * smb_mb() is needed. + * + * Normally the variable should be updated (the condition should be made + * to be true) by an operation with RELEASE semantics such as + * smp_store_release() so that any changes to memory made before the + * variable was update are guaranteed to be visible after the matching + * wait_var_event() completes. + */ void wake_up_var(void *var) { __wake_up_bit(__var_waitqueue(var), var, -1); From patchwork Mon Aug 26 06:31:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777139 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02C3B136671; Mon, 26 Aug 2024 06:37:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654269; cv=none; b=ZF6vgz5qM1qqJu0vBox/amIq4uieOc/n1U8TScQKra6YhgviWRGZYzM/TJRlc89wxMzRfaIsxEwqr/LCkS87bnXFTDYTC1eB9xm8XDQLhijVtxBvA6U3OcxW141J11g0Xzdo/QCIQXVbyAom1VefS1Bn0kfV/DJfW5r/CgFO0Dc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654269; c=relaxed/simple; bh=f+YQ6nhv9D59ELA4Osdsn2vvm9PNb3OivnaHAdUAe30=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qPowdiPR3Thaj+JK5Z72U9RX0Z1o3pksIzgct7w0xCUn3LWFL3GyS14nx46FMzdJTQW0I5Uyqwow+iaWNpzvD9xDwyhWc043cVVrYZ0dB9piZ9T22CFqGwCSeH2uTBLiERWwR45NXsnHO9GLXuW8dOtyFt2ABibh08+6fbiYMdo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=OosgwF+F; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=ACmOtA7f; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=OosgwF+F; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=ACmOtA7f; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="OosgwF+F"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ACmOtA7f"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="OosgwF+F"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ACmOtA7f" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 439451F83B; Mon, 26 Aug 2024 06:37:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654266; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6dA2P7oI8aE0aoZymFQ2h8vW+8D9z3FjMzGxgLt+4O8=; b=OosgwF+FXEvP/Y+oYKD9SPnghUfcAKSyIUMrUyGZ1uqbZX8r0mnpwBdWXBSjbbW5pO+t2/ nbHq4HToIm64a3f7xmkngifXQAVANZWmA4m2oKOQHmnb8x2BKJ7pol5SOSmxcTHrxnJL0M MqFYQJT8jvCJVyKYzBXz92m1hsrbjb4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654266; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6dA2P7oI8aE0aoZymFQ2h8vW+8D9z3FjMzGxgLt+4O8=; b=ACmOtA7f85ztGOUqIzpQ4zaITbfZw8xCEj/DzuRteqFb9nZrX411IrH/BJjooexTYlEzzs JVXwL35Y12upMgBQ== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=OosgwF+F; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=ACmOtA7f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654266; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6dA2P7oI8aE0aoZymFQ2h8vW+8D9z3FjMzGxgLt+4O8=; b=OosgwF+FXEvP/Y+oYKD9SPnghUfcAKSyIUMrUyGZ1uqbZX8r0mnpwBdWXBSjbbW5pO+t2/ nbHq4HToIm64a3f7xmkngifXQAVANZWmA4m2oKOQHmnb8x2BKJ7pol5SOSmxcTHrxnJL0M MqFYQJT8jvCJVyKYzBXz92m1hsrbjb4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654266; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6dA2P7oI8aE0aoZymFQ2h8vW+8D9z3FjMzGxgLt+4O8=; b=ACmOtA7f85ztGOUqIzpQ4zaITbfZw8xCEj/DzuRteqFb9nZrX411IrH/BJjooexTYlEzzs JVXwL35Y12upMgBQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id D38FA13724; Mon, 26 Aug 2024 06:37:43 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id yDL0IbcizGZROAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:43 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 5/7] sched: Add test_and_clear_wake_up_bit() and atomic_dec_and_wake_up() Date: Mon, 26 Aug 2024 16:31:02 +1000 Message-ID: <20240826063659.15327-6-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Queue-Id: 439451F83B X-Spam-Level: X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[99.99%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_TRACE(0.00)[suse.de:+]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; DWL_DNSWL_BLOCKED(0.00)[suse.de:dkim]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; RCPT_COUNT_SEVEN(0.00)[7]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.de:email,suse.de:dkim,suse.de:mid] X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: no action X-Spam-Score: -3.01 X-Spam-Flag: NO There are common patterns in the kernel of using test_and_clear_bit() before wake_up_bit(), and atomic_dec_and_test() before wake_up_var(). These combinations don't need extra barriers but sometimes include them unnecessarily. To help avoid the unnecessary barriers and to help discourage the general use of wake_up_bit/var (which is a fragile interface) introduce two combined functions which implement these patterns. Also add store_release_wake_up() which support the parts of simply setting a non-atomic variable as sending a wakeup. This pattern requires barriers which are often omitted. Signed-off-by: NeilBrown --- include/linux/wait_bit.h | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h index ca5c6e70f908..c1675457c077 100644 --- a/include/linux/wait_bit.h +++ b/include/linux/wait_bit.h @@ -419,4 +419,65 @@ static inline void clear_and_wake_up_bit(int bit, unsigned long *word) wake_up_bit(word, bit); } +/** + * test_and_clear_wake_up_bit - clear a bit if it was set: wake up anyone waiting on that bit + * + * @bit: the bit of the word being waited on + * @word: the address of memory containing that bit + * + * If the bit is set and can be atomically cleared, any tasks waiting in + * wait_on_bit() or similar will be woken. This call has the same + * complete ordering semantics as test_and_clear_bit(). Any changes to + * memory made before this call are guaranteed to be visible after the + * corresponding wait_on_bit() completes. + * + * Returns %true if the bit was successfully set and the wake up was sent. + */ +static inline bool test_and_clear_wake_up_bit(int bit, unsigned long *word) +{ + if (!test_and_clear_bit(bit, word)) + return false; + /* no extra barrier required */ + wake_up_bit(word, bit); + return true; +} + +/** + * atomic_dec_and_wake_up - decrement an atomic_t and if zero, wake up waiters + * + * @var: the variable to dec and test + * + * Decrements the atomic variable and if it reaches zero, send a wake_up to any + * processes waiting on the variable. + * + * This function has the same complete ordering semantics as atomic_dec_and_test. + * + * Returns %true is the variable reaches zero and the wake up was sent. + */ + +static inline bool atomic_dec_and_wake_up(atomic_t *var) +{ + if (!atomic_dec_and_test(var)) + return false; + wake_up_var(var); + return true; +} + +/** + * store_release_wake_up - update a variable and send a wake_up + * @var: the address of the variable to be updated and woken + * @val: the value to store in the variable. + * + * Store the given value in the variable send a wake up to any tasks + * waiting on the variable. All necessary barriers are included to ensure + * the task calling wait_var_event() sees the new value and all values + * written to memory before this call. + */ +#define store_release_wake_up(var, val) \ +do { \ + smp_store_release(var, val); \ + smp_mb(); \ + wake_up_var(var); \ +} while (0) + #endif /* _LINUX_WAIT_BIT_H */ From patchwork Mon Aug 26 06:31:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777140 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DF8913AD20; Mon, 26 Aug 2024 06:37:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654276; cv=none; b=j/vNwVcibV64RJv5VVbhK881fP/0U+AdEcDhDPmQ9GN27PxDAkYAldbSzA7FWQ/oC3qTIpdVHlxfaZKpBa7mO4CFleOyP4Y3DRY+ukeOP7APmxaTlfVyM5fJCHb2Px2DtfSCUNWEc4ERkhR+oVi1OzFxy4NRYlHChNtYMAOx+Cs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654276; c=relaxed/simple; bh=FOPU1HH/l0j8p6Wo0j6Drs9hqwTjhvd9gMs7xPvcH+A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k/QA4ZLs5jRXhgQ4NAsWRQXksg2gzP3e0P7UaylB0/UWqpqkm7xqRgIgHA5jzmSNFQTtEUaNTFkrwjIOo3Wr8dzaeMgzXBmoSF0oTP3wVKPOUkWIVKGc55afksa6PjaDd+ER5JXzB1lP8/CcjrxXRQkbCVNvQ7NJa/rvZt+4dz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=qpGzYT14; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=LHg/qG22; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=qpGzYT14; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=LHg/qG22; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="qpGzYT14"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="LHg/qG22"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="qpGzYT14"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="LHg/qG22" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 1ACAA2191F; Mon, 26 Aug 2024 06:37:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8zP2lU5ekTkep4e4EmZFqowgaxbTDfBEoEodX4TNVIk=; b=qpGzYT14bW7WGlNiLRFlnY/FPHgfzHtopG+hEeJ2baSUWPFjcuG2+dOhQMbz6R6s9bzxDk OXDFMTQD62g7kuks+UvDbtKRoh31zIGHXF18BlQZB1YlMuCpuemY6fXR9FqsAPljn7PC/X ZvXrzUe3lzwKVL98qQNUaicbC6Uk1lc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8zP2lU5ekTkep4e4EmZFqowgaxbTDfBEoEodX4TNVIk=; b=LHg/qG22PN4DESzHFBUev2oRC+BNPK1DThSzPZA4CWk/ur965Qipf/ykZJpY3hUUuW1iYg z3B9WZKwph/VuMAA== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8zP2lU5ekTkep4e4EmZFqowgaxbTDfBEoEodX4TNVIk=; b=qpGzYT14bW7WGlNiLRFlnY/FPHgfzHtopG+hEeJ2baSUWPFjcuG2+dOhQMbz6R6s9bzxDk OXDFMTQD62g7kuks+UvDbtKRoh31zIGHXF18BlQZB1YlMuCpuemY6fXR9FqsAPljn7PC/X ZvXrzUe3lzwKVL98qQNUaicbC6Uk1lc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654272; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8zP2lU5ekTkep4e4EmZFqowgaxbTDfBEoEodX4TNVIk=; b=LHg/qG22PN4DESzHFBUev2oRC+BNPK1DThSzPZA4CWk/ur965Qipf/ykZJpY3hUUuW1iYg z3B9WZKwph/VuMAA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A5B7013724; Mon, 26 Aug 2024 06:37:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Bt/lFr0izGZYOAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:49 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 6/7] sched: Add wait/wake interface for variable updated under a lock. Date: Mon, 26 Aug 2024 16:31:03 +1000 Message-ID: <20240826063659.15327-7-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Score: -2.80 X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[99.99%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,suse.de:email,imap1.dmz-prg2.suse.org:helo] X-Spam-Flag: NO X-Spam-Level: Sometimes we need to wait for a condition to be true which must be testing while holding a lock. Correspondingly the condition is made true while holing the lock and the wake up is sent under the lock. This patch provides wake and wait interfaces which can be used for this situation when the lock is either a mutex or a spinlock. Signed-off-by: NeilBrown --- include/linux/wait_bit.h | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h index c1675457c077..6995a0d89ebd 100644 --- a/include/linux/wait_bit.h +++ b/include/linux/wait_bit.h @@ -401,6 +401,75 @@ do { \ __ret; \ }) +/** + * wait_var_event_spinlock - wait for a variable to be updated under a spinlock + * @var: the address of the variable being waited on + * @condition: condition to wait for + * @lock: the spinlock which protects updates to the variable + * + * Wait for a condition which can only be reliably tested while holding + * a spinlock. The variables assessed in the condition will normal be updated + * under the same spinlock, and the wake up should be signalled with + * wake_up_var_locked() under the same spinlock. + * + * This is similar to wait_var_event(), but assume a spinlock is held + * while calling this function and while updating the variable. + * + * This must be called while the given lock is held and the lock will be + * dropped when schedule() is called to wait for a wake up, and will be + * reclaimed before testing the condition again. + */ +#define wait_var_event_spinlock(var, condition, lock) \ +do { \ + might_sleep(); \ + if (condition) \ + break; \ + ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ + spin_unlock(lock); schedule(); spin_lock(lock)); \ +} while (0) + +/** + * wait_var_event_mutex - wait for a variable to be updated under a mutex + * @var: the address of the variable being waited on + * @condition: condition to wait for + * @mutex: the mutex which protects updates to the variable + * + * Wait for a condition which can only be reliably tested while holding + * a mutex. The variables assessed in the condition will normal be + * updated under the same mutex, and the wake up should be signalled + * with wake_up_var_locked() under the same mutex. + * + * This is similar to wait_var_event(), but assume a mutex is held + * while calling this function and while updating the variable. + * + * This must be called while the given mutex is held and the mutex will be + * dropped when schedule() is called to wait for a wake up, and will be + * reclaimed before testing the condition again. + */ +#define wait_var_event_mutex(var, condition, mutex) \ +do { \ + might_sleep(); \ + if (condition) \ + break; \ + ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ + mutex_unlock(mutex); schedule(); mutex_lock(mutex)); \ +} while (0) + +/** + * wake_up_var_locked - wake up waits for a variable while holding a spinlock or mutex + * @var: the address of the variable being waited on + * @lock: The spinlock or mutex what protects the variable + * + * Send a wake up for the given variable which should be waited for with + * wait_var_event_spinlock() or wait_var_event_mutex(). Unlike wake_up_var(), + * no extra barriers are needed as the locking provides sufficient sequencing. + */ +#define wake_up_var_locked(var, lock) \ +do { \ + lockdep_assert_held(lock); \ + wake_up_var(var); \ +} while (0) + /** * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit * @bit: the bit of the word being waited on From patchwork Mon Aug 26 06:31:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: NeilBrown X-Patchwork-Id: 13777141 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA23E13BC35; Mon, 26 Aug 2024 06:37:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654281; cv=none; b=i7WHJ81wECCLUZJXcpUj14c/gq/zhsmSHsg9WDzs0VnJqonzTo4szmwDJmpm8vyu1Z7tmr8aQ2IC/Z9yJRq2X79ZCB6LmBOdVIUvdCVhzOKyDkWztQO74AujxO4GLwIuPmBZBVQfcoR4A2+dFDj/1+iGIquMojL1lucwCoj1vjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724654281; c=relaxed/simple; bh=8YiS0NVbpntah7NlxqDrvjOjb4m/yskhqzU/Dcc31JQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nrF4mLB3MYkarSGyu4PeR8P+fUCxuaBZQmGZjSUtAcKAHBVBVx++2q3lbIg4zwB9f5Qlr78itbbOGXcUSoj8y0dUlIM/JleLn8EzLJxMPTj0Y8I/7M90zPQtq5oFqkfjL6aPMyw12eIZfs+Zk5VuDwN6sm0bP5lR+dBGYmlUvxE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=RntaQXJB; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=7Fq6KpsS; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=RntaQXJB; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=7Fq6KpsS; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="RntaQXJB"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="7Fq6KpsS"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="RntaQXJB"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="7Fq6KpsS" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id CDA2721A9F; Mon, 26 Aug 2024 06:37:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654277; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+ZDV6v6nuwIxH+xFPpK0scrQAAGNBkk5VdIq6lbCH+0=; b=RntaQXJBBX+DVQyQCKn2+wVpQEsk0ZAhNxWvGhqKFtKbLcyef+dqrhyHuPXWs1zmfZ8M+x zIk3J+uXZ6orS/q8t51eSuQ74i6Rd0+GWfhImaiJCQXApQrnCewgbSxr8ArmrXI7m+cfb4 V8hbX2usSoGSWzkor+WMknP/1i3GFso= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654277; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+ZDV6v6nuwIxH+xFPpK0scrQAAGNBkk5VdIq6lbCH+0=; b=7Fq6KpsSIq2jqNgoz+Zg7JBheXbHo97zLZHuoRHzxz4/L1VSHLccsc8C3AFgf5OTZCUK6M MO3IThti+bZ34KBQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1724654277; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+ZDV6v6nuwIxH+xFPpK0scrQAAGNBkk5VdIq6lbCH+0=; b=RntaQXJBBX+DVQyQCKn2+wVpQEsk0ZAhNxWvGhqKFtKbLcyef+dqrhyHuPXWs1zmfZ8M+x zIk3J+uXZ6orS/q8t51eSuQ74i6Rd0+GWfhImaiJCQXApQrnCewgbSxr8ArmrXI7m+cfb4 V8hbX2usSoGSWzkor+WMknP/1i3GFso= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1724654277; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+ZDV6v6nuwIxH+xFPpK0scrQAAGNBkk5VdIq6lbCH+0=; b=7Fq6KpsSIq2jqNgoz+Zg7JBheXbHo97zLZHuoRHzxz4/L1VSHLccsc8C3AFgf5OTZCUK6M MO3IThti+bZ34KBQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 6EE8713724; Mon, 26 Aug 2024 06:37:55 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id +pZxCcMizGZkOAAAD6G6ig (envelope-from ); Mon, 26 Aug 2024 06:37:55 +0000 From: NeilBrown To: Ingo Molnar , Peter Zijlstra , Linus Torvalds , Jens Axboe Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 7/7] Block: switch bd_prepare_to_claim to use wait_var_event_mutex() Date: Mon, 26 Aug 2024 16:31:04 +1000 Message-ID: <20240826063659.15327-8-neilb@suse.de> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240826063659.15327-1-neilb@suse.de> References: <20240826063659.15327-1-neilb@suse.de> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Score: -2.80 X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; R_RATELIMIT(0.00)[from(RLewrxuus8mos16izbn)]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:mid,suse.de:email,imap1.dmz-prg2.suse.org:helo] X-Spam-Flag: NO X-Spam-Level: bd_prepare_to_claim() contains an open-coded version of the new wait_var_event_mutex(). Change it to use that function and re-organise the code to benefit from this change. Signed-off-by: NeilBrown --- block/bdev.c | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index 21e688fb6449..6e827ee02e7d 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -487,10 +487,10 @@ long nr_blockdev_pages(void) * Test whether @bdev can be claimed by @holder. * * RETURNS: - * %true if @bdev can be claimed, %false otherwise. + * %0 if @bdev can be claimed, %-EBUSY otherwise. */ -static bool bd_may_claim(struct block_device *bdev, void *holder, - const struct blk_holder_ops *hops) +static int bd_may_claim(struct block_device *bdev, void *holder, + const struct blk_holder_ops *hops) { struct block_device *whole = bdev_whole(bdev); @@ -503,9 +503,9 @@ static bool bd_may_claim(struct block_device *bdev, void *holder, if (bdev->bd_holder == holder) { if (WARN_ON_ONCE(bdev->bd_holder_ops != hops)) return false; - return true; + return 0; } - return false; + return -EBUSY; } /* @@ -514,8 +514,8 @@ static bool bd_may_claim(struct block_device *bdev, void *holder, */ if (whole != bdev && whole->bd_holder && whole->bd_holder != bd_may_claim) - return false; - return true; + return -EBUSY; + return 0; } /** @@ -535,43 +535,32 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder, const struct blk_holder_ops *hops) { struct block_device *whole = bdev_whole(bdev); + int err = 0; if (WARN_ON_ONCE(!holder)) return -EINVAL; -retry: - mutex_lock(&bdev_lock); - /* if someone else claimed, fail */ - if (!bd_may_claim(bdev, holder, hops)) { - mutex_unlock(&bdev_lock); - return -EBUSY; - } - /* if claiming is already in progress, wait for it to finish */ - if (whole->bd_claiming) { - wait_queue_head_t *wq = __var_waitqueue(&whole->bd_claiming); - DEFINE_WAIT(wait); - - prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); - mutex_unlock(&bdev_lock); - schedule(); - finish_wait(wq, &wait); - goto retry; - } + mutex_lock(&bdev_lock); + wait_var_event_mutex(&whole->bd_claiming, + (err = bd_may_claim(bdev, holder, hops)) != 0 || + whole->bd_claiming == NULL, + &bdev_lock); - /* yay, all mine */ - whole->bd_claiming = holder; + /* if someone else claimed, fail */ + if (!err) + /* yay, all mine */ + whole->bd_claiming = holder; mutex_unlock(&bdev_lock); - return 0; + return err; } EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ static void bd_clear_claiming(struct block_device *whole, void *holder) { - lockdep_assert_held(&bdev_lock); /* tell others that we're done */ BUG_ON(whole->bd_claiming != holder); whole->bd_claiming = NULL; - wake_up_var(&whole->bd_claiming); + wake_up_var_locked(&whole->bd_claiming, &bdev_lock); } /**