From patchwork Thu Jan 5 15:40:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 39656 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp365086wrt; Thu, 5 Jan 2023 07:52:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXt50ZLayLncUXo5gukzEe+UF7YUKjcNQBTmd5xzIcbk4llO0gHngiOlPh94nY+uTGzCPney X-Received: by 2002:a17:907:6d22:b0:7c1:52c:a960 with SMTP id sa34-20020a1709076d2200b007c1052ca960mr59413299ejc.8.1672933955344; Thu, 05 Jan 2023 07:52:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672933955; cv=none; d=google.com; s=arc-20160816; b=meuLK907klfJj2LGtgiJsA1WYqsSSELyM3hVCApsIch0ZBE51dow6GMmFWpFGuWuJU hzcc58V3vxl9y9FCMDMVj+rki99NDIixAx1zeS71uBT7RwTGK1aFAcXvrEwbM8dSuPD1 2CrhibMjKYuRg5Vco74WP314ly4y7H39XQ5YuILdow5QYdSFgEy0mU9bkiB5zIocbTb9 gJXAlsjeGbF1WrNkfeSv9A3iJYflb8hIivJ9a5WPINbNXa34VvrTf+OV8XAWN0/gEHm3 XfNEuq2T3VrgajGZBDt/ahV4IFH0hj/oOWAr9/QTBG6OlLcBwidicxKZGYQHGlNpd6OQ YgZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=+FVCCMhijBne0rvQztXHE47+6us6CT7mik+LadLbEec=; b=ku0tOatFjFKLb/1RufU6M6WbW0Ubog3/WcmXb56SQtevC9nbWlpyk+AbW2cPVFUTG1 roQmwqrP7iME0XbICawN+8+/KOcwSVcw3G9+2YXGo+D8UKdeEse+ePazUGG30tKPBP2C mWdNLirodP3BVtIxKNupb4OZ9aCbWQzq37braP8Gt8b0yQ6utz5n7qmDH6T0t3iEighb 9A0L/kPr/r/qRnmAzA1q3/eRz3WpjgMYAmAmt5SK124njv7AsZsDSyxJl9pyBi7TnGyn WrQs1fCRxxyqsgHwUN83uTx72Y9B0KOSJi7m7jjxpWWfbpgQnFKHz61Yb7lyw6CKZ2ew Iq7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EzdqAUPX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o22-20020a170906975600b007f1501a8c24si37402204ejy.216.2023.01.05.07.52.11; Thu, 05 Jan 2023 07:52:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EzdqAUPX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234137AbjAEPl0 (ORCPT + 99 others); Thu, 5 Jan 2023 10:41:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234385AbjAEPlY (ORCPT ); Thu, 5 Jan 2023 10:41:24 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78D954E415 for ; Thu, 5 Jan 2023 07:40:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672933238; 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; bh=+FVCCMhijBne0rvQztXHE47+6us6CT7mik+LadLbEec=; b=EzdqAUPXz8S6wAWwC+Dkr4oeGicXMoC9NURAah388vlZMuGUSm9gQ4HVoKVWfp0EmfhYZC Px+suwez2ryK9KIwqyZ3J0oP3MmMB3I4bRWYmUv5kWBwRDiJwvjbY5Agd/CX9FkKFbHYeH hr3RZJjS4InFCMHyQdbZELDk80bLOxM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-626-CPCFpsjwNCGxRkh36jYHMw-1; Thu, 05 Jan 2023 10:40:34 -0500 X-MC-Unique: CPCFpsjwNCGxRkh36jYHMw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4250E80234E; Thu, 5 Jan 2023 15:40:34 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CF7E9492B06; Thu, 5 Jan 2023 15:40:33 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 4A2904050E5BD; Thu, 5 Jan 2023 12:40:16 -0300 (-03) Date: Thu, 5 Jan 2023 12:40:16 -0300 From: Marcelo Tosatti To: linux-kernel@vger.kernel.org Cc: Matthew Wilcox , Christoph Hellwig , Jens Axboe , Frederic Weisbecker Subject: [PATCH RFC] fs/buffer.c: update per-CPU bh_lru cache via RCU Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754198394995120026?= X-GMAIL-MSGID: =?utf-8?q?1754198394995120026?= umount causes invalidate_bh_lrus which calls an IPI on each CPU that has non empty per-CPU cache: on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1); This interrupts CPUs which might be executing code sensitive to interferences. To avoid the IPI, free the per-CPU caches remotely via RCU. Two bh_lrus structures for each CPU are allocated: one is being used (assigned to per-CPU bh_lru pointer), and the other is being freed (or idle). Signed-off-by: Marcelo Tosatti diff --git a/fs/buffer.c b/fs/buffer.c index d9c6d1fbb6dd..9f9ed7fffec8 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -1203,7 +1203,21 @@ struct bh_lru { struct buffer_head *bhs[BH_LRU_SIZE]; }; -static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }}; + +/* + * Allocate two bh_lrus structures for each CPU. bh_lru points to the + * one that is currently in use, and the update path does + * (consider cpu->bh_lru = bh_lrus[0]). + * + * cpu->bh_lru = bh_lrus[1] + * synchronize_rcu() + * free bh's in bh_lrus[0] + */ +unsigned int bh_lru_idx; +static DEFINE_PER_CPU(struct bh_lru, bh_lrus[2]) = {{{ NULL }}, {{NULL}}}; +static DEFINE_PER_CPU(struct bh_lru, *bh_lru); + +static DEFINE_MUTEX(bh_lru_invalidate_mutex); #ifdef CONFIG_SMP #define bh_lru_lock() local_irq_disable() @@ -1245,16 +1259,19 @@ static void bh_lru_install(struct buffer_head *bh) return; } - b = this_cpu_ptr(&bh_lrus); + rcu_read_lock(); + b = rcu_dereference(per_cpu(bh_lru, smp_processor_id())); for (i = 0; i < BH_LRU_SIZE; i++) { swap(evictee, b->bhs[i]); if (evictee == bh) { + rcu_read_unlock(); bh_lru_unlock(); return; } } get_bh(bh); + rcu_read_unlock(); bh_lru_unlock(); brelse(evictee); } @@ -1266,28 +1283,32 @@ static struct buffer_head * lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size) { struct buffer_head *ret = NULL; + struct bh_lru *lru; unsigned int i; check_irqs_on(); bh_lru_lock(); + rcu_read_lock(); + + lru = rcu_dereference(per_cpu(bh_lru, smp_processor_id())); for (i = 0; i < BH_LRU_SIZE; i++) { - struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]); + struct buffer_head *bh = lru->bhs[i]; if (bh && bh->b_blocknr == block && bh->b_bdev == bdev && bh->b_size == size) { if (i) { while (i) { - __this_cpu_write(bh_lrus.bhs[i], - __this_cpu_read(bh_lrus.bhs[i - 1])); + lru->bhs[i] = lru->bhs[i - 1]; i--; } - __this_cpu_write(bh_lrus.bhs[0], bh); + lru->bhs[0] = bh; } get_bh(bh); ret = bh; break; } } + rcu_read_unlock(); bh_lru_unlock(); return ret; } @@ -1381,35 +1402,56 @@ static void __invalidate_bh_lrus(struct bh_lru *b) b->bhs[i] = NULL; } } -/* - * invalidate_bh_lrus() is called rarely - but not only at unmount. - * This doesn't race because it runs in each cpu either in irq - * or with preempt disabled. - */ -static void invalidate_bh_lru(void *arg) -{ - struct bh_lru *b = &get_cpu_var(bh_lrus); - - __invalidate_bh_lrus(b); - put_cpu_var(bh_lrus); -} bool has_bh_in_lru(int cpu, void *dummy) { - struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu); + struct bh_lru *b; int i; - + + rcu_read_lock(); + b = rcu_dereference(per_cpu(bh_lru, cpu)); for (i = 0; i < BH_LRU_SIZE; i++) { - if (b->bhs[i]) + if (b->bhs[i]) { + rcu_read_unlock(); return true; + } } + rcu_read_unlock(); return false; } +/* + * invalidate_bh_lrus() is called rarely - but not only at unmount. + */ void invalidate_bh_lrus(void) { - on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1); + int cpu, oidx, nidx; + + mutex_lock(&bh_lru_invalidate_mutex); + oidx = bh_lru_idx; + bh_lru_idx++; + if (bh_lru_idx >= 2) + bh_lru_idx = 0; + + nidx = bh_lru_idx; + /* Assign the per-CPU bh_lru pointer */ + cpus_read_lock(); + for_each_online_cpu(cpu) + rcu_assign_pointer(per_cpu(bh_lru, cpu), per_cpu_ptr(&bh_lrus[nidx], cpu)); + cpus_read_unlock(); + synchronize_rcu_expedited(); + + cpus_read_lock(); + for_each_online_cpu(cpu) { + struct bh_lru *b = per_cpu_ptr(&bh_lrus[oidx], cpu); + + bh_lru_lock(); + __invalidate_bh_lrus(b); + bh_lru_unlock(); + } + cpus_read_unlock(); + mutex_unlock(&bh_lru_invalidate_mutex); } EXPORT_SYMBOL_GPL(invalidate_bh_lrus); @@ -1422,8 +1464,10 @@ void invalidate_bh_lrus_cpu(void) struct bh_lru *b; bh_lru_lock(); - b = this_cpu_ptr(&bh_lrus); + rcu_read_lock(); + b = rcu_dereference(per_cpu(bh_lru, smp_processor_id())); __invalidate_bh_lrus(b); + rcu_read_unlock(); bh_lru_unlock(); } @@ -2923,12 +2967,15 @@ EXPORT_SYMBOL(free_buffer_head); static int buffer_exit_cpu_dead(unsigned int cpu) { int i; - struct bh_lru *b = &per_cpu(bh_lrus, cpu); + struct bh_lru *b; + rcu_read_lock(); + b = rcu_dereference(per_cpu(bh_lru, cpu)); for (i = 0; i < BH_LRU_SIZE; i++) { brelse(b->bhs[i]); b->bhs[i] = NULL; } + rcu_read_unlock(); this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr); per_cpu(bh_accounting, cpu).nr = 0; return 0; @@ -3021,7 +3068,7 @@ EXPORT_SYMBOL(__bh_read_batch); void __init buffer_init(void) { unsigned long nrpages; - int ret; + int ret, cpu; bh_cachep = kmem_cache_create("buffer_head", sizeof(struct buffer_head), 0, @@ -3029,6 +3076,11 @@ void __init buffer_init(void) SLAB_MEM_SPREAD), NULL); + cpus_read_lock(); + for_each_online_cpu(cpu) + per_cpu(bh_lru, cpu) = per_cpu_ptr(&bh_lrus[0], cpu); + cpus_read_unlock(); + /* * Limit the bh occupancy to 10% of ZONE_NORMAL */