From patchwork Sat Jan 20 06:55:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 189713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1476603dyb; Fri, 19 Jan 2024 22:56:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4LnHA6SEurbpycdHov3vrXIa3TT2X8baYAGdxWW3FvOrBL3K2/HmG+0TG9VSh2b+2MHrz X-Received: by 2002:a05:620a:2181:b0:783:8e25:735 with SMTP id g1-20020a05620a218100b007838e250735mr1182726qka.155.1705733777223; Fri, 19 Jan 2024 22:56:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705733777; cv=pass; d=google.com; s=arc-20160816; b=iEJ8zSS6LdD1GR/lO7CNnITU1FEbkM/DTfOgHs/MxSkMICzSHn7vex4DjLkXtvRCEm RopvDfiuYNyKeEJGnup1R6GabVeGoPnDEg2CtEjkYYx8zS5X9xwJkXntQv5gl6i5jhEu ZFTmvcG4hcCNHFba7PWYKJME4oUivZwBBDM3YgjtY04wYO9ONyYaDuLeTc2VjNSHA0sn MfIjN2+rBozMz8ZkFAcggI6di7jc8Q5WMutbU29zsJRMZg3zKKj9ii+R7UEYSffrZjf1 cXY75xH5egQ8axWE3gUCQirFCHht2QAivwuCSolo5pnqdY8+gsyjtViZsPncIEVRJFLH ATew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=drqLI5iv8tK+6H2RZsx4Mqly9yp+Pt1Pkvi8hiH2284=; fh=A8LfyeIAEqEjRUIFEf6FkShOWhYZjm4NqzioT68cjss=; b=UiL5ucEyLnoUzH1OdTtdHynpeyCKwti3hoFOKN1+je+dslSXuqx0SlrK4gSPLGGdqE AUcnvpEMabyCRM63cmdToMEUr7IyOfTDN9fsbD+r9LGBfKPWEjpcN3qo1F0Fejgwa3na mQ52G3YpQiGoO5TXF24t3d/G9zVZaVopiyGfvvR4m8Ydtqw88biFxeVnqEq/sbDFfLSG UWoqjWzt3lnTMfxmJR9WTXdLXy6rJ9GNfBH5zFJl8RG6O9JeiV2ak4/aPEHQ0592y6cP 0U64IB2v/kzYpRcksiGPvUdrw3SrRHWOrSentFeomcfGYep9xXhrerbah17jEzQh11rS l7Dg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UUBKTSWE; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-31724-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31724-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id pi37-20020a05620a37a500b0078393f45c20si327318qkn.20.2024.01.19.22.56.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 22:56:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-31724-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UUBKTSWE; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-31724-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31724-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0AE1F1C21B29 for ; Sat, 20 Jan 2024 06:56:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5495A3C10; Sat, 20 Jan 2024 06:56:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UUBKTSWE" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 154721858 for ; Sat, 20 Jan 2024 06:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705733760; cv=none; b=vFFdPthAZX7CWVtIWaqENPCkirhz/PhnZUSwEI0nRkCLSbNcOpA2IUmEQMHLi0PSGpQ2Yg+vcaBwxn2qx4Rv6O6RMrNdTqTeoOnrkxUrPxYYyqAnJ8WVWH8wbK8X+XQ6uM9A+hT5lGhMVGdTJLvt7LZgrgWKEnCBmHcS0VV1j4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705733760; c=relaxed/simple; bh=UKO1d22DE5qaYTzjwXgC1y9WVGi1HgsHirzj/ddBBlE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GMAtSo6MQIsbYPwsQMOGyjuBK3ayuLpI5OQgdJhgedLFyZDCmAVwZ8wvyO6xT4aE7wSbnO+rN6Gg7xQ5QshH4AJ+FyAJG0HdpRKuFKvnOdU1cBni59ZKNwU3Q4fVzl6SkOBGVcar1g0i3aCA4iRnUizDEaxv5CN7HUfG8CDpBiM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UUBKTSWE; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705733757; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=drqLI5iv8tK+6H2RZsx4Mqly9yp+Pt1Pkvi8hiH2284=; b=UUBKTSWEsKRKbLvKhICVXk1stxFNrRR52Gx8oI0GenV5eI7amGm1ZAv9Rpcre4yrHYXDZB CsZIJkzArY+9INOblYPaJsRUSIF26N++B1sd8na88RruT8mB2jyUhPumi2lBWNMNAbMJHv Bn5y/rVO4VwgX2lAH8V6trtZ14X7TKY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-96-ge-UWV4kMeG87RNIsk1Xjw-1; Sat, 20 Jan 2024 01:55:56 -0500 X-MC-Unique: ge-UWV4kMeG87RNIsk1Xjw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C2B7A3C0ED41; Sat, 20 Jan 2024 06:55:55 +0000 (UTC) Received: from localhost (unknown [10.72.116.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DEA451D5; Sat, 20 Jan 2024 06:55:54 +0000 (UTC) From: Ming Lei To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Thomas Gleixner , Ming Lei , Yury Norov Subject: [PATCH] lib/group_cpus.c: simplify grp_spread_init_one() Date: Sat, 20 Jan 2024 14:55:43 +0800 Message-ID: <20240120065543.739203-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788591501110089270 X-GMAIL-MSGID: 1788591501110089270 What the inner loop needs to do is to assign each cpu in `siblmsk & nmsk`. Clean it by using cpumask_next_and(), meantime add helper of grp_assign_cpu(). So grp_spread_init_one() becomes more readable now. Cc: Yury Norov Cc: Thomas Gleixner Signed-off-by: Ming Lei --- Hello Andrew, Please consider to use this one to replace the 1st, 2nd & 4th patches in Yury's V5: - avoid to add new kernel API - avoid to update iterator variable inside loop, which is tricky - fix bug in the 4th patch - add grp_assign_cpu() to make code more readable & clean lib/group_cpus.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/group_cpus.c b/lib/group_cpus.c index ee272c4cefcc..6cbc7379d954 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -11,32 +11,36 @@ #ifdef CONFIG_SMP +static bool grp_assign_cpu(int cpu, struct cpumask *irqmsk, + struct cpumask *nmsk) +{ + if (cpu >= nr_cpu_ids) + return false; + + cpumask_clear_cpu(cpu, nmsk); + cpumask_set_cpu(cpu, irqmsk); + return true; +} + static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk, unsigned int cpus_per_grp) { const struct cpumask *siblmsk; - int cpu, sibl; - - for ( ; cpus_per_grp > 0; ) { - cpu = cpumask_first(nmsk); + int cpu = -1, sibl; - /* Should not happen, but I'm too lazy to think about it */ - if (cpu >= nr_cpu_ids) + while (cpus_per_grp > 0) { + cpu = cpumask_next(cpu, nmsk); + if (!grp_assign_cpu(cpu, irqmsk, nmsk)) return; - - cpumask_clear_cpu(cpu, nmsk); - cpumask_set_cpu(cpu, irqmsk); cpus_per_grp--; /* If the cpu has siblings, use them first */ siblmsk = topology_sibling_cpumask(cpu); - for (sibl = -1; cpus_per_grp > 0; ) { - sibl = cpumask_next(sibl, siblmsk); - if (sibl >= nr_cpu_ids) + sibl = cpu; + while (cpus_per_grp > 0) { + sibl = cpumask_next_and(sibl, siblmsk, nmsk); + if (!grp_assign_cpu(sibl, irqmsk, nmsk)) break; - if (!cpumask_test_and_clear_cpu(sibl, nmsk)) - continue; - cpumask_set_cpu(sibl, irqmsk); cpus_per_grp--; } }