From patchwork Sat Aug 17 02:56:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766928 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 8D872C3DA4A for ; Sat, 17 Aug 2024 02:57:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EBD26B0257; Fri, 16 Aug 2024 22:57:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14D3E6B025C; Fri, 16 Aug 2024 22:57:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0150B6B0258; Fri, 16 Aug 2024 22:57:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D7D4B6B03F8 for ; Fri, 16 Aug 2024 22:57:44 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8285EA048E for ; Sat, 17 Aug 2024 02:57:44 +0000 (UTC) X-FDA: 82460227248.30.F8A6ECE Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf02.hostedemail.com (Postfix) with ESMTP id A82488000B for ; Sat, 17 Aug 2024 02:57:42 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pl2NAGtv; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 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=1723863448; 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=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=00JnLdgUThysgTa/0WGkmh3kr+2NOke2XQlbI5bjARTgsBhKQjAkoQkuZOQHJWdFuRaevH ANVI2Axq+argF1+DB4g2CMvPmGzZfKEP0QUsl6BhCnn0SSF0JsTjAfzOlRxscMhrpn5Ea6 +El0Hk62sEnUatsoquXYi9BizbQcFS0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pl2NAGtv; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863448; a=rsa-sha256; cv=none; b=54/R8YLQaozWs+rr4qX/Yq8wBVWroiSw4GBXpEU64UsmTAqgVxhFpICdFNfmcjtISUOvPf mHN70EsslEv8JTkLFG4W8Im4Kl128IWihS+ma6brDt+lvKWqsnJup5g9/JHFcamrMX2Uj4 ZcsGAtbQKIDDWjZdsm6SlGA3D7Tqq2Y= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-202146e93f6so9373785ad.3 for ; Fri, 16 Aug 2024 19:57:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863461; x=1724468261; 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=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=Pl2NAGtv90CUuzeJl9Nv2LL4z7rKFkfE5xAWNl5yHompDjFjxWbJ1rVhuFOaPgydlD 9Y6axcVd6GWojIWYl65lckOLhAFWhTtgYqwzhbSUvmJnpzpDpfp0SRoSJz4PJx2k+Tq2 UaPS4Ifz8Enc7q6rcuzOBC6wo2UmT9nbdxe8HOT/CPdvNHgjfes1DcK2u3MHzJZyRzRo uRIuFKLawxa3owlxKp5u/PCmpTNkSsWfxZaMJG64WNTXk/3VZOW0QcNqMN3rZEqXP6X1 E7ROqP4A6w17rtPfY6qTaci+n7lmNMjeoBoJJfr4bWTkLbuFr0c2zJvjMfAkHNldX/Uh NL4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863461; x=1724468261; 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=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=IfCggAr1dmF9jESdIuKLd0wOtHl/9VJWvrgSuSL1g5pA5ewGHuyclfrEaojxHcNTYi Aq/deWEdZeWSII0s6DasohO2XB4ws+NsjoDu8ZmwsHKxVJwwn92kfeJuxTAhlx77vCFb 5EI4rq8nCvmEiNXJbgpOn04J4vNJGPQPsEmzOLzoC+hcVuxV+ODuJvhk1v63dkznW16p Y7tM0NYQ6HfKJkFP2R44VJG96CMQnqjdCREgL5lVWdjrH70+Ice2vYFRyVpSDUFJqtFp nQxc/ENKkEt3ZvavAK0Gm325gWF0siGfq5uVsSpltjcI2KCj0FWkHVlwLgVR8VonTUx3 4SUQ== X-Forwarded-Encrypted: i=1; AJvYcCXsml/t9QLkQZc4MtyjHJNc8CI1kJUowo/BQnKtrG9fHPwHAdIXHDWYcUYLsm9wqhrETPwi1drSS5AAfNP1Ou7F5cw= X-Gm-Message-State: AOJu0YxGL9zCoFyYGbhH5kG1LoLBRpWx1OBXnjveVu+1GBWzPIsPpokF p9QkGyQQcErl4904RKKbLOjoSJ/PNNDQhQDuLL1OdYolZRmI/x4G X-Google-Smtp-Source: AGHT+IEL7YdwJrnU7j+3S0kOHwdlHJumXPs1OdWAsSouquvh+afE8cHfhlG2TFnMxzOYJRDIWJjJ4Q== X-Received: by 2002:a17:902:e549:b0:202:100f:7b9b with SMTP id d9443c01a7336-202100f7d6amr36403685ad.35.1723863461337; Fri, 16 Aug 2024 19:57:41 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:40 -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 v7 6/8] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Sat, 17 Aug 2024 10:56:22 +0800 Message-Id: <20240817025624.13157-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: h6tzc4yt6w6fzjiygtp8bmhcdry6mo1p X-Rspamd-Queue-Id: A82488000B X-Rspamd-Server: rspam11 X-HE-Tag: 1723863462-429933 X-HE-Meta: U2FsdGVkX19HQfFXOMMh4bth0yU59fJskBO+6viodeLIsRNTeb/XKEAj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EuHatHwV8VDm4Y2nhAAxHAS/xtc6m1FL0RLtonK4YYPpKD0nkvX0YwdD/crsUDse528ImXuOwtaT1z3XJOcopzu/1ZUsjytYojmJs6xjI+A1EZR8yKMhzDX/+XRRxnq0IzfeqANFEW4ObRv3Lz4rWxAoeSFuhwt5qtNILXlXpBPBFJ/WgeQS/eCFmuFPl7bDEkIbzujpoPtAyMsVDW14QHUq75Gw9KxJe2pwSMvQJm3ecFmBKu7Ij801dn/SERDHVfVkdqbYAb0hCu7yX6KRDNaOrg/xHN+YOO/rm+J+olPpF8Fl+MLmdZlH9hhK06Odh5VFHnOrQI2rLKKw6qAPmeoJdncjoRTrG+zZFZN65wDnAu26XyklbGx8Jo5GsJM8RML1ji9aqnveIhahd4MjmIx8D0TnwgqPrt0NOT3nOuoK/PDdP7j3akpEYLiI7q8hYbootnbAYw0r5rFWMgqY+hhkuUlSs2G4W768RXsUUu/IxZoJ/WjMifODnYAERFv8QQHMxD4y4axk8TnkTMJ86D/5yX+XMOMtT6nqX7Mheo7BorBU3ay3V46yzaS7goqaEJ/lQwrKNQeUHB9OimU9jr1fuEAo6YaaC3UIO0F2PHkt4ugc5CM4 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 4542d8a800d9..310c7735c617 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);