Received: by 2002:a05:7412:bb8d:b0:d7:7d3a:4fe2 with SMTP id js13csp1854109rdb; Thu, 17 Aug 2023 04:16:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTzURAbMPejg7k+6HIk5jsTGdIp2nBFfQWfAFEZf+Og2opovb3qMns5KKlaNSZe3R6LPJq X-Received: by 2002:a05:6512:2508:b0:4f8:7055:6f7e with SMTP id be8-20020a056512250800b004f870556f7emr4342246lfb.44.1692270993209; Thu, 17 Aug 2023 04:16:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692270993; cv=none; d=google.com; s=arc-20160816; b=hCT1taRH2243JOErbLnfq7DfsXYXiWJSW4ruqfXWLxC8+MhXxgjuYqI8wi5jzLjeNF 9thuJAsXfxFQO/qWBiRR4RWQlo/wNbJN7dh5F25TaY5mIX+UkGpYbpeFbM7hmd6vHaJc Jqt6BEHwpnlonaDOHcBeKqhyGQCKz3X0KHiuYegyGKM2DpTMbeydooLh7tNrUxycochu /P6/cTJxCecS/JBRjkTn/IxmFzhKFFQpoQPC/KP8mCWJb8zQVbbKbUN7LBAtY8WRyoqu mYxp3cnBY/sckaW/mrR9X10kpRXIIINOhtc0Ae9cwDEuEqDVOKALEOh1CTVT69Tuu84H f1Lw== 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=Ypn/i5Ce2Sqi5P+rPXAMIuSLfx2AOyF59htK0274gqk=; fh=k7XzfpyqnKqnwxpqVF0NftrWa2FIpeAcHVbykF5xarA=; b=bSG279f3rLGRdYXalk4MuZz79dj1C2cS21xh0kf9VxQj6es9/gElXdMVNAxXvqqWi/ PVYl0YoADn6jWr848hglftuCFoO7qSTu1Bv2tW0mdxCkxgcckLFsI180ZY5TIUJ1q5eR 4XI3FqTqaI0vmlLUQ4O1DSZgntr1nOS0kNGob/8wUvcviYLY1Nd2XqkDC5m7tPwugB8e E9z3mJsIkD2xDrzvoPwquF1sOqc4B53mTSRq8SH5sSVa98Gz4nu3t4+GRQR//uUZu0iw lPOVLie4yS1/LHQqHelawuikL4zQsD7AOY/aQfl4gvqGwqnS6JlHutspyAoXsxZ1XF7l QxEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YBgLjJ19; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w10-20020aa7d28a000000b00523caf5cea7si10143112edq.520.2023.08.17.04.16.06; Thu, 17 Aug 2023 04:16:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YBgLjJ19; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244449AbjHPLAg (ORCPT + 99 others); Wed, 16 Aug 2023 07:00:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244477AbjHPLAV (ORCPT ); Wed, 16 Aug 2023 07:00:21 -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 ADB2D2D4B; Wed, 16 Aug 2023 04:00:05 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 96EF5665FB; Wed, 16 Aug 2023 10:58:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E660FC433C9; Wed, 16 Aug 2023 10:58:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183533; bh=ESSt5X2RRzZnpsf5+yWnfafc+UieZd3b3Vb0DvJ2wvQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YBgLjJ19B9relkf6oHCdOzSNkolQuM0T/Y1vfecX/XUUmB2lPLpERnq3Wodxo2L7k lAGd3NVUV05WsySW6kS8qOI54HSdL0748jTcUtxW7g1HutSw0q6zSltofE5zPS1PLc a87I4bH00FlLUmypXenFRHNNJUJG9hNxPVyXA2LEbzrLXZBw41gojwEyOB9XVjqMZt C+5ITKT/v2Nntf9M+c6pWA4a3qFKtgjWNWvxHysZJlR1s/8jFo9f5sOEoRPh3zhPET QbukmGiDnKp915asfd6W9Xx9EtPfCGFWXtFkv0otaCr36FqKE5bWHaZ0nQkvB16AHN Qq7SPGDCJ353Q== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 14/14] tty: n_tty: deduplicate copy code in n_tty_receive_buf_real_raw() Date: Wed, 16 Aug 2023 12:58:22 +0200 Message-ID: <20230816105822.3685-19-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The code is duplicated to perform the copy twice -- to handle buffer wrap-around. Instead of the duplication, roll this into the loop. (And add some blank lines around to have the code a bit more readable.) Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index d4d00c530c58..646c67e1547b 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1528,19 +1528,18 @@ n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp, size_t count) { struct n_tty_data *ldata = tty->disc_data; - size_t n, head; - - head = MASK(ldata->read_head); - n = min(count, N_TTY_BUF_SIZE - head); - memcpy(read_buf_addr(ldata, head), cp, n); - ldata->read_head += n; - cp += n; - count -= n; - - head = MASK(ldata->read_head); - n = min(count, N_TTY_BUF_SIZE - head); - memcpy(read_buf_addr(ldata, head), cp, n); - ldata->read_head += n; + + /* handle buffer wrap-around by a loop */ + for (unsigned int i = 0; i < 2; i++) { + size_t head = MASK(ldata->read_head); + size_t n = min(count, N_TTY_BUF_SIZE - head); + + memcpy(read_buf_addr(ldata, head), cp, n); + + ldata->read_head += n; + cp += n; + count -= n; + } } static void -- 2.41.0