Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3413209lqp; Tue, 26 Mar 2024 08:31:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXLV4gbYzdeAz8aWLX4MRszVm6OweBAyL9Qy+tdNOoo1o8geoFjevrh0sEqF+FR6vYbQSG7+5TL0lXbnSJNKEdJXPvjrOamuFcypKkTew== X-Google-Smtp-Source: AGHT+IEj2DHbNzXjt0lWerGicgn3P4miaxieW3JnptP6oIIgSP4ZtRrmk0pVwsSarIUde7Bu8ixq X-Received: by 2002:a05:6358:6397:b0:17f:8831:21b4 with SMTP id k23-20020a056358639700b0017f883121b4mr142536rwh.23.1711467087494; Tue, 26 Mar 2024 08:31:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711467087; cv=pass; d=google.com; s=arc-20160816; b=f8ZoT0UQsn+XP8mFlb+HdKql41CIFziAXoMw4U2ETyGAcMB0KzM8dqBHCWLWrrpT+B 6uT8fbNltg3kuSxieMxTHDSGiOe8EgLgbwrnv92bskEHe+VONvlvouBRSSOWYfqRZVrP ZRhgFg7Phnd5eTb2RnVxIeQ2wtm9hHhBb0bd9aFWJLXjuCRbgCVO9Ceinnavi9KA14vv k5x+0qIdv0ByGflhfOgTlckga5wAxJI4VujiDGS7wUNbaP0uLilcEKUq6La6pSStalob W8lu99RychoDepnc51kfppwGmNq0vqyEQzZDlCgMdixp8qS+bZxp3BPJGwsnpZsHrugh gAxA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=ZX5Wvd0xktos9WBEJLXysYQZfAByNmYU61XZUR2rboI=; fh=sIW6He1KtL7PxzvRy/R6lnlC2JPl7++P4ymw21R5DzA=; b=u7t49ggpgih3f1Y5giFFuND0uReFqyUwQjmJq+sAkSrsHLbYIZUCh7P4P6pRr116AE 8hTa9RKLpyBR+5akSJCQTUPcKPxhIGx0s4Hk3bMPokB4IobGDM0RAE//T0G1Gze9xETq T5V15+KVEvfX2c8Q4MMmp2pyFA5rXUgl3ABn/cyYotUvh3yHUwb0nAIvMC6KYNX0B2wN pWcxgFhDncuSOuGC68Rqqtbe27yyt8Td3goVgQy3eRYZOMmLnbkmFVpKLe5yARe6zvc8 wfcEQdx5v9HdLxIpIS9uGodMJ6NUeERmuBqFKN/Idj5F1a5uA538y5T6ClnY3cmUqyvh vOvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xzGuM1KZ; arc=pass (i=1 spf=pass spfdomain=flex--kyletso.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-119289-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119289-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id b22-20020a63eb56000000b005e1c5c9a11esi9651210pgk.230.2024.03.26.08.31.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 08:31:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119289-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xzGuM1KZ; arc=pass (i=1 spf=pass spfdomain=flex--kyletso.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-119289-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119289-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C4B5FB25879 for ; Tue, 26 Mar 2024 15:19:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BA28813C82F; Tue, 26 Mar 2024 15:19:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xzGuM1KZ" Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95EE512D76F for ; Tue, 26 Mar 2024 15:19:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711466358; cv=none; b=U+486ahaYIGY1hbZqhs3+V8KexSZatfstyd7TNyhAFRUqXqifZzekkDN2TsKUU/ZREag4V9wps42iLXJSY3hF7UfA4xc1Rd4FjInOeO/v0NYKG8dzIjEdnsl8t6ioCz6QdgbQG5TT3X6StFJ1kbi38qsbqs3ZVF+Hatbv6GGOoI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711466358; c=relaxed/simple; bh=SraOnsE2ZK1vLT3c1NA6VI0uTZIh2ButPyGByZ0auQA=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Z2CR0BTyKL4ujmYdrRw2ty0GG3gFbD+jkJm+epLvrze0O5FJZ0ULXsOJwa5AZp/sZGzteG1De29oXVp0pDobTXB1jq+BchmWfbaA5Y0cX4eokdiHlwcFsNbKCNftpsau99ED9JVTHhFimBZBAZHHYGECD+6xqiOiD+1MaxndRzg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kyletso.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xzGuM1KZ; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kyletso.bounces.google.com Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1e0a4e28c06so22166085ad.2 for ; Tue, 26 Mar 2024 08:19:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711466357; x=1712071157; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=ZX5Wvd0xktos9WBEJLXysYQZfAByNmYU61XZUR2rboI=; b=xzGuM1KZb0vhUSloPR47tQ+K1gKAwkakkn7KULQBpl+ezMMid8cwKiYLps3KYQ/mkb M9YObxQcmmvWtqZP5szQoh76lZJSwyAqIzpDL2Y9tA6BQJs/C3V/EJ4OMjFTPCWoiM06 T6QUHjJ+4ycjMnYqgT+AXChbHKw8REBtDl3W/FNHT4ycjSOVYPF4utEEVd4WheJp0TOe UYqaVS4bWwV/d7yemM+VcU2cHlX4GMO0vBYWPwzp8jFB+Lk0SAUnIpEzVB0xnKBenr7n wbucMQ01UIl4qc0v7EmoxSMbYSalV72LfV0e8U+CNKKNCc0oKDzGl6YArBrPG3xd+DvG RGjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711466357; x=1712071157; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ZX5Wvd0xktos9WBEJLXysYQZfAByNmYU61XZUR2rboI=; b=U2I+3g765sRbzUC1Arct3hZhtZdBCxcfDPUtqdD/iVVgXIxAm3H3dHmFHpvZOk6+mA V6jCvHlfmk5iIh+eVmOy1Hp7Q9+ByLLjjfqMtjJ8/67uZOEx4Kt+eypwiSQHHIYMTQsf 4Ifd1c+iFkait3/kPrnV5Ib4fuPyq87138lXctYJUaP/K8snRrQ5ozlIwVf+auoXPiEA SqXhQgaXKHC/s6LsTKaqHJMhIByL6pT/LaMnoYh1u4axXmg4tlKO8HPmxb030iXs9FfA fYOejYwoxorrNMyuaKRqIr26sGklSEqdkx5Z2Bck0VIfQ+WuPNZUDX0ssBMjo2WmcHIc Y3lA== X-Forwarded-Encrypted: i=1; AJvYcCWSr60r3kfoHq4UD97W7RbAhdo1HloZhKnANokSgOEyJ5rt5PuLSurIlbKN2p/g0Zt6Uc5b+1jMuskRBCxEZwuW9GigLOoAUpwR/7Gv X-Gm-Message-State: AOJu0YzmIbh3dDp4MZ3xXzVBzSIggN8HbFSR1eHouPSChpT/uAc6+jer lynE2u6z70gBK1r8GiAqolgYUGJfRt74OnlAzcqnHFMWjwNO7s6YTE8faxtVV5/N7ataEnuQKP+ ZlFWjDA== X-Received: from kyletso-p620lin01.ntc.corp.google.com ([2401:fa00:a7:c:666:5c0e:98a6:f950]) (user=kyletso job=sendgmr) by 2002:a17:902:fb43:b0:1e0:9a7:e629 with SMTP id lf3-20020a170902fb4300b001e009a7e629mr206486plb.7.1711466356714; Tue, 26 Mar 2024 08:19:16 -0700 (PDT) Date: Tue, 26 Mar 2024 23:19:09 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240326151909.440275-1-kyletso@google.com> Subject: [PATCH v2] usb: typec: tcpm: Correct the PDO counting in pd_set From: Kyle Tso To: linux@roeck-us.net, heikki.krogerus@linux.intel.com, gregkh@linuxfoundation.org Cc: badhri@google.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Kyle Tso , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Off-by-one errors happen because nr_snk_pdo and nr_src_pdo are incorrectly added one. The index of the loop is equal to the number of PDOs to be updated when leaving the loop and it doesn't need to be added one. When doing the power negotiation, TCPM relies on the "nr_snk_pdo" as the size of the local sink PDO array to match the Source capabilities of the partner port. If the off-by-one overflow occurs, a wrong RDO might be sent and unexpected power transfer might happen such as over voltage or over current (than expected). "nr_src_pdo" is used to set the Rp level when the port is in Source role. It is also the array size of the local Source capabilities when filling up the buffer which will be sent as the Source PDOs (such as in Power Negotiation). If the off-by-one overflow occurs, a wrong Rp level might be set and wrong Source PDOs will be sent to the partner port. This could potentially cause over current or port resets. Fixes: cd099cde4ed2 ("usb: typec: tcpm: Support multiple capabilities") Cc: stable@vger.kernel.org Signed-off-by: Kyle Tso --- v1 -> v2: - update the commit message (adding the problems this patch solves) drivers/usb/typec/tcpm/tcpm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index ae2b6c94482d..2464710ea0c8 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -6855,14 +6855,14 @@ static int tcpm_pd_set(struct typec_port *p, struct usb_power_delivery *pd) if (data->sink_desc.pdo[0]) { for (i = 0; i < PDO_MAX_OBJECTS && data->sink_desc.pdo[i]; i++) port->snk_pdo[i] = data->sink_desc.pdo[i]; - port->nr_snk_pdo = i + 1; + port->nr_snk_pdo = i; port->operating_snk_mw = data->operating_snk_mw; } if (data->source_desc.pdo[0]) { for (i = 0; i < PDO_MAX_OBJECTS && data->source_desc.pdo[i]; i++) port->snk_pdo[i] = data->source_desc.pdo[i]; - port->nr_src_pdo = i + 1; + port->nr_src_pdo = i; } switch (port->state) { -- 2.44.0.396.g6e790dbe36-goog