From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B9DC310624 for ; Wed, 29 Oct 2025 15:20:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761751247; cv=none; b=W4PsT0EaZBlsPpjhELrNLpOM05NsSGE5gUtfzf5sPEYv+exSd4MhazZ8OEvFB8Bo//03fRIMHeEQJON3JAdgjIfPUsdJzT1HPA1yqLh9IgR180WOTOuVGDfQPxZI2GVVd4JS4qxoKUaGqCLXlgOD2gJaNrw+ZbGv63NuEINzNis= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761751247; c=relaxed/simple; bh=HtJoUupx6d/yAjUM+7GHFEp8E2bMSioh2TQ2z1eUvkg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uo3QTpNuxR6MvVgjjp3HwydUcfjHR2kN3z5ZwjbIiKMEbqSwkkAreqcEW8X6LAzbpWvrxKMnG/aj9iWABhnAdlROm03GurcsDwBWCEPpiBmZqmJLgF/DqSgkS39yr55UYP3oNwY8uImTH5dcAk+VYg0q26TeEj/iwZ4J0kjj0oo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YO8wlahf; arc=none smtp.client-ip=209.85.214.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YO8wlahf" Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-27c369f8986so67034175ad.3 for ; Wed, 29 Oct 2025 08:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761751245; x=1762356045; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=StYPN+Xnv/FlnNbB7eCIIAuswaamCaMZ2VJSzNeBonw=; b=YO8wlahfk7zhuSu1taBkNV56d397C4+ZSInUL/QkrdiaOsPQ5MA0soEKfiN1TYN4Qn CVmAV0FikZzMfUd5O4x7D7OxEgG6fnDOv3H7rfs0hJRemFrbiibfJzRJIQ4dHMS5jQKS Srl1pIJR42ieowOEibynXL6YaR8rd9bpVw7yDvaexC6ZxnDSViS+zAWs/ixP9306ZgnI mHA+umwm3ksAqMkg+n9sGZ5r+5SWmMuBl3j+uz0RdkM/xVdZRgHRWyOOg1CN3uw41hC1 1OXydasfIbqaSRVSdu9PIhk2aWeweSg929CU5mfJSCKROZqApyga5796HyQf5hm9A/u7 usqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761751245; x=1762356045; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=StYPN+Xnv/FlnNbB7eCIIAuswaamCaMZ2VJSzNeBonw=; b=addl4c1QMPw7rUSW7uRrmlLwXLQ/mmGQ6N4PZIcFdi8NquTTqHtIojqbuUa9+xidlP t4NHe6ELzLVXBbOtHXukEdrYrGX9N6dm5PqIsZ8liCiFXpxmW+Gutdnl2B3l9mKXT4pr taJJK7sCkbif6V2HJu0nLjNMWr4iyHSgoiCtvEkaKQwHselAcD0xyZr32uAhAc1Y0sKY +TKuYIvS+uzK7ShMwEnsf21v4ZxuDGK18lS508abdjUv+27qmYd3SD1AZelEVgMr2ovc FuaCf+T55XIpuucFV2OD57tbcMvSrl0wtrfMfS++ghg81YvsiwhglsThhjyUQ5AYDZQ6 p1hQ== X-Forwarded-Encrypted: i=1; AJvYcCVDczCJHOuHWCC3R0qF2GebFO/u9ApXqWf0v7HJeYNEMBqe3t32hC1sCHa51hOPY0unAr+XGS5ZEaGNPE4=@vger.kernel.org X-Gm-Message-State: AOJu0YyRlHNOg7J04KuBkl/199zKPHMjMvRDqEBrgyZbgUUb5cYq18y3 yjk7lZIDecnn+0JYuWnZA/zi/TY8k0erbVg6pOZiEZZGl6x9MjNLSvGg X-Gm-Gg: ASbGncumOs0yQRMkw/dSpEkCIoWBaOiHPE1HfVcAaKhfdgH2D+LF3cm6hhdhhNbmox7 Er0xRc1zhK4+PvbcsqZCAxvJY7srnNPXZmw1LX7qJ+a1swKwl1oyZma+M5SeUf3IETY/NexuHjc pxaUNUZGWlJTGQpA9ffPfWcUM6k+rFRuXM6cjxEQGR4eb6RLe3zrDel7zUFxMpzFi8KL97Z0aKM ZlkDjaIJLaq2uWA//YLx5LYXCqdiOW/n400vxCiz/i28N+0Mpe3+ux3cejTXWJNAwKCkuNof0OJ CUUZLKFK79ojidWq+E6nkxPmrHGQk+HVWNzPw7AFvhvoyhCwUg7n4rEcm5kIPXN3MPo0eHRrLa3 Zdv1NTjS5ZHI2yhuFBQ3e6l+p/QS+SE1RtPDWi/RywBC2luT/DLn+HX4Io8Z/oJclkmDV/bpYNP 0S73cXDF4dYWiUonUJcLk= X-Google-Smtp-Source: AGHT+IEpwGryO+Dsdw+yBiLYzvfG216rbfuFmYTVEwK59maJasPtn26KCM1zCvZKBwTCZ0ScENApeA== X-Received: by 2002:a17:902:f550:b0:277:3488:787e with SMTP id d9443c01a7336-294dee0cebemr41194955ad.12.1761751245449; Wed, 29 Oct 2025 08:20:45 -0700 (PDT) Received: from fedora ([103.120.31.122]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29498cf3bfesm157605975ad.4.2025.10.29.08.20.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Oct 2025 08:20:44 -0700 (PDT) Date: Wed, 29 Oct 2025 20:50:38 +0530 From: Ankit Khushwaha To: Andrew Morton Cc: linux-kselftest@vger.kernel.org, Shuah Khan , Bala-Vignesh-Reddy , Wei Yang , linux-kernel@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, Steven Rostedt Subject: Re: [PATCH] selftests/user_events: Avoid taking address of packed member in perf_test Message-ID: References: <20251027113439.36059-1-ankitkhushwaha.linux@gmail.com> <20251027162521.c56c7f89f6ad4e3d639c408c@linux-foundation.org> <20251028132605.2926d3ef5eb6ea60d22ceffe@linux-foundation.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251028132605.2926d3ef5eb6ea60d22ceffe@linux-foundation.org> On Tue, Oct 28, 2025 at 01:26:05PM -0700, Andrew Morton wrote: > On Tue, 28 Oct 2025 22:28:10 +0530 Ankit Khushwaha wrote: > > > > > @@ -236,7 +237,8 @@ TEST_F(user, perf_empty_events) { > > > > ASSERT_EQ(1 << reg.enable_bit, self->check); > > > > > > > > /* Ensure write shows up at correct offset */ > > > > - ASSERT_NE(-1, write(self->data_fd, ®.write_index, > > > > + memcpy(&write_index, ®.write_index, sizeof(reg.write_index)); > > > > + ASSERT_NE(-1, write(self->data_fd, &write_index, > > > > sizeof(reg.write_index))); > > > > > > Simply casting &write_index to void* would fix this? > > > > yes, this hides the type mismatch from the compiler. But i think > > casting to void * will not fix the alignment mismatch for packed struct. > > It works on x86, but might break on other platform. > > It's the second argument to write(2)! write(2) expects a const char *, > but void* will work. Hi Andrew, Indeed `ASSERT_NE(-1, write(self->data_fd, (void *)®.write_index, sizeof(reg.write_index)));` would work. However since `reg` is packed struct, directly taking the address of its member `®.write_index` may lead to unaligned access on some architectures. as indicated by the compiler warning perf_test.c:239:38: warning: taking address of packed member 'write_index' of class or structure 'user_reg' may result in an unaligned pointer value [-Waddress-of-packed-member] 239 | ASSERT_NE(-1, write(self->data_fd, ®.write_index, | ^~~~~~~~~~~~~~~ Using `memcpy` avoids this by performing a byte-wise copy, which is safe to use for packed structures. Thanks -- Ankit