Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9473501pxu; Mon, 28 Dec 2020 17:50:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJzmObClvvhOF5BBFkpv/3bpulgnkoqe+JVFuo7WWoRMxjNnQl94D3s72gLA4nGusSdAl00S X-Received: by 2002:a05:6402:3553:: with SMTP id f19mr30883018edd.129.1609206618265; Mon, 28 Dec 2020 17:50:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609206618; cv=none; d=google.com; s=arc-20160816; b=uZPcjw8kNI9zssDIXaXNc7/2tFiRTGiiSIgzURs/VoWfP9shczjV0U97VsjffQUlF3 9ybRuj9S1Q6bIJ8P+8lay0hPpDXhVNIsZ9MAW01kZ6WAcpHpMhIO96lYfY3SoOgmnQOF fl2YH271yBDqkoBvJSJ9fAKdc+7R76qqozlLK1dcwqL0Che6OtiJcl+WmRHii0uWGcXy Kk9FB47u4a/HZX6XQ+nop/m50wg81uCc+TzxQfZSXe+t9oCwUEvARoO8gzkzEK8v3d3x LReX+wuxOn2ptHXKrswDlp1aOamRSA3sPPHyRrhGdcSfojBuwptFZHuEFOPK5WuiRgKe Hceg== 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; bh=YWj57rhQqwU8Xuipg7jPwyBNmUNSo8j4Me/entcEOKU=; b=ovD80EL/1tt3d92wHSPXemS9u6K9Of1Bw5PZBPjd/eStAuTDi1lTFboP7lrxPEObZD WwKvKfOPA4NScPOoAwIjfT7Ymrz8wnNJmiUDvkEHjisjZcfGQdbUFjtW5E64DEY5tAIZ 2o9nu+QgfuBmJiNHEHqRUbzHGQbttnI+FamFwF/xzw1zYJbT/cKoT7KL3PvpKgjK8Qkw KcZ/pBa49oUOKfZmm/VKtB70jFm9t2WyewgN36FwaMdFc5nUe3qIh8ttj7g7OLoKrWsp 0L9tEINboTjErfngosZWJXB5ODsYwmqoffcvSVAecCx5IfLmk9tNg2pfg3xtv8IcglcI CQ7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 97si21199044edr.29.2020.12.28.17.49.56; Mon, 28 Dec 2020 17:50:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730171AbgL2ABC (ORCPT + 99 others); Mon, 28 Dec 2020 19:01:02 -0500 Received: from smtp.infotech.no ([82.134.31.41]:59795 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729046AbgL2AAR (ORCPT ); Mon, 28 Dec 2020 19:00:17 -0500 Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id D4C05204238; Tue, 29 Dec 2020 00:50:08 +0100 (CET) X-Virus-Scanned: by amavisd-new-2.6.6 (20110518) (Debian) at infotech.no Received: from smtp.infotech.no ([127.0.0.1]) by localhost (smtp.infotech.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yOsrXQPjSrST; Tue, 29 Dec 2020 00:50:07 +0100 (CET) Received: from xtwo70.bingwo.ca (host-104-157-204-209.dyn.295.ca [104.157.204.209]) by smtp.infotech.no (Postfix) with ESMTPA id 423E4204237; Tue, 29 Dec 2020 00:50:05 +0100 (CET) From: Douglas Gilbert To: linux-scsi@vger.kernel.org, linux-block@vger.kernel.org, target-devel@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Cc: martin.petersen@oracle.com, jejb@linux.vnet.ibm.com, bostroesser@gmail.com, bvanassche@acm.org, ddiss@suse.de Subject: [PATCH v5 4/4] scatterlist: add sgl_memset() Date: Mon, 28 Dec 2020 18:49:55 -0500 Message-Id: <20201228234955.190858-5-dgilbert@interlog.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201228234955.190858-1-dgilbert@interlog.com> References: <20201228234955.190858-1-dgilbert@interlog.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The existing sg_zero_buffer() function is a bit restrictive. For example protection information (PI) blocks are usually initialized to 0xff bytes. As its name suggests sgl_memset() is modelled on memset(). One difference is the type of the val argument which is u8 rather than int. Plus it returns the number of bytes (over)written. Change implementation of sg_zero_buffer() to call this new function. Reviewed-by: Bodo Stroesser Signed-off-by: Douglas Gilbert --- include/linux/scatterlist.h | 3 ++ lib/scatterlist.c | 65 +++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 71be65f9ebb5..70d3f1f73df1 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -333,6 +333,9 @@ bool sgl_compare_sgl_idx(struct scatterlist *x_sgl, unsigned int x_nents, off_t struct scatterlist *y_sgl, unsigned int y_nents, off_t y_skip, size_t n_bytes, size_t *miscompare_idx); +size_t sgl_memset(struct scatterlist *sgl, unsigned int nents, off_t skip, + u8 val, size_t n_bytes); + /* * Maximum number of entries that will be allocated in one piece, if * a list larger than this is required then chaining will be utilized. diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 9332365e7eb6..f06614a880c8 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -1038,26 +1038,7 @@ EXPORT_SYMBOL(sg_pcopy_to_buffer); size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents, size_t buflen, off_t skip) { - unsigned int offset = 0; - struct sg_mapping_iter miter; - unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; - - sg_miter_start(&miter, sgl, nents, sg_flags); - - if (!sg_miter_skip(&miter, skip)) - return false; - - while (offset < buflen && sg_miter_next(&miter)) { - unsigned int len; - - len = min(miter.length, buflen - offset); - memset(miter.addr, 0, len); - - offset += len; - } - - sg_miter_stop(&miter); - return offset; + return sgl_memset(sgl, nents, skip, 0, buflen); } EXPORT_SYMBOL(sg_zero_buffer); @@ -1243,3 +1224,47 @@ bool sgl_compare_sgl(struct scatterlist *x_sgl, unsigned int x_nents, off_t x_sk return sgl_compare_sgl_idx(x_sgl, x_nents, x_skip, y_sgl, y_nents, y_skip, n_bytes, NULL); } EXPORT_SYMBOL(sgl_compare_sgl); + +/** + * sgl_memset - set byte 'val' up to n_bytes times on SG list + * @sgl: The SG list + * @nents: Number of SG entries in sgl + * @skip: Number of bytes to skip before starting + * @val: byte value to write to sgl + * @n_bytes: The (maximum) number of bytes to modify + * + * Returns: + * The number of bytes written. + * + * Notes: + * Stops writing if either sgl or n_bytes is exhausted. If n_bytes is + * set SIZE_MAX then val will be written to each byte until the end + * of sgl. + * + * The notes in sgl_copy_sgl() about large sgl_s _applies here as well. + * + **/ +size_t sgl_memset(struct scatterlist *sgl, unsigned int nents, off_t skip, + u8 val, size_t n_bytes) +{ + size_t offset = 0; + size_t len; + struct sg_mapping_iter miter; + + if (n_bytes == 0) + return 0; + sg_miter_start(&miter, sgl, nents, SG_MITER_ATOMIC | SG_MITER_TO_SG); + if (!sg_miter_skip(&miter, skip)) + goto fini; + + while ((offset < n_bytes) && sg_miter_next(&miter)) { + len = min(miter.length, n_bytes - offset); + memset(miter.addr, val, len); + offset += len; + } +fini: + sg_miter_stop(&miter); + return offset; +} +EXPORT_SYMBOL(sgl_memset); + -- 2.25.1