From patchwork Mon Jul 29 02:37:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13744268 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 AA94EC3DA49 for ; Mon, 29 Jul 2024 02:44:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1816A6B009C; Sun, 28 Jul 2024 22:44:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 10B826B009E; Sun, 28 Jul 2024 22:44:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC6D46B00A0; Sun, 28 Jul 2024 22:44:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CE0306B009C for ; Sun, 28 Jul 2024 22:44:32 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6A24F1602F1 for ; Mon, 29 Jul 2024 02:44:32 +0000 (UTC) X-FDA: 82391246784.14.22474BD Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf13.hostedemail.com (Postfix) with ESMTP id 8998120005 for ; Mon, 29 Jul 2024 02:44:30 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IWEBgque; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.179 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=1722221017; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=QAM1VBUY7DNR1lZGe+1Vw2pyzaX5qBS9VFULGx83CKS6Lud4NuYeuIt17oXZCAPHhdkVkk 0+2s/HRAcrNgPeGTpHe+mLdWa12BsSOQ7IFFQ0ugBX0fppzHiy7TlDDAw3VBUUpvhMUqCc znTVsDdEhYBdBc2Y+nVBRw8qIaXB7Us= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722221017; a=rsa-sha256; cv=none; b=6JX+tsCPgi5nYDmdlkXGwxKfZUlFExMnIBL5zEv1aFclWQ0foTgirSB4OfALjioEEfQCYi NSbiNbcmH4TL+Gn3ashLc0UL/WNFoMTjIu+ILLpQ+Pimp1xPDkM+BwluhskWLxQa9oJsgp A1BqXOXVAantTJajE73g+OHQ/P5gODw= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IWEBgque; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-70d19d768c2so1926184b3a.3 for ; Sun, 28 Jul 2024 19:44:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722221069; x=1722825869; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=IWEBgqueO8/h28dzI1K/+xr54RaDob5FRCPVNZGE6IG8SOpUBsNZYkOTzdy9EAIdVv yL1fIAAuG3JEXxiHJenrtkEz+nHzBRjAThYKsa/z6axIpXJOcsNJCfCO9rUyxFs89p90 DKbiOa6nF8VFjrITmjarYd6Xw3iW9/s3HHXLTiEb5Ef4OHtVwx/0G+ZTUwSBLis6F9lu 8bbIGaL0JL8w1iTxdFpilubN0jWgCd9ppC0leptBHl6mJ/mpdWdKkx9FyaWZt7EnJnsW KTmOVLwq4zRUk8ZdG+/Gmc3upZ2opJSVdMFwjEEwbqZbnyOKLJVfcRnTFgAibyn3x6Pf j50w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722221069; x=1722825869; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=fo+XN5iGAJ6n3AEhmIT4/Bv+Mt/3rvLHf4r8FNsJ/hlQqM4eL9fcFKgYwZIXeCNEDK sEagbUGLAdssxk/d3V9dm0Yp6pcqXtL79kcxBOgF3EX1i6hKhS1Cp3oCRRTvCJ6KybkP ZtIo3rK+SSBxx1K9lcKqkD5JOIcFrJAPhpq496rJejqQjACEw89vOR/Ej7koVmIiPJpV wTiCYPRpvAOaKBicACzM7UaAFrkKn6/vLoeXcLmy3HWmBHyWdK0TtsiiQRX+VZ1YiXSA /bnPOKvIum/86PZE/mJdFPAFkSdMkwCd2r7n8vaROVzP1fc3rA5Do/7+EZ9C3vqfv/sh QAGw== X-Forwarded-Encrypted: i=1; AJvYcCVr6aFYHQkj8ECO+6h4MsqEgx1rdMsoFdhx5gqBwFHMJvCz9Uva44MSS8LkNgOO118IVElMpQmK/Ujo8dtF0MChC8A= X-Gm-Message-State: AOJu0Yy9ObE+1BosGKxJyT5Jw05L9x2L5dBSoYNpwOyFQfdJnypdKunY lZ/szPmbYZikMOY7giM6BslkBKiyoZ8BONP+j95uT4i66bR4Te0E X-Google-Smtp-Source: AGHT+IG7/eKYXg7QfYOb1CdYS0BMtkGw6Mkjs56lIRKp/S/546Q1z5Hj0Wkc3TWWQHawrwJ+z6MQVw== X-Received: by 2002:a05:6a20:1582:b0:1c4:c7ae:ecea with SMTP id adf61e73a8af0-1c4c7aeefb4mr1212793637.11.1722221069087; Sun, 28 Jul 2024 19:44:29 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.40.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:44:28 -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 v4 06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Mon, 29 Jul 2024 10:37:14 +0800 Message-Id: <20240729023719.1933-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8998120005 X-Stat-Signature: iz3sbztjz8njsxakh49ndsut1c5iqgue X-HE-Tag: 1722221070-716670 X-HE-Meta: U2FsdGVkX19PQNKgRoJnNql99MCCgr6ROMR61diAaA7QDgszLEFn2K+H68pjeqAalLf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ARi9mp3hHLQoIQ/iNgS3lOYIqUSMCkIJCoQDpSEOsWvdHuxGKNGaqBiPr/UwBRQGPXfANYJIDLTYj3BikBs8Iqach3Zvjb5si1oOdCFqMsEOLMPw8OdS7ZgqPgf9eHCnQp40bombIIQBKtzrn7BF3k/5UjS2neCC+r+xcwSbAv7MSpyIkiTYG5+afEDsq7eHVnHBEjDAa2aWnYqlqcQ4X29XqL5dY3UmZf7DND0ZQTXpC1b3Mn7uUTR1GiB6un2PxVZVku99uPzvwuy8fCx7LumGAaBMuZZPLbMttWDruaFMSXnqkUgoMq+eSMAX4+4MfWq1HOBJ7ferjdJaRX1mdUvYSE/W+tK0AFBxJXPGIIJ1BLkcAKmOCHWJ83Ew0QO6jvUx4PgqO6npR5rUyBI21ouppzwOnwMGESbakHV9JHNK//2+yMJ/Q4LfZvde+umnyTFsCOid/Jfn7llyEQMmeJqQQ+Rcq4eTBWNdPg6ErCR2s4mq9Ja+rDmyDqs8yWXNpjsiNGpFlhGyU= 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 41c7875572ed..62a4686352b9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -43,33 +43,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); /** @@ -104,19 +111,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); @@ -190,17 +185,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);