From patchwork Thu Dec 14 09:58:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 178630 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8430308dys; Thu, 14 Dec 2023 01:59:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4lfmRipIK579pApcx2XQVCiv/PPRxi96bC8Ucx5+o9M1X9/V52XVQLcgKkyYZbPj1j8f9 X-Received: by 2002:a05:6358:7253:b0:170:f2f3:1de1 with SMTP id i19-20020a056358725300b00170f2f31de1mr4729521rwa.33.1702547972030; Thu, 14 Dec 2023 01:59:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702547971; cv=none; d=google.com; s=arc-20160816; b=Jgssza0OMp6b7MhoksKbLl1tOdkzo5TrNXoqp94pFUXoqf18Zzef5kGPceXzLI270O VTfK/bbiCa/uxS0dqxPcHwi7bp6VRb+6xutVjMDmQhEvJ/MfS2QKZRJx1S8jS2j/Ftz6 Asc3lYWOoqDo6WY57ioA4y2OHiaa1u2GlZa0MOARfh2sOcEGybj+GMRURB+guJekVMb5 vh6DI0QbaOvK9P1C4kuGbD9K5hvc287TonvGR2BA/SJv7cuBjL0kdNnZPkQDVeQGo4tb ghUbEtoOnTsRM7jYGBcva36XTok5n0/yAIbI49hKSIMEa4yVCKyODZ77If5m5J/XJhCk 9s0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lsNugrMTLdavqUaTzPaiiSqSEI8478i76rEA8WzTmjs=; fh=GNOA1jdeAuH+tfAEzFx15vIukVGFZnqrW9xHApUHjl0=; b=KuQka2/jU+HOK5CEWa4bNTdRbO5Od5ZP+eYl61QZvK2ZgpBycAPE44fguuuyevo+FE 3pfrvLXNn2JxK1s/ZO96Wl76T39fHwyUrLiS+sz54DnaySscskRi+czuvWHrHZtLc4DF oussI9eoMwyS/k72PnPEp/QFZrlHyQGxFZtKMrwKOiwJHAJHYGNAHAFaQOjn36Ec3l4k UqVjm8ObGKhEGITZecyZHG1ggWkHMYNwF2fN54mimeW8jnaQrXvHk4EIwPh+3J57ydyw LLM3dgVtBW10lzpvDSf02cT0fK08fF6e8t5iy7IIDMM9H1jmGr68P8afuAlWlMxWIpEu PuXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="HYrx/Bmp"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id b10-20020a17090a800a00b0028065b30a0dsi12157813pjn.124.2023.12.14.01.59.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 01:59:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="HYrx/Bmp"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 9FC568026DA5; Thu, 14 Dec 2023 01:59:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443491AbjLNJ7O (ORCPT + 99 others); Thu, 14 Dec 2023 04:59:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443528AbjLNJ7L (ORCPT ); Thu, 14 Dec 2023 04:59:11 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A82A0106; Thu, 14 Dec 2023 01:59:17 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-5c68da9d639so4551720a12.3; Thu, 14 Dec 2023 01:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702547957; x=1703152757; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lsNugrMTLdavqUaTzPaiiSqSEI8478i76rEA8WzTmjs=; b=HYrx/Bmpb0/ztK7gQIfH+pYDdKhH9068NrDelmWb32ex09PSBiCSxIns9wUk7fDv/1 RspvSfurCi4wdMylvOMbys0w3uIKh9yhk8B3UFeB+UlAd7IrDnVokzcjWTMKPTmdWKBz D/H7gwu6iC/CYr7sz33A6JSLOHeg3lG7jM5/VgIc3xsc5suXY/xXY0IGle+pOQbjTuB3 D5tBjphS9sK2oXV444Xpg7Petn+Gjldm9f6LTI3jaTtBE1+lsuEJC4cV7D4SUDs5QeMW 52PULRB40T9eKsfisxhToW7phjzkswYhfoofI0kWjOg4zi9SEK5WZdV67mxgHRp7hYE8 Y/bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702547957; x=1703152757; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lsNugrMTLdavqUaTzPaiiSqSEI8478i76rEA8WzTmjs=; b=IE1JGUxFWR3yqK6p74fNvJRi/BDmnliCW0+7CHDXBwx8yoj1hoWZBL66LDSDHdwdYy FO8HSsJOSkwJLUi3iQCbpFwwaVKeegTr2+Arbakx4xiuFw93LMZZXlqz9oeVFqvbFZlX 8QXBE02f+OMA7ZLwMQlz0fM259mvYySydGvmYYvwYVA+mTYkFo/CogAnHBWd03aqAfYf bFUSAa6hR2HSRSdM+6mVPsf2UK64n69n5B9xUhmjXnLmb7dKoEGKcsPnCM2XRXkVjUvt S+1yEMHPpViN2TQ6JXFZWau51peAN7a5R5NwNOtz5zETkKmMt7d/alVK6nJ34M/bcVjU rdFg== X-Gm-Message-State: AOJu0Ywt1NqzglXG1C3z8gKq3ydS/62GSIzoOWbevv4vcIB6eySUvyTE np6KUevDwX1pqxn1a6lBmxbR+ed6+b4= X-Received: by 2002:a17:902:c40a:b0:1cf:c37f:7158 with SMTP id k10-20020a170902c40a00b001cfc37f7158mr5879146plk.23.1702547956967; Thu, 14 Dec 2023 01:59:16 -0800 (PST) Received: from rigel.home.arpa (60-241-235-125.tpgi.com.au. [60.241.235.125]) by smtp.gmail.com with ESMTPSA id r2-20020a1709028bc200b001bc6e6069a6sm922807plo.122.2023.12.14.01.59.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 01:59:16 -0800 (PST) From: Kent Gibson To: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, brgl@bgdev.pl, linus.walleij@linaro.org, andy@kernel.org Cc: Kent Gibson Subject: [PATCH v2 5/5] gpiolib: cdev: improve documentation of get/set values Date: Thu, 14 Dec 2023 17:58:14 +0800 Message-Id: <20231214095814.132400-6-warthog618@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231214095814.132400-1-warthog618@gmail.com> References: <20231214095814.132400-1-warthog618@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 14 Dec 2023 01:59:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785250942172943532 X-GMAIL-MSGID: 1785250942172943532 Add documentation of the algorithm used to perform scatter/gather of the requested lines and values in linereq_get_values() and linereq_set_values_unlocked() to improve maintainability. Signed-off-by: Kent Gibson --- drivers/gpio/gpiolib-cdev.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 73262305de0f..27cfed748b0a 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1391,9 +1391,18 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) if (copy_from_user(&lv, ip, sizeof(lv))) return -EFAULT; + /* + * gpiod_get_array_value_complex() requires compacted desc and val + * arrays, rather than the sparse ones in lv. + * Calculation of num_get and construction of the desc array is + * optimized to avoid allocation for the desc array for the common + * num_get == 1 case. + */ + /* scan requested lines to calculate the subset to get */ for (num_get = 0, i = 0; i < lr->num_lines; i++) { if (lv.mask & BIT_ULL(i)) { num_get++; + /* capture desc for the num_get == 1 case */ descs = &lr->lines[i].desc; } } @@ -1402,6 +1411,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) return -EINVAL; if (num_get != 1) { + /* build compacted desc array */ descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); if (!descs) return -ENOMEM; @@ -1422,6 +1432,7 @@ static long linereq_get_values(struct linereq *lr, void __user *ip) lv.bits = 0; for (didx = 0, i = 0; i < lr->num_lines; i++) { + /* unpack compacted vals for the response */ if (lv.mask & BIT_ULL(i)) { if (lr->lines[i].sw_debounced) val = debounced_value(&lr->lines[i]); @@ -1447,14 +1458,25 @@ static long linereq_set_values_unlocked(struct linereq *lr, unsigned int i, didx, num_set; int ret; + /* + * gpiod_set_array_value_complex() requires compacted desc and val + * arrays, rather than the sparse ones in lv. + * Calculation of num_set and construction of the descs and vals arrays + * is optimized to minimize scanning the lv->mask, and to avoid + * allocation for the desc array for the common num_set == 1 case. + */ bitmap_zero(vals, GPIO_V2_LINES_MAX); + /* scan requested lines to determine the subset to be set */ for (num_set = 0, i = 0; i < lr->num_lines; i++) { if (lv->mask & BIT_ULL(i)) { + /* setting inputs is not allowed */ if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) return -EPERM; + /* add to compacted values */ if (lv->bits & BIT_ULL(i)) __set_bit(num_set, vals); num_set++; + /* capture desc for the num_set == 1 case */ descs = &lr->lines[i].desc; } } @@ -1462,7 +1484,7 @@ static long linereq_set_values_unlocked(struct linereq *lr, return -EINVAL; if (num_set != 1) { - /* build compacted desc array and values */ + /* build compacted desc array */ descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); if (!descs) return -ENOMEM;