From patchwork Wed Aug 28 03:03:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780366 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE6B6C5474B for ; Wed, 28 Aug 2024 03:04:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3ECD86B0099; Tue, 27 Aug 2024 23:04:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 34E3F6B009A; Tue, 27 Aug 2024 23:04:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EF196B009B; Tue, 27 Aug 2024 23:04:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id F3E206B0099 for ; Tue, 27 Aug 2024 23:04:26 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ABFA581D35 for ; Wed, 28 Aug 2024 03:04:26 +0000 (UTC) X-FDA: 82500160932.01.0D2494E Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf22.hostedemail.com (Postfix) with ESMTP id D0B94C0006 for ; Wed, 28 Aug 2024 03:04:24 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mVEbTKA5; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724814178; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=EFN+H9sWuI87FXQaWgiwhxOnqMOIsIPIqRn+/db44+cY7rchhkmFwbNjqVQaGG6tiSI82K 5xZ9FFbvKuBnoc47laxFJRD7f9WExBnLFbolKdcisA0ZU8IEkaxRi0szhwoGqOs1H+GCt9 6CdJC+C2/vq5mOiCWJfzHLjxIO8Mu50= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724814178; a=rsa-sha256; cv=none; b=4gNdgn3UI9Czv5hW1lhZMoA+WrrCn+9rMv7fHL83K9JLOjdpXH7D8pB+iGlt0633l7tZQN oBLU1DNG6wHnz+deKs3+Ybt0Vg4tPCmoyhAjifbibfklyuXr1rBoP9bn99f6EZ1SZw5NK0 tYfULEd9mnq8qnSfOGf0H7fHya9BOBQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mVEbTKA5; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7141285db14so5369406b3a.1 for ; Tue, 27 Aug 2024 20:04:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814263; x=1725419063; darn=kvack.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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=mVEbTKA50OMV4HP2AhdaOLCkL4dfComRgHyJjymixwgMZW4ZUuw7YhNU9D5u2KszwF eh5UxDplWzBvbomllLKpEW2Low4VmGvscDbvgCE6kKHcybKRWphJEH53U4XiHF8gckdq B5rpHs8rrjdMROuDmUhqDRBaDkC0j/my5JSBYRJU3VYrANBQ4riSxMxzVyHM3CXEYSpD +0+5Rn7HAnIenuw4fes1A5RNnI4NG68DZ/SnEo64aVDa22ejroWuS4uYFPSd4mCWzspQ f3WFIVXOXoYJ8dnFdIg+2VZyKvE/SNlEcJkjz0RfMXrSQCN1v4OYFA63Vi6IOpr8VrH8 /LIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814263; x=1725419063; 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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=Ke7Bt0eiqhjCLS3xgQRuMp+6dsY5WLAiEQK+TVRWdkfhpquRFfQK4osRldSoh2nMMK ADn6bAdhkBoV1KFGwHyq9gPov6aaW255NIEA42YGyJSnQcXsnaxpOFL1ZsK3sWREqK4c 3s6XCwvcna056MwKYRh1eZ0TLrrwS/zEXLy7pl+VrnYuhqJ4NaikBSb+iPXv7XzUnVkR ZkKFgqUSPoMKaPyfAEle3KSZ1qzprKnNOaEo0HgB/PkJzLn4tPIZHrcGZiQ3pNZHlbzw UmKc3vwcIxxc+m/RTuitnqKm7SypYrr9L0Eh481uEQ2OSKE4Nj2Y3hVXtuo04RdXG/eH 2Xiw== X-Forwarded-Encrypted: i=1; AJvYcCX58F/I8Ch3zuibZ7RMy1VumNqeAffM/S07tW8AWFPKFLSMSUHwnfrkRzl379HthgNG5zvtfz/I3w==@kvack.org X-Gm-Message-State: AOJu0YxqaBcbTfnV7ilscdu7enQg71GFXnnlrKsItplrhQfsLQDTw1Jq 2JkgnbXArxYt+Aoh883A+tma1zRC4WNf0/2SmWDzFnVkK9LtqHOq X-Google-Smtp-Source: AGHT+IG7QBffnYc0dCLvaTOX5KXK0mJkKlpaXmWfgPhMMu+561lRAfU2Q11Tqx0ug2HVRiltuCcmIg== X-Received: by 2002:a05:6a20:b598:b0:1c4:b931:e2c4 with SMTP id adf61e73a8af0-1cc8b4bd8bamr18005852637.26.1724814263473; Tue, 27 Aug 2024 20:04:23 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:22 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Simon Horman , Matthew Wilcox Subject: [PATCH v8 6/8] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Wed, 28 Aug 2024 11:03:19 +0800 Message-Id: <20240828030321.20688-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D0B94C0006 X-Stat-Signature: wnkug5f598eqat1pyk86hhe7uedk4m1z X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1724814264-810844 X-HE-Meta: U2FsdGVkX1/J+S435fIOy8Z7L9am00qdTPW2BNP/+wBQ3AU0/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+8GVRtCWaj9p5La188g6BL0f7oE9A037R9zonqwsaKi1HSVZlqDO7zxu8yPuGiGCnkw/dQqJGVmpm35/EClPHACHotp0uglAbTBgpHaKhWAZ/3Li+tFfn+oVQRMYVi0MzFWL07ssDSJQXLxP1aSwZDg6ZTfkdwxemcVPFRCzNuJnrWhLoYxybisCYSYpQMRK8y/hZ0J0ceic0d/KueBYSEWZoCGkANPkca8nIMSolalUTYTXiVm2uJ4l9KCv0//TkvsSSVbJUw+h5sUpSR3UNE5TtCefWeb1gEoHE3J/qZ6aj04ULyIFTXug3BmjYoXALT8UANHU71ni/lcDTp+P7LWqhseEI2zoFtne4tK0Zc+XOnghVkpmJ/9Re9NKUf/uC0eS/qXuXjMuuoYXOmjckPQT4rg7NZ51K92K0xrgAQWQfaD0kdEv5s7wvhZ60vzbnjsXjxnmOCjAO52Dr1NvxHJray1+0EY9wYs/Rc87kapv1Lh8EnhCYsYALWLMuKqtGFSLeR+6a7nm5gz59taT1ODVWSKbQc1x/bpES7dxZSJWFDc2qn2JGc4g5j9Vt+t8w9M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox Cc: Alejandro Colomar Reviewed-by: Alejandro Colomar Signed-off-by: Alejandro Colomar --- mm/util.c | 68 ++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 9a77a347c385..42714fe13e24 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,41 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, not including the NUL terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + /* '+1' for the NUL terminator */ + buf = kmalloc_track_caller(len + 1, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a NUL termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s), gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +114,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max), gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +188,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul);