Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1514450lqp; Fri, 22 Mar 2024 19:11:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVDQ5KKMsjnaBJQA+m3+YriOLjxUO4gJTwC4CdxQSWcYvP+SdJY/+BO+2WegbLfQqttU7DEQA16tvDZTB0AMabTv5ftWXL5HROxiyU+pA== X-Google-Smtp-Source: AGHT+IHzyLcxfCclyGXemfU0viKng49qM6MlkrpDYcL3Jemzq6efxd/EHYjwxD6byiZ08i51B/qo X-Received: by 2002:a17:906:1687:b0:a46:be3d:1ed8 with SMTP id s7-20020a170906168700b00a46be3d1ed8mr944671ejd.54.1711159892970; Fri, 22 Mar 2024 19:11:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711159892; cv=pass; d=google.com; s=arc-20160816; b=lCXfLa+C6EtVku37tulONS8jREArJ4LLDSonCbP3GhJdooMac61V7S1d2iKGu3I354 01RDaWcYnbnGpHUzuE80a2xTNnnYJLF3zKfeQnS+CsZEyeEo7orV73s4L5wXoagxWp7D JAWa7cDMz61+ZXZW7JNcQEFb2XuKv1EDhZ1+jk+/Ct5b0xPzD9fjjoULnS/eTx/YDqej qHw8Iwlzdy54VON8E9W2ppYBgGwo8jNPWpYoVLP4InFWMN/yU+4U7QSgBa8u0AcNqIBF teg5uS1V3A3Nqm+qyNhu5IUXpa53LJfFTTVI/I10CvlVJt+dNAqgUvFFTkzxGemgSu8C IVwA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language:subject :references:cc:to:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=ytYwAkh4AZyvnFMvyg0Zf5hpxuIrcf2upm5pBUrao8c=; fh=5MKahsZWlQGw5R/DcJcpuc75nMJERLQzzP0KospBroo=; b=TgyhugVsUp9fI7YG080xhHr4OoAZvrv+8MKwQOLqT2dqsjrrZSHjLwrgVqyMZP3Q7O zGGU+CbCddue4T0zMDa23t2f2exp071ZE8zhvOA1yLknj0a6q3DUERxpe1CBINSdxiJo B5t2VjFd4LVb0Go/3SoXDgWtIHAHhhq/aAzmMK6rFfVXIAtSlRhxdcs6E+5Mck7OrCac 4KDP8vQhk86tZ9+KhQb+Xq3tv6GjteRkdzGduSLYPOSkL73Z/6GK8lkat5JihTWbU5Io cJCLwZwof8x2eFgSp+LoXBbM5kyoPz8UTjkcs3hPgrYVeh3Tc6O2k6avbN1cXn2hacS3 4ODw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JWm5Cv5A; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-112178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112178-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id b13-20020a1709062b4d00b00a47425fd684si224965ejg.77.2024.03.22.19.11.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 19:11:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JWm5Cv5A; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-112178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112178-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 84BCF1F227DF for ; Sat, 23 Mar 2024 02:11:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9C643137E; Sat, 23 Mar 2024 02:11:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JWm5Cv5A" Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 71FC6A55 for ; Sat, 23 Mar 2024 02:11:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711159885; cv=none; b=Gm9YHua0GfycMxNaUlJfmSL0mPjj2I4EGOBq4BxSrUOYwHezOs9Gh3yuuG/iE8mrq5KKX08dcNFz5Em16CoUtkPpubTAc1hXsi/uU5lWquiFLQDNIRDEeKvVEwK3K2MlA3tejyd+gMhpdSccDbPBgQDSKS0hVJsIpQ1nD3XD5xs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711159885; c=relaxed/simple; bh=Htqw8GSL4y8T14PX861x5cQ+UMbzyBgGFwv2eWHm6Cs=; h=Message-ID:Date:MIME-Version:To:Cc:References:Subject:From: In-Reply-To:Content-Type; b=t8l4IsKonLbLiOh3FFYBarhudP07NHRj9TO49PybN/Xm0h0lSx/kzbuaowqlZGB3tmbXp0xorm+9ZLxKSul366OX2s2vKW4ay0MTBSXgj1ZW+oLsdWwxgdemFcR2vfiqNDTZFfpcmCoXFy/xTDBqykqsQaNP8I9WhydiW+F/sJA= 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=JWm5Cv5A; arc=none smtp.client-ip=209.85.215.180 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 Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5bdbe2de25fso1660485a12.3 for ; Fri, 22 Mar 2024 19:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711159884; x=1711764684; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language:subject :references:cc:to:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=ytYwAkh4AZyvnFMvyg0Zf5hpxuIrcf2upm5pBUrao8c=; b=JWm5Cv5AwGGGin2Y6icjQGXqurkAxfjv0g6oOLm2Wg/L9ig7sLWWH1Kqh/bl6/flJq Opqjply0rOVfzl1TrXEerLdwTHETjQZZZUqkLHfx7QMIozOe4XO2Xir1PedxoBzvWT6n W2bDbNZaoZMfdrbsLrwGrXKehnn6+Z8YhDuxj96mRp8nrR4NrltjEpxIdjAdP7zMp7pA 8Yb0k2xRXGRQDXAM2eABCvaRm3WHBVd6Tsw5SVnPqgYKxFq4YSMnSFOyswsxs7rEyeHx hOqydVZ1v+w4zIcpjmpbSqQGlwKZ31NBAJh04GtvYUoX77UPwBJNfn1dvvHf6xGWyYD2 ALsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711159884; x=1711764684; h=content-transfer-encoding:in-reply-to:from:content-language:subject :references:cc:to:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ytYwAkh4AZyvnFMvyg0Zf5hpxuIrcf2upm5pBUrao8c=; b=YHo8xysdq2LXIpwQWf0gYNrdaYHUrOL/ubgBdgCgpp/hXUSVOlNcA6GZCrINUVgWrH dTY57Etnx7bdEqTuvfM4nsY1JPLdSSJWIaDbEXVe32ehYz1mauH464naXXssNrk38nQO 0yx8OampDrv/WbF70Alo6TGJAJmPkkOq8dLuTfjFBAjoMUOxdBpHhz+gcXXvqN3wnx6E 58Ga+0yx6AQ1DX/frF6RT0R5LHHEyMei+srcG+M/A7nRWEnKrrqLcInwdRHTNTOIjr1/ ExCQ03CyNLAqn2X9trao5Tv3rw4xxIvonsPiUMo8SJyba6sluzn25lKIiehGsvMa8LB2 XyiA== X-Gm-Message-State: AOJu0YxFK7OuCcDRtZEV24hyO+dTP1ynBnyWNw5RpdOeNe1QvOEKIibM /RGZCuQKvcxUCTxa/KF7wD65AXm38ZmA2bbP+JK4NBxX/18dTuiy X-Received: by 2002:a05:6a20:3ca2:b0:1a3:a669:747e with SMTP id b34-20020a056a203ca200b001a3a669747emr1806032pzj.12.1711159883651; Fri, 22 Mar 2024 19:11:23 -0700 (PDT) Received: from [10.0.2.15] (KD106167171201.ppp-bb.dion.ne.jp. [106.167.171.201]) by smtp.gmail.com with ESMTPSA id rs12-20020a17090b2b8c00b00298e11b600dsm2594669pjb.27.2024.03.22.19.11.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 Mar 2024 19:11:11 -0700 (PDT) Message-ID: Date: Sat, 23 Mar 2024 11:11:09 +0900 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: ying.huang@intel.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Paul E. McKenney" , ryan.roberts@arm.com, chrisl@kernel.org References: <87r0g3q9cz.fsf_-_@yhuang6-desk2.ccr.corp.intel.com> Subject: Re: Can you help us on memory barrier usage? (was Re: [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders) Content-Language: en-US From: Akira Yokosawa In-Reply-To: <87r0g3q9cz.fsf_-_@yhuang6-desk2.ccr.corp.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit [Use Paul's reachable address in CC; trimmed CC list, keeping only those who have responded so far.] 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". If you have further questions, please don't hesitate to ask. Regards, Akira (a LKMM Reveiwer). > > -- > Best Regards, > Huang, Ying