From patchwork Fri Feb 1 20:50:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Kozub X-Patchwork-Id: 10793727 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 406B514E1 for ; Fri, 1 Feb 2019 20:52:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3117F32F65 for ; Fri, 1 Feb 2019 20:52:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2599B32F67; Fri, 1 Feb 2019 20:52:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0CC032F65 for ; Fri, 1 Feb 2019 20:52:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727490AbfBAUwU (ORCPT ); Fri, 1 Feb 2019 15:52:20 -0500 Received: from mailgw1.fjfi.cvut.cz ([147.32.9.3]:33734 "EHLO mailgw1.fjfi.cvut.cz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730650AbfBAUvh (ORCPT ); Fri, 1 Feb 2019 15:51:37 -0500 Received: from localhost (localhost [127.0.0.1]) by mailgw1.fjfi.cvut.cz (Postfix) with ESMTP id EE5D6AD137; Fri, 1 Feb 2019 21:51:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fjfi.cvut.cz; s=20151024; t=1549054295; i=@fjfi.cvut.cz; bh=Abc6GojjSXE89XOADbuV1mdGLYBKa/jJ6yDNDtkPZas=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=icDrZHcWgC8ViQtzkhTh0I5SJzs0pZkZMJHn2LprE4R3NMDRgPKal4p8v2+tAibdc eD0p+iUjjc1A2sHcOJ11a9UePcsY9cJp63bcNgMjVa1UEvw5zOdWgGiydF79SlrGny bvRZmwiaWSuK4H91+vWDtZyD4VmzCRg9eIdPwK8o= X-CTU-FNSPE-Virus-Scanned: amavisd-new at fjfi.cvut.cz Received: from mailgw1.fjfi.cvut.cz ([127.0.0.1]) by localhost (mailgw1.fjfi.cvut.cz [127.0.0.1]) (amavisd-new, port 10022) with ESMTP id DDpKuiZ6okwe; Fri, 1 Feb 2019 21:51:26 +0100 (CET) Received: from linux.fjfi.cvut.cz (linux.fjfi.cvut.cz [147.32.5.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mailgw1.fjfi.cvut.cz (Postfix) with ESMTPS id 3A146AD10C; Fri, 1 Feb 2019 21:50:52 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 mailgw1.fjfi.cvut.cz 3A146AD10C Received: by linux.fjfi.cvut.cz (Postfix, from userid 1001) id 136646004E; Fri, 1 Feb 2019 21:50:52 +0100 (CET) From: David Kozub To: Jens Axboe , Jonathan Derrick , Scott Bauer , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Jonas Rabenstein Subject: [PATCH v4 09/16] block: sed-opal: split generation of bytestring header and content Date: Fri, 1 Feb 2019 21:50:16 +0100 Message-Id: <1549054223-12220-10-git-send-email-zub@linux.fjfi.cvut.cz> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1549054223-12220-1-git-send-email-zub@linux.fjfi.cvut.cz> References: <1549054223-12220-1-git-send-email-zub@linux.fjfi.cvut.cz> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jonas Rabenstein Split the header generation from the (normal) memcpy part if a bytestring is copied into the command buffer. This allows in-place generation of the bytestring content. For example, copy_from_user may be used without an intermediate buffer. Signed-off-by: Jonas Rabenstein Reviewed-by: Scott Bauer Reviewed-by: Christoph Hellwig --- block/sed-opal.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/block/sed-opal.c b/block/sed-opal.c index 4225f23b2165..4b0a63b9d7c9 100644 --- a/block/sed-opal.c +++ b/block/sed-opal.c @@ -586,14 +586,11 @@ static void add_token_u64(int *err, struct opal_dev *cmd, u64 number) add_token_u8(err, cmd, number >> (len * 8)); } -static void add_token_bytestring(int *err, struct opal_dev *cmd, - const u8 *bytestring, size_t len) +static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len) { size_t header_len = 1; bool is_short_atom = true; - - if (*err) - return; + char *start; if (len & ~SHORT_ATOM_LEN_MASK) { header_len = 2; @@ -602,17 +599,27 @@ static void add_token_bytestring(int *err, struct opal_dev *cmd, if (!can_add(err, cmd, header_len + len)) { pr_debug("Error adding bytestring: end of buffer.\n"); - return; + return NULL; } if (is_short_atom) add_short_atom_header(cmd, true, false, len); else add_medium_atom_header(cmd, true, false, len); + start = &cmd->cmd[cmd->pos]; + return start; +} - memcpy(&cmd->cmd[cmd->pos], bytestring, len); - cmd->pos += len; +static void add_token_bytestring(int *err, struct opal_dev *cmd, + const u8 *bytestring, size_t len) +{ + u8 *start; + start = add_bytestring_header(err, cmd, len); + if (!start) + return; + memcpy(start, bytestring, len); + cmd->pos += len; } static int build_locking_range(u8 *buffer, size_t length, u8 lr)