From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 972F1C76195 for ; Mon, 20 Mar 2023 17:58:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229944AbjCTR6g (ORCPT ); Mon, 20 Mar 2023 13:58:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230098AbjCTR6K (ORCPT ); Mon, 20 Mar 2023 13:58:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B01D33BDBB for ; Mon, 20 Mar 2023 10:53:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 08B326171D for ; Mon, 20 Mar 2023 17:51:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50FCAC4339B; Mon, 20 Mar 2023 17:51:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679334708; bh=O4RyR07yi2qGmH1BLYuResHhBz60D+xmKAhT1KEoQP4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ptIz4auyUDhigzQFj2Tx4iPbxUOPZDx7AEttC2UK5wgPbwF5w3753B4YqG2Ghc4sn tPkWya8TJ+fuNdDGPUCYw3z9imiDRS5brtEbzhK5LIu6UqbBS9VU8cNRTFkqFxqt3x +Z7dEQKKy3twuVwYsPPYxSwFclFALDBT8DBOsTeO75RcR/pYTQlRc39WEaLnD7DCFn bIavRq1ARxG5ESnwbCUULeA6pR4h0Jg7gVwyp1Wy3ZBTsZjOqSC0mm6OeG3Sdm+uhx t38dm/J9yMF4bVst4tLri6efsHY7bs0/WZlu2dYiHhhXQHG4TdUDR/4AWt8FkQRCkd efTgwWuQD4+MQ== From: Saeed Mahameed To: "David S. Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet Cc: Saeed Mahameed , netdev@vger.kernel.org, Tariq Toukan , Eli Cohen Subject: [net-next 02/14] lib: cpu_rmap: Use allocator for rmap entries Date: Mon, 20 Mar 2023 10:51:32 -0700 Message-Id: <20230320175144.153187-3-saeed@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230320175144.153187-1-saeed@kernel.org> References: <20230320175144.153187-1-saeed@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Eli Cohen Use a proper allocator for rmap entries using a naive for loop. The allocator relies on whether an entry is NULL to be considered free. Remove the used field of rmap which is not needed. Also, avoid crashing the kernel if an entry is not available. Signed-off-by: Eli Cohen Signed-off-by: Saeed Mahameed --- include/linux/cpu_rmap.h | 3 +-- lib/cpu_rmap.c | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/linux/cpu_rmap.h b/include/linux/cpu_rmap.h index be8aea04d023..0ec745e6cd36 100644 --- a/include/linux/cpu_rmap.h +++ b/include/linux/cpu_rmap.h @@ -16,14 +16,13 @@ * struct cpu_rmap - CPU affinity reverse-map * @refcount: kref for object * @size: Number of objects to be reverse-mapped - * @used: Number of objects added * @obj: Pointer to array of object pointers * @near: For each CPU, the index and distance to the nearest object, * based on affinity masks */ struct cpu_rmap { struct kref refcount; - u16 size, used; + u16 size; void **obj; struct { u16 index; diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c index e77f12bb3c77..e80ae6521f71 100644 --- a/lib/cpu_rmap.c +++ b/lib/cpu_rmap.c @@ -128,6 +128,17 @@ debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix) } #endif +static int get_free_index(struct cpu_rmap *rmap) +{ + int i; + + for (i = 0; i < rmap->size; i++) + if (!rmap->obj[i]) + return i; + + return -1; +} + /** * cpu_rmap_add - add object to a rmap * @rmap: CPU rmap allocated with alloc_cpu_rmap() @@ -137,10 +148,11 @@ debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix) */ int cpu_rmap_add(struct cpu_rmap *rmap, void *obj) { - u16 index; + int index = get_free_index(rmap); + + if (index == -1) + return index; - BUG_ON(rmap->used >= rmap->size); - index = rmap->used++; rmap->obj[index] = obj; return index; } @@ -230,7 +242,7 @@ void free_irq_cpu_rmap(struct cpu_rmap *rmap) if (!rmap) return; - for (index = 0; index < rmap->used; index++) { + for (index = 0; index < rmap->size; index++) { glue = rmap->obj[index]; if (glue) irq_set_affinity_notifier(glue->notify.irq, NULL); @@ -295,7 +307,11 @@ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq) glue->notify.release = irq_cpu_rmap_release; glue->rmap = rmap; cpu_rmap_get(rmap); - glue->index = cpu_rmap_add(rmap, glue); + rc = cpu_rmap_add(rmap, glue); + if (rc == -1) + return -ENOSPC; + + glue->index = rc; rc = irq_set_affinity_notifier(irq, &glue->notify); if (rc) { cpu_rmap_put(glue->rmap); -- 2.39.2