From b5c7e28be0775653001fecb9e5297dcfd26ac4c2 Mon Sep 17 00:00:00 2001 From: Alex Markuze Date: Sun, 16 Mar 2025 16:55:19 +0000 Subject: [PATCH] ceph_san more fixups --- net/ceph/ceph_san_batch.c | 98 ++++++++++++++++---------------------- net/ceph/ceph_san_logger.c | 8 +--- 2 files changed, 44 insertions(+), 62 deletions(-) diff --git a/net/ceph/ceph_san_batch.c b/net/ceph/ceph_san_batch.c index 85fbc9c7b09a5..4b3c37850434c 100644 --- a/net/ceph/ceph_san_batch.c +++ b/net/ceph/ceph_san_batch.c @@ -168,20 +168,22 @@ void *ceph_san_batch_get(struct ceph_san_batch *batch) cpu_mag->mag = NULL; } - /* Try to get a full magazine */ - spin_lock(&batch->full_lock); - if (!list_empty(&batch->full_magazines)) { - new_mag = list_first_entry(&batch->full_magazines, - struct ceph_san_magazine, list); - list_del(&new_mag->list); - batch->nr_full--; - spin_unlock(&batch->full_lock); - - cpu_mag->mag = new_mag; - if (new_mag->count > 0) - element = new_mag->elements[--new_mag->count]; - } else { - spin_unlock(&batch->full_lock); + if (batch->nr_full > 0) { + /* Try to get a full magazine */ + spin_lock(&batch->full_lock); + if (!list_empty(&batch->full_magazines)) { + new_mag = list_first_entry(&batch->full_magazines, + struct ceph_san_magazine, list); + list_del(&new_mag->list); + batch->nr_full--; + spin_unlock(&batch->full_lock); + + cpu_mag->mag = new_mag; + if (new_mag->count > 0) + element = new_mag->elements[--new_mag->count]; + } else { + spin_unlock(&batch->full_lock); + } } return element; @@ -196,61 +198,45 @@ EXPORT_SYMBOL(ceph_san_batch_get); void ceph_san_batch_put(struct ceph_san_batch *batch, void *element) { struct ceph_san_cpu_magazine *cpu_mag; - struct ceph_san_magazine *old_mag, *new_mag; + struct ceph_san_magazine *mag; cpu_mag = this_cpu_ptr(batch->cpu_magazines); - /* If we don't have a magazine, get an empty one */ - if (!cpu_mag->mag) { + /* Optimistically try to add to current magazine */ + if (likely(cpu_mag->mag && cpu_mag->mag->count < CEPH_SAN_MAGAZINE_SIZE)) { + cpu_mag->mag->elements[cpu_mag->mag->count++] = element; + return; + } + + /* If current magazine is full, move it to full pool */ + if (likely(cpu_mag->mag && cpu_mag->mag->count >= CEPH_SAN_MAGAZINE_SIZE)) { + spin_lock(&batch->full_lock); + list_add(&cpu_mag->mag->list, &batch->full_magazines); + batch->nr_full++; + spin_unlock(&batch->full_lock); + cpu_mag->mag = NULL; + } + + /* Get new magazine if needed */ + if (likely(!cpu_mag->mag)) { + /* Try to get from empty pool first */ spin_lock(&batch->empty_lock); if (!list_empty(&batch->empty_magazines)) { - cpu_mag->mag = list_first_entry(&batch->empty_magazines, - struct ceph_san_magazine, list); - list_del(&cpu_mag->mag->list); + mag = list_first_entry(&batch->empty_magazines, + struct ceph_san_magazine, list); + list_del(&mag->list); batch->nr_empty--; spin_unlock(&batch->empty_lock); + cpu_mag->mag = mag; } else { spin_unlock(&batch->empty_lock); - /* No empty magazine available, allocate a new one */ cpu_mag->mag = alloc_magazine(batch); } - if (!cpu_mag->mag) + if (unlikely(!cpu_mag->mag)) return; } - - /* If current magazine isn't full, add to it */ - if (cpu_mag->mag->count < CEPH_SAN_MAGAZINE_SIZE) { - cpu_mag->mag->elements[cpu_mag->mag->count++] = element; - return; - } - - /* Current magazine is full, move it to the full pool */ - old_mag = cpu_mag->mag; - - /* Try to get an empty magazine */ - spin_lock(&batch->empty_lock); - if (!list_empty(&batch->empty_magazines)) { - new_mag = list_first_entry(&batch->empty_magazines, - struct ceph_san_magazine, list); - list_del(&new_mag->list); - batch->nr_empty--; - spin_unlock(&batch->empty_lock); - } else { - spin_unlock(&batch->empty_lock); - new_mag = alloc_magazine(batch); - } - - if (new_mag) { - /* Move full magazine to full pool */ - spin_lock(&batch->full_lock); - list_add(&old_mag->list, &batch->full_magazines); - batch->nr_full++; - spin_unlock(&batch->full_lock); - - /* Use new magazine */ - cpu_mag->mag = new_mag; - new_mag->elements[new_mag->count++] = element; - } + /* Add element to magazine */ + cpu_mag->mag->elements[cpu_mag->mag->count++] = element; } EXPORT_SYMBOL(ceph_san_batch_put); diff --git a/net/ceph/ceph_san_logger.c b/net/ceph/ceph_san_logger.c index 529d612c2e1be..95582fbada850 100644 --- a/net/ceph/ceph_san_logger.c +++ b/net/ceph/ceph_san_logger.c @@ -138,16 +138,12 @@ int ceph_san_logger_init(void) spin_lock_init(&g_logger.lock); /* Initialize allocation batch */ - ret = ceph_san_batch_init(&g_logger.alloc_batch, - alloc_tls_ctx, - free_tls_ctx); + ret = ceph_san_batch_init(&g_logger.alloc_batch); if (ret) return ret; /* Initialize log batch */ - ret = ceph_san_batch_init(&g_logger.log_batch, - NULL, - NULL); + ret = ceph_san_batch_init(&g_logger.log_batch); if (ret) goto cleanup_alloc; -- 2.39.5