Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4273884rdb; Thu, 14 Sep 2023 18:19:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYjuQZ+KC4+uaiRPy3H8hoP9z2LWt6AXIsQfcV9fxbbfRaFNpCX9UQLXwIkzBBdImBd0eS X-Received: by 2002:a05:6a00:10c3:b0:690:3a0f:4165 with SMTP id d3-20020a056a0010c300b006903a0f4165mr321508pfu.32.1694740780224; Thu, 14 Sep 2023 18:19:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694740780; cv=none; d=google.com; s=arc-20160816; b=qs3yv1kmjRyFJlg7F1lz7c3LD/CyNB83maM7zVMf0vlWfhcyqWNM7b8aO/iQ0HVsJm 2WlVRicQplUzCU5pn/WZy5X6+H+oK9DsEHt9YrXwNKm2Vg39tt1UsVpslEjXbF1j/jGg y1+x0L/mm8+lJj3p5NI0GaT9Pshks+V77uG5HxN+nUQ9dCCn7ac70cTVky9AEyn/4sqS Q9qJD+LXXI7eI43OHvsghrC/tgIhAOwftzCC+kUGwC2k9zhzp/bvLbI6KxXIEQw/Awrd ehEw/rfonxmWXY1p/KpI8mGcmzdfugGRXE5ht4hrSKB9Ife+VwCCsrU8ugZHEunvEG0z pU8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:dkim-filter; bh=iDGXYNvDUZyGPqG85pC0NeOYH73MS/LV4u3HMbaEDgs=; fh=VvAK5TwosYkw/BHQCQrIhU7/Qvknrtw1foo6x4jfPJ4=; b=I3wnE0jDj3xwO6Rh4YeNJ+07fvk3BvaPo5ylMhrbpdUqzKKwhRHCTrod7VtgFjQ/tB NKWL4C3wZYJ2W1K8ZrDRSIqP9PJ71dNab7RotyqL2hAZ+9QPNZ2jGfGXqt5agk2ZT+WM Y84mNTjJ5X7VN4hwU5XPpXvguUMjqzlNETuqgQ8yBTpLSvW8aSa1T5JbtpikEgRN7roQ CgITjEjDNPfOG8NwKtW/T9lkklLf+8+dnRbsZpTOcxJYuuVhMldBwwJu0RYzq4cTEO08 A1PNXTxr6BTl1n1DPRdr2xf1kHR+yNMuGabb4gDvwOCnOG55feJpvgUD0w9mKbExTorN Xgdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=JyqKzhLZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id b20-20020a6567d4000000b00573fcfd4a19si2350202pgs.878.2023.09.14.18.19.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 18:19:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=JyqKzhLZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 47D98837D488; Thu, 14 Sep 2023 09:42:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236956AbjINQmf (ORCPT + 99 others); Thu, 14 Sep 2023 12:42:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231243AbjINQme (ORCPT ); Thu, 14 Sep 2023 12:42:34 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2E8401FDC; Thu, 14 Sep 2023 09:42:30 -0700 (PDT) Received: from DESKTOP-4OLSCEK. (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 6334D212BE5B; Thu, 14 Sep 2023 09:42:29 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6334D212BE5B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1694709749; bh=iDGXYNvDUZyGPqG85pC0NeOYH73MS/LV4u3HMbaEDgs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JyqKzhLZpHt8HC8NJuqoBe7StMaxqjDz/IsXqKF3Z0ou4VcRzQJ+pnrsgc7xF+73G P0pn/ixo/EckvQB7WfEFbl6nlub6vAUFUxr43ceUjcTlwdMGePymFsxltSpB8D5QBp Q7+YYOb0P++J2YAHTlWQadajbccfv2n067z9rK3s= Date: Thu, 14 Sep 2023 09:42:18 -0700 From: Beau Belgrave To: =?iso-8859-1?Q?Cl=E9ment_L=E9ger?= Cc: Steven Rostedt , Masami Hiramatsu , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: Re: [PATCH] tracing/user_events: align uaddr on unsigned long alignment Message-ID: <20230914164218.GA450-beaub@linux.microsoft.com> References: <20230914131102.179100-1-cleger@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230914131102.179100-1-cleger@rivosinc.com> 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 (fry.vger.email [0.0.0.0]); Thu, 14 Sep 2023 09:42:40 -0700 (PDT) X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email On Thu, Sep 14, 2023 at 03:11:02PM +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 Thanks for fixing! I have a few comments on this. I unfortunately do not have RISC-V hardware to validate this on. > --- > 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; I think for future readers of this code it would be more clear to use BITS_PER_BYTE instead of the hardcoded 8. Given we always align on a "natural" boundary, I believe the bit_offset will always be 32 bits. A comment here might help clarify why we do this as well in case folks don't see the change description. > + uaddr &= ~(sizeof(unsigned long) - 1); Shouldn't this uaddr change be done before calling pin_user_pages_remote() to ensure things cannot go bad? (I don't think they can, but it looks a little odd). Thanks, -Beau > + } > 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); > > kunmap_local(kaddr); > unpin_user_pages_dirty_lock(&page, 1, true); > -- > 2.40.1