Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2668391lqp; Mon, 25 Mar 2024 06:12:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXlLt4roDoSplBzfuZLjRSgpX9m6B+Be9KTJ7dqC5APMYNa8l5A/BQvGTIAcwc4r6kmptHzbxJTQwqrGJbxTxTAc1ePIMIv27NjIGGMdA== X-Google-Smtp-Source: AGHT+IFjJnuzvJwCepuYlAxRxjcf3OEvr9vg9Z2EUHVdDPpWmGNRj3VRTPiFeLgUcOWOZpugc1bt X-Received: by 2002:a17:90a:ba84:b0:29b:6e9d:5897 with SMTP id t4-20020a17090aba8400b0029b6e9d5897mr6543908pjr.23.1711372379141; Mon, 25 Mar 2024 06:12:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711372379; cv=pass; d=google.com; s=arc-20160816; b=hg+v+ND2NptxGCQhfR2fBQ6Wk1bmYmtxIKp8R7UG/gx3HF1+JSzBdKZVhUqwMBeVpk 4XqMni1i0rsvKRNAkwkvsNxde2EmzTHvPaEXcxUFI28szpNqkY8e3wcclK9BvceZPpIl x9+xd2N72iLw8vlT0uX3EQLhE4ZeoTcLNq9hGR6p55TetS1XgiQgjV849D6kPYFZdu9b f4+VbiMqxSIhVA0V6GlMyNpApfg/Q3U5uPBz4kP7d4LfCSdcVd+295kUTcgPPuwIdL0b 77BFaFBGBk4qRRcQE7nWmfy1hKlXlIjfIkKSJ5RRjiq37y7hfRRim/qYdtV7ZgBwIifn sA1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:message-id:date:references:in-reply-to:subject:cc:to :from:dkim-signature; bh=E1c8k07tOmT40QtlqN9CrxHVzYaD1yR7xZNQOdkwnO4=; fh=i4qT6t+kSFvfzd1MBoKL2bxpKdgiE/x4iFhbs4mxOh8=; b=A60i4p+w1gPNXetQr0QeiY2UCQQO2SfBBiI5JyOZmfGvdmR/LlZdOq0tn9bRbtBBV4 mp4Dgipqj1v2cws7eJ/3Fse9x7f363W5yq2ipwN6V8dwSQP8AkPGFHW5fGlfflZnjoq/ 12sHmEtCQMpwzPRMpCqu2RnOBVrsaD0iwV0ADIQ4Bit0JShCSeLu68ts/xxfmJWtFcvz v0hOsn235Pu3IRtzLxakurRvhX/K0kIofLHrobKVrZlC7sY5LckHcNnvKP8BJDJRbEzu tYlFBgEHmTGl6aq+JIjBlj+Z5F8FyEO2gF3AtQ1AalYE/D0jBUd59CwniiahfzBjeDds cTrQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kHMBeZtu; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-116429-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116429-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id fw8-20020a17090b128800b002a0726b07dasi1613400pjb.118.2024.03.25.06.12.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 06:12:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116429-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kHMBeZtu; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-116429-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116429-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2C3EBB65C23 for ; Mon, 25 Mar 2024 11:30:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 318A316D9D0; Mon, 25 Mar 2024 06:46:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kHMBeZtu" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BFD726424F for ; Mon, 25 Mar 2024 03:02:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711335746; cv=none; b=X6a//RQ6d4yM2/2AbjVJ5GQOQM156gH4hVHsVJ1JSJoR9qHk8jgt9GqTTq8cHlboqaCuj1bCExTkFD1QyuURXyXCtRoIkxk73mJu4fSoWSsIXUyHadRg+RVdyQczkm6DC8MHAZsL3JxtlRi/lDqcoqnFHZmIm/4Kj5/2IP50Qt0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711335746; c=relaxed/simple; bh=T9lldniHRtKLEDe/H+EmLhHRUTPjZExPOnV/qvs4Qrw=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=uTCFYU9nWJydb0p/EyxTV19IAbwQLaKHr7D4UlJmdderMkI2/S3gT6TUEyDKOeqBknzLDnpU5G2y//xOVzzG+YPxMrrsykkv2zLAl4NXk6QNyFKJqy22IqJ/udue5oh9qH4CyEHpqT/is398Y2Kjzl8qzJ1EODPaOrt5XnnlCF4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kHMBeZtu; arc=none smtp.client-ip=198.175.65.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711335744; x=1742871744; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=T9lldniHRtKLEDe/H+EmLhHRUTPjZExPOnV/qvs4Qrw=; b=kHMBeZtuqKbyBzDguQHjo/0OnqiuEJU9XdlhH8GWHZlGYBWXaJoaWonY JYZRJonmf1HFT+qwX1Bpqszet5R6dZYXJ7w7tYr0APwsP33hkJqn7wRtN XRNX+5c7AfyYD0ZULtZ88GKs8q5BjShjv9ER9hoylsIr/ij/JEa0MeKFG /TGJxBVE4i9hGOJVklJO42cyQMN5VS2B38suwXq6tl1vOsClFHuhMPrPx lJirSA0I3BD5UqMyn9+Ulhc/cHcI4AwByWAxAtJSjgruPogDG/7o+8qEO qsIgU5AH5LtXcvlBXpz3ZUIqk2oHPXr1cg8s/jjtdAbaAdEWVW6nNa6/M A==; X-IronPort-AV: E=McAfee;i="6600,9927,11023"; a="6445250" X-IronPort-AV: E=Sophos;i="6.07,152,1708416000"; d="scan'208";a="6445250" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2024 20:02:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,152,1708416000"; d="scan'208";a="20220682" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2024 20:02:21 -0700 From: "Huang, Ying" To: Akira Yokosawa Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Paul E. McKenney" , ryan.roberts@arm.com, chrisl@kernel.org Subject: Re: Can you help us on memory barrier usage? (was Re: [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders) In-Reply-To: (Akira Yokosawa's message of "Sat, 23 Mar 2024 11:11:09 +0900") References: <87r0g3q9cz.fsf_-_@yhuang6-desk2.ccr.corp.intel.com> Date: Mon, 25 Mar 2024 11:00:28 +0800 Message-ID: <87v85bow1f.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) 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=ascii Akira Yokosawa writes: > [Use Paul's reachable address in CC; > trimmed CC list, keeping only those who have responded so far.] Thanks a lot! > Hello Huang, > Let me chime in. > > On Fri, 22 Mar 2024 06:19:52 -0700, Huang, Ying wrote: >> Hi, Paul, >> >> Can you help us on WRITE_ONCE()/READ_ONCE()/barrier() usage as follows? >> For some example kernel code as follows, >> >> " >> unsigned char x[16]; >> >> void writer(void) >> { >> memset(x, 1, sizeof(x)); >> /* To make memset() take effect ASAP */ >> barrier(); >> } >> >> unsigned char reader(int n) >> { >> return READ_ONCE(x[n]); >> } >> " >> >> where, writer() and reader() may be called on 2 CPUs without any lock. >> It's acceptable for reader() to read the written value a little later. >> Our questions are, >> >> 1. because it's impossible for accessing "unsigned char" to cause >> tearing. So, WRITE_ONCE()/READ_ONCE()/barrier() isn't necessary for >> correctness, right? >> >> 2. we use barrier() and READ_ONCE() in writer() and reader(), because we >> want to make writing take effect ASAP. Is it a good practice? Or it's >> a micro-optimization that should be avoided? > > Why don't you consult Documentation/memory-barriers.txt, especially > the section titled "COMPILER BARRIER"? > > TL;DR: > > barrier(), WRITE_ONCE(), and READ_ONCE() are compiler barriers, not > memory barriers. They just restrict compiler optimizations and don't > have any effect with regard to "make writing take effect ASAP". Yes. In theory, this is absolutely correct. My question is, in practice, will compiler barriers make CPU runs (or sees) memory read/write instructions a little earlier via avoiding to reorder the operations after read/write (e.g., becomes before read/write)? > If you have further questions, please don't hesitate to ask. -- Best Regards, Huang, Ying