From patchwork Mon Aug 12 02:29:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760070 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 6A286C52D7C for ; Mon, 12 Aug 2024 02:31:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C700F6B00A5; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BD1616B00A6; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A71D86B00A7; Sun, 11 Aug 2024 22:31:15 -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 85B5A6B00A5 for ; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3527E160778 for ; Mon, 12 Aug 2024 02:31:15 +0000 (UTC) X-FDA: 82442016510.19.ABA307C Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf05.hostedemail.com (Postfix) with ESMTP id 5C25C100007 for ; Mon, 12 Aug 2024 02:31:13 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mzNa3V5F; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 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=1723429819; 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=PQtBz2E+Ey9ef4jNlDo45Bbfr9uECTrqZncYj6mR/qCGh5i5jmKffgjdSz8m3w5ec5+YiC LxlhBhMHExJOCC/XInXjXgW5cnVgBEM6yYrVX698pFpYFtKBZsuqnVx6h5VtAFTTZVdej+ Bkzvq7otEyt5FJKzzbBFwMevwRcHPVA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mzNa3V5F; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 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=1723429819; a=rsa-sha256; cv=none; b=B8oCf+DhMcLbBGhFrxT35fGPxtGVoTTkaE/VoDBCwKmQesLfpSaPMSVVS57/uxQXsbCt1I ZIBhWyMCPJRRnVTToXne/1GnO6dzaN6gX0BI/+U1CAxK1Aqf7TnmAHJulpPr5GDbKpU9VP G6qenwusDCnPhfeImVmO4VZ8ZjrTKUo= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1fd66cddd4dso37874385ad.2 for ; Sun, 11 Aug 2024 19:31:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429872; x=1724034672; 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=mzNa3V5FX1bH1l5FCvC4j6KcB2rI5vZSphxoqkF0xsJ+teaXAT1CzIfoNJlbF/aFoD PDvIWGJS6eJsBKkJmDbVUHfzbUpmbd0fJXJty/PkA1gcDDMpwOH3mGi/z+UPni5gfh6k 2wa3RV7qkA/yuqPz665acHQn/eGdNRhg2EzAJXY+bQnH2xNoJsf11+z+L7+CcNlo2LKb BsNz+H/d4VBtzolVUaD3XBFp+DVEfeDX/0Fvv37FtqTIGBXxioEToHJe/2pZ3NzhQJVc EGJhBlaXyj0+rvDDX3Ag+tHkq09CH1zZpJjYwy+Fzd5+2EGn3mcP3CutwKpobzSZVBn+ wdVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429872; x=1724034672; 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=i4kkOXmcQxWJOavPuESuYGxao420IRafuxIVOSCeFPqvqf4pF8lMzbMfhK62zKXwns kkeVG7SurhvLKu0AQFz2hgDjQWgJxR7wiCPDFKBpF9uATtnPKzefh3xylQ93zkYM+HZP XuChdY+hI5yDrHf3ATv42StvSQqcLOM9r5DFpt4JYESNpe5L0GfhnKP4XSwYRWY7M2zf uI7Mq22SogjHrQxd73k8H9EaqXQ17g2SEH9SsxhAwsVlpTd6BktUaiH6cltmKADIhFbv VM6HzuOtxQd/1b+vSTGVoFqxBBUoa5odZ/Z0B8lDV6fm9/I8sXqtr2W7t0AZ+ZebYjrM gNsA== X-Forwarded-Encrypted: i=1; AJvYcCXiy1WKpoqkkU/8V7paeO1rv4MBs54oarqlPbwzOkkMB5koSXUeGPpNLpVXE0obJkOhY2XtDtAG0waTsQR62RdgWQ0= X-Gm-Message-State: AOJu0Yy75sF6kTA5Da9UrYO/0aCMRSjJPO3/EP5vHMmMIIpU9+FkIRZp 4mmJVv+FMjhO74+F2eCUmuk69TEIB3hwWjs07Ji0qmkVbKzkYJ7I X-Google-Smtp-Source: AGHT+IGDYbe0RTpouvyRH8j57LNI0ygI4N2QpIwMJo/CclCqBa2C0H4JAnlCXTMiS2Sh5vz84U44EQ== X-Received: by 2002:a17:902:dac3:b0:1fb:48c6:a2b0 with SMTP id d9443c01a7336-200ae4dbc8amr93864745ad.5.1723429872026; Sun, 11 Aug 2024 19:31:12 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:11 -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 v6 6/9] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Mon, 12 Aug 2024 10:29:30 +0800 Message-Id: <20240812022933.69850-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: tfosdift4m4infjjxn5odbupqz4mr46z X-Rspam-User: X-Rspamd-Queue-Id: 5C25C100007 X-Rspamd-Server: rspam02 X-HE-Tag: 1723429873-429955 X-HE-Meta: U2FsdGVkX1+PrQH2FDQczCDvEtKZVsF4ORqkPXWmvvwbqZAfb2ho+ys7J/8J3KUoY8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mU/lyhCEOpsdai1VhJ9jTDs8WFfBhi1KGA2nJMV2KBbgSEdGEsj2QZPiILcto4XFUSh7vIM83P5PGeuO+9ZiJhjg64ekeE8ZJ7n9O/SHxXvKY6BYFHpJcGyQ5jlqrTuQUEB/HgGacDilUzE2IkFd+507tnFUALpFyJklKI94b+qIAaG/xKMefhLIn5EcPJYATtDSrGw8kWMYLP/r+Ha2yOGsOJx46ZxJzEt87MXmw7U4+wPhxmTMI95zcdbZBIXbBQBkLhFNWhLWo3SDY30Nsdc9gMcAQtLsSLe8Co5Dx0NnQDiQlIknekyK2PqJtfY4eTt1bC/HPXVkJ4Y/X8Wwkm0wlJc50l1xRNuRqghgpDRJZVik4uLn+vHdpo+XZOrVviNEZBPpxnuksFnEDTLRDVGyKTvGpSqil9dMQmsHnrFdH/dZPsHlvVCG3Fbb4/I/TDtt/DTwyD96zF0k67ovHp5xS9rL9xcu/rPc8JphpRZQLz2IuNKblEBDtrecQrOdwYKyloE2S5fycQ6N07M/S0TpLHRf8HHVYFXPmIbgTgxLKykuJvyb7MZaBgRAnL1b3jRVfwVl6H0LOxEtYIRSgErLlmARYTi79aQF0TAbwQWUa9Vn3bS0V21 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);