Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4313054rdb; Thu, 14 Sep 2023 20:15:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHbPjN4a+FJyY1hLI0ynDrQ0hrmpDalDrxlruH9FHUt3OPrn6HhifV2wq5RY3x5Co2s0gwA X-Received: by 2002:a05:6a00:410e:b0:68a:42d0:6b9b with SMTP id bu14-20020a056a00410e00b0068a42d06b9bmr483120pfb.3.1694747758149; Thu, 14 Sep 2023 20:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694747758; cv=none; d=google.com; s=arc-20160816; b=LpommnXE6HTnXnZmPtV+0QFreRgcRc5ULR/4QZR6mj4n6+mTO3aW5IeTbncx03njBd Hb3MxbmG3CrPzyiPxgHKF9ZjURuffMJBszsTxCX6rNCNyMy0UZQahToT/WcNRXejluxn ZsV67P+kGjQQ+/Mt4H4QxFiPXh9bo299fq45nmqxWGjKtS8LrCQIgiyvNrzFakKklnH9 YsuDZkM/qJbCMI+aWYr2JkdTHCqBVG9ag/uahr3t7l8KFT1FPgOZwFUUKr7wqVqf1eOM UZMFhhxUL8XwHGrp12qJONyKSVPn6MGvBjqGGGyx91bKSnnPpVyzUbdCJThXlRgMv0t0 7oHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=vRG5orfO5NfIt5edsdGNeVNm3Fe7JTqoQeu+YLtAS+4=; fh=ddp6n9/iXajIwZLT7NqCZn667aHOZojOg8U2D7fkiuA=; b=alKSjPFyeo27n35G7u4BUrW7mDiUwvzv1sipyIbmcbMkAsfnB3VL7CSbI4lrakVXYS qTAsH2reaftSR/j3I+moX3OxIni/wTNk5p2ZToi5sirqZxn7sNBCy/liuzSJs3qN2jP7 JtMkd8e0A6oLnZvjPMgTei3zC4hf8Ak1mmscP5AB7qm2GpWwTFugK8Tw7LhODU5XzGZY l3jEc3DtHjoPG4x7MFlZe7A6JAeDiQJ37LdCKAy829uoBK70I7D9wylz+isF4vcEl8oz aFdweP/TMkb39U2/E4uWPm5/+6ha2Qzjk3zXQPWsKm6MAYRJmHAGpVoqvMH2L7hB3TC5 rbZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NxB8WjdQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id k18-20020aa790d2000000b0068fe810e8a0si2430514pfk.197.2023.09.14.20.15.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 20:15:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NxB8WjdQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 113F082E750E; Thu, 14 Sep 2023 19:55:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231758AbjIOCy6 (ORCPT + 99 others); Thu, 14 Sep 2023 22:54:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231755AbjIOCy6 (ORCPT ); Thu, 14 Sep 2023 22:54:58 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 024D82703; Thu, 14 Sep 2023 19:54:54 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76176C433C8; Fri, 15 Sep 2023 02:54:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1694746493; bh=BaCEONwWbgZdViDwOmp0Htr99OIatdc7tnMiHomcjCw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=NxB8WjdQNtjwfyEehGuYUdRSjJNsIkR3oqVdYBLoFceBdnF+/Rpbmx+Bqkh0zKNj4 ZJFRescK9cXsVXU2WG5BjNaYHk+5OPmK/foHi8Ev74tIUR5CcglyYhevcUc76Dy1Gc 1HOtUl4/tYnyFe5ecdeS8nv5C/OjKVB4Fb1yWOv6owpAJl7wf1pD0EbAnTKgcND3kS EN5rYsomC0JHmeoOWeeLN0F2Vwxrb8CGbBMPw76+dOvBdiDNYNalYrjR8casLMKHhg VV06lxPVWcEkiCVmXBMHCN2kjkPkoqVG9AmGsnbCb5eiqOQ8/76sWYOq7PQliUANIM /tASxJU7FEdCw== Date: Fri, 15 Sep 2023 11:54:49 +0900 From: Masami Hiramatsu (Google) To: =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= Cc: Steven Rostedt , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Beau Belgrave Subject: Re: [PATCH] tracing/user_events: align uaddr on unsigned long alignment Message-Id: <20230915115449.3d7103b841cd593d1a09a129@kernel.org> In-Reply-To: <20230914131102.179100-1-cleger@rivosinc.com> References: <20230914131102.179100-1-cleger@rivosinc.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 14 Sep 2023 19:55:06 -0700 (PDT) On Thu, 14 Sep 2023 15:11:02 +0200 Clément Léger wrote: > enabler->uaddr can be aligned on 32 or 64 bits. If aligned on 32 bits, > this will result in a misaligned access on 64 bits architectures since > set_bit()/clear_bit() are expecting an unsigned long (aligned) pointer. > On architecture that do not support misaligned access, this will crash > the kernel. Align uaddr on unsigned long size to avoid such behavior. > This bug was found while running kselftests on RISC-V. > > Fixes: 7235759084a4 ("tracing/user_events: Use remote writes for event enablement") > Signed-off-by: Clément Léger > --- > kernel/trace/trace_events_user.c | 12 +++++++++--- > 1 file changed, 9 insertions(+), 3 deletions(-) > > diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c > index 6f046650e527..580c0fe4b23e 100644 > --- a/kernel/trace/trace_events_user.c > +++ b/kernel/trace/trace_events_user.c > @@ -479,7 +479,7 @@ static int user_event_enabler_write(struct user_event_mm *mm, > bool fixup_fault, int *attempt) > { > unsigned long uaddr = enabler->addr; > - unsigned long *ptr; > + unsigned long *ptr, bit_offset; > struct page *page; > void *kaddr; > int ret; > @@ -511,13 +511,19 @@ static int user_event_enabler_write(struct user_event_mm *mm, > } > > kaddr = kmap_local_page(page); > + > + bit_offset = uaddr & (sizeof(unsigned long) - 1); > + if (bit_offset) { > + bit_offset *= 8; > + uaddr &= ~(sizeof(unsigned long) - 1); > + } > ptr = kaddr + (uaddr & ~PAGE_MASK); > > /* Update bit atomically, user tracers must be atomic as well */ > if (enabler->event && enabler->event->status) > - set_bit(ENABLE_BIT(enabler), ptr); > + set_bit(ENABLE_BIT(enabler) + bit_offset, ptr); > else > - clear_bit(ENABLE_BIT(enabler), ptr); > + clear_bit(ENABLE_BIT(enabler) + bit_offset, ptr); What we need are generic set_bit_aligned() and clear_bit_aligned(), which align the ptr by unsigned long. (I think it should be done in set_bit/clear_bit, for architecture which requires aligned access...) #define LONG_ALIGN_DIFF(p) (p) & (sizeof(long) -1) #define LONG_ALINGNED(p) (p) & ~(sizeof(long) - 1) static inline void set_bit_aligned(int bit, unsigned long *ptr) { int offs = LONG_ALIGN_DIFF(ptr) * 8; #ifdef __BIGENDIAN if (bit >= offs) { set_bit(bit - offs, LONG_ALIGNED(ptr)); } else { set_bit(bit + BITS_PER_LONG - offs, LONG_ALIGNED(ptr) + 1); } #else if (bit < BITS_PER_LONG - offs) { set_bit(bit + offs, LONG_ALIGNED(ptr)); } else { set_bit(bit - BITS_PER_LONG + offs, LONG_ALIGNED(ptr) + 1); } #endif } And use it. Thank you, > > kunmap_local(kaddr); > unpin_user_pages_dirty_lock(&page, 1, true); > -- > 2.40.1 > -- Masami Hiramatsu (Google)