From mboxrd@z Thu Jan 1 00:00:00 1970 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 C6E75C25B78 for ; Mon, 3 Jun 2024 22:18:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 451236B0093; Mon, 3 Jun 2024 18:18:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 400666B0095; Mon, 3 Jun 2024 18:18:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EEF46B0096; Mon, 3 Jun 2024 18:18:43 -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 11FD86B0093 for ; Mon, 3 Jun 2024 18:18:43 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AF0EA1204F1 for ; Mon, 3 Jun 2024 22:18:42 +0000 (UTC) X-FDA: 82190992884.12.31A0BD1 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf30.hostedemail.com (Postfix) with ESMTP id 711588000A for ; Mon, 3 Jun 2024 22:18:39 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of "SRS0=/hzC=NF=goodmis.org=rostedt@kernel.org" designates 145.40.73.55 as permitted sender) smtp.mailfrom="SRS0=/hzC=NF=goodmis.org=rostedt@kernel.org"; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717453120; a=rsa-sha256; cv=none; b=ru8zDt76PrXf2D1Kpl6Rw6UwV1AnncnCOI2bQ8KgMyabdlDnQupWZRUPabY2J90nVWlPTh Naogji1VGzrnxAEVUgKsNJo4+/s2WdqKy2BYtWFvS6QnzCaVsIZp5Ar5C7hR+IR2tDtq1S 4fbflJmsKU/Oxyx53uM8jgzP2Dt/6so= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of "SRS0=/hzC=NF=goodmis.org=rostedt@kernel.org" designates 145.40.73.55 as permitted sender) smtp.mailfrom="SRS0=/hzC=NF=goodmis.org=rostedt@kernel.org"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717453120; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rhuhUR5AXH1Ns+xppJh0EvhmcAPyu/VLs1fH6zMkxX8=; b=Aof58V9u+5yDvac1pUxwPNF1HYpL/fvzIbsci665sx9RfmwiPoZsj681FxRKKkUiyHZl/e Grt7yV7NwOePBlOY9qwA/CjRjoe+BWVWIlNH0v/YiHEWrN2A3v1qNEHT1n1tAfggKZdfFK 8Jk98scrGdPU1K7OoMQ2lYx4GomfLnM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 16F4CCE0D89; Mon, 3 Jun 2024 22:18:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6369C2BD10; Mon, 3 Jun 2024 22:18:32 +0000 (UTC) Date: Mon, 3 Jun 2024 18:19:43 -0400 From: Steven Rostedt To: Linus Torvalds Cc: Yafang Shao , 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, Masami Hiramatsu , Mathieu Desnoyers Subject: Re: [PATCH 2/6] tracing: Replace memcpy() with __get_task_comm() Message-ID: <20240603181943.09a539aa@gandalf.local.home> In-Reply-To: References: <20240602023754.25443-1-laoar.shao@gmail.com> <20240602023754.25443-3-laoar.shao@gmail.com> <20240603172008.19ba98ff@gandalf.local.home> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 711588000A X-Stat-Signature: jk5y63qjsdqbgbt8u8dde4xkbs1xd78j X-HE-Tag: 1717453119-431644 X-HE-Meta: U2FsdGVkX1+/MGUau65kfZdc8mET8z/GMTDZhwfMhsOafP4Bg1ZiiKQ3MYbrWG2WGBQKuGPhKP2KmX4PweGzHa3sdqKkjk3K7Di0L02M+Q6goVPNzYOC1IqP/I2YVr0TIHqJgXVSaxeHY5xoOM2YHo2xalrXgL5FOjoPwZeDjucpcl24BGWQh4rUR6LL2vvDNCKMdK1UFHBDHtQmfZNSzavPuIckrd3gyRRictYExT8lXcZLVlHaFYhAVqoYtVEPu+k9mBQvB1OFL9xjJU1+Z8q6ulr6z5PKVBvEztVqavavi/NQsPMLm2EqjmLh1gxq+HnCYhUSyaU4kaiFkoE8FyYBvvIQO6rUeWzS2SbYGMSEiGfFmAL90lFYLbBGd871PiY9jVrPfdekZjv4bYK+v8rXtU/NI0WHH9v774bUZTScVUXfrslrkhLJlWZBYv8d9enbwnMWu4kx7dLZCfTycp+XCouEM77tYdR0NJ6XpqbQ1wTg2cdf/Tx+lSl1WqxzlBg7LKjlmr4bNq7rIDAk1sv1CBPBX4t9ImeQJx7R0I64WdE39aFALTUCyBqnOhkbT9TUx1ME7naJ1OfIGrJ8/8w3v181NM/1FcfWAXoypAOmQsHDTaIzO7ROYannmsS5cDUMQk45zQgK8es1rWLSdLYjYjPZD+xUq7FY9y2anGxWotFGqUPrtI5IDzSIjFy9UoXw57Ciqf0iqASe26sVdXwNdb4ek8ZAhu409XfAjZKCWAwXGCSagSJKUe7bgyWAZUa0hGnOCdpFVCLEwHPzhNSlqUunjQLY+zGZsdNMgEpvw6zVKVKRI2u7YTFzOwvUG9fgoUe+zTYDxo8oeoAc79uhZ1IHRjMbb414/16iUL7748gy5b0d910qWREdLGqsTJBcbLlCr4QJKVDNB58+H5sIr+Exwg6UBakJubdFZsw8PvUFTZL09nvN2EIIPT5IJ2Fp+7ekNNXMHO5aqWi xX5eB7uw PO/bvOMxlWUSucIUI1pBWdKsgE6oQdkfMG3FrNYKMwqLqofPpZCjvAOuSjb12Wi/MPE1ECw+nRKt7rOrG3c56p/GpMK3bL+NS8jwvG3bDlWWgg0jTUdCC11CjKAsYHqC8thYUv2bUBYcCAz+9LFX5k0Ao2uy4XxgZ0Rlne5kAhfwq1MPuDz/AO+Wi/ruGjQt2Y8fytmzwE6ix5nTddfeKewD9Kfw9lIrcK1w6FWcPd9a8h7gDxAAaC+Dm8NjntavDaVLvvfc/vz1dRwQgm0E4IEGY6GS4AH6eWILRyIXhUq63y3e5zrq6+Cv9jlQ+jtdziYdVXsty+JbG+pBUk23mdVyn3Zu5Cnm9wWIWQWLrRwZ2/PIt1IWl5TblzokamC9i7N2N6CpAdJsIeR0Pwcykin9D7a/3kBy/8jD7bLYVkW5b9/KjRfHmiWROdQp7XGfRvBoAufOXnTVNyIByYCn3Az+zLGo8cHCJXoYWFngAm4mI6u8= 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: On Mon, 3 Jun 2024 14:42:10 -0700 Linus Torvalds wrote: > On Mon, 3 Jun 2024 at 14:19, Steven Rostedt wrote: > > > > - __array( char, comm, TASK_COMM_LEN ) > > + __string( comm, strlen(comm) ) > > Is this actually safe is 'comm[]' is being modified at the same time? > The 'strlen()' will not be consistent with the string copy. First, I realized that it should actually be: __string( comm, task->comm ) But your question is still a valid question, as the internal logic will call strlen() on the second parameter. > > Because that is very much the case. It's not a stable source. > > For example, strlen() may return 5. But by the time you then actually > copy the data, the string may have changed, and there would not > necessarily be a NUL character at comm[5] any more. It might be > further in the string, or it might be earlier. The logic behind __string() and __assign_str() will always add a NUL character. __string() is defined as: static inline const char *__string_src(const char *str) { if (!str) return EVENT_NULL_STR; return str; } #undef __dynamic_array #define __dynamic_array(type, item, len) \ __item_length = (len) * sizeof(type); \ __data_offsets->item = __data_size + \ offsetof(typeof(*entry), __data); \ __data_offsets->item |= __item_length << 16; \ __data_size += __item_length; #undef __string #define __string(item, src) __dynamic_array(char, item, \ strlen(__string_src(src)) + 1) \ __data_offsets->item##_ptr_ = src; The above will use the strlen(src) to specify the amount of memory to allocate on the ring buffer: "strlen(__string_src(src)) + 1)" This is stored on a special structure for the entry and used in the __assign_str() (the reason I removed the source to that macro during this merge window). #undef __assign_str #define __assign_str(dst) \ do { \ char *__str__ = __get_str(dst); \ int __len__ = __get_dynamic_array_len(dst) - 1; \ memcpy(__str__, __data_offsets.dst##_ptr_ ? : \ EVENT_NULL_STR, __len__); \ __str__[__len__] = '\0'; \ } while (0) The source of the string is copied via memcpy() using the length stored from the __string() macro (minus 1), and then '\0' is added to it to force the NUL character to be in the memory for the string. -- Steve