From patchwork Sun Aug 4 07:56:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752529 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 CD96CC3DA64 for ; Sun, 4 Aug 2024 07:58:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F2B86B0099; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A2126B009A; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 442C76B009B; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 25B066B0099 for ; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D4E7514186D for ; Sun, 4 Aug 2024 07:58:23 +0000 (UTC) X-FDA: 82413810486.18.02E6D09 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf14.hostedemail.com (Postfix) with ESMTP id 0BB8A10000A for ; Sun, 4 Aug 2024 07:58:21 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FQ43AOAd; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.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=1722758241; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=lqHpLh80MUWZaJIxsPaTygFYapSoDr/kbKXmXNA1cmr537zeeSiuI8s1BFYhKSWet+n4Qq Ql3RUaF82HTVvZq90nlFlHQGiFieJKoGADCFBO6Jslnm14rUQwraZgB+sQJi3oYGb6cXE5 Jx80AtxoCotGRlG+oyAVCiDQ66C84Ak= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758241; a=rsa-sha256; cv=none; b=LtQ68eYMzD4CJAym/+mJ8xWboJncN3SYeIpydo2ddeqSEWJfXGTX+Gasab9vTHmnwHbuRP Q5H3407R38wR2H5AL4nLNwm7qILSzgcnWj94jzj4THCRf5Zg5ItIxiYsIyPnqnmD9bAAVS P5EpqqsqLSpyNgmuFhIgXAdzMqoqG8o= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FQ43AOAd; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1fc56fd4de1so32922355ad.0 for ; Sun, 04 Aug 2024 00:58:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758301; x=1723363101; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=FQ43AOAdpi2uzZotkoLKrRsUk0l/+gIn4UlYSXtC/AEC9RA4167dfCGZjteICzIG5z oXePdrhrMAhyFPumUacyEjjKR9IvFLgSlAMPxSN1/1XsEkdJYF4I7B8fxOPWFxRHT8GK nM6EKnAhQVuyshHBR0XoePVda9GN0HHWIx3h03xTHLCmMhU7wAWCdfLs3D60y0uwP8nq mmthlUA/pCuxmNNVmK47bux7URkRzYlUGQLHqB/qFDaOSXGhh0KlsyClbiBwYmQlbTb4 6LP2p4vsUl7yo72LvD3N564yuzDDdrAYFr4MJ3+yBIoNYYlAKwly7xdTpsdwPsGWWQiR y5Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758301; x=1723363101; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=GtHrIGdNFBrxNjJNu3fzs8W8YAg2TyxQDp7hqUm6tg66Ay5nZhNi7MBz2UJIrcZZe2 VP5F41W5vg1GA/UHwjrCzJDMrZVJ420VTChITO8I7VUbYNnUuzmXZMk/nE5DWGyacy8M azpLC7sFhvbqEnTzyL0zPfd2Pzq0NwDOBWbGG0y9ZjQh3HFKvb2Z7jy7n/M5HVntGyV5 CYCM3QByh4tNOlJbGnDtSCWqf8AgxOsdRB+Rj2olz3mcR94G5DjrOyq2QnnRoQbVnJv0 DDEccJhkLgf+z7tgsyAabm3x0veRj7uUsTWJazjsjc/Tz+GQEakPvZbeuxl3y1ddxc6S OQ/g== X-Forwarded-Encrypted: i=1; AJvYcCUQ2usXKhFJnpEkfQOOTYI67mTBCIZSXrspF1K5WGsYmFzCQmdDm6lSkoo7DBqHlnFdTpRUyqgEecy51G4Ze/yOWd0= X-Gm-Message-State: AOJu0Yx5+/IbWzVj1Q4KibfOR2uvSiwOSwN0/pUAe9o+MRGOz5LyIq1Q KAS+rV1u8m1OKXB/tsrG+AhLIUyuHjFN0Vj/8mXThnAOem84Jcxpc1veg2acI1w= X-Google-Smtp-Source: AGHT+IEGtCQCVMnQCKfJ0+BIkhT35+DpDH9a54ylBrsi080HAExdi6zY8c1ZJxWy2ZwMLSFkL3dmww== X-Received: by 2002:a17:903:1cb:b0:1fd:7ff5:c673 with SMTP id d9443c01a7336-1ff523ef0dfmr156564885ad.2.1722758300691; Sun, 04 Aug 2024 00:58:20 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:20 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, 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 v5 6/9] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Sun, 4 Aug 2024 15:56:16 +0800 Message-Id: <20240804075619.20804-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: tpzggihyu4ruqr3u6zt14p6wq8zx1h4g X-Rspamd-Queue-Id: 0BB8A10000A X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722758301-513349 X-HE-Meta: U2FsdGVkX1+lNGn23Y7wTbHIF2i3acuvfmtCNcbnrDa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NIKu8FSo2bc36fo0doTf4EBlu2EryIym85VNOGLqvAh3eTHQ+hZc5POC+tUgmW9nS3nvUWjfPD8LJHUpgo+7EuufgpMaKkvzLK9rqq2KxM2N9i7RrSquTU4sbLs/ABwCUOlp2TQk20T45vfWon0sQXXjFnni6xBl5P1MxcuUwH0Ccl8WCk6nvr2suWf2Sh+FQHoepWRpoFA8UQSUclcAq7sGxKfvoERJ6tBZpeyJA5ZHpW+QLlqHH+i78PF6s059PZ8K9e3LlLA2sLfoPSfDqqjzLm5e/x8yHE1KehHLbAjW6KlK9GN/YoCn5yemXikmOIbg22jNmUjWM7i/umrwKOJl1vZnTSu54GaSySDsw6B+PvpCVPkwSUHPrNeKLfkM3CVvZN/aym3UsA5aaxgcMelEAvUL8fxki81OkNa++PVpZMb6/Wa3IWNCsMGc9PVqfVoZZUFDEejit2fTgjYTAAvyUmJm52vpntU2CgF4h7XGuRMtQMSNcf1Awmct/sml7yoDLtuX3Trh/NoC6pp4ikWV+aFM1Fc91JxKwb0UwUI= 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 --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 912d64ede234..2c5addabd6f7 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,40 @@ 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, including the null 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) + buf = kmalloc_track_caller(len, 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 null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\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) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +113,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) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +187,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 + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul);